mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
Compare commits
14 Commits
remove-off
...
1387-persi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9ea4463cbc | ||
|
|
a83682d4b6 | ||
|
|
b1ec344bdb | ||
|
|
821327d62e | ||
|
|
cda722a8b2 | ||
|
|
ca5c18ece3 | ||
|
|
81ed5978d6 | ||
|
|
a63619ab47 | ||
|
|
2d88819e80 | ||
|
|
fef19c1329 | ||
|
|
7ce91066f5 | ||
|
|
dfb581ef12 | ||
|
|
d9b4c013bb | ||
|
|
61407c3e06 |
8
.idea/codeStyles/Project.xml
generated
8
.idea/codeStyles/Project.xml
generated
@@ -1,5 +1,8 @@
|
|||||||
<component name="ProjectCodeStyleConfiguration">
|
<component name="ProjectCodeStyleConfiguration">
|
||||||
<code_scheme name="Project" version="173">
|
<code_scheme name="Project" version="173">
|
||||||
|
<AndroidXmlCodeStyleSettings>
|
||||||
|
<option name="ARRANGEMENT_SETTINGS_MIGRATED_TO_191" value="true" />
|
||||||
|
</AndroidXmlCodeStyleSettings>
|
||||||
<JavaCodeStyleSettings>
|
<JavaCodeStyleSettings>
|
||||||
<option name="ANNOTATION_PARAMETER_WRAP" value="1" />
|
<option name="ANNOTATION_PARAMETER_WRAP" value="1" />
|
||||||
<option name="IMPORT_LAYOUT_TABLE">
|
<option name="IMPORT_LAYOUT_TABLE">
|
||||||
@@ -28,11 +31,6 @@
|
|||||||
<option name="JD_ALIGN_EXCEPTION_COMMENTS" value="false" />
|
<option name="JD_ALIGN_EXCEPTION_COMMENTS" value="false" />
|
||||||
</JavaCodeStyleSettings>
|
</JavaCodeStyleSettings>
|
||||||
<JetCodeStyleSettings>
|
<JetCodeStyleSettings>
|
||||||
<option name="PACKAGES_TO_USE_STAR_IMPORTS">
|
|
||||||
<value />
|
|
||||||
</option>
|
|
||||||
<option name="NAME_COUNT_TO_USE_STAR_IMPORT" value="2147483647" />
|
|
||||||
<option name="NAME_COUNT_TO_USE_STAR_IMPORT_FOR_MEMBERS" value="2147483647" />
|
|
||||||
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
|
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
|
||||||
</JetCodeStyleSettings>
|
</JetCodeStyleSettings>
|
||||||
<XML>
|
<XML>
|
||||||
|
|||||||
1
bramble-android/.gitignore
vendored
1
bramble-android/.gitignore
vendored
@@ -3,4 +3,3 @@ gen
|
|||||||
build
|
build
|
||||||
.settings
|
.settings
|
||||||
src/main/res/raw/*.zip
|
src/main/res/raw/*.zip
|
||||||
src/main/jniLibs
|
|
||||||
@@ -5,14 +5,14 @@ apply plugin: 'witness'
|
|||||||
apply from: 'witness.gradle'
|
apply from: 'witness.gradle'
|
||||||
|
|
||||||
android {
|
android {
|
||||||
compileSdkVersion rootProject.ext.compileSdkVersion
|
compileSdkVersion 29
|
||||||
buildToolsVersion rootProject.ext.buildToolsVersion
|
buildToolsVersion '29.0.2'
|
||||||
|
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
minSdkVersion rootProject.ext.minSdkVersion
|
minSdkVersion 16
|
||||||
targetSdkVersion rootProject.ext.targetSdkVersion
|
targetSdkVersion 28
|
||||||
versionCode rootProject.ext.versionCode
|
versionCode 10209
|
||||||
versionName rootProject.ext.versionName
|
versionName "1.2.9"
|
||||||
consumerProguardFiles 'proguard-rules.txt'
|
consumerProguardFiles 'proguard-rules.txt'
|
||||||
|
|
||||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||||
@@ -53,12 +53,10 @@ dependencies {
|
|||||||
}
|
}
|
||||||
|
|
||||||
def torBinariesDir = 'src/main/res/raw'
|
def torBinariesDir = 'src/main/res/raw'
|
||||||
def torLibsDir = 'src/main/jniLibs'
|
|
||||||
|
|
||||||
task cleanTorBinaries {
|
task cleanTorBinaries {
|
||||||
doLast {
|
doLast {
|
||||||
delete fileTree(torBinariesDir) { include '*.zip' }
|
delete fileTree(torBinariesDir) { include '*.zip' }
|
||||||
delete fileTree(torLibsDir) { include '**/*.so' }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,36 +67,8 @@ task unpackTorBinaries {
|
|||||||
copy {
|
copy {
|
||||||
from configurations.tor.collect { zipTree(it) }
|
from configurations.tor.collect { zipTree(it) }
|
||||||
into torBinariesDir
|
into torBinariesDir
|
||||||
include 'geoip.zip'
|
// TODO: Remove after next Tor upgrade, which won't include non-PIE binaries
|
||||||
}
|
include 'geoip.zip', '*_pie.zip'
|
||||||
configurations.tor.each { outer ->
|
|
||||||
zipTree(outer).each { inner ->
|
|
||||||
if (inner.name.endsWith('_arm_pie.zip')) {
|
|
||||||
copy {
|
|
||||||
from zipTree(inner)
|
|
||||||
into torLibsDir
|
|
||||||
rename '(.*)', 'armeabi-v7a/lib$1.so'
|
|
||||||
}
|
|
||||||
} else if (inner.name.endsWith('_arm64_pie.zip')) {
|
|
||||||
copy {
|
|
||||||
from zipTree(inner)
|
|
||||||
into torLibsDir
|
|
||||||
rename '(.*)', 'arm64-v8a/lib$1.so'
|
|
||||||
}
|
|
||||||
} else if (inner.name.endsWith('_x86_pie.zip')) {
|
|
||||||
copy {
|
|
||||||
from zipTree(inner)
|
|
||||||
into torLibsDir
|
|
||||||
rename '(.*)', 'x86/lib$1.so'
|
|
||||||
}
|
|
||||||
} else if (inner.name.endsWith('_x86_64_pie.zip')) {
|
|
||||||
copy {
|
|
||||||
from zipTree(inner)
|
|
||||||
into torLibsDir
|
|
||||||
rename '(.*)', 'x86_64/lib$1.so'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dependsOn cleanTorBinaries
|
dependsOn cleanTorBinaries
|
||||||
@@ -106,6 +76,5 @@ task unpackTorBinaries {
|
|||||||
|
|
||||||
tasks.withType(MergeResources) {
|
tasks.withType(MergeResources) {
|
||||||
inputs.dir torBinariesDir
|
inputs.dir torBinariesDir
|
||||||
inputs.dir torLibsDir
|
|
||||||
dependsOn unpackTorBinaries
|
dependsOn unpackTorBinaries
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,8 +16,6 @@
|
|||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
android:supportsRtl="true">
|
android:supportsRtl="true">
|
||||||
|
|
||||||
<receiver android:name=".system.AlarmReceiver" />
|
|
||||||
|
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
||||||
|
|||||||
@@ -6,8 +6,6 @@ import org.briarproject.bramble.plugin.tor.CircumventionModule;
|
|||||||
import org.briarproject.bramble.reporting.ReportingModule;
|
import org.briarproject.bramble.reporting.ReportingModule;
|
||||||
import org.briarproject.bramble.socks.SocksModule;
|
import org.briarproject.bramble.socks.SocksModule;
|
||||||
import org.briarproject.bramble.system.AndroidSystemModule;
|
import org.briarproject.bramble.system.AndroidSystemModule;
|
||||||
import org.briarproject.bramble.system.AndroidTaskSchedulerModule;
|
|
||||||
import org.briarproject.bramble.system.AndroidWakefulIoExecutorModule;
|
|
||||||
|
|
||||||
import dagger.Module;
|
import dagger.Module;
|
||||||
|
|
||||||
@@ -15,8 +13,6 @@ import dagger.Module;
|
|||||||
AndroidBatteryModule.class,
|
AndroidBatteryModule.class,
|
||||||
AndroidNetworkModule.class,
|
AndroidNetworkModule.class,
|
||||||
AndroidSystemModule.class,
|
AndroidSystemModule.class,
|
||||||
AndroidTaskSchedulerModule.class,
|
|
||||||
AndroidWakefulIoExecutorModule.class,
|
|
||||||
CircumventionModule.class,
|
CircumventionModule.class,
|
||||||
ReportingModule.class,
|
ReportingModule.class,
|
||||||
SocksModule.class
|
SocksModule.class
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
package org.briarproject.bramble;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.system.AlarmListener;
|
|
||||||
|
|
||||||
public interface BrambleAppComponent {
|
|
||||||
|
|
||||||
AlarmListener alarmListener();
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
package org.briarproject.bramble;
|
|
||||||
|
|
||||||
public interface BrambleApplication {
|
|
||||||
|
|
||||||
BrambleAppComponent getBrambleAppComponent();
|
|
||||||
}
|
|
||||||
@@ -9,8 +9,10 @@ import org.briarproject.bramble.api.account.AccountManager;
|
|||||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||||
import org.briarproject.bramble.api.db.DatabaseConfig;
|
import org.briarproject.bramble.api.db.DatabaseConfig;
|
||||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||||
|
import org.briarproject.bramble.api.logging.PersistentLogManager;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@@ -20,17 +22,21 @@ import javax.annotation.Nullable;
|
|||||||
import javax.annotation.concurrent.GuardedBy;
|
import javax.annotation.concurrent.GuardedBy;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import static android.content.Context.MODE_PRIVATE;
|
||||||
import static android.os.Build.VERSION.SDK_INT;
|
import static android.os.Build.VERSION.SDK_INT;
|
||||||
import static java.util.Arrays.asList;
|
import static java.util.Arrays.asList;
|
||||||
import static java.util.logging.Level.INFO;
|
import static java.util.logging.Level.INFO;
|
||||||
|
import static java.util.logging.Level.WARNING;
|
||||||
|
import static java.util.logging.Logger.getLogger;
|
||||||
import static org.briarproject.bramble.util.IoUtils.deleteFileOrDir;
|
import static org.briarproject.bramble.util.IoUtils.deleteFileOrDir;
|
||||||
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
import static org.briarproject.bramble.util.LogUtils.logFileOrDir;
|
import static org.briarproject.bramble.util.LogUtils.logFileOrDir;
|
||||||
|
|
||||||
class AndroidAccountManager extends AccountManagerImpl
|
class AndroidAccountManager extends AccountManagerImpl
|
||||||
implements AccountManager {
|
implements AccountManager {
|
||||||
|
|
||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
Logger.getLogger(AndroidAccountManager.class.getName());
|
getLogger(AndroidAccountManager.class.getName());
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Directories that shouldn't be deleted when deleting the user's account.
|
* Directories that shouldn't be deleted when deleting the user's account.
|
||||||
@@ -40,13 +46,16 @@ class AndroidAccountManager extends AccountManagerImpl
|
|||||||
|
|
||||||
protected final Context appContext;
|
protected final Context appContext;
|
||||||
private final SharedPreferences prefs;
|
private final SharedPreferences prefs;
|
||||||
|
private final PersistentLogManager logManager;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
AndroidAccountManager(DatabaseConfig databaseConfig,
|
AndroidAccountManager(DatabaseConfig databaseConfig,
|
||||||
CryptoComponent crypto, IdentityManager identityManager,
|
CryptoComponent crypto, IdentityManager identityManager,
|
||||||
SharedPreferences prefs, Application app) {
|
SharedPreferences prefs, PersistentLogManager logManager,
|
||||||
|
Application app) {
|
||||||
super(databaseConfig, crypto, identityManager);
|
super(databaseConfig, crypto, identityManager);
|
||||||
this.prefs = prefs;
|
this.prefs = prefs;
|
||||||
|
this.logManager = logManager;
|
||||||
appContext = app.getApplicationContext();
|
appContext = app.getApplicationContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,6 +83,7 @@ class AndroidAccountManager extends AccountManagerImpl
|
|||||||
LOG.info("Contents of account directory after deleting:");
|
LOG.info("Contents of account directory after deleting:");
|
||||||
logFileOrDir(LOG, INFO, getDataDir());
|
logFileOrDir(LOG, INFO, getDataDir());
|
||||||
}
|
}
|
||||||
|
replacePersistentLogger();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,4 +144,13 @@ class AndroidAccountManager extends AccountManagerImpl
|
|||||||
private void addIfNotNull(Set<File> files, @Nullable File file) {
|
private void addIfNotNull(Set<File> files, @Nullable File file) {
|
||||||
if (file != null) files.add(file);
|
if (file != null) files.add(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void replacePersistentLogger() {
|
||||||
|
File logDir = appContext.getDir("log", MODE_PRIVATE);
|
||||||
|
try {
|
||||||
|
logManager.addLogHandler(logDir, getLogger(""));
|
||||||
|
} catch (IOException e) {
|
||||||
|
logException(LOG, WARNING, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
package org.briarproject.bramble.api.system;
|
|
||||||
|
|
||||||
import android.content.Intent;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|
||||||
|
|
||||||
@NotNullByDefault
|
|
||||||
public interface AlarmListener {
|
|
||||||
|
|
||||||
void onAlarm(Intent intent);
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
package org.briarproject.bramble.api.system;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|
||||||
|
|
||||||
@NotNullByDefault
|
|
||||||
public interface AndroidWakeLock {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Acquires the wake lock. This has no effect if the wake lock has already
|
|
||||||
* been acquired.
|
|
||||||
*/
|
|
||||||
void acquire();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Releases the wake lock. This has no effect if the wake lock has already
|
|
||||||
* been released.
|
|
||||||
*/
|
|
||||||
void release();
|
|
||||||
}
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
package org.briarproject.bramble.api.system;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|
||||||
|
|
||||||
import java.util.concurrent.Executor;
|
|
||||||
|
|
||||||
@NotNullByDefault
|
|
||||||
public interface AndroidWakeLockManager {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a wake lock with the given tag. The tag is only used for
|
|
||||||
* logging; the underlying OS wake lock will use its own tag.
|
|
||||||
*/
|
|
||||||
AndroidWakeLock createWakeLock(String tag);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Runs the given task while holding a wake lock.
|
|
||||||
*/
|
|
||||||
void runWakefully(Runnable r, String tag);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Submits the given task to the given executor while holding a wake lock.
|
|
||||||
* The lock is released when the task completes, or if an exception is
|
|
||||||
* thrown while submitting or running the task.
|
|
||||||
*/
|
|
||||||
void executeWakefully(Runnable r, Executor executor, String tag);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Starts a dedicated thread to run the given task asynchronously. A wake
|
|
||||||
* lock is acquired before starting the thread and released when the task
|
|
||||||
* completes, or if an exception is thrown while starting the thread or
|
|
||||||
* running the task.
|
|
||||||
* <p>
|
|
||||||
* This method should only be used for lifecycle management tasks that
|
|
||||||
* can't be run on an executor.
|
|
||||||
*/
|
|
||||||
void executeWakefully(Runnable r, String tag);
|
|
||||||
}
|
|
||||||
@@ -9,17 +9,16 @@ import android.net.ConnectivityManager;
|
|||||||
import android.net.NetworkInfo;
|
import android.net.NetworkInfo;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.event.EventBus;
|
import org.briarproject.bramble.api.event.EventBus;
|
||||||
import org.briarproject.bramble.api.event.EventExecutor;
|
|
||||||
import org.briarproject.bramble.api.lifecycle.Service;
|
import org.briarproject.bramble.api.lifecycle.Service;
|
||||||
import org.briarproject.bramble.api.network.NetworkManager;
|
import org.briarproject.bramble.api.network.NetworkManager;
|
||||||
import org.briarproject.bramble.api.network.NetworkStatus;
|
import org.briarproject.bramble.api.network.NetworkStatus;
|
||||||
import org.briarproject.bramble.api.network.event.NetworkStatusEvent;
|
import org.briarproject.bramble.api.network.event.NetworkStatusEvent;
|
||||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
import org.briarproject.bramble.api.system.Scheduler;
|
||||||
import org.briarproject.bramble.api.system.TaskScheduler.Cancellable;
|
|
||||||
|
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Future;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
@@ -51,22 +50,20 @@ class AndroidNetworkManager implements NetworkManager, Service {
|
|||||||
private static final String WIFI_AP_STATE_CHANGED_ACTION =
|
private static final String WIFI_AP_STATE_CHANGED_ACTION =
|
||||||
"android.net.wifi.WIFI_AP_STATE_CHANGED";
|
"android.net.wifi.WIFI_AP_STATE_CHANGED";
|
||||||
|
|
||||||
private final TaskScheduler scheduler;
|
private final ScheduledExecutorService scheduler;
|
||||||
private final EventBus eventBus;
|
private final EventBus eventBus;
|
||||||
private final Executor eventExecutor;
|
|
||||||
private final Context appContext;
|
private final Context appContext;
|
||||||
private final AtomicReference<Cancellable> connectivityCheck =
|
private final AtomicReference<Future<?>> connectivityCheck =
|
||||||
new AtomicReference<>();
|
new AtomicReference<>();
|
||||||
private final AtomicBoolean used = new AtomicBoolean(false);
|
private final AtomicBoolean used = new AtomicBoolean(false);
|
||||||
|
|
||||||
private volatile BroadcastReceiver networkStateReceiver = null;
|
private volatile BroadcastReceiver networkStateReceiver = null;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
AndroidNetworkManager(TaskScheduler scheduler, EventBus eventBus,
|
AndroidNetworkManager(@Scheduler ScheduledExecutorService scheduler,
|
||||||
@EventExecutor Executor eventExecutor, Application app) {
|
EventBus eventBus, Application app) {
|
||||||
this.scheduler = scheduler;
|
this.scheduler = scheduler;
|
||||||
this.eventBus = eventBus;
|
this.eventBus = eventBus;
|
||||||
this.eventExecutor = eventExecutor;
|
|
||||||
this.appContext = app.getApplicationContext();
|
this.appContext = app.getApplicationContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,12 +104,11 @@ class AndroidNetworkManager implements NetworkManager, Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void scheduleConnectionStatusUpdate(int delay, TimeUnit unit) {
|
private void scheduleConnectionStatusUpdate(int delay, TimeUnit unit) {
|
||||||
Cancellable newConnectivityCheck =
|
Future<?> newConnectivityCheck =
|
||||||
scheduler.schedule(this::updateConnectionStatus, eventExecutor,
|
scheduler.schedule(this::updateConnectionStatus, delay, unit);
|
||||||
delay, unit);
|
Future<?> oldConnectivityCheck =
|
||||||
Cancellable oldConnectivityCheck =
|
|
||||||
connectivityCheck.getAndSet(newConnectivityCheck);
|
connectivityCheck.getAndSet(newConnectivityCheck);
|
||||||
if (oldConnectivityCheck != null) oldConnectivityCheck.cancel();
|
if (oldConnectivityCheck != null) oldConnectivityCheck.cancel(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private class NetworkStateReceiver extends BroadcastReceiver {
|
private class NetworkStateReceiver extends BroadcastReceiver {
|
||||||
|
|||||||
@@ -1,36 +0,0 @@
|
|||||||
package org.briarproject.bramble.plugin.bluetooth;
|
|
||||||
|
|
||||||
import android.bluetooth.BluetoothSocket;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.io.TimeoutMonitor;
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
|
||||||
import org.briarproject.bramble.api.system.AndroidWakeLockManager;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
@NotNullByDefault
|
|
||||||
class AndroidBluetoothConnectionFactory
|
|
||||||
implements BluetoothConnectionFactory<BluetoothSocket> {
|
|
||||||
|
|
||||||
private final BluetoothConnectionLimiter connectionLimiter;
|
|
||||||
private final AndroidWakeLockManager wakeLockManager;
|
|
||||||
private final TimeoutMonitor timeoutMonitor;
|
|
||||||
|
|
||||||
AndroidBluetoothConnectionFactory(
|
|
||||||
BluetoothConnectionLimiter connectionLimiter,
|
|
||||||
AndroidWakeLockManager wakeLockManager,
|
|
||||||
TimeoutMonitor timeoutMonitor) {
|
|
||||||
this.connectionLimiter = connectionLimiter;
|
|
||||||
this.wakeLockManager = wakeLockManager;
|
|
||||||
this.timeoutMonitor = timeoutMonitor;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public DuplexTransportConnection wrapSocket(DuplexPlugin plugin,
|
|
||||||
BluetoothSocket s) throws IOException {
|
|
||||||
return new AndroidBluetoothTransportConnection(plugin,
|
|
||||||
connectionLimiter, wakeLockManager, timeoutMonitor, s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
package org.briarproject.bramble.plugin.bluetooth;
|
package org.briarproject.bramble.plugin.bluetooth;
|
||||||
|
|
||||||
import android.app.Application;
|
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
import android.bluetooth.BluetoothServerSocket;
|
import android.bluetooth.BluetoothServerSocket;
|
||||||
@@ -10,6 +9,7 @@ import android.content.Context;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.io.TimeoutMonitor;
|
||||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.Backoff;
|
import org.briarproject.bramble.api.plugin.Backoff;
|
||||||
@@ -31,6 +31,7 @@ import java.util.concurrent.BlockingQueue;
|
|||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
@@ -59,40 +60,35 @@ import static org.briarproject.bramble.util.PrivacyUtils.scrubMacAddress;
|
|||||||
|
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@ParametersNotNullByDefault
|
@ParametersNotNullByDefault
|
||||||
class AndroidBluetoothPlugin
|
class AndroidBluetoothPlugin extends BluetoothPlugin<BluetoothServerSocket> {
|
||||||
extends BluetoothPlugin<BluetoothSocket, BluetoothServerSocket> {
|
|
||||||
|
|
||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
getLogger(AndroidBluetoothPlugin.class.getName());
|
getLogger(AndroidBluetoothPlugin.class.getName());
|
||||||
|
|
||||||
private static final int MAX_DISCOVERY_MS = 10_000;
|
private static final int MAX_DISCOVERY_MS = 10_000;
|
||||||
|
|
||||||
|
private final ScheduledExecutorService scheduler;
|
||||||
private final AndroidExecutor androidExecutor;
|
private final AndroidExecutor androidExecutor;
|
||||||
private final Application app;
|
private final Context appContext;
|
||||||
private final Clock clock;
|
private final Clock clock;
|
||||||
|
|
||||||
|
private volatile boolean wasEnabledByUs = false;
|
||||||
private volatile BluetoothStateReceiver receiver = null;
|
private volatile BluetoothStateReceiver receiver = null;
|
||||||
|
|
||||||
// Non-null if the plugin started successfully
|
// Non-null if the plugin started successfully
|
||||||
private volatile BluetoothAdapter adapter = null;
|
private volatile BluetoothAdapter adapter = null;
|
||||||
|
|
||||||
AndroidBluetoothPlugin(BluetoothConnectionLimiter connectionLimiter,
|
AndroidBluetoothPlugin(BluetoothConnectionLimiter connectionLimiter,
|
||||||
BluetoothConnectionFactory<BluetoothSocket> connectionFactory,
|
TimeoutMonitor timeoutMonitor, Executor ioExecutor,
|
||||||
Executor ioExecutor,
|
SecureRandom secureRandom, ScheduledExecutorService scheduler,
|
||||||
Executor wakefulIoExecutor,
|
AndroidExecutor androidExecutor, Context appContext, Clock clock,
|
||||||
SecureRandom secureRandom,
|
Backoff backoff, PluginCallback callback, int maxLatency,
|
||||||
AndroidExecutor androidExecutor,
|
|
||||||
Application app,
|
|
||||||
Clock clock,
|
|
||||||
Backoff backoff,
|
|
||||||
PluginCallback callback,
|
|
||||||
int maxLatency,
|
|
||||||
int maxIdleTime) {
|
int maxIdleTime) {
|
||||||
super(connectionLimiter, connectionFactory, ioExecutor,
|
super(connectionLimiter, timeoutMonitor, ioExecutor, secureRandom,
|
||||||
wakefulIoExecutor, secureRandom, backoff, callback,
|
backoff, callback, maxLatency, maxIdleTime);
|
||||||
maxLatency, maxIdleTime);
|
this.scheduler = scheduler;
|
||||||
this.androidExecutor = androidExecutor;
|
this.androidExecutor = androidExecutor;
|
||||||
this.app = app;
|
this.appContext = appContext;
|
||||||
this.clock = clock;
|
this.clock = clock;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,13 +100,13 @@ class AndroidBluetoothPlugin
|
|||||||
filter.addAction(ACTION_STATE_CHANGED);
|
filter.addAction(ACTION_STATE_CHANGED);
|
||||||
filter.addAction(ACTION_SCAN_MODE_CHANGED);
|
filter.addAction(ACTION_SCAN_MODE_CHANGED);
|
||||||
receiver = new BluetoothStateReceiver();
|
receiver = new BluetoothStateReceiver();
|
||||||
app.registerReceiver(receiver, filter);
|
appContext.registerReceiver(receiver, filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stop() {
|
public void stop() {
|
||||||
super.stop();
|
super.stop();
|
||||||
if (receiver != null) app.unregisterReceiver(receiver);
|
if (receiver != null) appContext.unregisterReceiver(receiver);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -132,11 +128,42 @@ class AndroidBluetoothPlugin
|
|||||||
return adapter != null && adapter.isEnabled();
|
return adapter != null && adapter.isEnabled();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void enableAdapter() {
|
||||||
|
if (adapter != null && !adapter.isEnabled()) {
|
||||||
|
if (adapter.enable()) {
|
||||||
|
LOG.info("Enabling Bluetooth");
|
||||||
|
wasEnabledByUs = true;
|
||||||
|
} else {
|
||||||
|
LOG.info("Could not enable Bluetooth");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void disableAdapterIfEnabledByUs() {
|
||||||
|
if (isAdapterEnabled() && wasEnabledByUs) {
|
||||||
|
if (adapter.disable()) LOG.info("Disabling Bluetooth");
|
||||||
|
else LOG.info("Could not disable Bluetooth");
|
||||||
|
wasEnabledByUs = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void setEnabledByUs() {
|
||||||
|
wasEnabledByUs = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void onAdapterDisabled() {
|
||||||
|
super.onAdapterDisabled();
|
||||||
|
wasEnabledByUs = false;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Nullable
|
@Nullable
|
||||||
String getBluetoothAddress() {
|
String getBluetoothAddress() {
|
||||||
if (adapter == null) return null;
|
String address = AndroidUtils.getBluetoothAddress(appContext, adapter);
|
||||||
String address = AndroidUtils.getBluetoothAddress(app, adapter);
|
|
||||||
return address.isEmpty() ? null : address;
|
return address.isEmpty() ? null : address;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,7 +181,13 @@ class AndroidBluetoothPlugin
|
|||||||
@Override
|
@Override
|
||||||
DuplexTransportConnection acceptConnection(BluetoothServerSocket ss)
|
DuplexTransportConnection acceptConnection(BluetoothServerSocket ss)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
return connectionFactory.wrapSocket(this, ss.accept());
|
return wrapSocket(ss.accept());
|
||||||
|
}
|
||||||
|
|
||||||
|
private DuplexTransportConnection wrapSocket(BluetoothSocket s)
|
||||||
|
throws IOException {
|
||||||
|
return new AndroidBluetoothTransportConnection(this, connectionLimiter,
|
||||||
|
timeoutMonitor, appContext, scheduler, s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -171,7 +204,7 @@ class AndroidBluetoothPlugin
|
|||||||
try {
|
try {
|
||||||
s = d.createInsecureRfcommSocketToServiceRecord(u);
|
s = d.createInsecureRfcommSocketToServiceRecord(u);
|
||||||
s.connect();
|
s.connect();
|
||||||
return connectionFactory.wrapSocket(this, s);
|
return wrapSocket(s);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
IoUtils.tryToClose(s, LOG, WARNING);
|
IoUtils.tryToClose(s, LOG, WARNING);
|
||||||
throw e;
|
throw e;
|
||||||
@@ -206,7 +239,7 @@ class AndroidBluetoothPlugin
|
|||||||
filter.addAction(ACTION_DISCOVERY_STARTED);
|
filter.addAction(ACTION_DISCOVERY_STARTED);
|
||||||
filter.addAction(ACTION_DISCOVERY_FINISHED);
|
filter.addAction(ACTION_DISCOVERY_FINISHED);
|
||||||
filter.addAction(ACTION_FOUND);
|
filter.addAction(ACTION_FOUND);
|
||||||
app.registerReceiver(receiver, filter);
|
appContext.registerReceiver(receiver, filter);
|
||||||
try {
|
try {
|
||||||
if (adapter.startDiscovery()) {
|
if (adapter.startDiscovery()) {
|
||||||
long now = clock.currentTimeMillis();
|
long now = clock.currentTimeMillis();
|
||||||
@@ -243,7 +276,7 @@ class AndroidBluetoothPlugin
|
|||||||
} finally {
|
} finally {
|
||||||
LOG.info("Cancelling discovery");
|
LOG.info("Cancelling discovery");
|
||||||
adapter.cancelDiscovery();
|
adapter.cancelDiscovery();
|
||||||
app.unregisterReceiver(receiver);
|
appContext.unregisterReceiver(receiver);
|
||||||
}
|
}
|
||||||
// Shuffle the addresses so we don't always try the same one first
|
// Shuffle the addresses so we don't always try the same one first
|
||||||
shuffle(addresses);
|
shuffle(addresses);
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
package org.briarproject.bramble.plugin.bluetooth;
|
package org.briarproject.bramble.plugin.bluetooth;
|
||||||
|
|
||||||
import android.app.Application;
|
import android.content.Context;
|
||||||
import android.bluetooth.BluetoothSocket;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.event.EventBus;
|
import org.briarproject.bramble.api.event.EventBus;
|
||||||
import org.briarproject.bramble.api.io.TimeoutMonitor;
|
import org.briarproject.bramble.api.io.TimeoutMonitor;
|
||||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.Backoff;
|
import org.briarproject.bramble.api.plugin.Backoff;
|
||||||
import org.briarproject.bramble.api.plugin.BackoffFactory;
|
import org.briarproject.bramble.api.plugin.BackoffFactory;
|
||||||
@@ -14,15 +12,13 @@ import org.briarproject.bramble.api.plugin.TransportId;
|
|||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
|
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
|
||||||
import org.briarproject.bramble.api.system.AndroidExecutor;
|
import org.briarproject.bramble.api.system.AndroidExecutor;
|
||||||
import org.briarproject.bramble.api.system.AndroidWakeLockManager;
|
|
||||||
import org.briarproject.bramble.api.system.Clock;
|
import org.briarproject.bramble.api.system.Clock;
|
||||||
import org.briarproject.bramble.api.system.WakefulIoExecutor;
|
|
||||||
|
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
|
||||||
import javax.annotation.concurrent.Immutable;
|
import javax.annotation.concurrent.Immutable;
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.ID;
|
import static org.briarproject.bramble.api.plugin.BluetoothConstants.ID;
|
||||||
|
|
||||||
@@ -36,32 +32,25 @@ public class AndroidBluetoothPluginFactory implements DuplexPluginFactory {
|
|||||||
private static final int MAX_POLLING_INTERVAL = 10 * 60 * 1000; // 10 mins
|
private static final int MAX_POLLING_INTERVAL = 10 * 60 * 1000; // 10 mins
|
||||||
private static final double BACKOFF_BASE = 1.2;
|
private static final double BACKOFF_BASE = 1.2;
|
||||||
|
|
||||||
private final Executor ioExecutor, wakefulIoExecutor;
|
private final Executor ioExecutor;
|
||||||
|
private final ScheduledExecutorService scheduler;
|
||||||
private final AndroidExecutor androidExecutor;
|
private final AndroidExecutor androidExecutor;
|
||||||
private final AndroidWakeLockManager wakeLockManager;
|
private final Context appContext;
|
||||||
private final Application app;
|
|
||||||
private final SecureRandom secureRandom;
|
private final SecureRandom secureRandom;
|
||||||
private final EventBus eventBus;
|
private final EventBus eventBus;
|
||||||
private final Clock clock;
|
private final Clock clock;
|
||||||
private final TimeoutMonitor timeoutMonitor;
|
private final TimeoutMonitor timeoutMonitor;
|
||||||
private final BackoffFactory backoffFactory;
|
private final BackoffFactory backoffFactory;
|
||||||
|
|
||||||
@Inject
|
public AndroidBluetoothPluginFactory(Executor ioExecutor,
|
||||||
public AndroidBluetoothPluginFactory(@IoExecutor Executor ioExecutor,
|
ScheduledExecutorService scheduler,
|
||||||
@WakefulIoExecutor Executor wakefulIoExecutor,
|
AndroidExecutor androidExecutor, Context appContext,
|
||||||
AndroidExecutor androidExecutor,
|
SecureRandom secureRandom, EventBus eventBus, Clock clock,
|
||||||
AndroidWakeLockManager wakeLockManager,
|
TimeoutMonitor timeoutMonitor, BackoffFactory backoffFactory) {
|
||||||
Application app,
|
|
||||||
SecureRandom secureRandom,
|
|
||||||
EventBus eventBus,
|
|
||||||
Clock clock,
|
|
||||||
TimeoutMonitor timeoutMonitor,
|
|
||||||
BackoffFactory backoffFactory) {
|
|
||||||
this.ioExecutor = ioExecutor;
|
this.ioExecutor = ioExecutor;
|
||||||
this.wakefulIoExecutor = wakefulIoExecutor;
|
this.scheduler = scheduler;
|
||||||
this.androidExecutor = androidExecutor;
|
this.androidExecutor = androidExecutor;
|
||||||
this.wakeLockManager = wakeLockManager;
|
this.appContext = appContext;
|
||||||
this.app = app;
|
|
||||||
this.secureRandom = secureRandom;
|
this.secureRandom = secureRandom;
|
||||||
this.eventBus = eventBus;
|
this.eventBus = eventBus;
|
||||||
this.clock = clock;
|
this.clock = clock;
|
||||||
@@ -83,15 +72,12 @@ public class AndroidBluetoothPluginFactory implements DuplexPluginFactory {
|
|||||||
public DuplexPlugin createPlugin(PluginCallback callback) {
|
public DuplexPlugin createPlugin(PluginCallback callback) {
|
||||||
BluetoothConnectionLimiter connectionLimiter =
|
BluetoothConnectionLimiter connectionLimiter =
|
||||||
new BluetoothConnectionLimiterImpl(eventBus);
|
new BluetoothConnectionLimiterImpl(eventBus);
|
||||||
BluetoothConnectionFactory<BluetoothSocket> connectionFactory =
|
|
||||||
new AndroidBluetoothConnectionFactory(connectionLimiter,
|
|
||||||
wakeLockManager, timeoutMonitor);
|
|
||||||
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
||||||
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
||||||
AndroidBluetoothPlugin plugin = new AndroidBluetoothPlugin(
|
AndroidBluetoothPlugin plugin = new AndroidBluetoothPlugin(
|
||||||
connectionLimiter, connectionFactory, ioExecutor,
|
connectionLimiter, timeoutMonitor, ioExecutor, secureRandom,
|
||||||
wakefulIoExecutor, secureRandom, androidExecutor, app,
|
scheduler, androidExecutor, appContext, clock, backoff,
|
||||||
clock, backoff, callback, MAX_LATENCY, MAX_IDLE_TIME);
|
callback, MAX_LATENCY, MAX_IDLE_TIME);
|
||||||
eventBus.addListener(plugin);
|
eventBus.addListener(plugin);
|
||||||
return plugin;
|
return plugin;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,26 @@
|
|||||||
package org.briarproject.bramble.plugin.bluetooth;
|
package org.briarproject.bramble.plugin.bluetooth;
|
||||||
|
|
||||||
import android.bluetooth.BluetoothSocket;
|
import android.bluetooth.BluetoothSocket;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.os.PowerManager;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.io.TimeoutMonitor;
|
import org.briarproject.bramble.api.io.TimeoutMonitor;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.Plugin;
|
import org.briarproject.bramble.api.plugin.Plugin;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.AbstractDuplexTransportConnection;
|
import org.briarproject.bramble.api.plugin.duplex.AbstractDuplexTransportConnection;
|
||||||
import org.briarproject.bramble.api.system.AndroidWakeLock;
|
import org.briarproject.bramble.util.RenewableWakeLock;
|
||||||
import org.briarproject.bramble.api.system.AndroidWakeLockManager;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
|
||||||
|
import static android.content.Context.POWER_SERVICE;
|
||||||
|
import static android.os.PowerManager.PARTIAL_WAKE_LOCK;
|
||||||
|
import static java.util.concurrent.TimeUnit.MINUTES;
|
||||||
|
import static org.briarproject.bramble.api.nullsafety.NullSafety.requireNonNull;
|
||||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PROP_ADDRESS;
|
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PROP_ADDRESS;
|
||||||
|
import static org.briarproject.bramble.util.AndroidUtils.getWakeLockTag;
|
||||||
import static org.briarproject.bramble.util.AndroidUtils.isValidBluetoothAddress;
|
import static org.briarproject.bramble.util.AndroidUtils.isValidBluetoothAddress;
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
@@ -21,21 +28,25 @@ class AndroidBluetoothTransportConnection
|
|||||||
extends AbstractDuplexTransportConnection {
|
extends AbstractDuplexTransportConnection {
|
||||||
|
|
||||||
private final BluetoothConnectionLimiter connectionLimiter;
|
private final BluetoothConnectionLimiter connectionLimiter;
|
||||||
|
private final RenewableWakeLock wakeLock;
|
||||||
private final BluetoothSocket socket;
|
private final BluetoothSocket socket;
|
||||||
private final InputStream in;
|
private final InputStream in;
|
||||||
private final AndroidWakeLock wakeLock;
|
|
||||||
|
|
||||||
AndroidBluetoothTransportConnection(Plugin plugin,
|
AndroidBluetoothTransportConnection(Plugin plugin,
|
||||||
BluetoothConnectionLimiter connectionLimiter,
|
BluetoothConnectionLimiter connectionLimiter,
|
||||||
AndroidWakeLockManager wakeLockManager,
|
TimeoutMonitor timeoutMonitor, Context appContext,
|
||||||
TimeoutMonitor timeoutMonitor,
|
ScheduledExecutorService scheduler, BluetoothSocket socket)
|
||||||
BluetoothSocket socket) throws IOException {
|
throws IOException {
|
||||||
super(plugin);
|
super(plugin);
|
||||||
this.connectionLimiter = connectionLimiter;
|
this.connectionLimiter = connectionLimiter;
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
in = timeoutMonitor.createTimeoutInputStream(
|
in = timeoutMonitor.createTimeoutInputStream(
|
||||||
socket.getInputStream(), plugin.getMaxIdleTime() * 2);
|
socket.getInputStream(), plugin.getMaxIdleTime() * 2);
|
||||||
wakeLock = wakeLockManager.createWakeLock("BluetoothConnection");
|
PowerManager powerManager = (PowerManager)
|
||||||
|
requireNonNull(appContext.getSystemService(POWER_SERVICE));
|
||||||
|
String tag = getWakeLockTag(appContext);
|
||||||
|
wakeLock = new RenewableWakeLock(powerManager, scheduler,
|
||||||
|
PARTIAL_WAKE_LOCK, tag, 1, MINUTES);
|
||||||
wakeLock.acquire();
|
wakeLock.acquire();
|
||||||
String address = socket.getRemoteDevice().getAddress();
|
String address = socket.getRemoteDevice().getAddress();
|
||||||
if (isValidBluetoothAddress(address)) remote.put(PROP_ADDRESS, address);
|
if (isValidBluetoothAddress(address)) remote.put(PROP_ADDRESS, address);
|
||||||
@@ -55,7 +66,6 @@ class AndroidBluetoothTransportConnection
|
|||||||
protected void closeConnection(boolean exception) throws IOException {
|
protected void closeConnection(boolean exception) throws IOException {
|
||||||
try {
|
try {
|
||||||
socket.close();
|
socket.close();
|
||||||
in.close();
|
|
||||||
} finally {
|
} finally {
|
||||||
wakeLock.release();
|
wakeLock.release();
|
||||||
connectionLimiter.connectionClosed(this);
|
connectionLimiter.connectionClosed(this);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package org.briarproject.bramble.plugin.tcp;
|
package org.briarproject.bramble.plugin.tcp;
|
||||||
|
|
||||||
import android.annotation.TargetApi;
|
import android.annotation.TargetApi;
|
||||||
import android.app.Application;
|
import android.content.Context;
|
||||||
import android.net.ConnectivityManager;
|
import android.net.ConnectivityManager;
|
||||||
import android.net.LinkAddress;
|
import android.net.LinkAddress;
|
||||||
import android.net.LinkProperties;
|
import android.net.LinkProperties;
|
||||||
@@ -42,7 +42,6 @@ import static java.util.Collections.list;
|
|||||||
import static java.util.Collections.singletonList;
|
import static java.util.Collections.singletonList;
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static java.util.logging.Logger.getLogger;
|
import static java.util.logging.Logger.getLogger;
|
||||||
import static org.briarproject.bramble.api.nullsafety.NullSafety.requireNonNull;
|
|
||||||
import static org.briarproject.bramble.api.plugin.LanTcpConstants.DEFAULT_PREF_PLUGIN_ENABLE;
|
import static org.briarproject.bramble.api.plugin.LanTcpConstants.DEFAULT_PREF_PLUGIN_ENABLE;
|
||||||
import static org.briarproject.bramble.api.plugin.Plugin.State.ACTIVE;
|
import static org.briarproject.bramble.api.plugin.Plugin.State.ACTIVE;
|
||||||
import static org.briarproject.bramble.api.plugin.Plugin.State.INACTIVE;
|
import static org.briarproject.bramble.api.plugin.Plugin.State.INACTIVE;
|
||||||
@@ -62,22 +61,20 @@ class AndroidLanTcpPlugin extends LanTcpPlugin {
|
|||||||
|
|
||||||
private volatile SocketFactory socketFactory;
|
private volatile SocketFactory socketFactory;
|
||||||
|
|
||||||
AndroidLanTcpPlugin(Executor ioExecutor,
|
AndroidLanTcpPlugin(Executor ioExecutor, Context appContext,
|
||||||
Executor wakefulIoExecutor,
|
Backoff backoff, PluginCallback callback, int maxLatency,
|
||||||
Application app,
|
int maxIdleTime, int connectionTimeout) {
|
||||||
Backoff backoff,
|
super(ioExecutor, backoff, callback, maxLatency, maxIdleTime,
|
||||||
PluginCallback callback,
|
connectionTimeout);
|
||||||
int maxLatency,
|
|
||||||
int maxIdleTime,
|
|
||||||
int connectionTimeout) {
|
|
||||||
super(ioExecutor, wakefulIoExecutor, backoff, callback, maxLatency,
|
|
||||||
maxIdleTime, connectionTimeout);
|
|
||||||
// Don't execute more than one connection status check at a time
|
// Don't execute more than one connection status check at a time
|
||||||
connectionStatusExecutor =
|
connectionStatusExecutor =
|
||||||
new PoliteExecutor("AndroidLanTcpPlugin", ioExecutor, 1);
|
new PoliteExecutor("AndroidLanTcpPlugin", ioExecutor, 1);
|
||||||
connectivityManager = (ConnectivityManager)
|
ConnectivityManager connectivityManager = (ConnectivityManager)
|
||||||
requireNonNull(app.getSystemService(CONNECTIVITY_SERVICE));
|
appContext.getSystemService(CONNECTIVITY_SERVICE);
|
||||||
wifiManager = (WifiManager) app.getSystemService(WIFI_SERVICE);
|
if (connectivityManager == null) throw new AssertionError();
|
||||||
|
this.connectivityManager = connectivityManager;
|
||||||
|
wifiManager = (WifiManager) appContext.getApplicationContext()
|
||||||
|
.getSystemService(WIFI_SERVICE);
|
||||||
socketFactory = SocketFactory.getDefault();
|
socketFactory = SocketFactory.getDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -277,11 +274,11 @@ class AndroidLanTcpPlugin extends LanTcpPlugin {
|
|||||||
// make outgoing connections on API 21+ if another network
|
// make outgoing connections on API 21+ if another network
|
||||||
// has internet access
|
// has internet access
|
||||||
socketFactory = SocketFactory.getDefault();
|
socketFactory = SocketFactory.getDefault();
|
||||||
bind();
|
if (s == INACTIVE) bind();
|
||||||
} else {
|
} else {
|
||||||
LOG.info("Connected to wifi");
|
LOG.info("Connected to wifi");
|
||||||
socketFactory = getSocketFactory();
|
socketFactory = getSocketFactory();
|
||||||
bind();
|
if (s == INACTIVE) bind();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
package org.briarproject.bramble.plugin.tcp;
|
package org.briarproject.bramble.plugin.tcp;
|
||||||
|
|
||||||
import android.app.Application;
|
import android.content.Context;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.event.EventBus;
|
import org.briarproject.bramble.api.event.EventBus;
|
||||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.Backoff;
|
import org.briarproject.bramble.api.plugin.Backoff;
|
||||||
import org.briarproject.bramble.api.plugin.BackoffFactory;
|
import org.briarproject.bramble.api.plugin.BackoffFactory;
|
||||||
@@ -11,12 +10,10 @@ import org.briarproject.bramble.api.plugin.PluginCallback;
|
|||||||
import org.briarproject.bramble.api.plugin.TransportId;
|
import org.briarproject.bramble.api.plugin.TransportId;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
|
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
|
||||||
import org.briarproject.bramble.api.system.WakefulIoExecutor;
|
|
||||||
|
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
import javax.annotation.concurrent.Immutable;
|
import javax.annotation.concurrent.Immutable;
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
import static org.briarproject.bramble.api.plugin.LanTcpConstants.ID;
|
import static org.briarproject.bramble.api.plugin.LanTcpConstants.ID;
|
||||||
|
|
||||||
@@ -31,22 +28,17 @@ public class AndroidLanTcpPluginFactory implements DuplexPluginFactory {
|
|||||||
private static final int MAX_POLLING_INTERVAL = 600_000; // 10 mins
|
private static final int MAX_POLLING_INTERVAL = 600_000; // 10 mins
|
||||||
private static final double BACKOFF_BASE = 1.2;
|
private static final double BACKOFF_BASE = 1.2;
|
||||||
|
|
||||||
private final Executor ioExecutor, wakefulIoExecutor;
|
private final Executor ioExecutor;
|
||||||
private final EventBus eventBus;
|
private final EventBus eventBus;
|
||||||
private final BackoffFactory backoffFactory;
|
private final BackoffFactory backoffFactory;
|
||||||
private final Application app;
|
private final Context appContext;
|
||||||
|
|
||||||
@Inject
|
public AndroidLanTcpPluginFactory(Executor ioExecutor, EventBus eventBus,
|
||||||
public AndroidLanTcpPluginFactory(@IoExecutor Executor ioExecutor,
|
BackoffFactory backoffFactory, Context appContext) {
|
||||||
@WakefulIoExecutor Executor wakefulIoExecutor,
|
|
||||||
EventBus eventBus,
|
|
||||||
BackoffFactory backoffFactory,
|
|
||||||
Application app) {
|
|
||||||
this.ioExecutor = ioExecutor;
|
this.ioExecutor = ioExecutor;
|
||||||
this.wakefulIoExecutor = wakefulIoExecutor;
|
|
||||||
this.eventBus = eventBus;
|
this.eventBus = eventBus;
|
||||||
this.backoffFactory = backoffFactory;
|
this.backoffFactory = backoffFactory;
|
||||||
this.app = app;
|
this.appContext = appContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -64,8 +56,8 @@ public class AndroidLanTcpPluginFactory implements DuplexPluginFactory {
|
|||||||
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
||||||
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
||||||
AndroidLanTcpPlugin plugin = new AndroidLanTcpPlugin(ioExecutor,
|
AndroidLanTcpPlugin plugin = new AndroidLanTcpPlugin(ioExecutor,
|
||||||
wakefulIoExecutor, app, backoff, callback,
|
appContext, backoff, callback, MAX_LATENCY, MAX_IDLE_TIME,
|
||||||
MAX_LATENCY, MAX_IDLE_TIME, CONNECTION_TIMEOUT);
|
CONNECTION_TIMEOUT);
|
||||||
eventBus.addListener(plugin);
|
eventBus.addListener(plugin);
|
||||||
return plugin;
|
return plugin;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
package org.briarproject.bramble.plugin.tor;
|
package org.briarproject.bramble.plugin.tor;
|
||||||
|
|
||||||
import android.app.Application;
|
import android.content.Context;
|
||||||
import android.content.pm.PackageInfo;
|
import android.content.pm.PackageInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.content.pm.PackageManager.NameNotFoundException;
|
import android.content.pm.PackageManager.NameNotFoundException;
|
||||||
|
import android.os.PowerManager;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.battery.BatteryManager;
|
import org.briarproject.bramble.api.battery.BatteryManager;
|
||||||
import org.briarproject.bramble.api.network.NetworkManager;
|
import org.briarproject.bramble.api.network.NetworkManager;
|
||||||
@@ -11,76 +12,49 @@ import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
|||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.Backoff;
|
import org.briarproject.bramble.api.plugin.Backoff;
|
||||||
import org.briarproject.bramble.api.plugin.PluginCallback;
|
import org.briarproject.bramble.api.plugin.PluginCallback;
|
||||||
import org.briarproject.bramble.api.system.AndroidWakeLock;
|
|
||||||
import org.briarproject.bramble.api.system.AndroidWakeLockManager;
|
|
||||||
import org.briarproject.bramble.api.system.Clock;
|
import org.briarproject.bramble.api.system.Clock;
|
||||||
import org.briarproject.bramble.api.system.LocationUtils;
|
import org.briarproject.bramble.api.system.LocationUtils;
|
||||||
import org.briarproject.bramble.api.system.ResourceProvider;
|
import org.briarproject.bramble.api.system.ResourceProvider;
|
||||||
import org.briarproject.bramble.util.AndroidUtils;
|
import org.briarproject.bramble.util.RenewableWakeLock;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.logging.Logger;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.zip.ZipEntry;
|
|
||||||
import java.util.zip.ZipInputStream;
|
|
||||||
|
|
||||||
import javax.net.SocketFactory;
|
import javax.net.SocketFactory;
|
||||||
|
|
||||||
import static android.os.Build.VERSION.SDK_INT;
|
import static android.content.Context.MODE_PRIVATE;
|
||||||
import static java.util.Arrays.asList;
|
import static android.content.Context.POWER_SERVICE;
|
||||||
import static java.util.logging.Level.INFO;
|
import static android.os.PowerManager.PARTIAL_WAKE_LOCK;
|
||||||
import static java.util.logging.Logger.getLogger;
|
import static java.util.concurrent.TimeUnit.MINUTES;
|
||||||
|
import static org.briarproject.bramble.util.AndroidUtils.getWakeLockTag;
|
||||||
|
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@ParametersNotNullByDefault
|
@ParametersNotNullByDefault
|
||||||
class AndroidTorPlugin extends TorPlugin {
|
class AndroidTorPlugin extends TorPlugin {
|
||||||
|
|
||||||
private static final List<String> LIBRARY_ARCHITECTURES =
|
private final Context appContext;
|
||||||
asList("armeabi-v7a", "arm64-v8a", "x86", "x86_64");
|
private final RenewableWakeLock wakeLock;
|
||||||
|
|
||||||
private static final String TOR_LIB_NAME = "libtor.so";
|
AndroidTorPlugin(Executor ioExecutor, ScheduledExecutorService scheduler,
|
||||||
private static final String OBFS4_LIB_NAME = "libobfs4proxy.so";
|
Context appContext, NetworkManager networkManager,
|
||||||
|
LocationUtils locationUtils, SocketFactory torSocketFactory,
|
||||||
private static final Logger LOG =
|
Clock clock, ResourceProvider resourceProvider,
|
||||||
getLogger(AndroidTorPlugin.class.getName());
|
|
||||||
|
|
||||||
private final Application app;
|
|
||||||
private final AndroidWakeLock wakeLock;
|
|
||||||
private final File torLib, obfs4Lib;
|
|
||||||
|
|
||||||
AndroidTorPlugin(Executor ioExecutor,
|
|
||||||
Executor wakefulIoExecutor,
|
|
||||||
Application app,
|
|
||||||
NetworkManager networkManager,
|
|
||||||
LocationUtils locationUtils,
|
|
||||||
SocketFactory torSocketFactory,
|
|
||||||
Clock clock,
|
|
||||||
ResourceProvider resourceProvider,
|
|
||||||
CircumventionProvider circumventionProvider,
|
CircumventionProvider circumventionProvider,
|
||||||
BatteryManager batteryManager,
|
BatteryManager batteryManager, Backoff backoff,
|
||||||
AndroidWakeLockManager wakeLockManager,
|
|
||||||
Backoff backoff,
|
|
||||||
TorRendezvousCrypto torRendezvousCrypto,
|
TorRendezvousCrypto torRendezvousCrypto,
|
||||||
PluginCallback callback,
|
PluginCallback callback, String architecture, int maxLatency,
|
||||||
String architecture,
|
int maxIdleTime) {
|
||||||
int maxLatency,
|
super(ioExecutor, networkManager, locationUtils, torSocketFactory,
|
||||||
int maxIdleTime,
|
clock, resourceProvider, circumventionProvider, batteryManager,
|
||||||
File torDirectory) {
|
backoff, torRendezvousCrypto, callback, architecture, maxLatency, maxIdleTime,
|
||||||
super(ioExecutor, wakefulIoExecutor, networkManager, locationUtils,
|
appContext.getDir("tor", MODE_PRIVATE));
|
||||||
torSocketFactory, clock, resourceProvider,
|
this.appContext = appContext;
|
||||||
circumventionProvider, batteryManager, backoff,
|
PowerManager pm = (PowerManager)
|
||||||
torRendezvousCrypto, callback, architecture, maxLatency,
|
appContext.getSystemService(POWER_SERVICE);
|
||||||
maxIdleTime, torDirectory);
|
if (pm == null) throw new AssertionError();
|
||||||
this.app = app;
|
wakeLock = new RenewableWakeLock(pm, scheduler, PARTIAL_WAKE_LOCK,
|
||||||
wakeLock = wakeLockManager.createWakeLock("TorPlugin");
|
getWakeLockTag(appContext), 1, MINUTES);
|
||||||
String nativeLibDir = app.getApplicationInfo().nativeLibraryDir;
|
|
||||||
torLib = new File(nativeLibDir, TOR_LIB_NAME);
|
|
||||||
obfs4Lib = new File(nativeLibDir, OBFS4_LIB_NAME);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -91,8 +65,8 @@ class AndroidTorPlugin extends TorPlugin {
|
|||||||
@Override
|
@Override
|
||||||
protected long getLastUpdateTime() {
|
protected long getLastUpdateTime() {
|
||||||
try {
|
try {
|
||||||
PackageManager pm = app.getPackageManager();
|
PackageManager pm = appContext.getPackageManager();
|
||||||
PackageInfo pi = pm.getPackageInfo(app.getPackageName(), 0);
|
PackageInfo pi = pm.getPackageInfo(appContext.getPackageName(), 0);
|
||||||
return pi.lastUpdateTime;
|
return pi.lastUpdateTime;
|
||||||
} catch (NameNotFoundException e) {
|
} catch (NameNotFoundException e) {
|
||||||
throw new AssertionError(e);
|
throw new AssertionError(e);
|
||||||
@@ -111,112 +85,4 @@ class AndroidTorPlugin extends TorPlugin {
|
|||||||
super.stop();
|
super.stop();
|
||||||
wakeLock.release();
|
wakeLock.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected File getTorExecutableFile() {
|
|
||||||
return torLib.exists() ? torLib : super.getTorExecutableFile();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected File getObfs4ExecutableFile() {
|
|
||||||
return obfs4Lib.exists() ? obfs4Lib : super.getObfs4ExecutableFile();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void installTorExecutable() throws IOException {
|
|
||||||
File extracted = super.getTorExecutableFile();
|
|
||||||
if (torLib.exists()) {
|
|
||||||
// If an older version left behind a Tor binary, delete it
|
|
||||||
if (extracted.exists()) {
|
|
||||||
if (extracted.delete()) LOG.info("Deleted Tor binary");
|
|
||||||
else LOG.info("Failed to delete Tor binary");
|
|
||||||
}
|
|
||||||
} else if (SDK_INT < 29) {
|
|
||||||
// The binary wasn't extracted at install time. Try to extract it
|
|
||||||
extractLibraryFromApk(TOR_LIB_NAME, extracted);
|
|
||||||
} else {
|
|
||||||
// No point extracting the binary, we won't be allowed to execute it
|
|
||||||
throw new FileNotFoundException(torLib.getAbsolutePath());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void installObfs4Executable() throws IOException {
|
|
||||||
File extracted = super.getObfs4ExecutableFile();
|
|
||||||
if (obfs4Lib.exists()) {
|
|
||||||
// If an older version left behind an obfs4 binary, delete it
|
|
||||||
if (extracted.exists()) {
|
|
||||||
if (extracted.delete()) LOG.info("Deleted obfs4 binary");
|
|
||||||
else LOG.info("Failed to delete obfs4 binary");
|
|
||||||
}
|
|
||||||
} else if (SDK_INT < 29) {
|
|
||||||
// The binary wasn't extracted at install time. Try to extract it
|
|
||||||
extractLibraryFromApk(OBFS4_LIB_NAME, extracted);
|
|
||||||
} else {
|
|
||||||
// No point extracting the binary, we won't be allowed to execute it
|
|
||||||
throw new FileNotFoundException(obfs4Lib.getAbsolutePath());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void extractLibraryFromApk(String libName, File dest)
|
|
||||||
throws IOException {
|
|
||||||
File sourceDir = new File(app.getApplicationInfo().sourceDir);
|
|
||||||
if (sourceDir.isFile()) {
|
|
||||||
// Look for other APK files in the same directory, if we're allowed
|
|
||||||
File parent = sourceDir.getParentFile();
|
|
||||||
if (parent != null) sourceDir = parent;
|
|
||||||
}
|
|
||||||
List<String> libPaths = getSupportedLibraryPaths(libName);
|
|
||||||
for (File apk : findApkFiles(sourceDir)) {
|
|
||||||
ZipInputStream zin = new ZipInputStream(new FileInputStream(apk));
|
|
||||||
for (ZipEntry e = zin.getNextEntry(); e != null;
|
|
||||||
e = zin.getNextEntry()) {
|
|
||||||
if (libPaths.contains(e.getName())) {
|
|
||||||
if (LOG.isLoggable(INFO)) {
|
|
||||||
LOG.info("Extracting " + e.getName()
|
|
||||||
+ " from " + apk.getAbsolutePath());
|
|
||||||
}
|
|
||||||
extract(zin, dest); // Zip input stream will be closed
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
zin.close();
|
|
||||||
}
|
|
||||||
throw new FileNotFoundException(libName);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns all files with the extension .apk or .APK under the given root.
|
|
||||||
*/
|
|
||||||
private List<File> findApkFiles(File root) {
|
|
||||||
List<File> files = new ArrayList<>();
|
|
||||||
findApkFiles(root, files);
|
|
||||||
return files;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void findApkFiles(File f, List<File> files) {
|
|
||||||
if (f.isFile() && f.getName().toLowerCase().endsWith(".apk")) {
|
|
||||||
files.add(f);
|
|
||||||
} else if (f.isDirectory()) {
|
|
||||||
File[] children = f.listFiles();
|
|
||||||
if (children != null) {
|
|
||||||
for (File child : children) findApkFiles(child, files);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the paths at which libraries with the given name would be found
|
|
||||||
* inside an APK file, for all architectures supported by the device, in
|
|
||||||
* order of preference.
|
|
||||||
*/
|
|
||||||
private List<String> getSupportedLibraryPaths(String libName) {
|
|
||||||
List<String> architectures = new ArrayList<>();
|
|
||||||
for (String abi : AndroidUtils.getSupportedArchitectures()) {
|
|
||||||
if (LIBRARY_ARCHITECTURES.contains(abi)) {
|
|
||||||
architectures.add("lib/" + abi + "/" + libName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return architectures;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,33 +1,28 @@
|
|||||||
package org.briarproject.bramble.plugin.tor;
|
package org.briarproject.bramble.plugin.tor;
|
||||||
|
|
||||||
import android.app.Application;
|
import android.content.Context;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.battery.BatteryManager;
|
import org.briarproject.bramble.api.battery.BatteryManager;
|
||||||
import org.briarproject.bramble.api.event.EventBus;
|
import org.briarproject.bramble.api.event.EventBus;
|
||||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
|
||||||
import org.briarproject.bramble.api.network.NetworkManager;
|
import org.briarproject.bramble.api.network.NetworkManager;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.Backoff;
|
import org.briarproject.bramble.api.plugin.Backoff;
|
||||||
import org.briarproject.bramble.api.plugin.BackoffFactory;
|
import org.briarproject.bramble.api.plugin.BackoffFactory;
|
||||||
import org.briarproject.bramble.api.plugin.PluginCallback;
|
import org.briarproject.bramble.api.plugin.PluginCallback;
|
||||||
import org.briarproject.bramble.api.plugin.TorConstants;
|
import org.briarproject.bramble.api.plugin.TorConstants;
|
||||||
import org.briarproject.bramble.api.plugin.TorDirectory;
|
|
||||||
import org.briarproject.bramble.api.plugin.TransportId;
|
import org.briarproject.bramble.api.plugin.TransportId;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
|
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
|
||||||
import org.briarproject.bramble.api.system.AndroidWakeLockManager;
|
|
||||||
import org.briarproject.bramble.api.system.Clock;
|
import org.briarproject.bramble.api.system.Clock;
|
||||||
import org.briarproject.bramble.api.system.LocationUtils;
|
import org.briarproject.bramble.api.system.LocationUtils;
|
||||||
import org.briarproject.bramble.api.system.ResourceProvider;
|
import org.briarproject.bramble.api.system.ResourceProvider;
|
||||||
import org.briarproject.bramble.api.system.WakefulIoExecutor;
|
|
||||||
import org.briarproject.bramble.util.AndroidUtils;
|
import org.briarproject.bramble.util.AndroidUtils;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.annotation.concurrent.Immutable;
|
import javax.annotation.concurrent.Immutable;
|
||||||
import javax.inject.Inject;
|
|
||||||
import javax.net.SocketFactory;
|
import javax.net.SocketFactory;
|
||||||
|
|
||||||
@Immutable
|
@Immutable
|
||||||
@@ -43,8 +38,9 @@ public class AndroidTorPluginFactory implements DuplexPluginFactory {
|
|||||||
private static final int MAX_POLLING_INTERVAL = 10 * 60 * 1000; // 10 mins
|
private static final int MAX_POLLING_INTERVAL = 10 * 60 * 1000; // 10 mins
|
||||||
private static final double BACKOFF_BASE = 1.2;
|
private static final double BACKOFF_BASE = 1.2;
|
||||||
|
|
||||||
private final Executor ioExecutor, wakefulIoExecutor;
|
private final Executor ioExecutor;
|
||||||
private final Application app;
|
private final ScheduledExecutorService scheduler;
|
||||||
|
private final Context appContext;
|
||||||
private final NetworkManager networkManager;
|
private final NetworkManager networkManager;
|
||||||
private final LocationUtils locationUtils;
|
private final LocationUtils locationUtils;
|
||||||
private final EventBus eventBus;
|
private final EventBus eventBus;
|
||||||
@@ -53,28 +49,18 @@ public class AndroidTorPluginFactory implements DuplexPluginFactory {
|
|||||||
private final ResourceProvider resourceProvider;
|
private final ResourceProvider resourceProvider;
|
||||||
private final CircumventionProvider circumventionProvider;
|
private final CircumventionProvider circumventionProvider;
|
||||||
private final BatteryManager batteryManager;
|
private final BatteryManager batteryManager;
|
||||||
private final AndroidWakeLockManager wakeLockManager;
|
|
||||||
private final Clock clock;
|
private final Clock clock;
|
||||||
private final File torDirectory;
|
|
||||||
|
|
||||||
@Inject
|
public AndroidTorPluginFactory(Executor ioExecutor,
|
||||||
public AndroidTorPluginFactory(@IoExecutor Executor ioExecutor,
|
ScheduledExecutorService scheduler, Context appContext,
|
||||||
@WakefulIoExecutor Executor wakefulIoExecutor,
|
NetworkManager networkManager, LocationUtils locationUtils,
|
||||||
Application app,
|
EventBus eventBus, SocketFactory torSocketFactory,
|
||||||
NetworkManager networkManager,
|
BackoffFactory backoffFactory, ResourceProvider resourceProvider,
|
||||||
LocationUtils locationUtils,
|
|
||||||
EventBus eventBus,
|
|
||||||
SocketFactory torSocketFactory,
|
|
||||||
BackoffFactory backoffFactory,
|
|
||||||
ResourceProvider resourceProvider,
|
|
||||||
CircumventionProvider circumventionProvider,
|
CircumventionProvider circumventionProvider,
|
||||||
BatteryManager batteryManager,
|
BatteryManager batteryManager, Clock clock) {
|
||||||
AndroidWakeLockManager wakeLockManager,
|
|
||||||
Clock clock,
|
|
||||||
@TorDirectory File torDirectory) {
|
|
||||||
this.ioExecutor = ioExecutor;
|
this.ioExecutor = ioExecutor;
|
||||||
this.wakefulIoExecutor = wakefulIoExecutor;
|
this.scheduler = scheduler;
|
||||||
this.app = app;
|
this.appContext = appContext;
|
||||||
this.networkManager = networkManager;
|
this.networkManager = networkManager;
|
||||||
this.locationUtils = locationUtils;
|
this.locationUtils = locationUtils;
|
||||||
this.eventBus = eventBus;
|
this.eventBus = eventBus;
|
||||||
@@ -83,9 +69,7 @@ public class AndroidTorPluginFactory implements DuplexPluginFactory {
|
|||||||
this.resourceProvider = resourceProvider;
|
this.resourceProvider = resourceProvider;
|
||||||
this.circumventionProvider = circumventionProvider;
|
this.circumventionProvider = circumventionProvider;
|
||||||
this.batteryManager = batteryManager;
|
this.batteryManager = batteryManager;
|
||||||
this.wakeLockManager = wakeLockManager;
|
|
||||||
this.clock = clock;
|
this.clock = clock;
|
||||||
this.torDirectory = torDirectory;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -128,12 +112,11 @@ public class AndroidTorPluginFactory implements DuplexPluginFactory {
|
|||||||
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
||||||
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
||||||
TorRendezvousCrypto torRendezvousCrypto = new TorRendezvousCryptoImpl();
|
TorRendezvousCrypto torRendezvousCrypto = new TorRendezvousCryptoImpl();
|
||||||
AndroidTorPlugin plugin = new AndroidTorPlugin(ioExecutor,
|
AndroidTorPlugin plugin = new AndroidTorPlugin(ioExecutor, scheduler,
|
||||||
wakefulIoExecutor, app, networkManager, locationUtils,
|
appContext, networkManager, locationUtils, torSocketFactory,
|
||||||
torSocketFactory, clock, resourceProvider,
|
clock, resourceProvider, circumventionProvider, batteryManager,
|
||||||
circumventionProvider, batteryManager, wakeLockManager,
|
|
||||||
backoff, torRendezvousCrypto, callback, architecture,
|
backoff, torRendezvousCrypto, callback, architecture,
|
||||||
MAX_LATENCY, MAX_IDLE_TIME, torDirectory);
|
MAX_LATENCY, MAX_IDLE_TIME);
|
||||||
eventBus.addListener(plugin);
|
eventBus.addListener(plugin);
|
||||||
return plugin;
|
return plugin;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
package org.briarproject.bramble.system;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|
||||||
|
|
||||||
@NotNullByDefault
|
|
||||||
interface AlarmConstants {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Request code for the broadcast intent attached to the periodic alarm.
|
|
||||||
*/
|
|
||||||
int REQUEST_ALARM = 1;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Key for storing the process ID in the extras of the periodic alarm's
|
|
||||||
* intent. This allows us to ignore alarms scheduled by dead processes.
|
|
||||||
*/
|
|
||||||
String EXTRA_PID = "org.briarproject.bramble.EXTRA_PID";
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
package org.briarproject.bramble.system;
|
|
||||||
|
|
||||||
import android.content.BroadcastReceiver;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.Intent;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.BrambleApplication;
|
|
||||||
|
|
||||||
public class AlarmReceiver extends BroadcastReceiver {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onReceive(Context ctx, Intent intent) {
|
|
||||||
BrambleApplication app =
|
|
||||||
(BrambleApplication) ctx.getApplicationContext();
|
|
||||||
app.getBrambleAppComponent().alarmListener().onAlarm(intent);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +1,12 @@
|
|||||||
package org.briarproject.bramble.system;
|
package org.briarproject.bramble.system;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.event.EventExecutor;
|
import org.briarproject.bramble.api.event.EventExecutor;
|
||||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
|
||||||
import org.briarproject.bramble.api.system.AndroidExecutor;
|
import org.briarproject.bramble.api.system.AndroidExecutor;
|
||||||
import org.briarproject.bramble.api.system.AndroidWakeLockManager;
|
|
||||||
import org.briarproject.bramble.api.system.LocationUtils;
|
import org.briarproject.bramble.api.system.LocationUtils;
|
||||||
import org.briarproject.bramble.api.system.ResourceProvider;
|
import org.briarproject.bramble.api.system.ResourceProvider;
|
||||||
import org.briarproject.bramble.api.system.SecureRandomProvider;
|
import org.briarproject.bramble.api.system.SecureRandomProvider;
|
||||||
|
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.concurrent.RejectedExecutionHandler;
|
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
|
||||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
|
||||||
|
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
@@ -21,23 +16,6 @@ import dagger.Provides;
|
|||||||
@Module
|
@Module
|
||||||
public class AndroidSystemModule {
|
public class AndroidSystemModule {
|
||||||
|
|
||||||
private final ScheduledExecutorService scheduledExecutorService;
|
|
||||||
|
|
||||||
public AndroidSystemModule() {
|
|
||||||
// Discard tasks that are submitted during shutdown
|
|
||||||
RejectedExecutionHandler policy =
|
|
||||||
new ScheduledThreadPoolExecutor.DiscardPolicy();
|
|
||||||
scheduledExecutorService = new ScheduledThreadPoolExecutor(1, policy);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Provides
|
|
||||||
@Singleton
|
|
||||||
ScheduledExecutorService provideScheduledExecutorService(
|
|
||||||
LifecycleManager lifecycleManager) {
|
|
||||||
lifecycleManager.registerForShutdown(scheduledExecutorService);
|
|
||||||
return scheduledExecutorService;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
SecureRandomProvider provideSecureRandomProvider(
|
SecureRandomProvider provideSecureRandomProvider(
|
||||||
@@ -69,11 +47,4 @@ public class AndroidSystemModule {
|
|||||||
ResourceProvider provideResourceProvider(AndroidResourceProvider provider) {
|
ResourceProvider provideResourceProvider(AndroidResourceProvider provider) {
|
||||||
return provider;
|
return provider;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
|
||||||
@Singleton
|
|
||||||
AndroidWakeLockManager provideWakeLockManager(
|
|
||||||
AndroidWakeLockManagerImpl wakeLockManager) {
|
|
||||||
return wakeLockManager;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,242 +0,0 @@
|
|||||||
package org.briarproject.bramble.system;
|
|
||||||
|
|
||||||
import android.annotation.TargetApi;
|
|
||||||
import android.app.AlarmManager;
|
|
||||||
import android.app.Application;
|
|
||||||
import android.app.PendingIntent;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.os.Process;
|
|
||||||
import android.os.SystemClock;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.lifecycle.Service;
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|
||||||
import org.briarproject.bramble.api.system.AlarmListener;
|
|
||||||
import org.briarproject.bramble.api.system.AndroidWakeLockManager;
|
|
||||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
|
||||||
import org.briarproject.bramble.api.system.Wakeful;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.PriorityQueue;
|
|
||||||
import java.util.Queue;
|
|
||||||
import java.util.concurrent.Executor;
|
|
||||||
import java.util.concurrent.Future;
|
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
import javax.annotation.concurrent.GuardedBy;
|
|
||||||
import javax.annotation.concurrent.ThreadSafe;
|
|
||||||
|
|
||||||
import static android.app.AlarmManager.ELAPSED_REALTIME_WAKEUP;
|
|
||||||
import static android.app.AlarmManager.INTERVAL_FIFTEEN_MINUTES;
|
|
||||||
import static android.app.PendingIntent.FLAG_CANCEL_CURRENT;
|
|
||||||
import static android.content.Context.ALARM_SERVICE;
|
|
||||||
import static android.os.Build.VERSION.SDK_INT;
|
|
||||||
import static java.util.Objects.requireNonNull;
|
|
||||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
|
||||||
import static java.util.logging.Level.INFO;
|
|
||||||
import static java.util.logging.Logger.getLogger;
|
|
||||||
import static org.briarproject.bramble.system.AlarmConstants.EXTRA_PID;
|
|
||||||
import static org.briarproject.bramble.system.AlarmConstants.REQUEST_ALARM;
|
|
||||||
|
|
||||||
@ThreadSafe
|
|
||||||
@NotNullByDefault
|
|
||||||
class AndroidTaskScheduler implements TaskScheduler, Service, AlarmListener {
|
|
||||||
|
|
||||||
private static final Logger LOG =
|
|
||||||
getLogger(AndroidTaskScheduler.class.getName());
|
|
||||||
|
|
||||||
private static final long ALARM_MS = INTERVAL_FIFTEEN_MINUTES;
|
|
||||||
|
|
||||||
private final Application app;
|
|
||||||
private final AndroidWakeLockManager wakeLockManager;
|
|
||||||
private final ScheduledExecutorService scheduledExecutorService;
|
|
||||||
private final AlarmManager alarmManager;
|
|
||||||
|
|
||||||
private final Object lock = new Object();
|
|
||||||
@GuardedBy("lock")
|
|
||||||
private final Queue<ScheduledTask> tasks = new PriorityQueue<>();
|
|
||||||
|
|
||||||
AndroidTaskScheduler(Application app,
|
|
||||||
AndroidWakeLockManager wakeLockManager,
|
|
||||||
ScheduledExecutorService scheduledExecutorService) {
|
|
||||||
this.app = app;
|
|
||||||
this.wakeLockManager = wakeLockManager;
|
|
||||||
this.scheduledExecutorService = scheduledExecutorService;
|
|
||||||
alarmManager = (AlarmManager)
|
|
||||||
requireNonNull(app.getSystemService(ALARM_SERVICE));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void startService() {
|
|
||||||
scheduleAlarm();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void stopService() {
|
|
||||||
cancelAlarm();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Cancellable schedule(Runnable task, Executor executor, long delay,
|
|
||||||
TimeUnit unit) {
|
|
||||||
AtomicBoolean cancelled = new AtomicBoolean(false);
|
|
||||||
return schedule(task, executor, delay, unit, cancelled);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Cancellable scheduleWithFixedDelay(Runnable task, Executor executor,
|
|
||||||
long delay, long interval, TimeUnit unit) {
|
|
||||||
AtomicBoolean cancelled = new AtomicBoolean(false);
|
|
||||||
return scheduleWithFixedDelay(task, executor, delay, interval, unit,
|
|
||||||
cancelled);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onAlarm(Intent intent) {
|
|
||||||
wakeLockManager.runWakefully(() -> {
|
|
||||||
int extraPid = intent.getIntExtra(EXTRA_PID, -1);
|
|
||||||
int currentPid = Process.myPid();
|
|
||||||
if (extraPid == currentPid) {
|
|
||||||
LOG.info("Alarm");
|
|
||||||
rescheduleAlarm();
|
|
||||||
runDueTasks();
|
|
||||||
} else if (LOG.isLoggable(INFO)) {
|
|
||||||
LOG.info("Ignoring alarm with PID " + extraPid
|
|
||||||
+ ", current PID is " + currentPid);
|
|
||||||
}
|
|
||||||
}, "TaskAlarm");
|
|
||||||
}
|
|
||||||
|
|
||||||
private Cancellable schedule(Runnable task, Executor executor, long delay,
|
|
||||||
TimeUnit unit, AtomicBoolean cancelled) {
|
|
||||||
long now = SystemClock.elapsedRealtime();
|
|
||||||
long dueMillis = now + MILLISECONDS.convert(delay, unit);
|
|
||||||
Runnable wakeful = () ->
|
|
||||||
wakeLockManager.executeWakefully(task, executor, "TaskHandoff");
|
|
||||||
Future<?> check = scheduleCheckForDueTasks(delay, unit);
|
|
||||||
ScheduledTask s = new ScheduledTask(wakeful, dueMillis, check,
|
|
||||||
cancelled);
|
|
||||||
synchronized (lock) {
|
|
||||||
tasks.add(s);
|
|
||||||
}
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Cancellable scheduleWithFixedDelay(Runnable task, Executor executor,
|
|
||||||
long delay, long interval, TimeUnit unit, AtomicBoolean cancelled) {
|
|
||||||
// All executions of this periodic task share a cancelled flag
|
|
||||||
Runnable wrapped = () -> {
|
|
||||||
task.run();
|
|
||||||
scheduleWithFixedDelay(task, executor, interval, interval, unit,
|
|
||||||
cancelled);
|
|
||||||
};
|
|
||||||
return schedule(wrapped, executor, delay, unit, cancelled);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Future<?> scheduleCheckForDueTasks(long delay, TimeUnit unit) {
|
|
||||||
Runnable wakeful = () -> wakeLockManager.runWakefully(
|
|
||||||
this::runDueTasks, "TaskScheduler");
|
|
||||||
return scheduledExecutorService.schedule(wakeful, delay, unit);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Wakeful
|
|
||||||
private void runDueTasks() {
|
|
||||||
long now = SystemClock.elapsedRealtime();
|
|
||||||
List<ScheduledTask> due = new ArrayList<>();
|
|
||||||
synchronized (lock) {
|
|
||||||
while (true) {
|
|
||||||
ScheduledTask s = tasks.peek();
|
|
||||||
if (s == null || s.dueMillis > now) break;
|
|
||||||
due.add(tasks.remove());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (LOG.isLoggable(INFO)) {
|
|
||||||
LOG.info("Running " + due.size() + " due tasks");
|
|
||||||
}
|
|
||||||
for (ScheduledTask s : due) {
|
|
||||||
if (LOG.isLoggable(INFO)) {
|
|
||||||
LOG.info("Task is " + (now - s.dueMillis) + " ms overdue");
|
|
||||||
}
|
|
||||||
s.run();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void scheduleAlarm() {
|
|
||||||
if (SDK_INT >= 23) scheduleIdleAlarm();
|
|
||||||
else scheduleInexactRepeatingAlarm();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void rescheduleAlarm() {
|
|
||||||
// If SDK_INT < 23 the alarm repeats automatically
|
|
||||||
if (SDK_INT >= 23) scheduleIdleAlarm();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void cancelAlarm() {
|
|
||||||
alarmManager.cancel(getAlarmPendingIntent());
|
|
||||||
}
|
|
||||||
|
|
||||||
private void scheduleInexactRepeatingAlarm() {
|
|
||||||
alarmManager.setInexactRepeating(ELAPSED_REALTIME_WAKEUP,
|
|
||||||
SystemClock.elapsedRealtime() + ALARM_MS, ALARM_MS,
|
|
||||||
getAlarmPendingIntent());
|
|
||||||
}
|
|
||||||
|
|
||||||
@TargetApi(23)
|
|
||||||
private void scheduleIdleAlarm() {
|
|
||||||
alarmManager.setAndAllowWhileIdle(ELAPSED_REALTIME_WAKEUP,
|
|
||||||
SystemClock.elapsedRealtime() + ALARM_MS,
|
|
||||||
getAlarmPendingIntent());
|
|
||||||
}
|
|
||||||
|
|
||||||
private PendingIntent getAlarmPendingIntent() {
|
|
||||||
Intent i = new Intent(app, AlarmReceiver.class);
|
|
||||||
i.putExtra(EXTRA_PID, android.os.Process.myPid());
|
|
||||||
return PendingIntent.getBroadcast(app, REQUEST_ALARM, i,
|
|
||||||
FLAG_CANCEL_CURRENT);
|
|
||||||
}
|
|
||||||
|
|
||||||
private class ScheduledTask
|
|
||||||
implements Runnable, Cancellable, Comparable<ScheduledTask> {
|
|
||||||
|
|
||||||
private final Runnable task;
|
|
||||||
private final long dueMillis;
|
|
||||||
private final Future<?> check;
|
|
||||||
private final AtomicBoolean cancelled;
|
|
||||||
|
|
||||||
public ScheduledTask(Runnable task, long dueMillis,
|
|
||||||
Future<?> check, AtomicBoolean cancelled) {
|
|
||||||
this.task = task;
|
|
||||||
this.dueMillis = dueMillis;
|
|
||||||
this.check = check;
|
|
||||||
this.cancelled = cancelled;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
if (!cancelled.get()) task.run();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void cancel() {
|
|
||||||
// Cancel any future executions of this task
|
|
||||||
cancelled.set(true);
|
|
||||||
// Cancel the scheduled check for due tasks
|
|
||||||
check.cancel(false);
|
|
||||||
// Remove the task from the queue
|
|
||||||
synchronized (lock) {
|
|
||||||
tasks.remove(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int compareTo(ScheduledTask s) {
|
|
||||||
//noinspection UseCompareMethod
|
|
||||||
if (dueMillis < s.dueMillis) return -1;
|
|
||||||
if (dueMillis > s.dueMillis) return 1;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
package org.briarproject.bramble.system;
|
|
||||||
|
|
||||||
import android.app.Application;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
|
||||||
import org.briarproject.bramble.api.system.AlarmListener;
|
|
||||||
import org.briarproject.bramble.api.system.AndroidWakeLockManager;
|
|
||||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
|
||||||
|
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import javax.inject.Singleton;
|
|
||||||
|
|
||||||
import dagger.Module;
|
|
||||||
import dagger.Provides;
|
|
||||||
|
|
||||||
@Module
|
|
||||||
public class AndroidTaskSchedulerModule {
|
|
||||||
|
|
||||||
public static class EagerSingletons {
|
|
||||||
@Inject
|
|
||||||
AndroidTaskScheduler scheduler;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Provides
|
|
||||||
@Singleton
|
|
||||||
AndroidTaskScheduler provideAndroidTaskScheduler(
|
|
||||||
LifecycleManager lifecycleManager, Application app,
|
|
||||||
AndroidWakeLockManager wakeLockManager,
|
|
||||||
ScheduledExecutorService scheduledExecutorService) {
|
|
||||||
AndroidTaskScheduler scheduler = new AndroidTaskScheduler(app,
|
|
||||||
wakeLockManager, scheduledExecutorService);
|
|
||||||
lifecycleManager.registerService(scheduler);
|
|
||||||
return scheduler;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Provides
|
|
||||||
@Singleton
|
|
||||||
AlarmListener provideAlarmListener(AndroidTaskScheduler scheduler) {
|
|
||||||
return scheduler;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Provides
|
|
||||||
@Singleton
|
|
||||||
TaskScheduler provideTaskScheduler(AndroidTaskScheduler scheduler) {
|
|
||||||
return scheduler;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,74 +0,0 @@
|
|||||||
package org.briarproject.bramble.system;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|
||||||
import org.briarproject.bramble.api.system.AndroidWakeLock;
|
|
||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
import javax.annotation.concurrent.GuardedBy;
|
|
||||||
import javax.annotation.concurrent.ThreadSafe;
|
|
||||||
|
|
||||||
import static java.util.logging.Level.FINE;
|
|
||||||
import static java.util.logging.Logger.getLogger;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A wrapper around a {@link SharedWakeLock} that provides the more convenient
|
|
||||||
* semantics of {@link AndroidWakeLock} (i.e. calls to acquire() and release()
|
|
||||||
* don't need to be balanced).
|
|
||||||
*/
|
|
||||||
@ThreadSafe
|
|
||||||
@NotNullByDefault
|
|
||||||
class AndroidWakeLockImpl implements AndroidWakeLock {
|
|
||||||
|
|
||||||
private static final Logger LOG =
|
|
||||||
getLogger(AndroidWakeLockImpl.class.getName());
|
|
||||||
|
|
||||||
private static final AtomicInteger INSTANCE_ID = new AtomicInteger(0);
|
|
||||||
|
|
||||||
private final SharedWakeLock sharedWakeLock;
|
|
||||||
private final String tag;
|
|
||||||
|
|
||||||
private final Object lock = new Object();
|
|
||||||
@GuardedBy("lock")
|
|
||||||
private boolean held = false;
|
|
||||||
|
|
||||||
AndroidWakeLockImpl(SharedWakeLock sharedWakeLock, String tag) {
|
|
||||||
this.sharedWakeLock = sharedWakeLock;
|
|
||||||
this.tag = tag + "_" + INSTANCE_ID.getAndIncrement();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void acquire() {
|
|
||||||
synchronized (lock) {
|
|
||||||
if (held) {
|
|
||||||
if (LOG.isLoggable(FINE)) {
|
|
||||||
LOG.fine(tag + " already acquired");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (LOG.isLoggable(FINE)) {
|
|
||||||
LOG.fine(tag + " acquiring shared wake lock");
|
|
||||||
}
|
|
||||||
held = true;
|
|
||||||
sharedWakeLock.acquire();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void release() {
|
|
||||||
synchronized (lock) {
|
|
||||||
if (held) {
|
|
||||||
if (LOG.isLoggable(FINE)) {
|
|
||||||
LOG.fine(tag + " releasing shared wake lock");
|
|
||||||
}
|
|
||||||
held = false;
|
|
||||||
sharedWakeLock.release();
|
|
||||||
} else {
|
|
||||||
if (LOG.isLoggable(FINE)) {
|
|
||||||
LOG.fine(tag + " already released");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,119 +0,0 @@
|
|||||||
package org.briarproject.bramble.system;
|
|
||||||
|
|
||||||
import android.app.Application;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.pm.PackageInfo;
|
|
||||||
import android.content.pm.PackageManager;
|
|
||||||
import android.os.PowerManager;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|
||||||
import org.briarproject.bramble.api.system.AndroidWakeLock;
|
|
||||||
import org.briarproject.bramble.api.system.AndroidWakeLockManager;
|
|
||||||
|
|
||||||
import java.util.concurrent.Executor;
|
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
import static android.content.Context.POWER_SERVICE;
|
|
||||||
import static android.os.PowerManager.PARTIAL_WAKE_LOCK;
|
|
||||||
import static java.util.concurrent.TimeUnit.MINUTES;
|
|
||||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
|
||||||
import static org.briarproject.bramble.api.nullsafety.NullSafety.requireNonNull;
|
|
||||||
|
|
||||||
@NotNullByDefault
|
|
||||||
class AndroidWakeLockManagerImpl implements AndroidWakeLockManager {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* How often to replace the wake lock.
|
|
||||||
*/
|
|
||||||
private static final long LOCK_DURATION_MS = MINUTES.toMillis(1);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Automatically release the lock this many milliseconds after it's due
|
|
||||||
* to have been replaced and released.
|
|
||||||
*/
|
|
||||||
private static final long SAFETY_MARGIN_MS = SECONDS.toMillis(30);
|
|
||||||
|
|
||||||
private final SharedWakeLock sharedWakeLock;
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
AndroidWakeLockManagerImpl(Application app,
|
|
||||||
ScheduledExecutorService scheduledExecutorService) {
|
|
||||||
PowerManager powerManager = (PowerManager)
|
|
||||||
requireNonNull(app.getSystemService(POWER_SERVICE));
|
|
||||||
String tag = getWakeLockTag(app);
|
|
||||||
sharedWakeLock = new RenewableWakeLock(powerManager,
|
|
||||||
scheduledExecutorService, PARTIAL_WAKE_LOCK, tag,
|
|
||||||
LOCK_DURATION_MS, SAFETY_MARGIN_MS);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public AndroidWakeLock createWakeLock(String tag) {
|
|
||||||
return new AndroidWakeLockImpl(sharedWakeLock, tag);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void runWakefully(Runnable r, String tag) {
|
|
||||||
AndroidWakeLock wakeLock = createWakeLock(tag);
|
|
||||||
wakeLock.acquire();
|
|
||||||
try {
|
|
||||||
r.run();
|
|
||||||
} finally {
|
|
||||||
wakeLock.release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void executeWakefully(Runnable r, Executor executor, String tag) {
|
|
||||||
AndroidWakeLock wakeLock = createWakeLock(tag);
|
|
||||||
wakeLock.acquire();
|
|
||||||
try {
|
|
||||||
executor.execute(() -> {
|
|
||||||
try {
|
|
||||||
r.run();
|
|
||||||
} finally {
|
|
||||||
// Release the wake lock if the task throws an exception
|
|
||||||
wakeLock.release();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (Exception e) {
|
|
||||||
// Release the wake lock if the executor throws an exception when
|
|
||||||
// we submit the task (in which case the release() call above won't
|
|
||||||
// happen)
|
|
||||||
wakeLock.release();
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void executeWakefully(Runnable r, String tag) {
|
|
||||||
AndroidWakeLock wakeLock = createWakeLock(tag);
|
|
||||||
wakeLock.acquire();
|
|
||||||
try {
|
|
||||||
new Thread(() -> {
|
|
||||||
try {
|
|
||||||
r.run();
|
|
||||||
} finally {
|
|
||||||
wakeLock.release();
|
|
||||||
}
|
|
||||||
}).start();
|
|
||||||
} catch (Exception e) {
|
|
||||||
wakeLock.release();
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getWakeLockTag(Context ctx) {
|
|
||||||
PackageManager pm = ctx.getPackageManager();
|
|
||||||
for (PackageInfo info : pm.getInstalledPackages(0)) {
|
|
||||||
String name = info.packageName.toLowerCase();
|
|
||||||
if (name.startsWith("com.huawei.powergenie")) {
|
|
||||||
return "LocationManagerService";
|
|
||||||
} else if (name.startsWith("com.evenwell.powermonitor")) {
|
|
||||||
return "AudioIn";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ctx.getPackageName();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
package org.briarproject.bramble.system;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
|
||||||
import org.briarproject.bramble.api.system.AndroidWakeLockManager;
|
|
||||||
import org.briarproject.bramble.api.system.WakefulIoExecutor;
|
|
||||||
|
|
||||||
import java.util.concurrent.Executor;
|
|
||||||
|
|
||||||
import dagger.Module;
|
|
||||||
import dagger.Provides;
|
|
||||||
|
|
||||||
@Module
|
|
||||||
public
|
|
||||||
class AndroidWakefulIoExecutorModule {
|
|
||||||
|
|
||||||
@Provides
|
|
||||||
@WakefulIoExecutor
|
|
||||||
Executor provideWakefulIoExecutor(@IoExecutor Executor ioExecutor,
|
|
||||||
AndroidWakeLockManager wakeLockManager) {
|
|
||||||
return r -> wakeLockManager.executeWakefully(r, ioExecutor,
|
|
||||||
"WakefulIoExecutor");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,130 +0,0 @@
|
|||||||
package org.briarproject.bramble.system;
|
|
||||||
|
|
||||||
import android.os.PowerManager;
|
|
||||||
import android.os.PowerManager.WakeLock;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|
||||||
|
|
||||||
import java.util.concurrent.Future;
|
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
|
||||||
import javax.annotation.concurrent.GuardedBy;
|
|
||||||
import javax.annotation.concurrent.ThreadSafe;
|
|
||||||
|
|
||||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
|
||||||
import static java.util.logging.Level.FINE;
|
|
||||||
import static java.util.logging.Level.INFO;
|
|
||||||
import static java.util.logging.Level.WARNING;
|
|
||||||
import static java.util.logging.Logger.getLogger;
|
|
||||||
import static org.briarproject.bramble.api.nullsafety.NullSafety.requireNonNull;
|
|
||||||
|
|
||||||
@ThreadSafe
|
|
||||||
@NotNullByDefault
|
|
||||||
class RenewableWakeLock implements SharedWakeLock {
|
|
||||||
|
|
||||||
private static final Logger LOG =
|
|
||||||
getLogger(RenewableWakeLock.class.getName());
|
|
||||||
|
|
||||||
private final PowerManager powerManager;
|
|
||||||
private final ScheduledExecutorService scheduledExecutorService;
|
|
||||||
private final int levelAndFlags;
|
|
||||||
private final String tag;
|
|
||||||
private final long durationMs, safetyMarginMs;
|
|
||||||
|
|
||||||
private final Object lock = new Object();
|
|
||||||
@GuardedBy("lock")
|
|
||||||
@Nullable
|
|
||||||
private WakeLock wakeLock;
|
|
||||||
@GuardedBy("lock")
|
|
||||||
@Nullable
|
|
||||||
private Future<?> future;
|
|
||||||
@GuardedBy("lock")
|
|
||||||
private int refCount = 0;
|
|
||||||
@GuardedBy("lock")
|
|
||||||
private long acquired = 0;
|
|
||||||
|
|
||||||
RenewableWakeLock(PowerManager powerManager,
|
|
||||||
ScheduledExecutorService scheduledExecutorService,
|
|
||||||
int levelAndFlags,
|
|
||||||
String tag,
|
|
||||||
long durationMs,
|
|
||||||
long safetyMarginMs) {
|
|
||||||
this.powerManager = powerManager;
|
|
||||||
this.scheduledExecutorService = scheduledExecutorService;
|
|
||||||
this.levelAndFlags = levelAndFlags;
|
|
||||||
this.tag = tag;
|
|
||||||
this.durationMs = durationMs;
|
|
||||||
this.safetyMarginMs = safetyMarginMs;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void acquire() {
|
|
||||||
synchronized (lock) {
|
|
||||||
refCount++;
|
|
||||||
if (refCount == 1) {
|
|
||||||
if (LOG.isLoggable(INFO)) {
|
|
||||||
LOG.info("Acquiring wake lock " + tag);
|
|
||||||
}
|
|
||||||
wakeLock = powerManager.newWakeLock(levelAndFlags, tag);
|
|
||||||
// We do our own reference counting so we can replace the lock
|
|
||||||
// TODO: Check whether using a ref-counted wake lock affects
|
|
||||||
// power management apps
|
|
||||||
wakeLock.setReferenceCounted(false);
|
|
||||||
wakeLock.acquire(durationMs + safetyMarginMs);
|
|
||||||
future = scheduledExecutorService.schedule(this::renew,
|
|
||||||
durationMs, MILLISECONDS);
|
|
||||||
acquired = android.os.SystemClock.elapsedRealtime();
|
|
||||||
} else if (LOG.isLoggable(FINE)) {
|
|
||||||
LOG.fine("Wake lock " + tag + " has " + refCount + " holders");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void renew() {
|
|
||||||
if (LOG.isLoggable(INFO)) LOG.info("Renewing wake lock " + tag);
|
|
||||||
synchronized (lock) {
|
|
||||||
if (wakeLock == null) {
|
|
||||||
LOG.info("Already released");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (LOG.isLoggable(FINE)) {
|
|
||||||
LOG.fine("Wake lock " + tag + " has " + refCount + " holders");
|
|
||||||
}
|
|
||||||
long now = android.os.SystemClock.elapsedRealtime();
|
|
||||||
long expiry = acquired + durationMs + safetyMarginMs;
|
|
||||||
if (now > expiry && LOG.isLoggable(WARNING)) {
|
|
||||||
LOG.warning("Wake lock expired " + (now - expiry) + " ms ago");
|
|
||||||
}
|
|
||||||
WakeLock oldWakeLock = wakeLock;
|
|
||||||
wakeLock = powerManager.newWakeLock(levelAndFlags, tag);
|
|
||||||
wakeLock.setReferenceCounted(false);
|
|
||||||
wakeLock.acquire(durationMs + safetyMarginMs);
|
|
||||||
oldWakeLock.release();
|
|
||||||
future = scheduledExecutorService.schedule(this::renew, durationMs,
|
|
||||||
MILLISECONDS);
|
|
||||||
acquired = now;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void release() {
|
|
||||||
synchronized (lock) {
|
|
||||||
refCount--;
|
|
||||||
if (refCount == 0) {
|
|
||||||
if (LOG.isLoggable(INFO)) {
|
|
||||||
LOG.info("Releasing wake lock " + tag);
|
|
||||||
}
|
|
||||||
requireNonNull(future).cancel(false);
|
|
||||||
future = null;
|
|
||||||
requireNonNull(wakeLock).release();
|
|
||||||
wakeLock = null;
|
|
||||||
acquired = 0;
|
|
||||||
} else if (LOG.isLoggable(FINE)) {
|
|
||||||
LOG.fine("Wake lock " + tag + " has " + refCount + " holders");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
package org.briarproject.bramble.system;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|
||||||
import org.briarproject.bramble.api.system.AndroidWakeLock;
|
|
||||||
|
|
||||||
@NotNullByDefault
|
|
||||||
interface SharedWakeLock {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Acquires the wake lock. This increments the wake lock's reference count,
|
|
||||||
* so unlike {@link AndroidWakeLock#acquire()} every call to this method
|
|
||||||
* must be followed by a balancing call to {@link #release()}.
|
|
||||||
*/
|
|
||||||
void acquire();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Releases the wake lock. This decrements the wake lock's reference count,
|
|
||||||
* so unlike {@link AndroidWakeLock#release()} every call to this method
|
|
||||||
* must follow a balancing call to {@link #acquire()}.
|
|
||||||
*/
|
|
||||||
void release();
|
|
||||||
}
|
|
||||||
@@ -3,6 +3,8 @@ package org.briarproject.bramble.util;
|
|||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.pm.PackageInfo;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
|
|
||||||
@@ -117,4 +119,17 @@ public class AndroidUtils {
|
|||||||
if (SDK_INT < 24) return new String[] {"image/jpeg", "image/png"};
|
if (SDK_INT < 24) return new String[] {"image/jpeg", "image/png"};
|
||||||
else return new String[] {"image/jpeg", "image/png", "image/gif"};
|
else return new String[] {"image/jpeg", "image/png", "image/gif"};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getWakeLockTag(Context ctx) {
|
||||||
|
PackageManager pm = ctx.getPackageManager();
|
||||||
|
for (PackageInfo info : pm.getInstalledPackages(0)) {
|
||||||
|
String name = info.packageName.toLowerCase();
|
||||||
|
if (name.startsWith("com.huawei.powergenie")) {
|
||||||
|
return "LocationManagerService";
|
||||||
|
} else if (name.startsWith("com.evenwell.powermonitor")) {
|
||||||
|
return "AudioIn";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ctx.getPackageName();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,100 @@
|
|||||||
|
package org.briarproject.bramble.util;
|
||||||
|
|
||||||
|
import android.os.PowerManager;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.ScheduledFuture;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
import javax.annotation.concurrent.ThreadSafe;
|
||||||
|
|
||||||
|
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||||
|
import static java.util.logging.Level.INFO;
|
||||||
|
|
||||||
|
@ThreadSafe
|
||||||
|
@NotNullByDefault
|
||||||
|
public class RenewableWakeLock {
|
||||||
|
|
||||||
|
private static final Logger LOG =
|
||||||
|
Logger.getLogger(RenewableWakeLock.class.getName());
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Automatically release the lock this many milliseconds after it's due
|
||||||
|
* to have been replaced and released.
|
||||||
|
*/
|
||||||
|
private static final int SAFETY_MARGIN_MS = 10_000;
|
||||||
|
|
||||||
|
private final PowerManager powerManager;
|
||||||
|
private final ScheduledExecutorService scheduler;
|
||||||
|
private final int levelAndFlags;
|
||||||
|
private final String tag;
|
||||||
|
private final long durationMs;
|
||||||
|
private final Runnable renewTask;
|
||||||
|
|
||||||
|
private final Object lock = new Object();
|
||||||
|
@Nullable
|
||||||
|
private PowerManager.WakeLock wakeLock; // Locking: lock
|
||||||
|
@Nullable
|
||||||
|
private ScheduledFuture future; // Locking: lock
|
||||||
|
|
||||||
|
public RenewableWakeLock(PowerManager powerManager,
|
||||||
|
ScheduledExecutorService scheduler, int levelAndFlags, String tag,
|
||||||
|
long duration, TimeUnit timeUnit) {
|
||||||
|
this.powerManager = powerManager;
|
||||||
|
this.scheduler = scheduler;
|
||||||
|
this.levelAndFlags = levelAndFlags;
|
||||||
|
this.tag = tag;
|
||||||
|
durationMs = MILLISECONDS.convert(duration, timeUnit);
|
||||||
|
renewTask = this::renew;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void acquire() {
|
||||||
|
if (LOG.isLoggable(INFO)) LOG.info("Acquiring wake lock " + tag);
|
||||||
|
synchronized (lock) {
|
||||||
|
if (wakeLock != null) {
|
||||||
|
LOG.info("Already acquired");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
wakeLock = powerManager.newWakeLock(levelAndFlags, tag);
|
||||||
|
wakeLock.setReferenceCounted(false);
|
||||||
|
wakeLock.acquire(durationMs + SAFETY_MARGIN_MS);
|
||||||
|
future = scheduler.schedule(renewTask, durationMs, MILLISECONDS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void renew() {
|
||||||
|
if (LOG.isLoggable(INFO)) LOG.info("Renewing wake lock " + tag);
|
||||||
|
synchronized (lock) {
|
||||||
|
if (wakeLock == null) {
|
||||||
|
LOG.info("Already released");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
PowerManager.WakeLock oldWakeLock = wakeLock;
|
||||||
|
wakeLock = powerManager.newWakeLock(levelAndFlags, tag);
|
||||||
|
wakeLock.setReferenceCounted(false);
|
||||||
|
wakeLock.acquire(durationMs + SAFETY_MARGIN_MS);
|
||||||
|
oldWakeLock.release();
|
||||||
|
future = scheduler.schedule(renewTask, durationMs, MILLISECONDS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void release() {
|
||||||
|
if (LOG.isLoggable(INFO)) LOG.info("Releasing wake lock " + tag);
|
||||||
|
synchronized (lock) {
|
||||||
|
if (wakeLock == null) {
|
||||||
|
LOG.info("Already released");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (future == null) throw new AssertionError();
|
||||||
|
future.cancel(false);
|
||||||
|
future = null;
|
||||||
|
wakeLock.release();
|
||||||
|
wakeLock = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -7,6 +7,7 @@ import android.content.pm.ApplicationInfo;
|
|||||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||||
import org.briarproject.bramble.api.db.DatabaseConfig;
|
import org.briarproject.bramble.api.db.DatabaseConfig;
|
||||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||||
|
import org.briarproject.bramble.api.logging.PersistentLogManager;
|
||||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||||
import org.jmock.Expectations;
|
import org.jmock.Expectations;
|
||||||
import org.jmock.lib.legacy.ClassImposteriser;
|
import org.jmock.lib.legacy.ClassImposteriser;
|
||||||
@@ -15,7 +16,9 @@ import org.junit.Before;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import static android.content.Context.MODE_PRIVATE;
|
||||||
import static junit.framework.Assert.assertFalse;
|
import static junit.framework.Assert.assertFalse;
|
||||||
import static junit.framework.Assert.assertTrue;
|
import static junit.framework.Assert.assertTrue;
|
||||||
import static org.briarproject.bramble.test.TestUtils.deleteTestDirectory;
|
import static org.briarproject.bramble.test.TestUtils.deleteTestDirectory;
|
||||||
@@ -27,6 +30,8 @@ public class AndroidAccountManagerTest extends BrambleMockTestCase {
|
|||||||
context.mock(SharedPreferences.class, "prefs");
|
context.mock(SharedPreferences.class, "prefs");
|
||||||
private final SharedPreferences defaultPrefs =
|
private final SharedPreferences defaultPrefs =
|
||||||
context.mock(SharedPreferences.class, "defaultPrefs");
|
context.mock(SharedPreferences.class, "defaultPrefs");
|
||||||
|
private final PersistentLogManager logManager =
|
||||||
|
context.mock(PersistentLogManager.class);
|
||||||
private final DatabaseConfig databaseConfig =
|
private final DatabaseConfig databaseConfig =
|
||||||
context.mock(DatabaseConfig.class);
|
context.mock(DatabaseConfig.class);
|
||||||
private final CryptoComponent crypto = context.mock(CryptoComponent.class);
|
private final CryptoComponent crypto = context.mock(CryptoComponent.class);
|
||||||
@@ -40,6 +45,7 @@ public class AndroidAccountManagerTest extends BrambleMockTestCase {
|
|||||||
private final File testDir = getTestDirectory();
|
private final File testDir = getTestDirectory();
|
||||||
private final File keyDir = new File(testDir, "key");
|
private final File keyDir = new File(testDir, "key");
|
||||||
private final File dbDir = new File(testDir, "db");
|
private final File dbDir = new File(testDir, "db");
|
||||||
|
private final File logDir = new File(testDir, "log");
|
||||||
|
|
||||||
private AndroidAccountManager accountManager;
|
private AndroidAccountManager accountManager;
|
||||||
|
|
||||||
@@ -61,7 +67,7 @@ public class AndroidAccountManagerTest extends BrambleMockTestCase {
|
|||||||
will(returnValue(app));
|
will(returnValue(app));
|
||||||
}});
|
}});
|
||||||
accountManager = new AndroidAccountManager(databaseConfig, crypto,
|
accountManager = new AndroidAccountManager(databaseConfig, crypto,
|
||||||
identityManager, prefs, app) {
|
identityManager, prefs, logManager, app) {
|
||||||
@Override
|
@Override
|
||||||
SharedPreferences getDefaultSharedPreferences() {
|
SharedPreferences getDefaultSharedPreferences() {
|
||||||
return defaultPrefs;
|
return defaultPrefs;
|
||||||
@@ -109,10 +115,15 @@ public class AndroidAccountManagerTest extends BrambleMockTestCase {
|
|||||||
will(returnValue(cacheDir));
|
will(returnValue(cacheDir));
|
||||||
oneOf(app).getExternalCacheDir();
|
oneOf(app).getExternalCacheDir();
|
||||||
will(returnValue(externalCacheDir));
|
will(returnValue(externalCacheDir));
|
||||||
|
oneOf(app).getDir("log", MODE_PRIVATE);
|
||||||
|
will(returnValue(logDir));
|
||||||
|
oneOf(logManager).addLogHandler(with(logDir),
|
||||||
|
with(any(Logger.class)));
|
||||||
}});
|
}});
|
||||||
|
|
||||||
assertTrue(dbDir.mkdirs());
|
assertTrue(dbDir.mkdirs());
|
||||||
assertTrue(keyDir.mkdirs());
|
assertTrue(keyDir.mkdirs());
|
||||||
|
assertTrue(logDir.mkdirs());
|
||||||
assertTrue(codeCacheDir.mkdirs());
|
assertTrue(codeCacheDir.mkdirs());
|
||||||
assertTrue(codeCacheFile.createNewFile());
|
assertTrue(codeCacheFile.createNewFile());
|
||||||
assertTrue(libDir.mkdirs());
|
assertTrue(libDir.mkdirs());
|
||||||
@@ -130,6 +141,7 @@ public class AndroidAccountManagerTest extends BrambleMockTestCase {
|
|||||||
|
|
||||||
assertFalse(dbDir.exists());
|
assertFalse(dbDir.exists());
|
||||||
assertFalse(keyDir.exists());
|
assertFalse(keyDir.exists());
|
||||||
|
assertFalse(logDir.exists());
|
||||||
assertTrue(codeCacheDir.exists());
|
assertTrue(codeCacheDir.exists());
|
||||||
assertTrue(codeCacheFile.exists());
|
assertTrue(codeCacheFile.exists());
|
||||||
assertTrue(libDir.exists());
|
assertTrue(libDir.exists());
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
package org.briarproject.bramble.api;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|
||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An item that can be consumed once.
|
|
||||||
*/
|
|
||||||
@NotNullByDefault
|
|
||||||
public class Consumable<T> {
|
|
||||||
|
|
||||||
private final AtomicReference<T> reference;
|
|
||||||
|
|
||||||
public Consumable(T item) {
|
|
||||||
reference = new AtomicReference<>(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public T consume() {
|
|
||||||
return reference.getAndSet(null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +1,16 @@
|
|||||||
package org.briarproject.bramble.api;
|
package org.briarproject.bramble.api;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
|
||||||
import java.util.Hashtable;
|
import java.util.Hashtable;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
import static org.briarproject.bramble.util.StringUtils.fromHexString;
|
||||||
|
import static org.briarproject.bramble.util.StringUtils.toHexString;
|
||||||
|
|
||||||
|
@NotNullByDefault
|
||||||
public abstract class StringMap extends Hashtable<String, String> {
|
public abstract class StringMap extends Hashtable<String, String> {
|
||||||
|
|
||||||
protected StringMap(Map<String, String> m) {
|
protected StringMap(Map<String, String> m) {
|
||||||
@@ -52,4 +60,19 @@ public abstract class StringMap extends Hashtable<String, String> {
|
|||||||
public void putLong(String key, long value) {
|
public void putLong(String key, long value) {
|
||||||
put(key, String.valueOf(value));
|
put(key, String.valueOf(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public byte[] getBytes(String key) {
|
||||||
|
String s = get(key);
|
||||||
|
if (s == null) return null;
|
||||||
|
try {
|
||||||
|
return fromHexString(s);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void putBytes(String key, byte[] value) {
|
||||||
|
put(key, toHexString(value));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,24 +35,24 @@ public interface ClientHelper {
|
|||||||
|
|
||||||
Message createMessageForStoringMetadata(GroupId g);
|
Message createMessageForStoringMetadata(GroupId g);
|
||||||
|
|
||||||
Message getSmallMessage(MessageId m) throws DbException;
|
Message getMessage(MessageId m) throws DbException;
|
||||||
|
|
||||||
Message getSmallMessage(Transaction txn, MessageId m) throws DbException;
|
Message getMessage(Transaction txn, MessageId m) throws DbException;
|
||||||
|
|
||||||
BdfList getSmallMessageAsList(MessageId m)
|
BdfList getMessageAsList(MessageId m) throws DbException, FormatException;
|
||||||
throws DbException, FormatException;
|
|
||||||
|
|
||||||
BdfList getSmallMessageAsList(Transaction txn, MessageId m)
|
BdfList getMessageAsList(Transaction txn, MessageId m) throws DbException,
|
||||||
throws DbException, FormatException;
|
FormatException;
|
||||||
|
|
||||||
BdfDictionary getGroupMetadataAsDictionary(GroupId g)
|
BdfDictionary getGroupMetadataAsDictionary(GroupId g) throws DbException,
|
||||||
throws DbException, FormatException;
|
FormatException;
|
||||||
|
|
||||||
BdfDictionary getGroupMetadataAsDictionary(Transaction txn, GroupId g)
|
BdfDictionary getGroupMetadataAsDictionary(Transaction txn, GroupId g)
|
||||||
throws DbException, FormatException;
|
throws DbException, FormatException;
|
||||||
|
|
||||||
BdfDictionary getMessageMetadataAsDictionary(MessageId m)
|
BdfDictionary getMessageMetadataAsDictionary(MessageId m)
|
||||||
throws DbException, FormatException;
|
throws DbException,
|
||||||
|
FormatException;
|
||||||
|
|
||||||
BdfDictionary getMessageMetadataAsDictionary(Transaction txn, MessageId m)
|
BdfDictionary getMessageMetadataAsDictionary(Transaction txn, MessageId m)
|
||||||
throws DbException, FormatException;
|
throws DbException, FormatException;
|
||||||
@@ -67,8 +67,8 @@ public interface ClientHelper {
|
|||||||
BdfDictionary query) throws DbException, FormatException;
|
BdfDictionary query) throws DbException, FormatException;
|
||||||
|
|
||||||
Map<MessageId, BdfDictionary> getMessageMetadataAsDictionary(
|
Map<MessageId, BdfDictionary> getMessageMetadataAsDictionary(
|
||||||
Transaction txn, GroupId g, BdfDictionary query)
|
Transaction txn, GroupId g, BdfDictionary query) throws DbException,
|
||||||
throws DbException, FormatException;
|
FormatException;
|
||||||
|
|
||||||
void mergeGroupMetadata(GroupId g, BdfDictionary metadata)
|
void mergeGroupMetadata(GroupId g, BdfDictionary metadata)
|
||||||
throws DbException, FormatException;
|
throws DbException, FormatException;
|
||||||
|
|||||||
@@ -19,4 +19,10 @@ public interface StreamDecrypterFactory {
|
|||||||
*/
|
*/
|
||||||
StreamDecrypter createContactExchangeStreamDecrypter(InputStream in,
|
StreamDecrypter createContactExchangeStreamDecrypter(InputStream in,
|
||||||
SecretKey headerKey);
|
SecretKey headerKey);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a {@link StreamDecrypter} for decrypting a log stream.
|
||||||
|
*/
|
||||||
|
StreamDecrypter createLogStreamDecrypter(InputStream in,
|
||||||
|
SecretKey headerKey);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,12 @@ public interface StreamEncrypterFactory {
|
|||||||
* Creates a {@link StreamEncrypter} for encrypting a contact exchange
|
* Creates a {@link StreamEncrypter} for encrypting a contact exchange
|
||||||
* stream.
|
* stream.
|
||||||
*/
|
*/
|
||||||
StreamEncrypter createContactExchangeStreamDecrypter(OutputStream out,
|
StreamEncrypter createContactExchangeStreamEncrypter(OutputStream out,
|
||||||
|
SecretKey headerKey);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a {@link StreamEncrypter} for encrypting a log stream.
|
||||||
|
*/
|
||||||
|
StreamEncrypter createLogStreamEncrypter(OutputStream out,
|
||||||
SecretKey headerKey);
|
SecretKey headerKey);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -163,23 +163,27 @@ public interface DatabaseComponent extends TransactionManager {
|
|||||||
* less than or equal to the given length, for transmission over a
|
* less than or equal to the given length, for transmission over a
|
||||||
* transport with the given maximum latency. Returns null if there are no
|
* transport with the given maximum latency. Returns null if there are no
|
||||||
* sendable messages that fit in the given length.
|
* sendable messages that fit in the given length.
|
||||||
*
|
|
||||||
* @param small True if only single-block messages should be sent
|
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
Collection<Message> generateBatch(Transaction txn, ContactId c,
|
Collection<Message> generateBatch(Transaction txn, ContactId c,
|
||||||
int maxLength, int maxLatency, boolean small) throws DbException;
|
int maxLength, int maxLatency) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an offer for the given contact for transmission over a
|
* Returns an offer for the given contact for transmission over a
|
||||||
* transport with the given maximum latency, or null if there are no
|
* transport with the given maximum latency, or null if there are no
|
||||||
* messages to offer.
|
* messages to offer.
|
||||||
*
|
|
||||||
* @param small True if only single-block messages should be offered
|
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
Offer generateOffer(Transaction txn, ContactId c, int maxMessages,
|
Offer generateOffer(Transaction txn, ContactId c, int maxMessages,
|
||||||
int maxLatency, boolean small) throws DbException;
|
int maxLatency) throws DbException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a request for the given contact, or null if there are no
|
||||||
|
* messages to request.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
Request generateRequest(Transaction txn, ContactId c, int maxMessages)
|
||||||
|
throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a batch of messages for the given contact, with a total length
|
* Returns a batch of messages for the given contact, with a total length
|
||||||
@@ -268,14 +272,13 @@ public interface DatabaseComponent extends TransactionManager {
|
|||||||
Collection<Identity> getIdentities(Transaction txn) throws DbException;
|
Collection<Identity> getIdentities(Transaction txn) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the single-block message with the given ID.
|
* Returns the message with the given ID.
|
||||||
* <p/>
|
* <p/>
|
||||||
* Read-only.
|
* Read-only.
|
||||||
*
|
*
|
||||||
* @throws MessageDeletedException if the message has been deleted
|
* @throws MessageDeletedException if the message has been deleted
|
||||||
* @throws MessageTooLargeException if the message has more than one block
|
|
||||||
*/
|
*/
|
||||||
Message getSmallMessage(Transaction txn, MessageId m) throws DbException;
|
Message getMessage(Transaction txn, MessageId m) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the IDs of all delivered messages in the given group.
|
* Returns the IDs of all delivered messages in the given group.
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
package org.briarproject.bramble.api.db;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Thrown when a multi-block message is requested from the database via a
|
|
||||||
* method that is only suitable for requesting single-block messages.
|
|
||||||
*/
|
|
||||||
public class MessageTooLargeException extends DbException {
|
|
||||||
}
|
|
||||||
@@ -7,10 +7,6 @@ public interface TimeoutMonitor {
|
|||||||
/**
|
/**
|
||||||
* Returns an {@link InputStream} that wraps the given stream and allows
|
* Returns an {@link InputStream} that wraps the given stream and allows
|
||||||
* read timeouts to be detected.
|
* read timeouts to be detected.
|
||||||
* <p>
|
|
||||||
* The returned stream must be {@link InputStream#close() closed} when it's
|
|
||||||
* no longer needed to ensure that resources held by the timeout monitor
|
|
||||||
* are released.
|
|
||||||
*
|
*
|
||||||
* @param timeoutMs The read timeout in milliseconds. Timeouts will be
|
* @param timeoutMs The read timeout in milliseconds. Timeouts will be
|
||||||
* detected eventually but are not guaranteed to be detected immediately.
|
* detected eventually but are not guaranteed to be detected immediately.
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import org.briarproject.bramble.api.db.DatabaseComponent;
|
|||||||
import org.briarproject.bramble.api.db.DbException;
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
import org.briarproject.bramble.api.db.Transaction;
|
import org.briarproject.bramble.api.db.Transaction;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.system.Wakeful;
|
|
||||||
|
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
|
|
||||||
@@ -66,7 +65,6 @@ public interface LifecycleManager {
|
|||||||
* Opens the {@link DatabaseComponent} using the given key and starts any
|
* Opens the {@link DatabaseComponent} using the given key and starts any
|
||||||
* registered {@link Service Services}.
|
* registered {@link Service Services}.
|
||||||
*/
|
*/
|
||||||
@Wakeful
|
|
||||||
StartResult startServices(SecretKey dbKey);
|
StartResult startServices(SecretKey dbKey);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -74,7 +72,6 @@ public interface LifecycleManager {
|
|||||||
* registered {@link ExecutorService ExecutorServices}, and closes the
|
* registered {@link ExecutorService ExecutorServices}, and closes the
|
||||||
* {@link DatabaseComponent}.
|
* {@link DatabaseComponent}.
|
||||||
*/
|
*/
|
||||||
@Wakeful
|
|
||||||
void stopServices();
|
void stopServices();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -107,7 +104,6 @@ public interface LifecycleManager {
|
|||||||
*
|
*
|
||||||
* @param txn A read-write transaction
|
* @param txn A read-write transaction
|
||||||
*/
|
*/
|
||||||
@Wakeful
|
|
||||||
void onDatabaseOpened(Transaction txn) throws DbException;
|
void onDatabaseOpened(Transaction txn) throws DbException;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,20 +1,16 @@
|
|||||||
package org.briarproject.bramble.api.lifecycle;
|
package org.briarproject.bramble.api.lifecycle;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.system.Wakeful;
|
|
||||||
|
|
||||||
public interface Service {
|
public interface Service {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts the service. This method must not be called concurrently with
|
* Starts the service.This method must not be called concurrently with
|
||||||
* {@link #stopService()}.
|
* {@link #stopService()}.
|
||||||
*/
|
*/
|
||||||
@Wakeful
|
|
||||||
void startService() throws ServiceException;
|
void startService() throws ServiceException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stops the service. This method must not be called concurrently with
|
* Stops the service. This method must not be called concurrently with
|
||||||
* {@link #startService()}.
|
* {@link #startService()}.
|
||||||
*/
|
*/
|
||||||
@Wakeful
|
|
||||||
void stopService() throws ServiceException;
|
void stopService() throws ServiceException;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package org.briarproject.bramble.api.logging;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.settings.Settings;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.logging.Handler;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
@NotNullByDefault
|
||||||
|
public interface PersistentLogManager {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The namespace of the (@link Settings) where the log key is stored.
|
||||||
|
*/
|
||||||
|
String LOG_SETTINGS_NAMESPACE = "log";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link Settings} key under which the log key is stored.
|
||||||
|
*/
|
||||||
|
String LOG_KEY_KEY = "logKey";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates and returns a persistent log handler that stores its logs in
|
||||||
|
* the given directory.
|
||||||
|
*/
|
||||||
|
Handler createLogHandler(File dir) throws IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a persistent log handler that stores its logs in the given
|
||||||
|
* directory and adds the handler to the given logger, replacing any
|
||||||
|
* existing persistent log handler.
|
||||||
|
*/
|
||||||
|
void addLogHandler(File dir, Logger logger) throws IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a {@link Scanner} for reading the persistent log entries stored
|
||||||
|
* in the given directory.
|
||||||
|
*
|
||||||
|
* @param old True if the previous session's log should be loaded, or false
|
||||||
|
* if the current session's log should be loaded
|
||||||
|
*/
|
||||||
|
Scanner getPersistedLog(File dir, boolean old) throws IOException;
|
||||||
|
}
|
||||||
@@ -10,12 +10,6 @@ public interface BluetoothConstants {
|
|||||||
String PROP_ADDRESS = "address";
|
String PROP_ADDRESS = "address";
|
||||||
String PROP_UUID = "uuid";
|
String PROP_UUID = "uuid";
|
||||||
|
|
||||||
// Local settings (not shared with contacts)
|
// Default value for PREF_PLUGIN_ENABLE
|
||||||
String PREF_ADDRESS_IS_REFLECTED = "addressIsReflected";
|
|
||||||
String PREF_EVER_CONNECTED = "everConnected";
|
|
||||||
|
|
||||||
// Default values for local settings
|
|
||||||
boolean DEFAULT_PREF_PLUGIN_ENABLE = false;
|
boolean DEFAULT_PREF_PLUGIN_ENABLE = false;
|
||||||
boolean DEFAULT_PREF_ADDRESS_IS_REFLECTED = false;
|
|
||||||
boolean DEFAULT_PREF_EVER_CONNECTED = false;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import org.briarproject.bramble.api.Pair;
|
|||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||||
import org.briarproject.bramble.api.settings.SettingsManager;
|
import org.briarproject.bramble.api.settings.SettingsManager;
|
||||||
import org.briarproject.bramble.api.system.Wakeful;
|
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
@@ -71,13 +70,11 @@ public interface Plugin {
|
|||||||
/**
|
/**
|
||||||
* Starts the plugin.
|
* Starts the plugin.
|
||||||
*/
|
*/
|
||||||
@Wakeful
|
|
||||||
void start() throws PluginException;
|
void start() throws PluginException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stops the plugin.
|
* Stops the plugin.
|
||||||
*/
|
*/
|
||||||
@Wakeful
|
|
||||||
void stop() throws PluginException;
|
void stop() throws PluginException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -109,7 +106,6 @@ public interface Plugin {
|
|||||||
* Attempts to create connections using the given transport properties,
|
* Attempts to create connections using the given transport properties,
|
||||||
* passing any created connections to the corresponding handlers.
|
* passing any created connections to the corresponding handlers.
|
||||||
*/
|
*/
|
||||||
@Wakeful
|
|
||||||
void poll(Collection<Pair<TransportProperties, ConnectionHandler>>
|
void poll(Collection<Pair<TransportProperties, ConnectionHandler>>
|
||||||
properties);
|
properties);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ import org.briarproject.bramble.api.plugin.event.TransportStateEvent;
|
|||||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||||
import org.briarproject.bramble.api.settings.Settings;
|
import org.briarproject.bramble.api.settings.Settings;
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An interface through which a transport plugin interacts with the rest of
|
* An interface through which a transport plugin interacts with the rest of
|
||||||
* the application.
|
* the application.
|
||||||
@@ -27,11 +25,6 @@ public interface PluginCallback extends ConnectionHandler {
|
|||||||
*/
|
*/
|
||||||
TransportProperties getLocalProperties();
|
TransportProperties getLocalProperties();
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the plugin's remote transport properties.
|
|
||||||
*/
|
|
||||||
Collection<TransportProperties> getRemoteProperties();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Merges the given settings with the plugin's settings
|
* Merges the given settings with the plugin's settings
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
package org.briarproject.bramble.api.plugin;
|
package org.briarproject.bramble.api.plugin;
|
||||||
|
|
||||||
import static java.util.concurrent.TimeUnit.DAYS;
|
|
||||||
|
|
||||||
public interface TorConstants {
|
public interface TorConstants {
|
||||||
|
|
||||||
TransportId ID = new TransportId("org.briarproject.bramble.tor");
|
TransportId ID = new TransportId("org.briarproject.bramble.tor");
|
||||||
@@ -21,14 +19,6 @@ public interface TorConstants {
|
|||||||
String PREF_TOR_PORT = "port";
|
String PREF_TOR_PORT = "port";
|
||||||
String PREF_TOR_MOBILE = "useMobileData";
|
String PREF_TOR_MOBILE = "useMobileData";
|
||||||
String PREF_TOR_ONLY_WHEN_CHARGING = "onlyWhenCharging";
|
String PREF_TOR_ONLY_WHEN_CHARGING = "onlyWhenCharging";
|
||||||
String HS_PRIVATE_KEY_V2 = "onionPrivKey";
|
|
||||||
String HS_PRIVATE_KEY_V3 = "onionPrivKey3";
|
|
||||||
String HS_V3_CREATED = "onionPrivKey3Created";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* How long to publish a v3 hidden service before retiring the v2 service.
|
|
||||||
*/
|
|
||||||
long V3_MIGRATION_PERIOD_MS = DAYS.toMillis(180);
|
|
||||||
|
|
||||||
// Values for PREF_TOR_NETWORK
|
// Values for PREF_TOR_NETWORK
|
||||||
int PREF_TOR_NETWORK_AUTOMATIC = 0;
|
int PREF_TOR_NETWORK_AUTOMATIC = 0;
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import org.briarproject.bramble.api.plugin.Plugin;
|
|||||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||||
import org.briarproject.bramble.api.rendezvous.KeyMaterialSource;
|
import org.briarproject.bramble.api.rendezvous.KeyMaterialSource;
|
||||||
import org.briarproject.bramble.api.rendezvous.RendezvousEndpoint;
|
import org.briarproject.bramble.api.rendezvous.RendezvousEndpoint;
|
||||||
import org.briarproject.bramble.api.system.Wakeful;
|
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
@@ -22,7 +21,6 @@ public interface DuplexPlugin extends Plugin {
|
|||||||
* Attempts to create and return a connection using the given transport
|
* Attempts to create and return a connection using the given transport
|
||||||
* properties. Returns null if a connection cannot be created.
|
* properties. Returns null if a connection cannot be created.
|
||||||
*/
|
*/
|
||||||
@Wakeful
|
|
||||||
@Nullable
|
@Nullable
|
||||||
DuplexTransportConnection createConnection(TransportProperties p);
|
DuplexTransportConnection createConnection(TransportProperties p);
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package org.briarproject.bramble.api.plugin.event;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.event.Event;
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
|
||||||
|
import javax.annotation.concurrent.Immutable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An event that informs the Bluetooth plugin that we have enabled the
|
||||||
|
* Bluetooth adapter.
|
||||||
|
*/
|
||||||
|
@Immutable
|
||||||
|
@NotNullByDefault
|
||||||
|
public class BluetoothEnabledEvent extends Event {
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package org.briarproject.bramble.api.plugin.event;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.event.Event;
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
|
||||||
|
import javax.annotation.concurrent.Immutable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An event that asks the Bluetooth plugin to disable the Bluetooth adapter if
|
||||||
|
* we previously enabled it.
|
||||||
|
*/
|
||||||
|
@Immutable
|
||||||
|
@NotNullByDefault
|
||||||
|
public class DisableBluetoothEvent extends Event {
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package org.briarproject.bramble.api.plugin.event;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.event.Event;
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
|
||||||
|
import javax.annotation.concurrent.Immutable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An event that asks the Bluetooth plugin to enable the Bluetooth adapter.
|
||||||
|
*/
|
||||||
|
@Immutable
|
||||||
|
@NotNullByDefault
|
||||||
|
public class EnableBluetoothEvent extends Event {
|
||||||
|
}
|
||||||
@@ -5,7 +5,6 @@ import org.briarproject.bramble.api.plugin.Plugin;
|
|||||||
import org.briarproject.bramble.api.plugin.TransportConnectionReader;
|
import org.briarproject.bramble.api.plugin.TransportConnectionReader;
|
||||||
import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
||||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||||
import org.briarproject.bramble.api.system.Wakeful;
|
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
@@ -19,7 +18,6 @@ public interface SimplexPlugin extends Plugin {
|
|||||||
* Attempts to create and return a reader for the given transport
|
* Attempts to create and return a reader for the given transport
|
||||||
* properties. Returns null if a reader cannot be created.
|
* properties. Returns null if a reader cannot be created.
|
||||||
*/
|
*/
|
||||||
@Wakeful
|
|
||||||
@Nullable
|
@Nullable
|
||||||
TransportConnectionReader createReader(TransportProperties p);
|
TransportConnectionReader createReader(TransportProperties p);
|
||||||
|
|
||||||
@@ -27,7 +25,6 @@ public interface SimplexPlugin extends Plugin {
|
|||||||
* Attempts to create and return a writer for the given transport
|
* Attempts to create and return a writer for the given transport
|
||||||
* properties. Returns null if a writer cannot be created.
|
* properties. Returns null if a writer cannot be created.
|
||||||
*/
|
*/
|
||||||
@Wakeful
|
|
||||||
@Nullable
|
@Nullable
|
||||||
TransportConnectionWriter createWriter(TransportProperties p);
|
TransportConnectionWriter createWriter(TransportProperties p);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,11 +12,6 @@ public interface TransportPropertyConstants {
|
|||||||
*/
|
*/
|
||||||
int MAX_PROPERTY_LENGTH = 100;
|
int MAX_PROPERTY_LENGTH = 100;
|
||||||
|
|
||||||
/**
|
|
||||||
* Prefix for keys that represent reflected properties.
|
|
||||||
*/
|
|
||||||
String REFLECTED_PROPERTY_PREFIX = "u:";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Message metadata key for the transport ID of a local or remote update,
|
* Message metadata key for the transport ID of a local or remote update,
|
||||||
* as a BDF string.
|
* as a BDF string.
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
package org.briarproject.bramble.api.properties.event;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.event.Event;
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|
||||||
import org.briarproject.bramble.api.plugin.TransportId;
|
|
||||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
|
||||||
|
|
||||||
import javax.annotation.concurrent.Immutable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An event that is broadcast when {@link TransportProperties} are received
|
|
||||||
* from a contact.
|
|
||||||
*/
|
|
||||||
@Immutable
|
|
||||||
@NotNullByDefault
|
|
||||||
public class RemoteTransportPropertiesUpdatedEvent extends Event {
|
|
||||||
|
|
||||||
private final TransportId transportId;
|
|
||||||
|
|
||||||
public RemoteTransportPropertiesUpdatedEvent(TransportId transportId) {
|
|
||||||
this.transportId = transportId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TransportId getTransportId() {
|
|
||||||
return transportId;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -29,15 +29,10 @@ public interface SyncConstants {
|
|||||||
*/
|
*/
|
||||||
int MESSAGE_HEADER_LENGTH = UniqueId.LENGTH + 8;
|
int MESSAGE_HEADER_LENGTH = UniqueId.LENGTH + 8;
|
||||||
|
|
||||||
/**
|
|
||||||
* The maximum length of a block in bytes.
|
|
||||||
*/
|
|
||||||
int MAX_BLOCK_LENGTH = 32 * 1024; // 32 KiB
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The maximum length of a message body in bytes.
|
* The maximum length of a message body in bytes.
|
||||||
*/
|
*/
|
||||||
int MAX_MESSAGE_BODY_LENGTH = MAX_BLOCK_LENGTH;
|
int MAX_MESSAGE_BODY_LENGTH = 32 * 1024; // 32 KiB
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The maximum length of a message in bytes.
|
* The maximum length of a message in bytes.
|
||||||
|
|||||||
@@ -7,16 +7,16 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|||||||
import javax.annotation.concurrent.Immutable;
|
import javax.annotation.concurrent.Immutable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An event that is broadcast when one or more messages are received from, or
|
* An event that is broadcast when a message is received from, or offered by, a
|
||||||
* offered by, a contact and need to be acknowledged.
|
* contact and needs to be acknowledged.
|
||||||
*/
|
*/
|
||||||
@Immutable
|
@Immutable
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public class MessagesToAckEvent extends Event {
|
public class MessageToAckEvent extends Event {
|
||||||
|
|
||||||
private final ContactId contactId;
|
private final ContactId contactId;
|
||||||
|
|
||||||
public MessagesToAckEvent(ContactId contactId) {
|
public MessageToAckEvent(ContactId contactId) {
|
||||||
this.contactId = contactId;
|
this.contactId = contactId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package org.briarproject.bramble.api.sync.event;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.contact.ContactId;
|
||||||
|
import org.briarproject.bramble.api.event.Event;
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
|
||||||
|
import javax.annotation.concurrent.Immutable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An event that is broadcast when a message is offered by a contact and needs
|
||||||
|
* to be requested.
|
||||||
|
*/
|
||||||
|
@Immutable
|
||||||
|
@NotNullByDefault
|
||||||
|
public class MessageToRequestEvent extends Event {
|
||||||
|
|
||||||
|
private final ContactId contactId;
|
||||||
|
|
||||||
|
public MessageToRequestEvent(ContactId contactId) {
|
||||||
|
this.contactId = contactId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ContactId getContactId() {
|
||||||
|
return contactId;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
package org.briarproject.bramble.api.sync.event;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.Consumable;
|
|
||||||
import org.briarproject.bramble.api.contact.ContactId;
|
|
||||||
import org.briarproject.bramble.api.event.Event;
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|
||||||
import org.briarproject.bramble.api.sync.MessageId;
|
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
|
||||||
import javax.annotation.concurrent.Immutable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An event that is broadcast when one or more messages are offered by a
|
|
||||||
* contact and need to be requested.
|
|
||||||
*/
|
|
||||||
@Immutable
|
|
||||||
@NotNullByDefault
|
|
||||||
public class MessagesToRequestEvent extends Event {
|
|
||||||
|
|
||||||
private final ContactId contactId;
|
|
||||||
private final Consumable<Collection<MessageId>> ids;
|
|
||||||
|
|
||||||
public MessagesToRequestEvent(ContactId contactId,
|
|
||||||
Collection<MessageId> ids) {
|
|
||||||
this.contactId = contactId;
|
|
||||||
this.ids = new Consumable<>(ids);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ContactId getContactId() {
|
|
||||||
return contactId;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public Collection<MessageId> consumeIds() {
|
|
||||||
return ids.consume();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
package org.briarproject.bramble.api.plugin;
|
package org.briarproject.bramble.api.system;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
@@ -12,11 +11,15 @@ import static java.lang.annotation.ElementType.PARAMETER;
|
|||||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Annotation for injecting the {@link File directory} where the Tor plugin
|
* Annotation for injecting a scheduled executor service
|
||||||
* should store its state.
|
* that can be used to schedule the execution of tasks.
|
||||||
|
* <p>
|
||||||
|
* The service should <b>only</b> be used for running tasks on other executors
|
||||||
|
* at scheduled times.
|
||||||
|
* No significant work should be run by the service itself!
|
||||||
*/
|
*/
|
||||||
@Qualifier
|
@Qualifier
|
||||||
@Target({FIELD, METHOD, PARAMETER})
|
@Target({FIELD, METHOD, PARAMETER})
|
||||||
@Retention(RUNTIME)
|
@Retention(RUNTIME)
|
||||||
public @interface TorDirectory {
|
public @interface Scheduler {
|
||||||
}
|
}
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
package org.briarproject.bramble.api.system;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|
||||||
|
|
||||||
import java.util.concurrent.Executor;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A service that can be used to schedule the execution of tasks.
|
|
||||||
*/
|
|
||||||
@NotNullByDefault
|
|
||||||
public interface TaskScheduler {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Submits the given task to the given executor after the given delay.
|
|
||||||
* <p>
|
|
||||||
* If the platform supports wake locks, a wake lock will be held while
|
|
||||||
* submitting and running the task.
|
|
||||||
*/
|
|
||||||
Cancellable schedule(Runnable task, Executor executor, long delay,
|
|
||||||
TimeUnit unit);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Submits the given task to the given executor after the given delay,
|
|
||||||
* and then repeatedly with the given interval between executions
|
|
||||||
* (measured from the end of one execution to the beginning of the next).
|
|
||||||
* <p>
|
|
||||||
* If the platform supports wake locks, a wake lock will be held while
|
|
||||||
* submitting and running the task.
|
|
||||||
*/
|
|
||||||
Cancellable scheduleWithFixedDelay(Runnable task, Executor executor,
|
|
||||||
long delay, long interval, TimeUnit unit);
|
|
||||||
|
|
||||||
interface Cancellable {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Cancels the task if it has not already started running. If the task
|
|
||||||
* is {@link #scheduleWithFixedDelay(Runnable, Executor, long, long, TimeUnit) periodic},
|
|
||||||
* all future executions of the task are cancelled.
|
|
||||||
*/
|
|
||||||
void cancel();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
package org.briarproject.bramble.api.system;
|
|
||||||
|
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.lang.annotation.Target;
|
|
||||||
|
|
||||||
import javax.inject.Qualifier;
|
|
||||||
|
|
||||||
import static java.lang.annotation.ElementType.METHOD;
|
|
||||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Annotation for methods that must be called while holding a wake lock, if
|
|
||||||
* the platform supports wake locks.
|
|
||||||
*/
|
|
||||||
@Qualifier
|
|
||||||
@Target(METHOD)
|
|
||||||
@Retention(RUNTIME)
|
|
||||||
public @interface Wakeful {
|
|
||||||
}
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
package org.briarproject.bramble.api.system;
|
|
||||||
|
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.lang.annotation.Target;
|
|
||||||
|
|
||||||
import javax.inject.Qualifier;
|
|
||||||
|
|
||||||
import static java.lang.annotation.ElementType.FIELD;
|
|
||||||
import static java.lang.annotation.ElementType.METHOD;
|
|
||||||
import static java.lang.annotation.ElementType.PARAMETER;
|
|
||||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Annotation for injecting the executor for long-running IO tasks that should
|
|
||||||
* run without sleeping. Also used for annotating methods that should run on
|
|
||||||
* this executor.
|
|
||||||
* <p>
|
|
||||||
* The contract of this executor is that tasks may be run concurrently, and
|
|
||||||
* submitting a task will never block. Tasks must not run indefinitely. Tasks
|
|
||||||
* submitted during shutdown are discarded.
|
|
||||||
*/
|
|
||||||
@Qualifier
|
|
||||||
@Target({FIELD, METHOD, PARAMETER})
|
|
||||||
@Retention(RUNTIME)
|
|
||||||
public @interface WakefulIoExecutor {
|
|
||||||
}
|
|
||||||
@@ -16,8 +16,13 @@ public interface StreamReaderFactory {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an {@link InputStream InputStream} for reading from a contact
|
* Creates an {@link InputStream InputStream} for reading from a contact
|
||||||
* exchangestream.
|
* exchange stream.
|
||||||
*/
|
*/
|
||||||
InputStream createContactExchangeStreamReader(InputStream in,
|
InputStream createContactExchangeStreamReader(InputStream in,
|
||||||
SecretKey headerKey);
|
SecretKey headerKey);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an {@link InputStream} for reading from a log stream.
|
||||||
|
*/
|
||||||
|
InputStream createLogStreamReader(InputStream in, SecretKey headerKey);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,15 +9,18 @@ import java.io.OutputStream;
|
|||||||
public interface StreamWriterFactory {
|
public interface StreamWriterFactory {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an {@link OutputStream OutputStream} for writing to a
|
* Creates a {@link StreamWriter} for writing to a transport stream.
|
||||||
* transport stream
|
|
||||||
*/
|
*/
|
||||||
StreamWriter createStreamWriter(OutputStream out, StreamContext ctx);
|
StreamWriter createStreamWriter(OutputStream out, StreamContext ctx);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an {@link OutputStream OutputStream} for writing to a contact
|
* Creates a {@link StreamWriter} for writing to a contact exchange stream.
|
||||||
* exchange stream.
|
|
||||||
*/
|
*/
|
||||||
StreamWriter createContactExchangeStreamWriter(OutputStream out,
|
StreamWriter createContactExchangeStreamWriter(OutputStream out,
|
||||||
SecretKey headerKey);
|
SecretKey headerKey);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a {@link StreamWriter} for writing to a log stream.
|
||||||
|
*/
|
||||||
|
StreamWriter createLogStreamWriter(OutputStream out, SecretKey headerKey);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import org.briarproject.bramble.plugin.PluginModule;
|
|||||||
import org.briarproject.bramble.properties.PropertiesModule;
|
import org.briarproject.bramble.properties.PropertiesModule;
|
||||||
import org.briarproject.bramble.rendezvous.RendezvousModule;
|
import org.briarproject.bramble.rendezvous.RendezvousModule;
|
||||||
import org.briarproject.bramble.sync.validation.ValidationModule;
|
import org.briarproject.bramble.sync.validation.ValidationModule;
|
||||||
|
import org.briarproject.bramble.system.SystemModule;
|
||||||
import org.briarproject.bramble.transport.TransportModule;
|
import org.briarproject.bramble.transport.TransportModule;
|
||||||
import org.briarproject.bramble.versioning.VersioningModule;
|
import org.briarproject.bramble.versioning.VersioningModule;
|
||||||
|
|
||||||
@@ -30,6 +31,8 @@ public interface BrambleCoreEagerSingletons {
|
|||||||
|
|
||||||
void inject(RendezvousModule.EagerSingletons init);
|
void inject(RendezvousModule.EagerSingletons init);
|
||||||
|
|
||||||
|
void inject(SystemModule.EagerSingletons init);
|
||||||
|
|
||||||
void inject(TransportModule.EagerSingletons init);
|
void inject(TransportModule.EagerSingletons init);
|
||||||
|
|
||||||
void inject(ValidationModule.EagerSingletons init);
|
void inject(ValidationModule.EagerSingletons init);
|
||||||
@@ -47,6 +50,7 @@ public interface BrambleCoreEagerSingletons {
|
|||||||
c.inject(new RendezvousModule.EagerSingletons());
|
c.inject(new RendezvousModule.EagerSingletons());
|
||||||
c.inject(new PluginModule.EagerSingletons());
|
c.inject(new PluginModule.EagerSingletons());
|
||||||
c.inject(new PropertiesModule.EagerSingletons());
|
c.inject(new PropertiesModule.EagerSingletons());
|
||||||
|
c.inject(new SystemModule.EagerSingletons());
|
||||||
c.inject(new TransportModule.EagerSingletons());
|
c.inject(new TransportModule.EagerSingletons());
|
||||||
c.inject(new ValidationModule.EagerSingletons());
|
c.inject(new ValidationModule.EagerSingletons());
|
||||||
c.inject(new VersioningModule.EagerSingletons());
|
c.inject(new VersioningModule.EagerSingletons());
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import org.briarproject.bramble.identity.IdentityModule;
|
|||||||
import org.briarproject.bramble.io.IoModule;
|
import org.briarproject.bramble.io.IoModule;
|
||||||
import org.briarproject.bramble.keyagreement.KeyAgreementModule;
|
import org.briarproject.bramble.keyagreement.KeyAgreementModule;
|
||||||
import org.briarproject.bramble.lifecycle.LifecycleModule;
|
import org.briarproject.bramble.lifecycle.LifecycleModule;
|
||||||
|
import org.briarproject.bramble.logging.LoggingModule;
|
||||||
import org.briarproject.bramble.plugin.PluginModule;
|
import org.briarproject.bramble.plugin.PluginModule;
|
||||||
import org.briarproject.bramble.properties.PropertiesModule;
|
import org.briarproject.bramble.properties.PropertiesModule;
|
||||||
import org.briarproject.bramble.record.RecordModule;
|
import org.briarproject.bramble.record.RecordModule;
|
||||||
@@ -21,7 +22,7 @@ import org.briarproject.bramble.rendezvous.RendezvousModule;
|
|||||||
import org.briarproject.bramble.settings.SettingsModule;
|
import org.briarproject.bramble.settings.SettingsModule;
|
||||||
import org.briarproject.bramble.sync.SyncModule;
|
import org.briarproject.bramble.sync.SyncModule;
|
||||||
import org.briarproject.bramble.sync.validation.ValidationModule;
|
import org.briarproject.bramble.sync.validation.ValidationModule;
|
||||||
import org.briarproject.bramble.system.ClockModule;
|
import org.briarproject.bramble.system.SystemModule;
|
||||||
import org.briarproject.bramble.transport.TransportModule;
|
import org.briarproject.bramble.transport.TransportModule;
|
||||||
import org.briarproject.bramble.versioning.VersioningModule;
|
import org.briarproject.bramble.versioning.VersioningModule;
|
||||||
|
|
||||||
@@ -29,7 +30,6 @@ import dagger.Module;
|
|||||||
|
|
||||||
@Module(includes = {
|
@Module(includes = {
|
||||||
ClientModule.class,
|
ClientModule.class,
|
||||||
ClockModule.class,
|
|
||||||
ConnectionModule.class,
|
ConnectionModule.class,
|
||||||
ContactModule.class,
|
ContactModule.class,
|
||||||
CryptoModule.class,
|
CryptoModule.class,
|
||||||
@@ -42,6 +42,7 @@ import dagger.Module;
|
|||||||
IoModule.class,
|
IoModule.class,
|
||||||
KeyAgreementModule.class,
|
KeyAgreementModule.class,
|
||||||
LifecycleModule.class,
|
LifecycleModule.class,
|
||||||
|
LoggingModule.class,
|
||||||
PluginModule.class,
|
PluginModule.class,
|
||||||
PropertiesModule.class,
|
PropertiesModule.class,
|
||||||
RecordModule.class,
|
RecordModule.class,
|
||||||
@@ -49,6 +50,7 @@ import dagger.Module;
|
|||||||
RendezvousModule.class,
|
RendezvousModule.class,
|
||||||
SettingsModule.class,
|
SettingsModule.class,
|
||||||
SyncModule.class,
|
SyncModule.class,
|
||||||
|
SystemModule.class,
|
||||||
TransportModule.class,
|
TransportModule.class,
|
||||||
ValidationModule.class,
|
ValidationModule.class,
|
||||||
VersioningModule.class
|
VersioningModule.class
|
||||||
|
|||||||
@@ -116,27 +116,25 @@ class ClientHelperImpl implements ClientHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Message getSmallMessage(MessageId m) throws DbException {
|
public Message getMessage(MessageId m) throws DbException {
|
||||||
return db.transactionWithResult(true, txn -> getSmallMessage(txn, m));
|
return db.transactionWithResult(true, txn -> getMessage(txn, m));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Message getSmallMessage(Transaction txn, MessageId m)
|
public Message getMessage(Transaction txn, MessageId m) throws DbException {
|
||||||
throws DbException {
|
return db.getMessage(txn, m);
|
||||||
return db.getSmallMessage(txn, m);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BdfList getSmallMessageAsList(MessageId m)
|
public BdfList getMessageAsList(MessageId m) throws DbException,
|
||||||
|
FormatException {
|
||||||
|
return db.transactionWithResult(true, txn -> getMessageAsList(txn, m));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BdfList getMessageAsList(Transaction txn, MessageId m)
|
||||||
throws DbException, FormatException {
|
throws DbException, FormatException {
|
||||||
return db.transactionWithResult(true, txn ->
|
return toList(db.getMessage(txn, m).getBody());
|
||||||
getSmallMessageAsList(txn, m));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BdfList getSmallMessageAsList(Transaction txn, MessageId m)
|
|
||||||
throws DbException, FormatException {
|
|
||||||
return toList(db.getSmallMessage(txn, m).getBody());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -36,4 +36,10 @@ class StreamDecrypterFactoryImpl implements StreamDecrypterFactory {
|
|||||||
SecretKey headerKey) {
|
SecretKey headerKey) {
|
||||||
return new StreamDecrypterImpl(in, cipherProvider.get(), 0, headerKey);
|
return new StreamDecrypterImpl(in, cipherProvider.get(), 0, headerKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public StreamDecrypter createLogStreamDecrypter(InputStream in,
|
||||||
|
SecretKey headerKey) {
|
||||||
|
return createContactExchangeStreamDecrypter(in, headerKey);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ class StreamEncrypterFactoryImpl implements StreamEncrypterFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StreamEncrypter createContactExchangeStreamDecrypter(
|
public StreamEncrypter createContactExchangeStreamEncrypter(
|
||||||
OutputStream out, SecretKey headerKey) {
|
OutputStream out, SecretKey headerKey) {
|
||||||
AuthenticatedCipher cipher = cipherProvider.get();
|
AuthenticatedCipher cipher = cipherProvider.get();
|
||||||
byte[] streamHeaderNonce = new byte[STREAM_HEADER_NONCE_LENGTH];
|
byte[] streamHeaderNonce = new byte[STREAM_HEADER_NONCE_LENGTH];
|
||||||
@@ -60,4 +60,10 @@ class StreamEncrypterFactoryImpl implements StreamEncrypterFactory {
|
|||||||
return new StreamEncrypterImpl(out, cipher, 0, null, streamHeaderNonce,
|
return new StreamEncrypterImpl(out, cipher, 0, null, streamHeaderNonce,
|
||||||
headerKey, frameKey);
|
headerKey, frameKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public StreamEncrypter createLogStreamEncrypter(OutputStream out,
|
||||||
|
SecretKey headerKey) {
|
||||||
|
return createContactExchangeStreamEncrypter(out, headerKey);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import org.briarproject.bramble.api.db.DataTooOldException;
|
|||||||
import org.briarproject.bramble.api.db.DatabaseComponent;
|
import org.briarproject.bramble.api.db.DatabaseComponent;
|
||||||
import org.briarproject.bramble.api.db.DbException;
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
import org.briarproject.bramble.api.db.MessageDeletedException;
|
import org.briarproject.bramble.api.db.MessageDeletedException;
|
||||||
import org.briarproject.bramble.api.db.MessageTooLargeException;
|
|
||||||
import org.briarproject.bramble.api.db.Metadata;
|
import org.briarproject.bramble.api.db.Metadata;
|
||||||
import org.briarproject.bramble.api.db.MigrationListener;
|
import org.briarproject.bramble.api.db.MigrationListener;
|
||||||
import org.briarproject.bramble.api.identity.Author;
|
import org.briarproject.bramble.api.identity.Author;
|
||||||
@@ -126,6 +125,11 @@ interface Database<T> {
|
|||||||
void addMessageDependency(T txn, Message dependent, MessageId dependency,
|
void addMessageDependency(T txn, Message dependent, MessageId dependency,
|
||||||
MessageState dependentState) throws DbException;
|
MessageState dependentState) throws DbException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Records that a message has been offered by the given contact.
|
||||||
|
*/
|
||||||
|
void addOfferedMessage(T txn, ContactId c, MessageId m) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stores a pending contact.
|
* Stores a pending contact.
|
||||||
*/
|
*/
|
||||||
@@ -214,6 +218,13 @@ interface Database<T> {
|
|||||||
boolean containsVisibleMessage(T txn, ContactId c, MessageId m)
|
boolean containsVisibleMessage(T txn, ContactId c, MessageId m)
|
||||||
throws DbException;
|
throws DbException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the number of messages offered by the given contact.
|
||||||
|
* <p/>
|
||||||
|
* Read-only.
|
||||||
|
*/
|
||||||
|
int countOfferedMessages(T txn, ContactId c) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes the message with the given ID. Unlike
|
* Deletes the message with the given ID. Unlike
|
||||||
* {@link #removeMessage(Object, MessageId)}, the message ID and any other
|
* {@link #removeMessage(Object, MessageId)}, the message ID and any other
|
||||||
@@ -321,14 +332,13 @@ interface Database<T> {
|
|||||||
Collection<Identity> getIdentities(T txn) throws DbException;
|
Collection<Identity> getIdentities(T txn) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the single-block message with the given ID.
|
* Returns the message with the given ID.
|
||||||
* <p/>
|
* <p/>
|
||||||
* Read-only.
|
* Read-only.
|
||||||
*
|
*
|
||||||
* @throws MessageDeletedException if the message has been deleted
|
* @throws MessageDeletedException if the message has been deleted
|
||||||
* @throws MessageTooLargeException if the message has more than one block
|
|
||||||
*/
|
*/
|
||||||
Message getSmallMessage(T txn, MessageId m) throws DbException;
|
Message getMessage(T txn, MessageId m) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the IDs and states of all dependencies of the given message.
|
* Returns the IDs and states of all dependencies of the given message.
|
||||||
@@ -447,13 +457,13 @@ interface Database<T> {
|
|||||||
int maxMessages, int maxLatency) throws DbException;
|
int maxMessages, int maxLatency) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the IDs of some single-block messages that are eligible to be
|
* Returns the IDs of some messages that are eligible to be requested from
|
||||||
* offered to the given contact, up to the given number of messages.
|
* the given contact, up to the given number of messages.
|
||||||
* <p/>
|
* <p/>
|
||||||
* Read-only.
|
* Read-only.
|
||||||
*/
|
*/
|
||||||
Collection<MessageId> getSmallMessagesToOffer(T txn, ContactId c,
|
Collection<MessageId> getMessagesToRequest(T txn, ContactId c,
|
||||||
int maxMessages, int maxLatency) throws DbException;
|
int maxMessages) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the IDs of some messages that are eligible to be sent to the
|
* Returns the IDs of some messages that are eligible to be sent to the
|
||||||
@@ -464,15 +474,6 @@ interface Database<T> {
|
|||||||
Collection<MessageId> getMessagesToSend(T txn, ContactId c, int maxLength,
|
Collection<MessageId> getMessagesToSend(T txn, ContactId c, int maxLength,
|
||||||
int maxLatency) throws DbException;
|
int maxLatency) throws DbException;
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the IDs of some single-block messages that are eligible to be
|
|
||||||
* sent to the given contact, up to the given total length.
|
|
||||||
* <p/>
|
|
||||||
* Read-only.
|
|
||||||
*/
|
|
||||||
Collection<MessageId> getSmallMessagesToSend(T txn, ContactId c,
|
|
||||||
int maxLength, int maxLatency) throws DbException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the IDs of any messages that need to be validated.
|
* Returns the IDs of any messages that need to be validated.
|
||||||
* <p/>
|
* <p/>
|
||||||
@@ -635,6 +636,13 @@ interface Database<T> {
|
|||||||
*/
|
*/
|
||||||
void removeMessage(T txn, MessageId m) throws DbException;
|
void removeMessage(T txn, MessageId m) throws DbException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the given offered messages that were offered by the given
|
||||||
|
* contact.
|
||||||
|
*/
|
||||||
|
void removeOfferedMessages(T txn, ContactId c,
|
||||||
|
Collection<MessageId> requested) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes a pending contact (and all associated state) from the database.
|
* Removes a pending contact (and all associated state) from the database.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -61,10 +61,10 @@ import org.briarproject.bramble.api.sync.event.MessageAddedEvent;
|
|||||||
import org.briarproject.bramble.api.sync.event.MessageRequestedEvent;
|
import org.briarproject.bramble.api.sync.event.MessageRequestedEvent;
|
||||||
import org.briarproject.bramble.api.sync.event.MessageSharedEvent;
|
import org.briarproject.bramble.api.sync.event.MessageSharedEvent;
|
||||||
import org.briarproject.bramble.api.sync.event.MessageStateChangedEvent;
|
import org.briarproject.bramble.api.sync.event.MessageStateChangedEvent;
|
||||||
|
import org.briarproject.bramble.api.sync.event.MessageToAckEvent;
|
||||||
|
import org.briarproject.bramble.api.sync.event.MessageToRequestEvent;
|
||||||
import org.briarproject.bramble.api.sync.event.MessagesAckedEvent;
|
import org.briarproject.bramble.api.sync.event.MessagesAckedEvent;
|
||||||
import org.briarproject.bramble.api.sync.event.MessagesSentEvent;
|
import org.briarproject.bramble.api.sync.event.MessagesSentEvent;
|
||||||
import org.briarproject.bramble.api.sync.event.MessagesToAckEvent;
|
|
||||||
import org.briarproject.bramble.api.sync.event.MessagesToRequestEvent;
|
|
||||||
import org.briarproject.bramble.api.sync.event.SyncVersionsUpdatedEvent;
|
import org.briarproject.bramble.api.sync.event.SyncVersionsUpdatedEvent;
|
||||||
import org.briarproject.bramble.api.sync.validation.MessageState;
|
import org.briarproject.bramble.api.sync.validation.MessageState;
|
||||||
import org.briarproject.bramble.api.transport.KeySetId;
|
import org.briarproject.bramble.api.transport.KeySetId;
|
||||||
@@ -91,6 +91,7 @@ import static org.briarproject.bramble.api.sync.Group.Visibility.INVISIBLE;
|
|||||||
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
|
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
|
||||||
import static org.briarproject.bramble.api.sync.validation.MessageState.DELIVERED;
|
import static org.briarproject.bramble.api.sync.validation.MessageState.DELIVERED;
|
||||||
import static org.briarproject.bramble.api.sync.validation.MessageState.UNKNOWN;
|
import static org.briarproject.bramble.api.sync.validation.MessageState.UNKNOWN;
|
||||||
|
import static org.briarproject.bramble.db.DatabaseConstants.MAX_OFFERED_MESSAGES;
|
||||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
import static org.briarproject.bramble.util.LogUtils.now;
|
import static org.briarproject.bramble.util.LogUtils.now;
|
||||||
@@ -405,22 +406,19 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
|||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
public Collection<Message> generateBatch(Transaction transaction,
|
public Collection<Message> generateBatch(Transaction transaction,
|
||||||
ContactId c, int maxLength, int maxLatency, boolean small)
|
ContactId c, int maxLength, int maxLatency) throws DbException {
|
||||||
throws DbException {
|
|
||||||
if (transaction.isReadOnly()) throw new IllegalArgumentException();
|
if (transaction.isReadOnly()) throw new IllegalArgumentException();
|
||||||
T txn = unbox(transaction);
|
T txn = unbox(transaction);
|
||||||
if (!db.containsContact(txn, c))
|
if (!db.containsContact(txn, c))
|
||||||
throw new NoSuchContactException();
|
throw new NoSuchContactException();
|
||||||
Collection<MessageId> ids;
|
Collection<MessageId> ids =
|
||||||
if (small)
|
db.getMessagesToSend(txn, c, maxLength, maxLatency);
|
||||||
ids = db.getSmallMessagesToSend(txn, c, maxLength, maxLatency);
|
|
||||||
else ids = db.getMessagesToSend(txn, c, maxLength, maxLatency);
|
|
||||||
if (ids.isEmpty()) return null;
|
|
||||||
List<Message> messages = new ArrayList<>(ids.size());
|
List<Message> messages = new ArrayList<>(ids.size());
|
||||||
for (MessageId m : ids) {
|
for (MessageId m : ids) {
|
||||||
messages.add(db.getSmallMessage(txn, m));
|
messages.add(db.getMessage(txn, m));
|
||||||
db.updateExpiryTimeAndEta(txn, c, m, maxLatency);
|
db.updateExpiryTimeAndEta(txn, c, m, maxLatency);
|
||||||
}
|
}
|
||||||
|
if (ids.isEmpty()) return null;
|
||||||
db.lowerRequestedFlag(txn, c, ids);
|
db.lowerRequestedFlag(txn, c, ids);
|
||||||
transaction.attach(new MessagesSentEvent(c, ids));
|
transaction.attach(new MessagesSentEvent(c, ids));
|
||||||
return messages;
|
return messages;
|
||||||
@@ -429,21 +427,34 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
|||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
public Offer generateOffer(Transaction transaction, ContactId c,
|
public Offer generateOffer(Transaction transaction, ContactId c,
|
||||||
int maxMessages, int maxLatency, boolean small) throws DbException {
|
int maxMessages, int maxLatency) throws DbException {
|
||||||
if (transaction.isReadOnly()) throw new IllegalArgumentException();
|
if (transaction.isReadOnly()) throw new IllegalArgumentException();
|
||||||
T txn = unbox(transaction);
|
T txn = unbox(transaction);
|
||||||
if (!db.containsContact(txn, c))
|
if (!db.containsContact(txn, c))
|
||||||
throw new NoSuchContactException();
|
throw new NoSuchContactException();
|
||||||
Collection<MessageId> ids;
|
Collection<MessageId> ids =
|
||||||
if (small)
|
db.getMessagesToOffer(txn, c, maxMessages, maxLatency);
|
||||||
ids = db.getSmallMessagesToOffer(txn, c, maxMessages, maxLatency);
|
|
||||||
else ids = db.getMessagesToOffer(txn, c, maxMessages, maxLatency);
|
|
||||||
if (ids.isEmpty()) return null;
|
if (ids.isEmpty()) return null;
|
||||||
for (MessageId m : ids)
|
for (MessageId m : ids)
|
||||||
db.updateExpiryTimeAndEta(txn, c, m, maxLatency);
|
db.updateExpiryTimeAndEta(txn, c, m, maxLatency);
|
||||||
return new Offer(ids);
|
return new Offer(ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public Request generateRequest(Transaction transaction, ContactId c,
|
||||||
|
int maxMessages) throws DbException {
|
||||||
|
if (transaction.isReadOnly()) throw new IllegalArgumentException();
|
||||||
|
T txn = unbox(transaction);
|
||||||
|
if (!db.containsContact(txn, c))
|
||||||
|
throw new NoSuchContactException();
|
||||||
|
Collection<MessageId> ids = db.getMessagesToRequest(txn, c,
|
||||||
|
maxMessages);
|
||||||
|
if (ids.isEmpty()) return null;
|
||||||
|
db.removeOfferedMessages(txn, c, ids);
|
||||||
|
return new Request(ids);
|
||||||
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
public Collection<Message> generateRequestedBatch(Transaction transaction,
|
public Collection<Message> generateRequestedBatch(Transaction transaction,
|
||||||
@@ -454,12 +465,12 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
|||||||
throw new NoSuchContactException();
|
throw new NoSuchContactException();
|
||||||
Collection<MessageId> ids =
|
Collection<MessageId> ids =
|
||||||
db.getRequestedMessagesToSend(txn, c, maxLength, maxLatency);
|
db.getRequestedMessagesToSend(txn, c, maxLength, maxLatency);
|
||||||
if (ids.isEmpty()) return null;
|
|
||||||
List<Message> messages = new ArrayList<>(ids.size());
|
List<Message> messages = new ArrayList<>(ids.size());
|
||||||
for (MessageId m : ids) {
|
for (MessageId m : ids) {
|
||||||
messages.add(db.getSmallMessage(txn, m));
|
messages.add(db.getMessage(txn, m));
|
||||||
db.updateExpiryTimeAndEta(txn, c, m, maxLatency);
|
db.updateExpiryTimeAndEta(txn, c, m, maxLatency);
|
||||||
}
|
}
|
||||||
|
if (ids.isEmpty()) return null;
|
||||||
db.lowerRequestedFlag(txn, c, ids);
|
db.lowerRequestedFlag(txn, c, ids);
|
||||||
transaction.attach(new MessagesSentEvent(c, ids));
|
transaction.attach(new MessagesSentEvent(c, ids));
|
||||||
return messages;
|
return messages;
|
||||||
@@ -548,12 +559,12 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Message getSmallMessage(Transaction transaction, MessageId m)
|
public Message getMessage(Transaction transaction, MessageId m)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
T txn = unbox(transaction);
|
T txn = unbox(transaction);
|
||||||
if (!db.containsMessage(txn, m))
|
if (!db.containsMessage(txn, m))
|
||||||
throw new NoSuchMessageException();
|
throw new NoSuchMessageException();
|
||||||
return db.getSmallMessage(txn, m);
|
return db.getMessage(txn, m);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -808,7 +819,7 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
|||||||
db.addMessage(txn, m, UNKNOWN, false, false, c);
|
db.addMessage(txn, m, UNKNOWN, false, false, c);
|
||||||
transaction.attach(new MessageAddedEvent(m, c));
|
transaction.attach(new MessageAddedEvent(m, c));
|
||||||
}
|
}
|
||||||
transaction.attach(new MessagesToAckEvent(c));
|
transaction.attach(new MessageToAckEvent(c));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -819,20 +830,21 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
|||||||
T txn = unbox(transaction);
|
T txn = unbox(transaction);
|
||||||
if (!db.containsContact(txn, c))
|
if (!db.containsContact(txn, c))
|
||||||
throw new NoSuchContactException();
|
throw new NoSuchContactException();
|
||||||
boolean ack = false;
|
boolean ack = false, request = false;
|
||||||
List<MessageId> request = new ArrayList<>(o.getMessageIds().size());
|
int count = db.countOfferedMessages(txn, c);
|
||||||
for (MessageId m : o.getMessageIds()) {
|
for (MessageId m : o.getMessageIds()) {
|
||||||
if (db.containsVisibleMessage(txn, c, m)) {
|
if (db.containsVisibleMessage(txn, c, m)) {
|
||||||
db.raiseSeenFlag(txn, c, m);
|
db.raiseSeenFlag(txn, c, m);
|
||||||
db.raiseAckFlag(txn, c, m);
|
db.raiseAckFlag(txn, c, m);
|
||||||
ack = true;
|
ack = true;
|
||||||
} else {
|
} else if (count < MAX_OFFERED_MESSAGES) {
|
||||||
request.add(m);
|
db.addOfferedMessage(txn, c, m);
|
||||||
|
request = true;
|
||||||
|
count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (ack) transaction.attach(new MessagesToAckEvent(c));
|
if (ack) transaction.attach(new MessageToAckEvent(c));
|
||||||
if (!request.isEmpty())
|
if (request) transaction.attach(new MessageToRequestEvent(c));
|
||||||
transaction.attach(new MessagesToRequestEvent(c, request));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -6,6 +6,13 @@ import static java.util.concurrent.TimeUnit.DAYS;
|
|||||||
|
|
||||||
interface DatabaseConstants {
|
interface DatabaseConstants {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The maximum number of offered messages from each contact that will be
|
||||||
|
* stored. If offers arrive more quickly than requests can be sent and this
|
||||||
|
* limit is reached, additional offers will not be stored.
|
||||||
|
*/
|
||||||
|
int MAX_OFFERED_MESSAGES = 1000;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The namespace of the {@link Settings} where the database schema version
|
* The namespace of the {@link Settings} where the database schema version
|
||||||
* is stored.
|
* is stored.
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import org.briarproject.bramble.api.db.TransactionManager;
|
|||||||
import org.briarproject.bramble.api.event.EventBus;
|
import org.briarproject.bramble.api.event.EventBus;
|
||||||
import org.briarproject.bramble.api.event.EventExecutor;
|
import org.briarproject.bramble.api.event.EventExecutor;
|
||||||
import org.briarproject.bramble.api.lifecycle.ShutdownManager;
|
import org.briarproject.bramble.api.lifecycle.ShutdownManager;
|
||||||
|
import org.briarproject.bramble.api.sync.MessageFactory;
|
||||||
import org.briarproject.bramble.api.system.Clock;
|
import org.briarproject.bramble.api.system.Clock;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
@@ -21,8 +22,9 @@ public class DatabaseModule {
|
|||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
Database<Connection> provideDatabase(DatabaseConfig config, Clock clock) {
|
Database<Connection> provideDatabase(DatabaseConfig config,
|
||||||
return new H2Database(config, clock);
|
MessageFactory messageFactory, Clock clock) {
|
||||||
|
return new H2Database(config, messageFactory, clock);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import org.briarproject.bramble.api.db.DbClosedException;
|
|||||||
import org.briarproject.bramble.api.db.DbException;
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
import org.briarproject.bramble.api.db.MigrationListener;
|
import org.briarproject.bramble.api.db.MigrationListener;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.sync.MessageFactory;
|
||||||
import org.briarproject.bramble.api.system.Clock;
|
import org.briarproject.bramble.api.system.Clock;
|
||||||
import org.briarproject.bramble.util.StringUtils;
|
import org.briarproject.bramble.util.StringUtils;
|
||||||
|
|
||||||
@@ -50,8 +51,9 @@ class H2Database extends JdbcDatabase {
|
|||||||
private volatile SecretKey key = null;
|
private volatile SecretKey key = null;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
H2Database(DatabaseConfig config, Clock clock) {
|
H2Database(DatabaseConfig config, MessageFactory messageFactory,
|
||||||
super(dbTypes, clock);
|
Clock clock) {
|
||||||
|
super(dbTypes, messageFactory, clock);
|
||||||
this.config = config;
|
this.config = config;
|
||||||
File dir = config.getDatabaseDirectory();
|
File dir = config.getDatabaseDirectory();
|
||||||
String path = new File(dir, "db").getAbsolutePath();
|
String path = new File(dir, "db").getAbsolutePath();
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import org.briarproject.bramble.api.db.DbClosedException;
|
|||||||
import org.briarproject.bramble.api.db.DbException;
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
import org.briarproject.bramble.api.db.MigrationListener;
|
import org.briarproject.bramble.api.db.MigrationListener;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.sync.MessageFactory;
|
||||||
import org.briarproject.bramble.api.system.Clock;
|
import org.briarproject.bramble.api.system.Clock;
|
||||||
import org.briarproject.bramble.util.StringUtils;
|
import org.briarproject.bramble.util.StringUtils;
|
||||||
|
|
||||||
@@ -50,8 +51,9 @@ class HyperSqlDatabase extends JdbcDatabase {
|
|||||||
private volatile SecretKey key = null;
|
private volatile SecretKey key = null;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
HyperSqlDatabase(DatabaseConfig config, Clock clock) {
|
HyperSqlDatabase(DatabaseConfig config, MessageFactory messageFactory,
|
||||||
super(dbTypes, clock);
|
Clock clock) {
|
||||||
|
super(dbTypes, messageFactory, clock);
|
||||||
this.config = config;
|
this.config = config;
|
||||||
File dir = config.getDatabaseDirectory();
|
File dir = config.getDatabaseDirectory();
|
||||||
String path = new File(dir, "db").getAbsolutePath();
|
String path = new File(dir, "db").getAbsolutePath();
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ import org.briarproject.bramble.api.db.DataTooOldException;
|
|||||||
import org.briarproject.bramble.api.db.DbClosedException;
|
import org.briarproject.bramble.api.db.DbClosedException;
|
||||||
import org.briarproject.bramble.api.db.DbException;
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
import org.briarproject.bramble.api.db.MessageDeletedException;
|
import org.briarproject.bramble.api.db.MessageDeletedException;
|
||||||
import org.briarproject.bramble.api.db.MessageTooLargeException;
|
|
||||||
import org.briarproject.bramble.api.db.Metadata;
|
import org.briarproject.bramble.api.db.Metadata;
|
||||||
import org.briarproject.bramble.api.db.MigrationListener;
|
import org.briarproject.bramble.api.db.MigrationListener;
|
||||||
import org.briarproject.bramble.api.identity.Author;
|
import org.briarproject.bramble.api.identity.Author;
|
||||||
@@ -31,6 +30,7 @@ import org.briarproject.bramble.api.sync.Group;
|
|||||||
import org.briarproject.bramble.api.sync.Group.Visibility;
|
import org.briarproject.bramble.api.sync.Group.Visibility;
|
||||||
import org.briarproject.bramble.api.sync.GroupId;
|
import org.briarproject.bramble.api.sync.GroupId;
|
||||||
import org.briarproject.bramble.api.sync.Message;
|
import org.briarproject.bramble.api.sync.Message;
|
||||||
|
import org.briarproject.bramble.api.sync.MessageFactory;
|
||||||
import org.briarproject.bramble.api.sync.MessageId;
|
import org.briarproject.bramble.api.sync.MessageId;
|
||||||
import org.briarproject.bramble.api.sync.MessageStatus;
|
import org.briarproject.bramble.api.sync.MessageStatus;
|
||||||
import org.briarproject.bramble.api.sync.validation.MessageState;
|
import org.briarproject.bramble.api.sync.validation.MessageState;
|
||||||
@@ -76,7 +76,6 @@ import static org.briarproject.bramble.api.db.Metadata.REMOVE;
|
|||||||
import static org.briarproject.bramble.api.sync.Group.Visibility.INVISIBLE;
|
import static org.briarproject.bramble.api.sync.Group.Visibility.INVISIBLE;
|
||||||
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
|
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
|
||||||
import static org.briarproject.bramble.api.sync.Group.Visibility.VISIBLE;
|
import static org.briarproject.bramble.api.sync.Group.Visibility.VISIBLE;
|
||||||
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_BLOCK_LENGTH;
|
|
||||||
import static org.briarproject.bramble.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
|
import static org.briarproject.bramble.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
|
||||||
import static org.briarproject.bramble.api.sync.validation.MessageState.DELIVERED;
|
import static org.briarproject.bramble.api.sync.validation.MessageState.DELIVERED;
|
||||||
import static org.briarproject.bramble.api.sync.validation.MessageState.PENDING;
|
import static org.briarproject.bramble.api.sync.validation.MessageState.PENDING;
|
||||||
@@ -99,7 +98,7 @@ import static org.briarproject.bramble.util.LogUtils.now;
|
|||||||
abstract class JdbcDatabase implements Database<Connection> {
|
abstract class JdbcDatabase implements Database<Connection> {
|
||||||
|
|
||||||
// Package access for testing
|
// Package access for testing
|
||||||
static final int CODE_SCHEMA_VERSION = 49;
|
static final int CODE_SCHEMA_VERSION = 47;
|
||||||
|
|
||||||
// Time period offsets for incoming transport keys
|
// Time period offsets for incoming transport keys
|
||||||
private static final int OFFSET_PREV = -1;
|
private static final int OFFSET_PREV = -1;
|
||||||
@@ -181,9 +180,8 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
+ " state INT NOT NULL,"
|
+ " state INT NOT NULL,"
|
||||||
+ " shared BOOLEAN NOT NULL,"
|
+ " shared BOOLEAN NOT NULL,"
|
||||||
+ " temporary BOOLEAN NOT NULL,"
|
+ " temporary BOOLEAN NOT NULL,"
|
||||||
+ " length INT NOT NULL," // Includes message header
|
+ " length INT NOT NULL,"
|
||||||
+ " deleted BOOLEAN NOT NULL,"
|
+ " raw BLOB," // Null if message has been deleted
|
||||||
+ " blockCount INT NOT NULL,"
|
|
||||||
+ " PRIMARY KEY (messageId),"
|
+ " PRIMARY KEY (messageId),"
|
||||||
+ " FOREIGN KEY (groupId)"
|
+ " FOREIGN KEY (groupId)"
|
||||||
+ " REFERENCES groups (groupId)"
|
+ " REFERENCES groups (groupId)"
|
||||||
@@ -220,15 +218,13 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
+ " REFERENCES messages (messageId)"
|
+ " REFERENCES messages (messageId)"
|
||||||
+ " ON DELETE CASCADE)";
|
+ " ON DELETE CASCADE)";
|
||||||
|
|
||||||
private static final String CREATE_BLOCKS =
|
private static final String CREATE_OFFERS =
|
||||||
"CREATE TABLE blocks"
|
"CREATE TABLE offers"
|
||||||
+ " (messageId _HASH NOT NULL,"
|
+ " (messageId _HASH NOT NULL," // Not a foreign key
|
||||||
+ " blockNumber INT NOT NULL,"
|
+ " contactId INT NOT NULL,"
|
||||||
+ " blockLength INT NOT NULL," // Excludes block header
|
+ " PRIMARY KEY (messageId, contactId),"
|
||||||
+ " data BLOB," // Null if message has been deleted
|
+ " FOREIGN KEY (contactId)"
|
||||||
+ " PRIMARY KEY (messageId, blockNumber),"
|
+ " REFERENCES contacts (contactId)"
|
||||||
+ " FOREIGN KEY (messageId)"
|
|
||||||
+ " REFERENCES messages (messageId)"
|
|
||||||
+ " ON DELETE CASCADE)";
|
+ " ON DELETE CASCADE)";
|
||||||
|
|
||||||
private static final String CREATE_STATUSES =
|
private static final String CREATE_STATUSES =
|
||||||
@@ -343,6 +339,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
getLogger(JdbcDatabase.class.getName());
|
getLogger(JdbcDatabase.class.getName());
|
||||||
|
|
||||||
|
private final MessageFactory messageFactory;
|
||||||
private final Clock clock;
|
private final Clock clock;
|
||||||
private final DatabaseTypes dbTypes;
|
private final DatabaseTypes dbTypes;
|
||||||
|
|
||||||
@@ -362,8 +359,10 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
|
|
||||||
protected abstract void compactAndClose() throws DbException;
|
protected abstract void compactAndClose() throws DbException;
|
||||||
|
|
||||||
JdbcDatabase(DatabaseTypes databaseTypes, Clock clock) {
|
JdbcDatabase(DatabaseTypes databaseTypes, MessageFactory messageFactory,
|
||||||
|
Clock clock) {
|
||||||
this.dbTypes = databaseTypes;
|
this.dbTypes = databaseTypes;
|
||||||
|
this.messageFactory = messageFactory;
|
||||||
this.clock = clock;
|
this.clock = clock;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -441,12 +440,10 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO))
|
||||||
LOG.info("Migrating from schema " + start + " to " + end);
|
LOG.info("Migrating from schema " + start + " to " + end);
|
||||||
if (listener != null) listener.onDatabaseMigration();
|
if (listener != null) listener.onDatabaseMigration();
|
||||||
long startTime = now();
|
|
||||||
// Apply the migration
|
// Apply the migration
|
||||||
m.migrate(txn);
|
m.migrate(txn);
|
||||||
// Store the new schema version
|
// Store the new schema version
|
||||||
storeSchemaVersion(txn, end);
|
storeSchemaVersion(txn, end);
|
||||||
logDuration(LOG, "Migration", startTime);
|
|
||||||
dataSchemaVersion = end;
|
dataSchemaVersion = end;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -466,9 +463,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
new Migration43_44(dbTypes),
|
new Migration43_44(dbTypes),
|
||||||
new Migration44_45(),
|
new Migration44_45(),
|
||||||
new Migration45_46(),
|
new Migration45_46(),
|
||||||
new Migration46_47(dbTypes),
|
new Migration46_47(dbTypes)
|
||||||
new Migration47_48(dbTypes),
|
|
||||||
new Migration48_49()
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -513,7 +508,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
s.executeUpdate(dbTypes.replaceTypes(CREATE_MESSAGES));
|
s.executeUpdate(dbTypes.replaceTypes(CREATE_MESSAGES));
|
||||||
s.executeUpdate(dbTypes.replaceTypes(CREATE_MESSAGE_METADATA));
|
s.executeUpdate(dbTypes.replaceTypes(CREATE_MESSAGE_METADATA));
|
||||||
s.executeUpdate(dbTypes.replaceTypes(CREATE_MESSAGE_DEPENDENCIES));
|
s.executeUpdate(dbTypes.replaceTypes(CREATE_MESSAGE_DEPENDENCIES));
|
||||||
s.executeUpdate(dbTypes.replaceTypes(CREATE_BLOCKS));
|
s.executeUpdate(dbTypes.replaceTypes(CREATE_OFFERS));
|
||||||
s.executeUpdate(dbTypes.replaceTypes(CREATE_STATUSES));
|
s.executeUpdate(dbTypes.replaceTypes(CREATE_STATUSES));
|
||||||
s.executeUpdate(dbTypes.replaceTypes(CREATE_TRANSPORTS));
|
s.executeUpdate(dbTypes.replaceTypes(CREATE_TRANSPORTS));
|
||||||
s.executeUpdate(dbTypes.replaceTypes(CREATE_PENDING_CONTACTS));
|
s.executeUpdate(dbTypes.replaceTypes(CREATE_PENDING_CONTACTS));
|
||||||
@@ -731,7 +726,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
try {
|
try {
|
||||||
String sql = "SELECT messageId, timestamp, state, shared,"
|
String sql = "SELECT messageId, timestamp, state, shared,"
|
||||||
+ " length, deleted"
|
+ " length, raw IS NULL"
|
||||||
+ " FROM messages"
|
+ " FROM messages"
|
||||||
+ " WHERE groupId = ?";
|
+ " WHERE groupId = ?";
|
||||||
ps = txn.prepareStatement(sql);
|
ps = txn.prepareStatement(sql);
|
||||||
@@ -744,8 +739,9 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
boolean messageShared = rs.getBoolean(4);
|
boolean messageShared = rs.getBoolean(4);
|
||||||
int length = rs.getInt(5);
|
int length = rs.getInt(5);
|
||||||
boolean deleted = rs.getBoolean(6);
|
boolean deleted = rs.getBoolean(6);
|
||||||
|
boolean seen = removeOfferedMessage(txn, c, id);
|
||||||
addStatus(txn, id, c, g, timestamp, length, state, groupShared,
|
addStatus(txn, id, c, g, timestamp, length, state, groupShared,
|
||||||
messageShared, deleted, false);
|
messageShared, deleted, seen);
|
||||||
}
|
}
|
||||||
rs.close();
|
rs.close();
|
||||||
ps.close();
|
ps.close();
|
||||||
@@ -792,8 +788,8 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
try {
|
try {
|
||||||
String sql = "INSERT INTO messages (messageId, groupId, timestamp,"
|
String sql = "INSERT INTO messages (messageId, groupId, timestamp,"
|
||||||
+ " state, shared, temporary, length, deleted, blockCount)"
|
+ " state, shared, temporary, length, raw)"
|
||||||
+ " VALUES (?, ?, ?, ?, ?, ?, ?, FALSE, 1)";
|
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
|
||||||
ps = txn.prepareStatement(sql);
|
ps = txn.prepareStatement(sql);
|
||||||
ps.setBytes(1, m.getId().getBytes());
|
ps.setBytes(1, m.getId().getBytes());
|
||||||
ps.setBytes(2, m.getGroupId().getBytes());
|
ps.setBytes(2, m.getGroupId().getBytes());
|
||||||
@@ -801,29 +797,21 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
ps.setInt(4, state.getValue());
|
ps.setInt(4, state.getValue());
|
||||||
ps.setBoolean(5, shared);
|
ps.setBoolean(5, shared);
|
||||||
ps.setBoolean(6, temporary);
|
ps.setBoolean(6, temporary);
|
||||||
ps.setInt(7, m.getRawLength());
|
byte[] raw = messageFactory.getRawMessage(m);
|
||||||
|
ps.setInt(7, raw.length);
|
||||||
|
ps.setBytes(8, raw);
|
||||||
int affected = ps.executeUpdate();
|
int affected = ps.executeUpdate();
|
||||||
if (affected != 1) throw new DbStateException();
|
if (affected != 1) throw new DbStateException();
|
||||||
ps.close();
|
ps.close();
|
||||||
sql = "INSERT INTO blocks (messageId, blockNumber, blockLength,"
|
|
||||||
+ " data)"
|
|
||||||
+ " VALUES (?, 0, ?, ?)";
|
|
||||||
ps = txn.prepareStatement(sql);
|
|
||||||
ps.setBytes(1, m.getId().getBytes());
|
|
||||||
ps.setInt(2, m.getBody().length);
|
|
||||||
ps.setBytes(3, m.getBody());
|
|
||||||
affected = ps.executeUpdate();
|
|
||||||
if (affected != 1) throw new DbStateException();
|
|
||||||
ps.close();
|
|
||||||
// Create a status row for each contact that can see the group
|
// Create a status row for each contact that can see the group
|
||||||
Map<ContactId, Boolean> visibility =
|
Map<ContactId, Boolean> visibility =
|
||||||
getGroupVisibility(txn, m.getGroupId());
|
getGroupVisibility(txn, m.getGroupId());
|
||||||
for (Entry<ContactId, Boolean> e : visibility.entrySet()) {
|
for (Entry<ContactId, Boolean> e : visibility.entrySet()) {
|
||||||
ContactId c = e.getKey();
|
ContactId c = e.getKey();
|
||||||
boolean seen = c.equals(sender);
|
boolean offered = removeOfferedMessage(txn, c, m.getId());
|
||||||
|
boolean seen = offered || c.equals(sender);
|
||||||
addStatus(txn, m.getId(), c, m.getGroupId(), m.getTimestamp(),
|
addStatus(txn, m.getId(), c, m.getGroupId(), m.getTimestamp(),
|
||||||
m.getRawLength(), state, e.getValue(), shared, false,
|
raw.length, state, e.getValue(), shared, false, seen);
|
||||||
seen);
|
|
||||||
}
|
}
|
||||||
// Update denormalised column in messageDependencies if dependency
|
// Update denormalised column in messageDependencies if dependency
|
||||||
// is in same group as dependent
|
// is in same group as dependent
|
||||||
@@ -842,6 +830,37 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addOfferedMessage(Connection txn, ContactId c, MessageId m)
|
||||||
|
throws DbException {
|
||||||
|
PreparedStatement ps = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
try {
|
||||||
|
String sql = "SELECT NULL FROM offers"
|
||||||
|
+ " WHERE messageId = ? AND contactId = ?";
|
||||||
|
ps = txn.prepareStatement(sql);
|
||||||
|
ps.setBytes(1, m.getBytes());
|
||||||
|
ps.setInt(2, c.getInt());
|
||||||
|
rs = ps.executeQuery();
|
||||||
|
boolean found = rs.next();
|
||||||
|
if (rs.next()) throw new DbStateException();
|
||||||
|
rs.close();
|
||||||
|
ps.close();
|
||||||
|
if (found) return;
|
||||||
|
sql = "INSERT INTO offers (messageId, contactId) VALUES (?, ?)";
|
||||||
|
ps = txn.prepareStatement(sql);
|
||||||
|
ps.setBytes(1, m.getBytes());
|
||||||
|
ps.setInt(2, c.getInt());
|
||||||
|
int affected = ps.executeUpdate();
|
||||||
|
if (affected != 1) throw new DbStateException();
|
||||||
|
ps.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
tryToClose(rs, LOG, WARNING);
|
||||||
|
tryToClose(ps, LOG, WARNING);
|
||||||
|
throw new DbException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void addStatus(Connection txn, MessageId m, ContactId c, GroupId g,
|
private void addStatus(Connection txn, MessageId m, ContactId c, GroupId g,
|
||||||
long timestamp, int length, MessageState state, boolean groupShared,
|
long timestamp, int length, MessageState state, boolean groupShared,
|
||||||
boolean messageShared, boolean deleted, boolean seen)
|
boolean messageShared, boolean deleted, boolean seen)
|
||||||
@@ -1243,22 +1262,40 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int countOfferedMessages(Connection txn, ContactId c)
|
||||||
|
throws DbException {
|
||||||
|
PreparedStatement ps = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
try {
|
||||||
|
String sql = "SELECT COUNT (messageId) FROM offers "
|
||||||
|
+ " WHERE contactId = ?";
|
||||||
|
ps = txn.prepareStatement(sql);
|
||||||
|
ps.setInt(1, c.getInt());
|
||||||
|
rs = ps.executeQuery();
|
||||||
|
if (!rs.next()) throw new DbException();
|
||||||
|
int count = rs.getInt(1);
|
||||||
|
if (rs.next()) throw new DbException();
|
||||||
|
rs.close();
|
||||||
|
ps.close();
|
||||||
|
return count;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
tryToClose(rs, LOG, WARNING);
|
||||||
|
tryToClose(ps, LOG, WARNING);
|
||||||
|
throw new DbException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deleteMessage(Connection txn, MessageId m) throws DbException {
|
public void deleteMessage(Connection txn, MessageId m) throws DbException {
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
try {
|
try {
|
||||||
String sql = "UPDATE messages SET deleted = TRUE"
|
String sql = "UPDATE messages SET raw = NULL WHERE messageId = ?";
|
||||||
+ " WHERE messageId = ?";
|
|
||||||
ps = txn.prepareStatement(sql);
|
ps = txn.prepareStatement(sql);
|
||||||
ps.setBytes(1, m.getBytes());
|
ps.setBytes(1, m.getBytes());
|
||||||
int affected = ps.executeUpdate();
|
int affected = ps.executeUpdate();
|
||||||
if (affected < 0 || affected > 1) throw new DbStateException();
|
|
||||||
ps.close();
|
|
||||||
sql = "UPDATE blocks SET data = NULL WHERE messageId = ?";
|
|
||||||
ps = txn.prepareStatement(sql);
|
|
||||||
ps.setBytes(1, m.getBytes());
|
|
||||||
affected = ps.executeUpdate();
|
|
||||||
if (affected < 0) throw new DbStateException();
|
if (affected < 0) throw new DbStateException();
|
||||||
|
if (affected > 1) throw new DbStateException();
|
||||||
ps.close();
|
ps.close();
|
||||||
// Update denormalised column in statuses
|
// Update denormalised column in statuses
|
||||||
sql = "UPDATE statuses SET deleted = TRUE WHERE messageId = ?";
|
sql = "UPDATE statuses SET deleted = TRUE WHERE messageId = ?";
|
||||||
@@ -1651,13 +1688,11 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Message getSmallMessage(Connection txn, MessageId m)
|
public Message getMessage(Connection txn, MessageId m) throws DbException {
|
||||||
throws DbException {
|
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
try {
|
try {
|
||||||
String sql = "SELECT groupId, timestamp, deleted, blockCount"
|
String sql = "SELECT groupId, timestamp, raw FROM messages"
|
||||||
+ " FROM messages"
|
|
||||||
+ " WHERE messageId = ?";
|
+ " WHERE messageId = ?";
|
||||||
ps = txn.prepareStatement(sql);
|
ps = txn.prepareStatement(sql);
|
||||||
ps.setBytes(1, m.getBytes());
|
ps.setBytes(1, m.getBytes());
|
||||||
@@ -1665,25 +1700,15 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
if (!rs.next()) throw new DbStateException();
|
if (!rs.next()) throw new DbStateException();
|
||||||
GroupId g = new GroupId(rs.getBytes(1));
|
GroupId g = new GroupId(rs.getBytes(1));
|
||||||
long timestamp = rs.getLong(2);
|
long timestamp = rs.getLong(2);
|
||||||
boolean deleted = rs.getBoolean(3);
|
byte[] raw = rs.getBytes(3);
|
||||||
int blockCount = rs.getInt(4);
|
|
||||||
if (rs.next()) throw new DbStateException();
|
if (rs.next()) throw new DbStateException();
|
||||||
rs.close();
|
rs.close();
|
||||||
ps.close();
|
ps.close();
|
||||||
if (deleted) throw new MessageDeletedException();
|
if (raw == null) throw new MessageDeletedException();
|
||||||
if (blockCount > 1) throw new MessageTooLargeException();
|
if (raw.length <= MESSAGE_HEADER_LENGTH) throw new AssertionError();
|
||||||
sql = "SELECT data FROM blocks"
|
byte[] body = new byte[raw.length - MESSAGE_HEADER_LENGTH];
|
||||||
+ " WHERE messageId = ? AND blockNumber = 0";
|
System.arraycopy(raw, MESSAGE_HEADER_LENGTH, body, 0, body.length);
|
||||||
ps = txn.prepareStatement(sql);
|
return new Message(m, g, timestamp, body);
|
||||||
ps.setBytes(1, m.getBytes());
|
|
||||||
rs = ps.executeQuery();
|
|
||||||
if (!rs.next()) throw new DbStateException();
|
|
||||||
byte[] data = rs.getBytes(1);
|
|
||||||
if (data == null) throw new DbStateException();
|
|
||||||
if (rs.next()) throw new DbStateException();
|
|
||||||
rs.close();
|
|
||||||
ps.close();
|
|
||||||
return new Message(m, g, timestamp, data);
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
tryToClose(rs, LOG, WARNING);
|
tryToClose(rs, LOG, WARNING);
|
||||||
tryToClose(ps, LOG, WARNING);
|
tryToClose(ps, LOG, WARNING);
|
||||||
@@ -2076,28 +2101,17 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<MessageId> getSmallMessagesToOffer(Connection txn,
|
public Collection<MessageId> getMessagesToRequest(Connection txn,
|
||||||
ContactId c, int maxMessages, int maxLatency) throws DbException {
|
ContactId c, int maxMessages) throws DbException {
|
||||||
long now = clock.currentTimeMillis();
|
|
||||||
long eta = now + maxLatency;
|
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
try {
|
try {
|
||||||
String sql = "SELECT messageId FROM statuses"
|
String sql = "SELECT messageId FROM offers"
|
||||||
+ " WHERE contactId = ? AND state = ?"
|
+ " WHERE contactId = ?"
|
||||||
+ " AND length <= ?"
|
+ " LIMIT ?";
|
||||||
+ " AND groupShared = TRUE AND messageShared = TRUE"
|
|
||||||
+ " AND deleted = FALSE"
|
|
||||||
+ " AND seen = FALSE AND requested = FALSE"
|
|
||||||
+ " AND (expiry <= ? OR eta > ?)"
|
|
||||||
+ " ORDER BY timestamp LIMIT ?";
|
|
||||||
ps = txn.prepareStatement(sql);
|
ps = txn.prepareStatement(sql);
|
||||||
ps.setInt(1, c.getInt());
|
ps.setInt(1, c.getInt());
|
||||||
ps.setInt(2, DELIVERED.getValue());
|
ps.setInt(2, maxMessages);
|
||||||
ps.setInt(3, MESSAGE_HEADER_LENGTH + MAX_BLOCK_LENGTH);
|
|
||||||
ps.setLong(4, now);
|
|
||||||
ps.setLong(5, eta);
|
|
||||||
ps.setInt(6, maxMessages);
|
|
||||||
rs = ps.executeQuery();
|
rs = ps.executeQuery();
|
||||||
List<MessageId> ids = new ArrayList<>();
|
List<MessageId> ids = new ArrayList<>();
|
||||||
while (rs.next()) ids.add(new MessageId(rs.getBytes(1)));
|
while (rs.next()) ids.add(new MessageId(rs.getBytes(1)));
|
||||||
@@ -2150,47 +2164,6 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Collection<MessageId> getSmallMessagesToSend(Connection txn,
|
|
||||||
ContactId c, int maxLength, int maxLatency) throws DbException {
|
|
||||||
long now = clock.currentTimeMillis();
|
|
||||||
long eta = now + maxLatency;
|
|
||||||
PreparedStatement ps = null;
|
|
||||||
ResultSet rs = null;
|
|
||||||
try {
|
|
||||||
String sql = "SELECT length, messageId FROM statuses"
|
|
||||||
+ " WHERE contactId = ? AND state = ?"
|
|
||||||
+ " AND length <= ?"
|
|
||||||
+ " AND groupShared = TRUE AND messageShared = TRUE"
|
|
||||||
+ " AND deleted = FALSE"
|
|
||||||
+ " AND seen = FALSE"
|
|
||||||
+ " AND (expiry <= ? OR eta > ?)"
|
|
||||||
+ " ORDER BY timestamp";
|
|
||||||
ps = txn.prepareStatement(sql);
|
|
||||||
ps.setInt(1, c.getInt());
|
|
||||||
ps.setInt(2, DELIVERED.getValue());
|
|
||||||
ps.setInt(3, MESSAGE_HEADER_LENGTH + MAX_BLOCK_LENGTH);
|
|
||||||
ps.setLong(4, now);
|
|
||||||
ps.setLong(5, eta);
|
|
||||||
rs = ps.executeQuery();
|
|
||||||
List<MessageId> ids = new ArrayList<>();
|
|
||||||
int total = 0;
|
|
||||||
while (rs.next()) {
|
|
||||||
int length = rs.getInt(1);
|
|
||||||
if (total + length > maxLength) break;
|
|
||||||
ids.add(new MessageId(rs.getBytes(2)));
|
|
||||||
total += length;
|
|
||||||
}
|
|
||||||
rs.close();
|
|
||||||
ps.close();
|
|
||||||
return ids;
|
|
||||||
} catch (SQLException e) {
|
|
||||||
tryToClose(rs, LOG, WARNING);
|
|
||||||
tryToClose(ps, LOG, WARNING);
|
|
||||||
throw new DbException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<MessageId> getMessagesToValidate(Connection txn)
|
public Collection<MessageId> getMessagesToValidate(Connection txn)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
@@ -2209,7 +2182,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
try {
|
try {
|
||||||
String sql = "SELECT messageId FROM messages"
|
String sql = "SELECT messageId FROM messages"
|
||||||
+ " WHERE state = ? AND deleted = FALSE";
|
+ " WHERE state = ? AND raw IS NOT NULL";
|
||||||
ps = txn.prepareStatement(sql);
|
ps = txn.prepareStatement(sql);
|
||||||
ps.setInt(1, state.getValue());
|
ps.setInt(1, state.getValue());
|
||||||
rs = ps.executeQuery();
|
rs = ps.executeQuery();
|
||||||
@@ -2914,6 +2887,50 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean removeOfferedMessage(Connection txn, ContactId c,
|
||||||
|
MessageId m) throws DbException {
|
||||||
|
PreparedStatement ps = null;
|
||||||
|
try {
|
||||||
|
String sql = "DELETE FROM offers"
|
||||||
|
+ " WHERE contactId = ? AND messageId = ?";
|
||||||
|
ps = txn.prepareStatement(sql);
|
||||||
|
ps.setInt(1, c.getInt());
|
||||||
|
ps.setBytes(2, m.getBytes());
|
||||||
|
int affected = ps.executeUpdate();
|
||||||
|
if (affected < 0 || affected > 1) throw new DbStateException();
|
||||||
|
ps.close();
|
||||||
|
return affected == 1;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
tryToClose(ps, LOG, WARNING);
|
||||||
|
throw new DbException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeOfferedMessages(Connection txn, ContactId c,
|
||||||
|
Collection<MessageId> requested) throws DbException {
|
||||||
|
PreparedStatement ps = null;
|
||||||
|
try {
|
||||||
|
String sql = "DELETE FROM offers"
|
||||||
|
+ " WHERE contactId = ? AND messageId = ?";
|
||||||
|
ps = txn.prepareStatement(sql);
|
||||||
|
ps.setInt(1, c.getInt());
|
||||||
|
for (MessageId m : requested) {
|
||||||
|
ps.setBytes(2, m.getBytes());
|
||||||
|
ps.addBatch();
|
||||||
|
}
|
||||||
|
int[] batchAffected = ps.executeBatch();
|
||||||
|
if (batchAffected.length != requested.size())
|
||||||
|
throw new DbStateException();
|
||||||
|
for (int rows : batchAffected)
|
||||||
|
if (rows != 1) throw new DbStateException();
|
||||||
|
ps.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
tryToClose(ps, LOG, WARNING);
|
||||||
|
throw new DbException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void removePendingContact(Connection txn, PendingContactId p)
|
public void removePendingContact(Connection txn, PendingContactId p)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
|
|||||||
@@ -37,7 +37,6 @@ class Migration38_39 implements Migration<Connection> {
|
|||||||
s.execute("ALTER TABLE incomingKeys"
|
s.execute("ALTER TABLE incomingKeys"
|
||||||
+ " ALTER COLUMN contactId"
|
+ " ALTER COLUMN contactId"
|
||||||
+ " SET NOT NULL");
|
+ " SET NOT NULL");
|
||||||
s.close();
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
tryToClose(s, LOG, WARNING);
|
tryToClose(s, LOG, WARNING);
|
||||||
throw new DbException(e);
|
throw new DbException(e);
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ class Migration39_40 implements Migration<Connection> {
|
|||||||
s.execute("ALTER TABLE statuses"
|
s.execute("ALTER TABLE statuses"
|
||||||
+ " ALTER COLUMN eta"
|
+ " ALTER COLUMN eta"
|
||||||
+ " SET NOT NULL");
|
+ " SET NOT NULL");
|
||||||
s.close();
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
tryToClose(s, LOG, WARNING);
|
tryToClose(s, LOG, WARNING);
|
||||||
throw new DbException(e);
|
throw new DbException(e);
|
||||||
|
|||||||
@@ -38,7 +38,6 @@ class Migration40_41 implements Migration<Connection> {
|
|||||||
s = txn.createStatement();
|
s = txn.createStatement();
|
||||||
s.execute("ALTER TABLE contacts"
|
s.execute("ALTER TABLE contacts"
|
||||||
+ dbTypes.replaceTypes(" ADD alias _STRING"));
|
+ dbTypes.replaceTypes(" ADD alias _STRING"));
|
||||||
s.close();
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
tryToClose(s, LOG, WARNING);
|
tryToClose(s, LOG, WARNING);
|
||||||
throw new DbException(e);
|
throw new DbException(e);
|
||||||
|
|||||||
@@ -89,7 +89,6 @@ class Migration41_42 implements Migration<Connection> {
|
|||||||
+ " FOREIGN KEY (keySetId)"
|
+ " FOREIGN KEY (keySetId)"
|
||||||
+ " REFERENCES outgoingHandshakeKeys (keySetId)"
|
+ " REFERENCES outgoingHandshakeKeys (keySetId)"
|
||||||
+ " ON DELETE CASCADE)"));
|
+ " ON DELETE CASCADE)"));
|
||||||
s.close();
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
tryToClose(s, LOG, WARNING);
|
tryToClose(s, LOG, WARNING);
|
||||||
throw new DbException(e);
|
throw new DbException(e);
|
||||||
|
|||||||
@@ -44,7 +44,6 @@ class Migration42_43 implements Migration<Connection> {
|
|||||||
+ " ADD COLUMN handshakePublicKey _BINARY"));
|
+ " ADD COLUMN handshakePublicKey _BINARY"));
|
||||||
s.execute("ALTER TABLE contacts"
|
s.execute("ALTER TABLE contacts"
|
||||||
+ " DROP COLUMN active");
|
+ " DROP COLUMN active");
|
||||||
s.close();
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
tryToClose(s, LOG, WARNING);
|
tryToClose(s, LOG, WARNING);
|
||||||
throw new DbException(e);
|
throw new DbException(e);
|
||||||
|
|||||||
@@ -50,7 +50,6 @@ class Migration43_44 implements Migration<Connection> {
|
|||||||
+ " ADD COLUMN rootKey _SECRET"));
|
+ " ADD COLUMN rootKey _SECRET"));
|
||||||
s.execute("ALTER TABLE outgoingKeys"
|
s.execute("ALTER TABLE outgoingKeys"
|
||||||
+ " ADD COLUMN alice BOOLEAN");
|
+ " ADD COLUMN alice BOOLEAN");
|
||||||
s.close();
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
tryToClose(s, LOG, WARNING);
|
tryToClose(s, LOG, WARNING);
|
||||||
throw new DbException(e);
|
throw new DbException(e);
|
||||||
|
|||||||
@@ -31,7 +31,6 @@ class Migration44_45 implements Migration<Connection> {
|
|||||||
try {
|
try {
|
||||||
s = txn.createStatement();
|
s = txn.createStatement();
|
||||||
s.execute("ALTER TABLE pendingContacts DROP COLUMN state");
|
s.execute("ALTER TABLE pendingContacts DROP COLUMN state");
|
||||||
s.close();
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
tryToClose(s, LOG, WARNING);
|
tryToClose(s, LOG, WARNING);
|
||||||
throw new DbException(e);
|
throw new DbException(e);
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ class Migration45_46 implements Migration<Connection> {
|
|||||||
s = txn.createStatement();
|
s = txn.createStatement();
|
||||||
s.execute("ALTER TABLE messages"
|
s.execute("ALTER TABLE messages"
|
||||||
+ " ADD COLUMN temporary BOOLEAN DEFAULT FALSE NOT NULL");
|
+ " ADD COLUMN temporary BOOLEAN DEFAULT FALSE NOT NULL");
|
||||||
s.close();
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
tryToClose(s, LOG, WARNING);
|
tryToClose(s, LOG, WARNING);
|
||||||
throw new DbException(e);
|
throw new DbException(e);
|
||||||
|
|||||||
@@ -39,7 +39,6 @@ class Migration46_47 implements Migration<Connection> {
|
|||||||
s.execute(dbTypes.replaceTypes("ALTER TABLE contacts"
|
s.execute(dbTypes.replaceTypes("ALTER TABLE contacts"
|
||||||
+ " ADD COLUMN syncVersions"
|
+ " ADD COLUMN syncVersions"
|
||||||
+ " _BINARY DEFAULT '00' NOT NULL"));
|
+ " _BINARY DEFAULT '00' NOT NULL"));
|
||||||
s.close();
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
tryToClose(s, LOG, WARNING);
|
tryToClose(s, LOG, WARNING);
|
||||||
throw new DbException(e);
|
throw new DbException(e);
|
||||||
|
|||||||
@@ -1,95 +0,0 @@
|
|||||||
package org.briarproject.bramble.db;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.db.DbException;
|
|
||||||
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.PreparedStatement;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
import static java.lang.System.arraycopy;
|
|
||||||
import static java.sql.Types.BINARY;
|
|
||||||
import static java.util.logging.Level.INFO;
|
|
||||||
import static java.util.logging.Level.WARNING;
|
|
||||||
import static java.util.logging.Logger.getLogger;
|
|
||||||
import static org.briarproject.bramble.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
|
|
||||||
import static org.briarproject.bramble.db.JdbcUtils.tryToClose;
|
|
||||||
|
|
||||||
class Migration47_48 implements Migration<Connection> {
|
|
||||||
|
|
||||||
private static final Logger LOG = getLogger(Migration47_48.class.getName());
|
|
||||||
|
|
||||||
private final DatabaseTypes dbTypes;
|
|
||||||
|
|
||||||
Migration47_48(DatabaseTypes dbTypes) {
|
|
||||||
this.dbTypes = dbTypes;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getStartVersion() {
|
|
||||||
return 47;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getEndVersion() {
|
|
||||||
return 48;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void migrate(Connection txn) throws DbException {
|
|
||||||
Statement s = null;
|
|
||||||
ResultSet rs = null;
|
|
||||||
PreparedStatement ps = null;
|
|
||||||
try {
|
|
||||||
s = txn.createStatement();
|
|
||||||
s.execute("ALTER TABLE messages"
|
|
||||||
+ " ADD COLUMN deleted BOOLEAN DEFAULT FALSE NOT NULL");
|
|
||||||
s.execute("UPDATE messages SET deleted = TRUE WHERE raw IS NULL");
|
|
||||||
s.execute("ALTER TABLE messages"
|
|
||||||
+ " ADD COLUMN blockCount INT DEFAULT 1 NOT NULL");
|
|
||||||
s.execute(dbTypes.replaceTypes("CREATE TABLE blocks"
|
|
||||||
+ " (messageId _HASH NOT NULL,"
|
|
||||||
+ " blockNumber INT NOT NULL,"
|
|
||||||
+ " blockLength INT NOT NULL," // Excludes block header
|
|
||||||
+ " data BLOB," // Null if message has been deleted
|
|
||||||
+ " PRIMARY KEY (messageId, blockNumber),"
|
|
||||||
+ " FOREIGN KEY (messageId)"
|
|
||||||
+ " REFERENCES messages (messageId)"
|
|
||||||
+ " ON DELETE CASCADE)"));
|
|
||||||
rs = s.executeQuery("SELECT messageId, length, raw FROM messages");
|
|
||||||
ps = txn.prepareStatement("INSERT INTO blocks"
|
|
||||||
+ " (messageId, blockNumber, blockLength, data)"
|
|
||||||
+ " VALUES (?, 0, ?, ?)");
|
|
||||||
int migrated = 0;
|
|
||||||
while (rs.next()) {
|
|
||||||
byte[] id = rs.getBytes(1);
|
|
||||||
int length = rs.getInt(2);
|
|
||||||
byte[] raw = rs.getBytes(3);
|
|
||||||
ps.setBytes(1, id);
|
|
||||||
ps.setInt(2, length - MESSAGE_HEADER_LENGTH);
|
|
||||||
if (raw == null) {
|
|
||||||
ps.setNull(3, BINARY);
|
|
||||||
} else {
|
|
||||||
byte[] data = new byte[raw.length - MESSAGE_HEADER_LENGTH];
|
|
||||||
arraycopy(raw, MESSAGE_HEADER_LENGTH, data, 0, data.length);
|
|
||||||
ps.setBytes(3, data);
|
|
||||||
}
|
|
||||||
if (ps.executeUpdate() != 1) throw new DbStateException();
|
|
||||||
migrated++;
|
|
||||||
}
|
|
||||||
ps.close();
|
|
||||||
rs.close();
|
|
||||||
s.execute("ALTER TABLE messages DROP COLUMN raw");
|
|
||||||
s.close();
|
|
||||||
if (LOG.isLoggable(INFO))
|
|
||||||
LOG.info("Migrated " + migrated + " messages");
|
|
||||||
} catch (SQLException e) {
|
|
||||||
tryToClose(ps, LOG, WARNING);
|
|
||||||
tryToClose(rs, LOG, WARNING);
|
|
||||||
tryToClose(s, LOG, WARNING);
|
|
||||||
throw new DbException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
package org.briarproject.bramble.db;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.db.DbException;
|
|
||||||
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
import static java.util.logging.Level.WARNING;
|
|
||||||
import static java.util.logging.Logger.getLogger;
|
|
||||||
import static org.briarproject.bramble.db.JdbcUtils.tryToClose;
|
|
||||||
|
|
||||||
class Migration48_49 implements Migration<Connection> {
|
|
||||||
|
|
||||||
private static final Logger LOG = getLogger(Migration48_49.class.getName());
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getStartVersion() {
|
|
||||||
return 48;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getEndVersion() {
|
|
||||||
return 49;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void migrate(Connection txn) throws DbException {
|
|
||||||
Statement s = null;
|
|
||||||
try {
|
|
||||||
s = txn.createStatement();
|
|
||||||
s.execute("DROP TABLE offers");
|
|
||||||
s.close();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
tryToClose(s, LOG, WARNING);
|
|
||||||
throw new DbException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -3,15 +3,15 @@ package org.briarproject.bramble.io;
|
|||||||
import org.briarproject.bramble.api.io.TimeoutMonitor;
|
import org.briarproject.bramble.api.io.TimeoutMonitor;
|
||||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||||
import org.briarproject.bramble.api.system.Clock;
|
import org.briarproject.bramble.api.system.Clock;
|
||||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
import org.briarproject.bramble.api.system.Scheduler;
|
||||||
import org.briarproject.bramble.api.system.TaskScheduler.Cancellable;
|
|
||||||
import org.briarproject.bramble.api.system.Wakeful;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.annotation.concurrent.GuardedBy;
|
import javax.annotation.concurrent.GuardedBy;
|
||||||
@@ -30,7 +30,7 @@ class TimeoutMonitorImpl implements TimeoutMonitor {
|
|||||||
|
|
||||||
private static final long CHECK_INTERVAL_MS = SECONDS.toMillis(10);
|
private static final long CHECK_INTERVAL_MS = SECONDS.toMillis(10);
|
||||||
|
|
||||||
private final TaskScheduler scheduler;
|
private final ScheduledExecutorService scheduler;
|
||||||
private final Executor ioExecutor;
|
private final Executor ioExecutor;
|
||||||
private final Clock clock;
|
private final Clock clock;
|
||||||
private final Object lock = new Object();
|
private final Object lock = new Object();
|
||||||
@@ -38,10 +38,10 @@ class TimeoutMonitorImpl implements TimeoutMonitor {
|
|||||||
private final List<TimeoutInputStream> streams = new ArrayList<>();
|
private final List<TimeoutInputStream> streams = new ArrayList<>();
|
||||||
|
|
||||||
@GuardedBy("lock")
|
@GuardedBy("lock")
|
||||||
private Cancellable cancellable = null;
|
private Future<?> task = null;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
TimeoutMonitorImpl(TaskScheduler scheduler,
|
TimeoutMonitorImpl(@Scheduler ScheduledExecutorService scheduler,
|
||||||
@IoExecutor Executor ioExecutor, Clock clock) {
|
@IoExecutor Executor ioExecutor, Clock clock) {
|
||||||
this.scheduler = scheduler;
|
this.scheduler = scheduler;
|
||||||
this.ioExecutor = ioExecutor;
|
this.ioExecutor = ioExecutor;
|
||||||
@@ -55,9 +55,8 @@ class TimeoutMonitorImpl implements TimeoutMonitor {
|
|||||||
timeoutMs, this::removeStream);
|
timeoutMs, this::removeStream);
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
if (streams.isEmpty()) {
|
if (streams.isEmpty()) {
|
||||||
cancellable = scheduler.scheduleWithFixedDelay(
|
task = scheduler.scheduleWithFixedDelay(this::checkTimeouts,
|
||||||
this::checkTimeouts, ioExecutor, CHECK_INTERVAL_MS,
|
CHECK_INTERVAL_MS, CHECK_INTERVAL_MS, MILLISECONDS);
|
||||||
CHECK_INTERVAL_MS, MILLISECONDS);
|
|
||||||
}
|
}
|
||||||
streams.add(stream);
|
streams.add(stream);
|
||||||
}
|
}
|
||||||
@@ -65,35 +64,33 @@ class TimeoutMonitorImpl implements TimeoutMonitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void removeStream(TimeoutInputStream stream) {
|
private void removeStream(TimeoutInputStream stream) {
|
||||||
Cancellable toCancel = null;
|
Future<?> toCancel = null;
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
if (streams.remove(stream) && streams.isEmpty()) {
|
if (streams.remove(stream) && streams.isEmpty()) {
|
||||||
toCancel = cancellable;
|
toCancel = task;
|
||||||
cancellable = null;
|
task = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (toCancel != null) {
|
if (toCancel != null) toCancel.cancel(false);
|
||||||
LOG.info("Cancelling timeout monitor task");
|
|
||||||
toCancel.cancel();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@IoExecutor
|
@Scheduler
|
||||||
@Wakeful
|
|
||||||
private void checkTimeouts() {
|
private void checkTimeouts() {
|
||||||
List<TimeoutInputStream> snapshot;
|
ioExecutor.execute(() -> {
|
||||||
synchronized (lock) {
|
List<TimeoutInputStream> snapshot;
|
||||||
snapshot = new ArrayList<>(streams);
|
synchronized (lock) {
|
||||||
}
|
snapshot = new ArrayList<>(streams);
|
||||||
for (TimeoutInputStream stream : snapshot) {
|
}
|
||||||
if (stream.hasTimedOut()) {
|
for (TimeoutInputStream stream : snapshot) {
|
||||||
LOG.info("Input stream has timed out");
|
if (stream.hasTimedOut()) {
|
||||||
try {
|
LOG.info("Input stream has timed out");
|
||||||
stream.close();
|
try {
|
||||||
} catch (IOException e) {
|
stream.close();
|
||||||
logException(LOG, INFO, e);
|
} catch (IOException e) {
|
||||||
|
logException(LOG, INFO, e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package org.briarproject.bramble.keyagreement;
|
package org.briarproject.bramble.keyagreement;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.Pair;
|
|
||||||
import org.briarproject.bramble.api.crypto.KeyAgreementCrypto;
|
import org.briarproject.bramble.api.crypto.KeyAgreementCrypto;
|
||||||
import org.briarproject.bramble.api.crypto.KeyPair;
|
import org.briarproject.bramble.api.crypto.KeyPair;
|
||||||
import org.briarproject.bramble.api.data.BdfList;
|
import org.briarproject.bramble.api.data.BdfList;
|
||||||
@@ -9,8 +8,6 @@ import org.briarproject.bramble.api.keyagreement.KeyAgreementListener;
|
|||||||
import org.briarproject.bramble.api.keyagreement.Payload;
|
import org.briarproject.bramble.api.keyagreement.Payload;
|
||||||
import org.briarproject.bramble.api.keyagreement.TransportDescriptor;
|
import org.briarproject.bramble.api.keyagreement.TransportDescriptor;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.BluetoothConstants;
|
|
||||||
import org.briarproject.bramble.api.plugin.LanTcpConstants;
|
|
||||||
import org.briarproject.bramble.api.plugin.Plugin;
|
import org.briarproject.bramble.api.plugin.Plugin;
|
||||||
import org.briarproject.bramble.api.plugin.PluginManager;
|
import org.briarproject.bramble.api.plugin.PluginManager;
|
||||||
import org.briarproject.bramble.api.plugin.TransportId;
|
import org.briarproject.bramble.api.plugin.TransportId;
|
||||||
@@ -22,9 +19,7 @@ import org.briarproject.bramble.api.record.RecordWriterFactory;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
@@ -33,10 +28,8 @@ import java.util.logging.Logger;
|
|||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import static java.util.Arrays.asList;
|
|
||||||
import static java.util.logging.Level.INFO;
|
import static java.util.logging.Level.INFO;
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static java.util.logging.Logger.getLogger;
|
|
||||||
import static org.briarproject.bramble.api.keyagreement.KeyAgreementConstants.CONNECTION_TIMEOUT;
|
import static org.briarproject.bramble.api.keyagreement.KeyAgreementConstants.CONNECTION_TIMEOUT;
|
||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
|
|
||||||
@@ -48,10 +41,7 @@ class KeyAgreementConnector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
getLogger(KeyAgreementConnector.class.getName());
|
Logger.getLogger(KeyAgreementConnector.class.getName());
|
||||||
|
|
||||||
private static final List<TransportId> PREFERRED_TRANSPORTS =
|
|
||||||
asList(BluetoothConstants.ID, LanTcpConstants.ID);
|
|
||||||
|
|
||||||
private final Callbacks callbacks;
|
private final Callbacks callbacks;
|
||||||
private final KeyAgreementCrypto keyAgreementCrypto;
|
private final KeyAgreementCrypto keyAgreementCrypto;
|
||||||
@@ -115,35 +105,24 @@ class KeyAgreementConnector {
|
|||||||
this.alice = alice;
|
this.alice = alice;
|
||||||
aliceLatch.countDown();
|
aliceLatch.countDown();
|
||||||
|
|
||||||
// Start connecting over supported transports in order of preference
|
// Start connecting over supported transports
|
||||||
if (LOG.isLoggable(INFO)) {
|
if (LOG.isLoggable(INFO)) {
|
||||||
LOG.info("Starting outgoing BQP connections as "
|
LOG.info("Starting outgoing BQP connections as "
|
||||||
+ (alice ? "Alice" : "Bob"));
|
+ (alice ? "Alice" : "Bob"));
|
||||||
}
|
}
|
||||||
Map<TransportId, TransportDescriptor> descriptors = new HashMap<>();
|
|
||||||
for (TransportDescriptor d : remotePayload.getTransportDescriptors()) {
|
for (TransportDescriptor d : remotePayload.getTransportDescriptors()) {
|
||||||
descriptors.put(d.getId(), d);
|
Plugin p = pluginManager.getPlugin(d.getId());
|
||||||
}
|
if (p instanceof DuplexPlugin) {
|
||||||
List<Pair<DuplexPlugin, BdfList>> transports = new ArrayList<>();
|
|
||||||
for (TransportId id : PREFERRED_TRANSPORTS) {
|
|
||||||
TransportDescriptor d = descriptors.get(id);
|
|
||||||
Plugin p = pluginManager.getPlugin(id);
|
|
||||||
if (d != null && p instanceof DuplexPlugin) {
|
|
||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO))
|
||||||
LOG.info("Connecting via " + id);
|
LOG.info("Connecting via " + d.getId());
|
||||||
transports.add(new Pair<>((DuplexPlugin) p, d.getDescriptor()));
|
DuplexPlugin plugin = (DuplexPlugin) p;
|
||||||
|
byte[] commitment = remotePayload.getCommitment();
|
||||||
|
BdfList descriptor = d.getDescriptor();
|
||||||
|
connectionChooser.submit(new ReadableTask(
|
||||||
|
new ConnectorTask(plugin, commitment, descriptor)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: If we don't have any transports in common with the peer,
|
|
||||||
// warn the user and give up (#1224)
|
|
||||||
|
|
||||||
if (!transports.isEmpty()) {
|
|
||||||
byte[] commitment = remotePayload.getCommitment();
|
|
||||||
connectionChooser.submit(new ReadableTask(new ConnectorTask(
|
|
||||||
transports, commitment)));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get chosen connection
|
// Get chosen connection
|
||||||
try {
|
try {
|
||||||
KeyAgreementConnection chosen =
|
KeyAgreementConnection chosen =
|
||||||
@@ -169,13 +148,15 @@ class KeyAgreementConnector {
|
|||||||
|
|
||||||
private class ConnectorTask implements Callable<KeyAgreementConnection> {
|
private class ConnectorTask implements Callable<KeyAgreementConnection> {
|
||||||
|
|
||||||
private final List<Pair<DuplexPlugin, BdfList>> transports;
|
|
||||||
private final byte[] commitment;
|
private final byte[] commitment;
|
||||||
|
private final BdfList descriptor;
|
||||||
|
private final DuplexPlugin plugin;
|
||||||
|
|
||||||
private ConnectorTask(List<Pair<DuplexPlugin, BdfList>> transports,
|
private ConnectorTask(DuplexPlugin plugin, byte[] commitment,
|
||||||
byte[] commitment) {
|
BdfList descriptor) {
|
||||||
this.transports = transports;
|
this.plugin = plugin;
|
||||||
this.commitment = commitment;
|
this.commitment = commitment;
|
||||||
|
this.descriptor = descriptor;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@@ -183,18 +164,13 @@ class KeyAgreementConnector {
|
|||||||
public KeyAgreementConnection call() throws Exception {
|
public KeyAgreementConnection call() throws Exception {
|
||||||
// Repeat attempts until we connect, get stopped, or get interrupted
|
// Repeat attempts until we connect, get stopped, or get interrupted
|
||||||
while (!stopped) {
|
while (!stopped) {
|
||||||
for (Pair<DuplexPlugin, BdfList> pair : transports) {
|
DuplexTransportConnection conn =
|
||||||
if (stopped) return null;
|
plugin.createKeyAgreementConnection(commitment,
|
||||||
DuplexPlugin plugin = pair.getFirst();
|
descriptor);
|
||||||
BdfList descriptor = pair.getSecond();
|
if (conn != null) {
|
||||||
DuplexTransportConnection conn =
|
if (LOG.isLoggable(INFO))
|
||||||
plugin.createKeyAgreementConnection(commitment,
|
LOG.info(plugin.getId() + ": Outgoing connection");
|
||||||
descriptor);
|
return new KeyAgreementConnection(conn, plugin.getId());
|
||||||
if (conn != null) {
|
|
||||||
if (LOG.isLoggable(INFO))
|
|
||||||
LOG.info(plugin.getId() + ": Outgoing connection");
|
|
||||||
return new KeyAgreementConnection(conn, plugin.getId());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// Wait 2s before retry (to circumvent transient failures)
|
// Wait 2s before retry (to circumvent transient failures)
|
||||||
Thread.sleep(2000);
|
Thread.sleep(2000);
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.briarproject.briar.android.logging;
|
package org.briarproject.bramble.logging;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
|
||||||
@@ -47,6 +47,7 @@ public class BriefLogFormatter extends Formatter {
|
|||||||
sb.append('\n');
|
sb.append('\n');
|
||||||
appendThrowable(sb, t);
|
appendThrowable(sb, t);
|
||||||
}
|
}
|
||||||
|
sb.append('\n');
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package org.briarproject.bramble.logging;
|
||||||
|
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
import java.util.logging.Formatter;
|
||||||
|
import java.util.logging.LogRecord;
|
||||||
|
import java.util.logging.StreamHandler;
|
||||||
|
|
||||||
|
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||||
|
|
||||||
|
class FlushingStreamHandler extends StreamHandler {
|
||||||
|
|
||||||
|
private static final int FLUSH_DELAY_MS = 5_000;
|
||||||
|
|
||||||
|
private final ScheduledExecutorService scheduler;
|
||||||
|
private final Executor ioExecutor;
|
||||||
|
private final AtomicBoolean flushScheduled = new AtomicBoolean(false);
|
||||||
|
|
||||||
|
FlushingStreamHandler(ScheduledExecutorService scheduler,
|
||||||
|
Executor ioExecutor, OutputStream out, Formatter formatter) {
|
||||||
|
super(out, formatter);
|
||||||
|
this.scheduler = scheduler;
|
||||||
|
this.ioExecutor = ioExecutor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void publish(LogRecord record) {
|
||||||
|
super.publish(record);
|
||||||
|
if (!flushScheduled.getAndSet(true)) {
|
||||||
|
scheduler.schedule(this::scheduledFlush,
|
||||||
|
FLUSH_DELAY_MS, MILLISECONDS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void scheduledFlush() {
|
||||||
|
ioExecutor.execute(() -> {
|
||||||
|
flushScheduled.set(false);
|
||||||
|
flush();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package org.briarproject.bramble.logging;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||||
|
import org.briarproject.bramble.api.logging.PersistentLogManager;
|
||||||
|
|
||||||
|
import java.util.logging.Formatter;
|
||||||
|
|
||||||
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
|
import dagger.Module;
|
||||||
|
import dagger.Provides;
|
||||||
|
|
||||||
|
@Module
|
||||||
|
public class LoggingModule {
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
Formatter provideFormatter() {
|
||||||
|
return new BriefLogFormatter();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
@Singleton
|
||||||
|
PersistentLogManager providePersistentLogManager(
|
||||||
|
LifecycleManager lifecycleManager,
|
||||||
|
PersistentLogManagerImpl persistentLogManager) {
|
||||||
|
lifecycleManager.registerOpenDatabaseHook(persistentLogManager);
|
||||||
|
return persistentLogManager;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,177 @@
|
|||||||
|
package org.briarproject.bramble.logging;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||||
|
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||||
|
import org.briarproject.bramble.api.db.DatabaseComponent;
|
||||||
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
|
import org.briarproject.bramble.api.db.Transaction;
|
||||||
|
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||||
|
import org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook;
|
||||||
|
import org.briarproject.bramble.api.lifecycle.ShutdownManager;
|
||||||
|
import org.briarproject.bramble.api.logging.PersistentLogManager;
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.settings.Settings;
|
||||||
|
import org.briarproject.bramble.api.system.Scheduler;
|
||||||
|
import org.briarproject.bramble.api.transport.StreamReaderFactory;
|
||||||
|
import org.briarproject.bramble.api.transport.StreamWriter;
|
||||||
|
import org.briarproject.bramble.api.transport.StreamWriterFactory;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
import java.util.logging.Formatter;
|
||||||
|
import java.util.logging.Handler;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
import java.util.logging.StreamHandler;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
import javax.annotation.concurrent.ThreadSafe;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import static java.util.logging.Level.WARNING;
|
||||||
|
import static java.util.logging.Logger.getLogger;
|
||||||
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
|
|
||||||
|
@ThreadSafe
|
||||||
|
@NotNullByDefault
|
||||||
|
class PersistentLogManagerImpl implements PersistentLogManager,
|
||||||
|
OpenDatabaseHook {
|
||||||
|
|
||||||
|
private static final Logger LOG =
|
||||||
|
getLogger(PersistentLogManagerImpl.class.getName());
|
||||||
|
|
||||||
|
private static final String LOG_FILE = "briar.log";
|
||||||
|
private static final String OLD_LOG_FILE = "briar.log.old";
|
||||||
|
|
||||||
|
private final ScheduledExecutorService scheduler;
|
||||||
|
private final Executor ioExecutor;
|
||||||
|
private final ShutdownManager shutdownManager;
|
||||||
|
private final DatabaseComponent db;
|
||||||
|
private final StreamReaderFactory streamReaderFactory;
|
||||||
|
private final StreamWriterFactory streamWriterFactory;
|
||||||
|
private final Formatter formatter;
|
||||||
|
private final SecretKey logKey;
|
||||||
|
private final AtomicReference<Integer> shutdownHookHandle =
|
||||||
|
new AtomicReference<>();
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private volatile SecretKey oldLogKey = null;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
PersistentLogManagerImpl(
|
||||||
|
@Scheduler ScheduledExecutorService scheduler,
|
||||||
|
@IoExecutor Executor ioExecutor,
|
||||||
|
ShutdownManager shutdownManager,
|
||||||
|
DatabaseComponent db,
|
||||||
|
StreamReaderFactory streamReaderFactory,
|
||||||
|
StreamWriterFactory streamWriterFactory,
|
||||||
|
Formatter formatter,
|
||||||
|
CryptoComponent crypto) {
|
||||||
|
this.scheduler = scheduler;
|
||||||
|
this.ioExecutor = ioExecutor;
|
||||||
|
this.shutdownManager = shutdownManager;
|
||||||
|
this.db = db;
|
||||||
|
this.streamReaderFactory = streamReaderFactory;
|
||||||
|
this.streamWriterFactory = streamWriterFactory;
|
||||||
|
this.formatter = formatter;
|
||||||
|
logKey = crypto.generateSecretKey();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDatabaseOpened(Transaction txn) throws DbException {
|
||||||
|
Settings s = db.getSettings(txn, LOG_SETTINGS_NAMESPACE);
|
||||||
|
// Load the old log key, if any
|
||||||
|
byte[] oldKeyBytes = s.getBytes(LOG_KEY_KEY);
|
||||||
|
if (oldKeyBytes != null && oldKeyBytes.length == SecretKey.LENGTH) {
|
||||||
|
LOG.info("Loaded old log key");
|
||||||
|
oldLogKey = new SecretKey(oldKeyBytes);
|
||||||
|
}
|
||||||
|
// Store the current log key
|
||||||
|
s.putBytes(LOG_KEY_KEY, logKey.getBytes());
|
||||||
|
db.mergeSettings(txn, s, LOG_SETTINGS_NAMESPACE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Handler createLogHandler(File dir) throws IOException {
|
||||||
|
File logFile = new File(dir, LOG_FILE);
|
||||||
|
File oldLogFile = new File(dir, OLD_LOG_FILE);
|
||||||
|
if (oldLogFile.exists() && !oldLogFile.delete())
|
||||||
|
LOG.warning("Failed to delete old log file");
|
||||||
|
if (logFile.exists() && !logFile.renameTo(oldLogFile))
|
||||||
|
LOG.warning("Failed to rename log file");
|
||||||
|
try {
|
||||||
|
OutputStream out = new FileOutputStream(logFile);
|
||||||
|
StreamWriter writer =
|
||||||
|
streamWriterFactory.createLogStreamWriter(out, logKey);
|
||||||
|
StreamHandler handler = new FlushingStreamHandler(scheduler,
|
||||||
|
ioExecutor, writer.getOutputStream(), formatter);
|
||||||
|
// Flush the log and terminate the stream at shutdown
|
||||||
|
Runnable shutdownHook = () -> {
|
||||||
|
LOG.info("Shutting down");
|
||||||
|
handler.flush();
|
||||||
|
try {
|
||||||
|
writer.sendEndOfStream();
|
||||||
|
} catch (IOException e) {
|
||||||
|
logException(LOG, WARNING, e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
int handle = shutdownManager.addShutdownHook(shutdownHook);
|
||||||
|
// If a previous handler registered a shutdown hook, remove it
|
||||||
|
Integer oldHandle = shutdownHookHandle.getAndSet(handle);
|
||||||
|
if (oldHandle != null) {
|
||||||
|
shutdownManager.removeShutdownHook(oldHandle);
|
||||||
|
}
|
||||||
|
return handler;
|
||||||
|
} catch (SecurityException e) {
|
||||||
|
throw new IOException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addLogHandler(File dir, Logger logger) throws IOException {
|
||||||
|
for (Handler h : logger.getHandlers()) {
|
||||||
|
if (h instanceof FlushingStreamHandler) logger.removeHandler(h);
|
||||||
|
}
|
||||||
|
logger.addHandler(createLogHandler(dir));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Scanner getPersistedLog(File dir, boolean old)
|
||||||
|
throws IOException {
|
||||||
|
if (old) {
|
||||||
|
SecretKey oldLogKey = this.oldLogKey;
|
||||||
|
if (oldLogKey == null) {
|
||||||
|
LOG.info("Old log key has not been loaded");
|
||||||
|
return emptyScanner();
|
||||||
|
}
|
||||||
|
return getPersistedLog(new File(dir, OLD_LOG_FILE), oldLogKey);
|
||||||
|
} else {
|
||||||
|
return getPersistedLog(new File(dir, LOG_FILE), logKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Scanner getPersistedLog(File logFile, SecretKey key)
|
||||||
|
throws IOException {
|
||||||
|
if (logFile.exists()) {
|
||||||
|
LOG.info("Reading log file");
|
||||||
|
InputStream in = new FileInputStream(logFile);
|
||||||
|
return new Scanner(streamReaderFactory.createLogStreamReader(in,
|
||||||
|
key));
|
||||||
|
} else {
|
||||||
|
LOG.info("Log file does not exist");
|
||||||
|
return emptyScanner();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Scanner emptyScanner() {
|
||||||
|
return new Scanner(new ByteArrayInputStream(new byte[0]));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package org.briarproject.bramble.plugin;
|
package org.briarproject.bramble.plugin;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.connection.ConnectionManager;
|
import org.briarproject.bramble.api.connection.ConnectionManager;
|
||||||
import org.briarproject.bramble.api.contact.ContactId;
|
|
||||||
import org.briarproject.bramble.api.db.DbException;
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
import org.briarproject.bramble.api.event.EventBus;
|
import org.briarproject.bramble.api.event.EventBus;
|
||||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||||
@@ -29,7 +28,6 @@ import org.briarproject.bramble.api.properties.TransportProperties;
|
|||||||
import org.briarproject.bramble.api.properties.TransportPropertyManager;
|
import org.briarproject.bramble.api.properties.TransportPropertyManager;
|
||||||
import org.briarproject.bramble.api.settings.Settings;
|
import org.briarproject.bramble.api.settings.Settings;
|
||||||
import org.briarproject.bramble.api.settings.SettingsManager;
|
import org.briarproject.bramble.api.settings.SettingsManager;
|
||||||
import org.briarproject.bramble.api.system.WakefulIoExecutor;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
@@ -46,14 +44,12 @@ import java.util.logging.Logger;
|
|||||||
import javax.annotation.concurrent.ThreadSafe;
|
import javax.annotation.concurrent.ThreadSafe;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import static java.util.Collections.emptyList;
|
|
||||||
import static java.util.logging.Level.FINE;
|
import static java.util.logging.Level.FINE;
|
||||||
import static java.util.logging.Level.INFO;
|
import static java.util.logging.Level.INFO;
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static java.util.logging.Logger.getLogger;
|
import static java.util.logging.Logger.getLogger;
|
||||||
import static org.briarproject.bramble.api.plugin.Plugin.PREF_PLUGIN_ENABLE;
|
import static org.briarproject.bramble.api.plugin.Plugin.PREF_PLUGIN_ENABLE;
|
||||||
import static org.briarproject.bramble.api.plugin.Plugin.State.ACTIVE;
|
import static org.briarproject.bramble.api.plugin.Plugin.State.ACTIVE;
|
||||||
import static org.briarproject.bramble.api.plugin.Plugin.State.DISABLED;
|
|
||||||
import static org.briarproject.bramble.api.plugin.Plugin.State.STARTING_STOPPING;
|
import static org.briarproject.bramble.api.plugin.Plugin.State.STARTING_STOPPING;
|
||||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
@@ -66,7 +62,7 @@ class PluginManagerImpl implements PluginManager, Service {
|
|||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
getLogger(PluginManagerImpl.class.getName());
|
getLogger(PluginManagerImpl.class.getName());
|
||||||
|
|
||||||
private final Executor ioExecutor, wakefulIoExecutor;
|
private final Executor ioExecutor;
|
||||||
private final EventBus eventBus;
|
private final EventBus eventBus;
|
||||||
private final PluginConfig pluginConfig;
|
private final PluginConfig pluginConfig;
|
||||||
private final ConnectionManager connectionManager;
|
private final ConnectionManager connectionManager;
|
||||||
@@ -79,15 +75,11 @@ class PluginManagerImpl implements PluginManager, Service {
|
|||||||
private final AtomicBoolean used = new AtomicBoolean(false);
|
private final AtomicBoolean used = new AtomicBoolean(false);
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
PluginManagerImpl(@IoExecutor Executor ioExecutor,
|
PluginManagerImpl(@IoExecutor Executor ioExecutor, EventBus eventBus,
|
||||||
@WakefulIoExecutor Executor wakefulIoExecutor,
|
PluginConfig pluginConfig, ConnectionManager connectionManager,
|
||||||
EventBus eventBus,
|
|
||||||
PluginConfig pluginConfig,
|
|
||||||
ConnectionManager connectionManager,
|
|
||||||
SettingsManager settingsManager,
|
SettingsManager settingsManager,
|
||||||
TransportPropertyManager transportPropertyManager) {
|
TransportPropertyManager transportPropertyManager) {
|
||||||
this.ioExecutor = ioExecutor;
|
this.ioExecutor = ioExecutor;
|
||||||
this.wakefulIoExecutor = wakefulIoExecutor;
|
|
||||||
this.eventBus = eventBus;
|
this.eventBus = eventBus;
|
||||||
this.pluginConfig = pluginConfig;
|
this.pluginConfig = pluginConfig;
|
||||||
this.connectionManager = connectionManager;
|
this.connectionManager = connectionManager;
|
||||||
@@ -115,7 +107,7 @@ class PluginManagerImpl implements PluginManager, Service {
|
|||||||
simplexPlugins.add(s);
|
simplexPlugins.add(s);
|
||||||
CountDownLatch startLatch = new CountDownLatch(1);
|
CountDownLatch startLatch = new CountDownLatch(1);
|
||||||
startLatches.put(t, startLatch);
|
startLatches.put(t, startLatch);
|
||||||
wakefulIoExecutor.execute(new PluginStarter(s, startLatch));
|
ioExecutor.execute(new PluginStarter(s, startLatch));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Instantiate the duplex plugins and start them asynchronously
|
// Instantiate the duplex plugins and start them asynchronously
|
||||||
@@ -131,7 +123,7 @@ class PluginManagerImpl implements PluginManager, Service {
|
|||||||
duplexPlugins.add(d);
|
duplexPlugins.add(d);
|
||||||
CountDownLatch startLatch = new CountDownLatch(1);
|
CountDownLatch startLatch = new CountDownLatch(1);
|
||||||
startLatches.put(t, startLatch);
|
startLatches.put(t, startLatch);
|
||||||
wakefulIoExecutor.execute(new PluginStarter(d, startLatch));
|
ioExecutor.execute(new PluginStarter(d, startLatch));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -143,16 +135,12 @@ class PluginManagerImpl implements PluginManager, Service {
|
|||||||
LOG.info("Stopping simplex plugins");
|
LOG.info("Stopping simplex plugins");
|
||||||
for (SimplexPlugin s : simplexPlugins) {
|
for (SimplexPlugin s : simplexPlugins) {
|
||||||
CountDownLatch startLatch = startLatches.get(s.getId());
|
CountDownLatch startLatch = startLatches.get(s.getId());
|
||||||
// Don't need the wakeful executor here as we wait for the plugin
|
|
||||||
// to stop before returning
|
|
||||||
ioExecutor.execute(new PluginStopper(s, startLatch, stopLatch));
|
ioExecutor.execute(new PluginStopper(s, startLatch, stopLatch));
|
||||||
}
|
}
|
||||||
// Stop the duplex plugins
|
// Stop the duplex plugins
|
||||||
LOG.info("Stopping duplex plugins");
|
LOG.info("Stopping duplex plugins");
|
||||||
for (DuplexPlugin d : duplexPlugins) {
|
for (DuplexPlugin d : duplexPlugins) {
|
||||||
CountDownLatch startLatch = startLatches.get(d.getId());
|
CountDownLatch startLatch = startLatches.get(d.getId());
|
||||||
// Don't need the wakeful executor here as we wait for the plugin
|
|
||||||
// to stop before returning
|
|
||||||
ioExecutor.execute(new PluginStopper(d, startLatch, stopLatch));
|
ioExecutor.execute(new PluginStopper(d, startLatch, stopLatch));
|
||||||
}
|
}
|
||||||
// Wait for all the plugins to stop
|
// Wait for all the plugins to stop
|
||||||
@@ -215,7 +203,7 @@ class PluginManagerImpl implements PluginManager, Service {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class PluginStarter implements Runnable {
|
private class PluginStarter implements Runnable {
|
||||||
|
|
||||||
private final Plugin plugin;
|
private final Plugin plugin;
|
||||||
private final CountDownLatch startLatch;
|
private final CountDownLatch startLatch;
|
||||||
@@ -245,7 +233,7 @@ class PluginManagerImpl implements PluginManager, Service {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class PluginStopper implements Runnable {
|
private class PluginStopper implements Runnable {
|
||||||
|
|
||||||
private final Plugin plugin;
|
private final Plugin plugin;
|
||||||
private final CountDownLatch startLatch, stopLatch;
|
private final CountDownLatch startLatch, stopLatch;
|
||||||
@@ -315,18 +303,6 @@ class PluginManagerImpl implements PluginManager, Service {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Collection<TransportProperties> getRemoteProperties() {
|
|
||||||
try {
|
|
||||||
Map<ContactId, TransportProperties> remote =
|
|
||||||
transportPropertyManager.getRemoteProperties(id);
|
|
||||||
return remote.values();
|
|
||||||
} catch (DbException e) {
|
|
||||||
logException(LOG, WARNING, e);
|
|
||||||
return emptyList();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mergeSettings(Settings s) {
|
public void mergeSettings(Settings s) {
|
||||||
PluginManagerImpl.this.mergeSettings(s, id.getString());
|
PluginManagerImpl.this.mergeSettings(s, id.getString());
|
||||||
@@ -355,10 +331,6 @@ class PluginManagerImpl implements PluginManager, Service {
|
|||||||
} else if (oldState == ACTIVE) {
|
} else if (oldState == ACTIVE) {
|
||||||
eventBus.broadcast(new TransportInactiveEvent(id));
|
eventBus.broadcast(new TransportInactiveEvent(id));
|
||||||
}
|
}
|
||||||
} else if (newState == DISABLED) {
|
|
||||||
// Broadcast an event even though the state hasn't changed, as
|
|
||||||
// the reasons for the plugin being disabled may have changed
|
|
||||||
eventBus.broadcast(new TransportStateEvent(id, newState));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,10 +26,7 @@ import org.briarproject.bramble.api.plugin.simplex.SimplexPlugin;
|
|||||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||||
import org.briarproject.bramble.api.properties.TransportPropertyManager;
|
import org.briarproject.bramble.api.properties.TransportPropertyManager;
|
||||||
import org.briarproject.bramble.api.system.Clock;
|
import org.briarproject.bramble.api.system.Clock;
|
||||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
import org.briarproject.bramble.api.system.Scheduler;
|
||||||
import org.briarproject.bramble.api.system.TaskScheduler.Cancellable;
|
|
||||||
import org.briarproject.bramble.api.system.Wakeful;
|
|
||||||
import org.briarproject.bramble.api.system.WakefulIoExecutor;
|
|
||||||
|
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -38,6 +35,8 @@ import java.util.HashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.Lock;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
@@ -58,8 +57,8 @@ class PollerImpl implements Poller, EventListener {
|
|||||||
|
|
||||||
private static final Logger LOG = getLogger(PollerImpl.class.getName());
|
private static final Logger LOG = getLogger(PollerImpl.class.getName());
|
||||||
|
|
||||||
private final Executor ioExecutor, wakefulIoExecutor;
|
private final Executor ioExecutor;
|
||||||
private final TaskScheduler scheduler;
|
private final ScheduledExecutorService scheduler;
|
||||||
private final ConnectionManager connectionManager;
|
private final ConnectionManager connectionManager;
|
||||||
private final ConnectionRegistry connectionRegistry;
|
private final ConnectionRegistry connectionRegistry;
|
||||||
private final PluginManager pluginManager;
|
private final PluginManager pluginManager;
|
||||||
@@ -72,16 +71,12 @@ class PollerImpl implements Poller, EventListener {
|
|||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
PollerImpl(@IoExecutor Executor ioExecutor,
|
PollerImpl(@IoExecutor Executor ioExecutor,
|
||||||
@WakefulIoExecutor Executor wakefulIoExecutor,
|
@Scheduler ScheduledExecutorService scheduler,
|
||||||
TaskScheduler scheduler,
|
|
||||||
ConnectionManager connectionManager,
|
ConnectionManager connectionManager,
|
||||||
ConnectionRegistry connectionRegistry,
|
ConnectionRegistry connectionRegistry, PluginManager pluginManager,
|
||||||
PluginManager pluginManager,
|
|
||||||
TransportPropertyManager transportPropertyManager,
|
TransportPropertyManager transportPropertyManager,
|
||||||
SecureRandom random,
|
SecureRandom random, Clock clock) {
|
||||||
Clock clock) {
|
|
||||||
this.ioExecutor = ioExecutor;
|
this.ioExecutor = ioExecutor;
|
||||||
this.wakefulIoExecutor = wakefulIoExecutor;
|
|
||||||
this.scheduler = scheduler;
|
this.scheduler = scheduler;
|
||||||
this.connectionManager = connectionManager;
|
this.connectionManager = connectionManager;
|
||||||
this.connectionRegistry = connectionRegistry;
|
this.connectionRegistry = connectionRegistry;
|
||||||
@@ -138,7 +133,7 @@ class PollerImpl implements Poller, EventListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void connectToContact(ContactId c, SimplexPlugin p) {
|
private void connectToContact(ContactId c, SimplexPlugin p) {
|
||||||
wakefulIoExecutor.execute(() -> {
|
ioExecutor.execute(() -> {
|
||||||
TransportId t = p.getId();
|
TransportId t = p.getId();
|
||||||
if (connectionRegistry.isConnected(c, t)) return;
|
if (connectionRegistry.isConnected(c, t)) return;
|
||||||
try {
|
try {
|
||||||
@@ -154,7 +149,7 @@ class PollerImpl implements Poller, EventListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void connectToContact(ContactId c, DuplexPlugin p) {
|
private void connectToContact(ContactId c, DuplexPlugin p) {
|
||||||
wakefulIoExecutor.execute(() -> {
|
ioExecutor.execute(() -> {
|
||||||
TransportId t = p.getId();
|
TransportId t = p.getId();
|
||||||
if (connectionRegistry.isConnected(c, t)) return;
|
if (connectionRegistry.isConnected(c, t)) return;
|
||||||
try {
|
try {
|
||||||
@@ -191,11 +186,11 @@ class PollerImpl implements Poller, EventListener {
|
|||||||
if (scheduled == null || due < scheduled.task.due) {
|
if (scheduled == null || due < scheduled.task.due) {
|
||||||
// If a later task exists, cancel it. If it's already started
|
// If a later task exists, cancel it. If it's already started
|
||||||
// it will abort safely when it finds it's been replaced
|
// it will abort safely when it finds it's been replaced
|
||||||
if (scheduled != null) scheduled.cancellable.cancel();
|
if (scheduled != null) scheduled.future.cancel(false);
|
||||||
PollTask task = new PollTask(p, due, randomiseNext);
|
PollTask task = new PollTask(p, due, randomiseNext);
|
||||||
Cancellable cancellable = scheduler.schedule(task, ioExecutor,
|
Future future = scheduler.schedule(() ->
|
||||||
delay, MILLISECONDS);
|
ioExecutor.execute(task), delay, MILLISECONDS);
|
||||||
tasks.put(t, new ScheduledPollTask(task, cancellable));
|
tasks.put(t, new ScheduledPollTask(task, future));
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
@@ -206,7 +201,7 @@ class PollerImpl implements Poller, EventListener {
|
|||||||
lock.lock();
|
lock.lock();
|
||||||
try {
|
try {
|
||||||
ScheduledPollTask scheduled = tasks.remove(t);
|
ScheduledPollTask scheduled = tasks.remove(t);
|
||||||
if (scheduled != null) scheduled.cancellable.cancel();
|
if (scheduled != null) scheduled.future.cancel(false);
|
||||||
} finally {
|
} finally {
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
}
|
}
|
||||||
@@ -237,11 +232,11 @@ class PollerImpl implements Poller, EventListener {
|
|||||||
private class ScheduledPollTask {
|
private class ScheduledPollTask {
|
||||||
|
|
||||||
private final PollTask task;
|
private final PollTask task;
|
||||||
private final Cancellable cancellable;
|
private final Future future;
|
||||||
|
|
||||||
private ScheduledPollTask(PollTask task, Cancellable cancellable) {
|
private ScheduledPollTask(PollTask task, Future future) {
|
||||||
this.task = task;
|
this.task = task;
|
||||||
this.cancellable = cancellable;
|
this.future = future;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -259,7 +254,6 @@ class PollerImpl implements Poller, EventListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
@IoExecutor
|
@IoExecutor
|
||||||
@Wakeful
|
|
||||||
public void run() {
|
public void run() {
|
||||||
lock.lock();
|
lock.lock();
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
package org.briarproject.bramble.plugin.bluetooth;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
@NotNullByDefault
|
|
||||||
interface BluetoothConnectionFactory<S> {
|
|
||||||
|
|
||||||
DuplexTransportConnection wrapSocket(DuplexPlugin plugin, S socket)
|
|
||||||
throws IOException;
|
|
||||||
}
|
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
package org.briarproject.bramble.plugin.bluetooth;
|
package org.briarproject.bramble.plugin.bluetooth;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.FormatException;
|
import org.briarproject.bramble.api.FormatException;
|
||||||
import org.briarproject.bramble.api.Multiset;
|
|
||||||
import org.briarproject.bramble.api.Pair;
|
import org.briarproject.bramble.api.Pair;
|
||||||
import org.briarproject.bramble.api.data.BdfList;
|
import org.briarproject.bramble.api.data.BdfList;
|
||||||
import org.briarproject.bramble.api.event.Event;
|
import org.briarproject.bramble.api.event.Event;
|
||||||
import org.briarproject.bramble.api.event.EventListener;
|
import org.briarproject.bramble.api.event.EventListener;
|
||||||
|
import org.briarproject.bramble.api.io.TimeoutMonitor;
|
||||||
import org.briarproject.bramble.api.keyagreement.KeyAgreementConnection;
|
import org.briarproject.bramble.api.keyagreement.KeyAgreementConnection;
|
||||||
import org.briarproject.bramble.api.keyagreement.KeyAgreementListener;
|
import org.briarproject.bramble.api.keyagreement.KeyAgreementListener;
|
||||||
import org.briarproject.bramble.api.keyagreement.event.KeyAgreementListeningEvent;
|
import org.briarproject.bramble.api.keyagreement.event.KeyAgreementListeningEvent;
|
||||||
@@ -21,8 +21,10 @@ import org.briarproject.bramble.api.plugin.PluginException;
|
|||||||
import org.briarproject.bramble.api.plugin.TransportId;
|
import org.briarproject.bramble.api.plugin.TransportId;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||||
|
import org.briarproject.bramble.api.plugin.event.BluetoothEnabledEvent;
|
||||||
|
import org.briarproject.bramble.api.plugin.event.DisableBluetoothEvent;
|
||||||
|
import org.briarproject.bramble.api.plugin.event.EnableBluetoothEvent;
|
||||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||||
import org.briarproject.bramble.api.properties.event.RemoteTransportPropertiesUpdatedEvent;
|
|
||||||
import org.briarproject.bramble.api.rendezvous.KeyMaterialSource;
|
import org.briarproject.bramble.api.rendezvous.KeyMaterialSource;
|
||||||
import org.briarproject.bramble.api.rendezvous.RendezvousEndpoint;
|
import org.briarproject.bramble.api.rendezvous.RendezvousEndpoint;
|
||||||
import org.briarproject.bramble.api.settings.Settings;
|
import org.briarproject.bramble.api.settings.Settings;
|
||||||
@@ -44,12 +46,8 @@ import static java.util.logging.Level.INFO;
|
|||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static java.util.logging.Logger.getLogger;
|
import static java.util.logging.Logger.getLogger;
|
||||||
import static org.briarproject.bramble.api.keyagreement.KeyAgreementConstants.TRANSPORT_ID_BLUETOOTH;
|
import static org.briarproject.bramble.api.keyagreement.KeyAgreementConstants.TRANSPORT_ID_BLUETOOTH;
|
||||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.DEFAULT_PREF_ADDRESS_IS_REFLECTED;
|
|
||||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.DEFAULT_PREF_EVER_CONNECTED;
|
|
||||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.DEFAULT_PREF_PLUGIN_ENABLE;
|
import static org.briarproject.bramble.api.plugin.BluetoothConstants.DEFAULT_PREF_PLUGIN_ENABLE;
|
||||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.ID;
|
import static org.briarproject.bramble.api.plugin.BluetoothConstants.ID;
|
||||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PREF_ADDRESS_IS_REFLECTED;
|
|
||||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PREF_EVER_CONNECTED;
|
|
||||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PROP_ADDRESS;
|
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PROP_ADDRESS;
|
||||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PROP_UUID;
|
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PROP_UUID;
|
||||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.UUID_BYTES;
|
import static org.briarproject.bramble.api.plugin.BluetoothConstants.UUID_BYTES;
|
||||||
@@ -57,7 +55,6 @@ import static org.briarproject.bramble.api.plugin.Plugin.State.ACTIVE;
|
|||||||
import static org.briarproject.bramble.api.plugin.Plugin.State.DISABLED;
|
import static org.briarproject.bramble.api.plugin.Plugin.State.DISABLED;
|
||||||
import static org.briarproject.bramble.api.plugin.Plugin.State.INACTIVE;
|
import static org.briarproject.bramble.api.plugin.Plugin.State.INACTIVE;
|
||||||
import static org.briarproject.bramble.api.plugin.Plugin.State.STARTING_STOPPING;
|
import static org.briarproject.bramble.api.plugin.Plugin.State.STARTING_STOPPING;
|
||||||
import static org.briarproject.bramble.api.properties.TransportPropertyConstants.REFLECTED_PROPERTY_PREFIX;
|
|
||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
import static org.briarproject.bramble.util.PrivacyUtils.scrubMacAddress;
|
import static org.briarproject.bramble.util.PrivacyUtils.scrubMacAddress;
|
||||||
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
||||||
@@ -66,21 +63,20 @@ import static org.briarproject.bramble.util.StringUtils.macToString;
|
|||||||
|
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@ParametersNotNullByDefault
|
@ParametersNotNullByDefault
|
||||||
abstract class BluetoothPlugin<S, SS> implements DuplexPlugin, EventListener {
|
abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
||||||
|
|
||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
getLogger(BluetoothPlugin.class.getName());
|
getLogger(BluetoothPlugin.class.getName());
|
||||||
|
|
||||||
final BluetoothConnectionLimiter connectionLimiter;
|
final BluetoothConnectionLimiter connectionLimiter;
|
||||||
final BluetoothConnectionFactory<S> connectionFactory;
|
final TimeoutMonitor timeoutMonitor;
|
||||||
|
|
||||||
private final Executor ioExecutor, wakefulIoExecutor;
|
private final Executor ioExecutor;
|
||||||
private final SecureRandom secureRandom;
|
private final SecureRandom secureRandom;
|
||||||
private final Backoff backoff;
|
private final Backoff backoff;
|
||||||
private final PluginCallback callback;
|
private final PluginCallback callback;
|
||||||
private final int maxLatency, maxIdleTime;
|
private final int maxLatency, maxIdleTime;
|
||||||
private final AtomicBoolean used = new AtomicBoolean(false);
|
private final AtomicBoolean used = new AtomicBoolean(false);
|
||||||
private final AtomicBoolean everConnected = new AtomicBoolean(false);
|
|
||||||
|
|
||||||
protected final PluginState state = new PluginState();
|
protected final PluginState state = new PluginState();
|
||||||
|
|
||||||
@@ -90,6 +86,12 @@ abstract class BluetoothPlugin<S, SS> implements DuplexPlugin, EventListener {
|
|||||||
|
|
||||||
abstract boolean isAdapterEnabled();
|
abstract boolean isAdapterEnabled();
|
||||||
|
|
||||||
|
abstract void enableAdapter();
|
||||||
|
|
||||||
|
abstract void disableAdapterIfEnabledByUs();
|
||||||
|
|
||||||
|
abstract void setEnabledByUs();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the local Bluetooth address, or null if no valid address can
|
* Returns the local Bluetooth address, or null if no valid address can
|
||||||
* be found.
|
* be found.
|
||||||
@@ -113,18 +115,12 @@ abstract class BluetoothPlugin<S, SS> implements DuplexPlugin, EventListener {
|
|||||||
abstract DuplexTransportConnection discoverAndConnect(String uuid);
|
abstract DuplexTransportConnection discoverAndConnect(String uuid);
|
||||||
|
|
||||||
BluetoothPlugin(BluetoothConnectionLimiter connectionLimiter,
|
BluetoothPlugin(BluetoothConnectionLimiter connectionLimiter,
|
||||||
BluetoothConnectionFactory<S> connectionFactory,
|
TimeoutMonitor timeoutMonitor, Executor ioExecutor,
|
||||||
Executor ioExecutor,
|
SecureRandom secureRandom, Backoff backoff,
|
||||||
Executor wakefulIoExecutor,
|
PluginCallback callback, int maxLatency, int maxIdleTime) {
|
||||||
SecureRandom secureRandom,
|
|
||||||
Backoff backoff,
|
|
||||||
PluginCallback callback,
|
|
||||||
int maxLatency,
|
|
||||||
int maxIdleTime) {
|
|
||||||
this.connectionLimiter = connectionLimiter;
|
this.connectionLimiter = connectionLimiter;
|
||||||
this.connectionFactory = connectionFactory;
|
this.timeoutMonitor = timeoutMonitor;
|
||||||
this.ioExecutor = ioExecutor;
|
this.ioExecutor = ioExecutor;
|
||||||
this.wakefulIoExecutor = wakefulIoExecutor;
|
|
||||||
this.secureRandom = secureRandom;
|
this.secureRandom = secureRandom;
|
||||||
this.backoff = backoff;
|
this.backoff = backoff;
|
||||||
this.callback = callback;
|
this.callback = callback;
|
||||||
@@ -171,8 +167,6 @@ abstract class BluetoothPlugin<S, SS> implements DuplexPlugin, EventListener {
|
|||||||
Settings settings = callback.getSettings();
|
Settings settings = callback.getSettings();
|
||||||
boolean enabledByUser = settings.getBoolean(PREF_PLUGIN_ENABLE,
|
boolean enabledByUser = settings.getBoolean(PREF_PLUGIN_ENABLE,
|
||||||
DEFAULT_PREF_PLUGIN_ENABLE);
|
DEFAULT_PREF_PLUGIN_ENABLE);
|
||||||
everConnected.set(settings.getBoolean(PREF_EVER_CONNECTED,
|
|
||||||
DEFAULT_PREF_EVER_CONNECTED));
|
|
||||||
state.setStarted(enabledByUser);
|
state.setStarted(enabledByUser);
|
||||||
try {
|
try {
|
||||||
initialiseAdapter();
|
initialiseAdapter();
|
||||||
@@ -180,7 +174,10 @@ abstract class BluetoothPlugin<S, SS> implements DuplexPlugin, EventListener {
|
|||||||
throw new PluginException(e);
|
throw new PluginException(e);
|
||||||
}
|
}
|
||||||
updateProperties();
|
updateProperties();
|
||||||
if (enabledByUser && isAdapterEnabled()) bind();
|
if (enabledByUser) {
|
||||||
|
if (isAdapterEnabled()) bind();
|
||||||
|
else enableAdapter();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void bind() {
|
private void bind() {
|
||||||
@@ -208,68 +205,25 @@ abstract class BluetoothPlugin<S, SS> implements DuplexPlugin, EventListener {
|
|||||||
TransportProperties p = callback.getLocalProperties();
|
TransportProperties p = callback.getLocalProperties();
|
||||||
String address = p.get(PROP_ADDRESS);
|
String address = p.get(PROP_ADDRESS);
|
||||||
String uuid = p.get(PROP_UUID);
|
String uuid = p.get(PROP_UUID);
|
||||||
Settings s = callback.getSettings();
|
|
||||||
boolean isReflected = s.getBoolean(PREF_ADDRESS_IS_REFLECTED,
|
|
||||||
DEFAULT_PREF_ADDRESS_IS_REFLECTED);
|
|
||||||
boolean changed = false;
|
boolean changed = false;
|
||||||
if (address == null || isReflected) {
|
if (address == null) {
|
||||||
address = getBluetoothAddress();
|
address = getBluetoothAddress();
|
||||||
if (LOG.isLoggable(INFO)) {
|
if (LOG.isLoggable(INFO))
|
||||||
LOG.info("Local address " + scrubMacAddress(address));
|
LOG.info("Local address " + scrubMacAddress(address));
|
||||||
}
|
if (!isNullOrEmpty(address)) {
|
||||||
if (address == null) {
|
p.put(PROP_ADDRESS, address);
|
||||||
if (everConnected.get()) {
|
|
||||||
address = getReflectedAddress();
|
|
||||||
if (LOG.isLoggable(INFO)) {
|
|
||||||
LOG.info("Reflected address " +
|
|
||||||
scrubMacAddress(address));
|
|
||||||
}
|
|
||||||
if (address != null) {
|
|
||||||
changed = true;
|
|
||||||
isReflected = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
changed = true;
|
changed = true;
|
||||||
isReflected = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (uuid == null) {
|
if (uuid == null) {
|
||||||
byte[] random = new byte[UUID_BYTES];
|
byte[] random = new byte[UUID_BYTES];
|
||||||
secureRandom.nextBytes(random);
|
secureRandom.nextBytes(random);
|
||||||
uuid = UUID.nameUUIDFromBytes(random).toString();
|
uuid = UUID.nameUUIDFromBytes(random).toString();
|
||||||
|
p.put(PROP_UUID, uuid);
|
||||||
changed = true;
|
changed = true;
|
||||||
}
|
}
|
||||||
contactConnectionsUuid = uuid;
|
contactConnectionsUuid = uuid;
|
||||||
if (changed) {
|
if (changed) callback.mergeLocalProperties(p);
|
||||||
p = new TransportProperties();
|
|
||||||
// If we previously used a reflected address and there's no longer
|
|
||||||
// a reflected address with enough votes to be used, we'll continue
|
|
||||||
// to use the old reflected address until there's a new winner
|
|
||||||
if (address != null) p.put(PROP_ADDRESS, address);
|
|
||||||
p.put(PROP_UUID, uuid);
|
|
||||||
callback.mergeLocalProperties(p);
|
|
||||||
s = new Settings();
|
|
||||||
s.putBoolean(PREF_ADDRESS_IS_REFLECTED, isReflected);
|
|
||||||
callback.mergeSettings(s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private String getReflectedAddress() {
|
|
||||||
// Count the number of votes for each reflected address
|
|
||||||
String key = REFLECTED_PROPERTY_PREFIX + PROP_ADDRESS;
|
|
||||||
Multiset<String> votes = new Multiset<>();
|
|
||||||
for (TransportProperties p : callback.getRemoteProperties()) {
|
|
||||||
String address = p.get(key);
|
|
||||||
if (address != null && isValidAddress(address)) votes.add(address);
|
|
||||||
}
|
|
||||||
// If an address gets more than half of the votes, accept it
|
|
||||||
int total = votes.getTotal();
|
|
||||||
for (String address : votes.keySet()) {
|
|
||||||
if (votes.getCount(address) * 2 > total) return address;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void acceptContactConnections(SS ss) {
|
private void acceptContactConnections(SS ss) {
|
||||||
@@ -286,27 +240,15 @@ abstract class BluetoothPlugin<S, SS> implements DuplexPlugin, EventListener {
|
|||||||
LOG.info("Connection received");
|
LOG.info("Connection received");
|
||||||
connectionLimiter.connectionOpened(conn);
|
connectionLimiter.connectionOpened(conn);
|
||||||
backoff.reset();
|
backoff.reset();
|
||||||
setEverConnected();
|
|
||||||
callback.handleConnection(conn);
|
callback.handleConnection(conn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setEverConnected() {
|
|
||||||
if (!everConnected.getAndSet(true)) {
|
|
||||||
ioExecutor.execute(() -> {
|
|
||||||
Settings s = new Settings();
|
|
||||||
s.putBoolean(PREF_EVER_CONNECTED, true);
|
|
||||||
callback.mergeSettings(s);
|
|
||||||
// Contacts may already have sent a reflected address
|
|
||||||
updateProperties();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stop() {
|
public void stop() {
|
||||||
SS ss = state.setStopped();
|
SS ss = state.setStopped();
|
||||||
tryToClose(ss);
|
tryToClose(ss);
|
||||||
|
disableAdapterIfEnabledByUs();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -344,11 +286,10 @@ abstract class BluetoothPlugin<S, SS> implements DuplexPlugin, EventListener {
|
|||||||
if (isNullOrEmpty(address)) return;
|
if (isNullOrEmpty(address)) return;
|
||||||
String uuid = p.get(PROP_UUID);
|
String uuid = p.get(PROP_UUID);
|
||||||
if (isNullOrEmpty(uuid)) return;
|
if (isNullOrEmpty(uuid)) return;
|
||||||
wakefulIoExecutor.execute(() -> {
|
ioExecutor.execute(() -> {
|
||||||
DuplexTransportConnection d = createConnection(p);
|
DuplexTransportConnection d = createConnection(p);
|
||||||
if (d != null) {
|
if (d != null) {
|
||||||
backoff.reset();
|
backoff.reset();
|
||||||
setEverConnected();
|
|
||||||
h.handleConnection(d);
|
h.handleConnection(d);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -436,10 +377,8 @@ abstract class BluetoothPlugin<S, SS> implements DuplexPlugin, EventListener {
|
|||||||
String uuid = UUID.nameUUIDFromBytes(commitment).toString();
|
String uuid = UUID.nameUUIDFromBytes(commitment).toString();
|
||||||
DuplexTransportConnection conn;
|
DuplexTransportConnection conn;
|
||||||
if (descriptor.size() == 1) {
|
if (descriptor.size() == 1) {
|
||||||
if (LOG.isLoggable(INFO)) {
|
if (LOG.isLoggable(INFO))
|
||||||
LOG.info("Discovering address for key agreement UUID " +
|
LOG.info("Discovering address for key agreement UUID " + uuid);
|
||||||
uuid);
|
|
||||||
}
|
|
||||||
conn = discoverAndConnect(uuid);
|
conn = discoverAndConnect(uuid);
|
||||||
} else {
|
} else {
|
||||||
String address;
|
String address;
|
||||||
@@ -453,10 +392,7 @@ abstract class BluetoothPlugin<S, SS> implements DuplexPlugin, EventListener {
|
|||||||
LOG.info("Connecting to key agreement UUID " + uuid);
|
LOG.info("Connecting to key agreement UUID " + uuid);
|
||||||
conn = connect(address, uuid);
|
conn = connect(address, uuid);
|
||||||
}
|
}
|
||||||
if (conn != null) {
|
if (conn != null) connectionLimiter.connectionOpened(conn);
|
||||||
connectionLimiter.connectionOpened(conn);
|
|
||||||
setEverConnected();
|
|
||||||
}
|
|
||||||
return conn;
|
return conn;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -479,7 +415,13 @@ abstract class BluetoothPlugin<S, SS> implements DuplexPlugin, EventListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void eventOccurred(Event e) {
|
public void eventOccurred(Event e) {
|
||||||
if (e instanceof SettingsUpdatedEvent) {
|
if (e instanceof EnableBluetoothEvent) {
|
||||||
|
ioExecutor.execute(this::enableAdapter);
|
||||||
|
} else if (e instanceof DisableBluetoothEvent) {
|
||||||
|
ioExecutor.execute(this::disableAdapterIfEnabledByUs);
|
||||||
|
} else if (e instanceof BluetoothEnabledEvent) {
|
||||||
|
setEnabledByUs();
|
||||||
|
} else if (e instanceof SettingsUpdatedEvent) {
|
||||||
SettingsUpdatedEvent s = (SettingsUpdatedEvent) e;
|
SettingsUpdatedEvent s = (SettingsUpdatedEvent) e;
|
||||||
if (s.getNamespace().equals(ID.getString()))
|
if (s.getNamespace().equals(ID.getString()))
|
||||||
ioExecutor.execute(() -> onSettingsUpdated(s.getSettings()));
|
ioExecutor.execute(() -> onSettingsUpdated(s.getSettings()));
|
||||||
@@ -487,12 +429,6 @@ abstract class BluetoothPlugin<S, SS> implements DuplexPlugin, EventListener {
|
|||||||
ioExecutor.execute(connectionLimiter::keyAgreementStarted);
|
ioExecutor.execute(connectionLimiter::keyAgreementStarted);
|
||||||
} else if (e instanceof KeyAgreementStoppedListeningEvent) {
|
} else if (e instanceof KeyAgreementStoppedListeningEvent) {
|
||||||
ioExecutor.execute(connectionLimiter::keyAgreementEnded);
|
ioExecutor.execute(connectionLimiter::keyAgreementEnded);
|
||||||
} else if (e instanceof RemoteTransportPropertiesUpdatedEvent) {
|
|
||||||
RemoteTransportPropertiesUpdatedEvent r =
|
|
||||||
(RemoteTransportPropertiesUpdatedEvent) e;
|
|
||||||
if (r.getTransportId().equals(ID)) {
|
|
||||||
ioExecutor.execute(this::updateProperties);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -505,13 +441,11 @@ abstract class BluetoothPlugin<S, SS> implements DuplexPlugin, EventListener {
|
|||||||
if (ss != null) {
|
if (ss != null) {
|
||||||
LOG.info("Disabled by user, closing server socket");
|
LOG.info("Disabled by user, closing server socket");
|
||||||
tryToClose(ss);
|
tryToClose(ss);
|
||||||
|
disableAdapterIfEnabledByUs();
|
||||||
} else if (s == INACTIVE) {
|
} else if (s == INACTIVE) {
|
||||||
if (isAdapterEnabled()) {
|
LOG.info("Enabled by user, opening server socket");
|
||||||
LOG.info("Enabled by user, opening server socket");
|
if (isAdapterEnabled()) bind();
|
||||||
bind();
|
else enableAdapter();
|
||||||
} else {
|
|
||||||
LOG.info("Enabled by user but adapter is disabled");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -88,15 +88,10 @@ class LanTcpPlugin extends TcpPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LanTcpPlugin(Executor ioExecutor,
|
LanTcpPlugin(Executor ioExecutor, Backoff backoff, PluginCallback callback,
|
||||||
Executor wakefulIoExecutor,
|
int maxLatency, int maxIdleTime, int connectionTimeout) {
|
||||||
Backoff backoff,
|
super(ioExecutor, backoff, callback, maxLatency, maxIdleTime,
|
||||||
PluginCallback callback,
|
connectionTimeout);
|
||||||
int maxLatency,
|
|
||||||
int maxIdleTime,
|
|
||||||
int connectionTimeout) {
|
|
||||||
super(ioExecutor, wakefulIoExecutor, backoff, callback, maxLatency,
|
|
||||||
maxIdleTime, connectionTimeout);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package org.briarproject.bramble.plugin.tcp;
|
package org.briarproject.bramble.plugin.tcp;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.event.EventBus;
|
import org.briarproject.bramble.api.event.EventBus;
|
||||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.Backoff;
|
import org.briarproject.bramble.api.plugin.Backoff;
|
||||||
import org.briarproject.bramble.api.plugin.BackoffFactory;
|
import org.briarproject.bramble.api.plugin.BackoffFactory;
|
||||||
@@ -9,12 +8,10 @@ import org.briarproject.bramble.api.plugin.PluginCallback;
|
|||||||
import org.briarproject.bramble.api.plugin.TransportId;
|
import org.briarproject.bramble.api.plugin.TransportId;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
|
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
|
||||||
import org.briarproject.bramble.api.system.WakefulIoExecutor;
|
|
||||||
|
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
import javax.annotation.concurrent.Immutable;
|
import javax.annotation.concurrent.Immutable;
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
import static org.briarproject.bramble.api.plugin.LanTcpConstants.ID;
|
import static org.briarproject.bramble.api.plugin.LanTcpConstants.ID;
|
||||||
|
|
||||||
@@ -29,17 +26,13 @@ public class LanTcpPluginFactory implements DuplexPluginFactory {
|
|||||||
private static final int MAX_POLLING_INTERVAL = 600_000; // 10 mins
|
private static final int MAX_POLLING_INTERVAL = 600_000; // 10 mins
|
||||||
private static final double BACKOFF_BASE = 1.2;
|
private static final double BACKOFF_BASE = 1.2;
|
||||||
|
|
||||||
private final Executor ioExecutor, wakefulIoExecutor;
|
private final Executor ioExecutor;
|
||||||
private final EventBus eventBus;
|
private final EventBus eventBus;
|
||||||
private final BackoffFactory backoffFactory;
|
private final BackoffFactory backoffFactory;
|
||||||
|
|
||||||
@Inject
|
public LanTcpPluginFactory(Executor ioExecutor, EventBus eventBus,
|
||||||
public LanTcpPluginFactory(@IoExecutor Executor ioExecutor,
|
|
||||||
@WakefulIoExecutor Executor wakefulIoExecutor,
|
|
||||||
EventBus eventBus,
|
|
||||||
BackoffFactory backoffFactory) {
|
BackoffFactory backoffFactory) {
|
||||||
this.ioExecutor = ioExecutor;
|
this.ioExecutor = ioExecutor;
|
||||||
this.wakefulIoExecutor = wakefulIoExecutor;
|
|
||||||
this.eventBus = eventBus;
|
this.eventBus = eventBus;
|
||||||
this.backoffFactory = backoffFactory;
|
this.backoffFactory = backoffFactory;
|
||||||
}
|
}
|
||||||
@@ -58,9 +51,8 @@ public class LanTcpPluginFactory implements DuplexPluginFactory {
|
|||||||
public DuplexPlugin createPlugin(PluginCallback callback) {
|
public DuplexPlugin createPlugin(PluginCallback callback) {
|
||||||
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
||||||
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
||||||
LanTcpPlugin plugin = new LanTcpPlugin(ioExecutor, wakefulIoExecutor,
|
LanTcpPlugin plugin = new LanTcpPlugin(ioExecutor, backoff, callback, MAX_LATENCY,
|
||||||
backoff, callback, MAX_LATENCY, MAX_IDLE_TIME,
|
MAX_IDLE_TIME, CONNECTION_TIMEOUT);
|
||||||
CONNECTION_TIMEOUT);
|
|
||||||
eventBus.addListener(plugin);
|
eventBus.addListener(plugin);
|
||||||
return plugin;
|
return plugin;
|
||||||
}
|
}
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user