diff --git a/bramble-android/src/main/java/org/briarproject/bramble/BrambleAndroidModule.java b/bramble-android/src/main/java/org/briarproject/bramble/BrambleAndroidModule.java index e0087bed5..917926ed2 100644 --- a/bramble-android/src/main/java/org/briarproject/bramble/BrambleAndroidModule.java +++ b/bramble-android/src/main/java/org/briarproject/bramble/BrambleAndroidModule.java @@ -7,6 +7,7 @@ import org.briarproject.bramble.reporting.ReportingModule; import org.briarproject.bramble.socks.SocksModule; import org.briarproject.bramble.system.AndroidSystemModule; import org.briarproject.bramble.system.AndroidTaskSchedulerModule; +import org.briarproject.bramble.system.AndroidWakefulIoExecutorModule; import dagger.Module; @@ -15,6 +16,7 @@ import dagger.Module; AndroidNetworkModule.class, AndroidSystemModule.class, AndroidTaskSchedulerModule.class, + AndroidWakefulIoExecutorModule.class, CircumventionModule.class, ReportingModule.class, SocksModule.class diff --git a/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothPlugin.java b/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothPlugin.java index 68df9233a..c59b45c03 100644 --- a/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothPlugin.java +++ b/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothPlugin.java @@ -79,6 +79,7 @@ class AndroidBluetoothPlugin AndroidBluetoothPlugin(BluetoothConnectionLimiter connectionLimiter, BluetoothConnectionFactory connectionFactory, Executor ioExecutor, + Executor wakefulIoExecutor, SecureRandom secureRandom, AndroidExecutor androidExecutor, Context appContext, @@ -87,8 +88,9 @@ class AndroidBluetoothPlugin PluginCallback callback, int maxLatency, int maxIdleTime) { - super(connectionLimiter, connectionFactory, ioExecutor, secureRandom, - backoff, callback, maxLatency, maxIdleTime); + super(connectionLimiter, connectionFactory, ioExecutor, + wakefulIoExecutor, secureRandom, backoff, callback, + maxLatency, maxIdleTime); this.androidExecutor = androidExecutor; this.appContext = appContext; this.clock = clock; diff --git a/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothPluginFactory.java b/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothPluginFactory.java index ecd6b95b6..caeec30f9 100644 --- a/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothPluginFactory.java +++ b/bramble-android/src/main/java/org/briarproject/bramble/plugin/bluetooth/AndroidBluetoothPluginFactory.java @@ -33,7 +33,7 @@ public class AndroidBluetoothPluginFactory implements DuplexPluginFactory { private static final int MAX_POLLING_INTERVAL = 10 * 60 * 1000; // 10 mins private static final double BACKOFF_BASE = 1.2; - private final Executor ioExecutor; + private final Executor ioExecutor, wakefulIoExecutor; private final AndroidExecutor androidExecutor; private final AndroidWakeLockManager wakeLockManager; private final Context appContext; @@ -44,6 +44,7 @@ public class AndroidBluetoothPluginFactory implements DuplexPluginFactory { private final BackoffFactory backoffFactory; public AndroidBluetoothPluginFactory(Executor ioExecutor, + Executor wakefulIoExecutor, AndroidExecutor androidExecutor, AndroidWakeLockManager wakeLockManager, Context appContext, @@ -53,6 +54,7 @@ public class AndroidBluetoothPluginFactory implements DuplexPluginFactory { TimeoutMonitor timeoutMonitor, BackoffFactory backoffFactory) { this.ioExecutor = ioExecutor; + this.wakefulIoExecutor = wakefulIoExecutor; this.androidExecutor = androidExecutor; this.wakeLockManager = wakeLockManager; this.appContext = appContext; @@ -83,9 +85,9 @@ public class AndroidBluetoothPluginFactory implements DuplexPluginFactory { Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL, MAX_POLLING_INTERVAL, BACKOFF_BASE); AndroidBluetoothPlugin plugin = new AndroidBluetoothPlugin( - connectionLimiter, connectionFactory, ioExecutor, secureRandom, - androidExecutor, appContext, clock, backoff, callback, - MAX_LATENCY, MAX_IDLE_TIME); + connectionLimiter, connectionFactory, ioExecutor, + wakefulIoExecutor, secureRandom, androidExecutor, appContext, + clock, backoff, callback, MAX_LATENCY, MAX_IDLE_TIME); eventBus.addListener(plugin); return plugin; } diff --git a/bramble-android/src/main/java/org/briarproject/bramble/plugin/tcp/AndroidLanTcpPlugin.java b/bramble-android/src/main/java/org/briarproject/bramble/plugin/tcp/AndroidLanTcpPlugin.java index 84d58c61e..6faeef1b1 100644 --- a/bramble-android/src/main/java/org/briarproject/bramble/plugin/tcp/AndroidLanTcpPlugin.java +++ b/bramble-android/src/main/java/org/briarproject/bramble/plugin/tcp/AndroidLanTcpPlugin.java @@ -61,11 +61,16 @@ class AndroidLanTcpPlugin extends LanTcpPlugin { private volatile SocketFactory socketFactory; - AndroidLanTcpPlugin(Executor ioExecutor, Context appContext, - Backoff backoff, PluginCallback callback, int maxLatency, - int maxIdleTime, int connectionTimeout) { - super(ioExecutor, backoff, callback, maxLatency, maxIdleTime, - connectionTimeout); + AndroidLanTcpPlugin(Executor ioExecutor, + Executor wakefulIoExecutor, + Context appContext, + Backoff backoff, + PluginCallback callback, + 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 connectionStatusExecutor = new PoliteExecutor("AndroidLanTcpPlugin", ioExecutor, 1); diff --git a/bramble-android/src/main/java/org/briarproject/bramble/plugin/tcp/AndroidLanTcpPluginFactory.java b/bramble-android/src/main/java/org/briarproject/bramble/plugin/tcp/AndroidLanTcpPluginFactory.java index 6326dfc60..aea720458 100644 --- a/bramble-android/src/main/java/org/briarproject/bramble/plugin/tcp/AndroidLanTcpPluginFactory.java +++ b/bramble-android/src/main/java/org/briarproject/bramble/plugin/tcp/AndroidLanTcpPluginFactory.java @@ -28,14 +28,18 @@ public class AndroidLanTcpPluginFactory implements DuplexPluginFactory { private static final int MAX_POLLING_INTERVAL = 600_000; // 10 mins private static final double BACKOFF_BASE = 1.2; - private final Executor ioExecutor; + private final Executor ioExecutor, wakefulIoExecutor; private final EventBus eventBus; private final BackoffFactory backoffFactory; private final Context appContext; - public AndroidLanTcpPluginFactory(Executor ioExecutor, EventBus eventBus, - BackoffFactory backoffFactory, Context appContext) { + public AndroidLanTcpPluginFactory(Executor ioExecutor, + Executor wakefulIoExecutor, + EventBus eventBus, + BackoffFactory backoffFactory, + Context appContext) { this.ioExecutor = ioExecutor; + this.wakefulIoExecutor = wakefulIoExecutor; this.eventBus = eventBus; this.backoffFactory = backoffFactory; this.appContext = appContext; @@ -56,8 +60,8 @@ public class AndroidLanTcpPluginFactory implements DuplexPluginFactory { Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL, MAX_POLLING_INTERVAL, BACKOFF_BASE); AndroidLanTcpPlugin plugin = new AndroidLanTcpPlugin(ioExecutor, - appContext, backoff, callback, MAX_LATENCY, MAX_IDLE_TIME, - CONNECTION_TIMEOUT); + wakefulIoExecutor, appContext, backoff, callback, + MAX_LATENCY, MAX_IDLE_TIME, CONNECTION_TIMEOUT); eventBus.addListener(plugin); return plugin; } diff --git a/bramble-android/src/main/java/org/briarproject/bramble/plugin/tor/AndroidTorPlugin.java b/bramble-android/src/main/java/org/briarproject/bramble/plugin/tor/AndroidTorPlugin.java index 79e35151c..9558e85e6 100644 --- a/bramble-android/src/main/java/org/briarproject/bramble/plugin/tor/AndroidTorPlugin.java +++ b/bramble-android/src/main/java/org/briarproject/bramble/plugin/tor/AndroidTorPlugin.java @@ -32,6 +32,7 @@ class AndroidTorPlugin extends TorPlugin { private final AndroidWakeLock wakeLock; AndroidTorPlugin(Executor ioExecutor, + Executor wakefulIoExecutor, Context appContext, NetworkManager networkManager, LocationUtils locationUtils, @@ -47,11 +48,11 @@ class AndroidTorPlugin extends TorPlugin { String architecture, int maxLatency, int maxIdleTime) { - super(ioExecutor, networkManager, locationUtils, torSocketFactory, - clock, resourceProvider, circumventionProvider, batteryManager, - backoff, torRendezvousCrypto, callback, architecture, - maxLatency, maxIdleTime, - appContext.getDir("tor", MODE_PRIVATE)); + super(ioExecutor, wakefulIoExecutor, networkManager, locationUtils, + torSocketFactory, clock, resourceProvider, + circumventionProvider, batteryManager, backoff, + torRendezvousCrypto, callback, architecture, maxLatency, + maxIdleTime, appContext.getDir("tor", MODE_PRIVATE)); this.appContext = appContext; wakeLock = wakeLockManager.createWakeLock("TorPlugin"); } diff --git a/bramble-android/src/main/java/org/briarproject/bramble/plugin/tor/AndroidTorPluginFactory.java b/bramble-android/src/main/java/org/briarproject/bramble/plugin/tor/AndroidTorPluginFactory.java index 8f2b936ee..8103700f5 100644 --- a/bramble-android/src/main/java/org/briarproject/bramble/plugin/tor/AndroidTorPluginFactory.java +++ b/bramble-android/src/main/java/org/briarproject/bramble/plugin/tor/AndroidTorPluginFactory.java @@ -38,7 +38,7 @@ public class AndroidTorPluginFactory implements DuplexPluginFactory { private static final int MAX_POLLING_INTERVAL = 10 * 60 * 1000; // 10 mins private static final double BACKOFF_BASE = 1.2; - private final Executor ioExecutor; + private final Executor ioExecutor, wakefulIoExecutor; private final Context appContext; private final NetworkManager networkManager; private final LocationUtils locationUtils; @@ -52,6 +52,7 @@ public class AndroidTorPluginFactory implements DuplexPluginFactory { private final Clock clock; public AndroidTorPluginFactory(Executor ioExecutor, + Executor wakefulIoExecutor, Context appContext, NetworkManager networkManager, LocationUtils locationUtils, @@ -64,6 +65,7 @@ public class AndroidTorPluginFactory implements DuplexPluginFactory { AndroidWakeLockManager wakeLockManager, Clock clock) { this.ioExecutor = ioExecutor; + this.wakefulIoExecutor = wakefulIoExecutor; this.appContext = appContext; this.networkManager = networkManager; this.locationUtils = locationUtils; @@ -118,10 +120,11 @@ public class AndroidTorPluginFactory implements DuplexPluginFactory { MAX_POLLING_INTERVAL, BACKOFF_BASE); TorRendezvousCrypto torRendezvousCrypto = new TorRendezvousCryptoImpl(); AndroidTorPlugin plugin = new AndroidTorPlugin(ioExecutor, - appContext, networkManager, locationUtils, torSocketFactory, - clock, resourceProvider, circumventionProvider, batteryManager, - wakeLockManager, backoff, torRendezvousCrypto, callback, - architecture, MAX_LATENCY, MAX_IDLE_TIME); + wakefulIoExecutor, appContext, networkManager, locationUtils, + torSocketFactory, clock, resourceProvider, + circumventionProvider, batteryManager, wakeLockManager, + backoff, torRendezvousCrypto, callback, architecture, + MAX_LATENCY, MAX_IDLE_TIME); eventBus.addListener(plugin); return plugin; } diff --git a/bramble-android/src/main/java/org/briarproject/bramble/system/AndroidWakefulIoExecutorModule.java b/bramble-android/src/main/java/org/briarproject/bramble/system/AndroidWakefulIoExecutorModule.java new file mode 100644 index 000000000..994b50483 --- /dev/null +++ b/bramble-android/src/main/java/org/briarproject/bramble/system/AndroidWakefulIoExecutorModule.java @@ -0,0 +1,23 @@ +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"); + } +} diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/system/WakefulIoExecutor.java b/bramble-api/src/main/java/org/briarproject/bramble/api/system/WakefulIoExecutor.java new file mode 100644 index 000000000..517833807 --- /dev/null +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/system/WakefulIoExecutor.java @@ -0,0 +1,26 @@ +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. + *

+ * 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 { +} \ No newline at end of file diff --git a/bramble-core/src/main/java/org/briarproject/bramble/plugin/PollerImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/plugin/PollerImpl.java index 361ac244b..439e78efb 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/plugin/PollerImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/plugin/PollerImpl.java @@ -27,6 +27,7 @@ import org.briarproject.bramble.api.properties.TransportProperties; import org.briarproject.bramble.api.properties.TransportPropertyManager; import org.briarproject.bramble.api.system.Clock; import org.briarproject.bramble.api.system.TaskScheduler; +import org.briarproject.bramble.api.system.WakefulIoExecutor; import java.security.SecureRandom; import java.util.ArrayList; @@ -56,7 +57,7 @@ class PollerImpl implements Poller, EventListener { private static final Logger LOG = getLogger(PollerImpl.class.getName()); - private final Executor ioExecutor; + private final Executor wakefulIoExecutor; private final TaskScheduler scheduler; private final ConnectionManager connectionManager; private final ConnectionRegistry connectionRegistry; @@ -69,7 +70,7 @@ class PollerImpl implements Poller, EventListener { private final Map tasks; @Inject - PollerImpl(@IoExecutor Executor ioExecutor, + PollerImpl(@WakefulIoExecutor Executor wakefulIoExecutor, TaskScheduler scheduler, ConnectionManager connectionManager, ConnectionRegistry connectionRegistry, @@ -77,7 +78,7 @@ class PollerImpl implements Poller, EventListener { TransportPropertyManager transportPropertyManager, SecureRandom random, Clock clock) { - this.ioExecutor = ioExecutor; + this.wakefulIoExecutor = wakefulIoExecutor; this.scheduler = scheduler; this.connectionManager = connectionManager; this.connectionRegistry = connectionRegistry; @@ -118,7 +119,6 @@ class PollerImpl implements Poller, EventListener { } } - // TODO: Make this wakeful private void connectToContact(ContactId c) { for (SimplexPlugin s : pluginManager.getSimplexPlugins()) if (s.shouldPoll()) connectToContact(c, s); @@ -135,7 +135,7 @@ class PollerImpl implements Poller, EventListener { } private void connectToContact(ContactId c, SimplexPlugin p) { - ioExecutor.execute(() -> { + wakefulIoExecutor.execute(() -> { TransportId t = p.getId(); if (connectionRegistry.isConnected(c, t)) return; try { @@ -151,7 +151,7 @@ class PollerImpl implements Poller, EventListener { } private void connectToContact(ContactId c, DuplexPlugin p) { - ioExecutor.execute(() -> { + wakefulIoExecutor.execute(() -> { TransportId t = p.getId(); if (connectionRegistry.isConnected(c, t)) return; try { @@ -190,8 +190,8 @@ class PollerImpl implements Poller, EventListener { // it will abort safely when it finds it's been replaced if (scheduled != null) scheduled.future.cancel(false); PollTask task = new PollTask(p, due, randomiseNext); - Future future = scheduler.schedule(task, ioExecutor, delay, - MILLISECONDS); + Future future = scheduler.schedule(task, wakefulIoExecutor, + delay, MILLISECONDS); tasks.put(t, new ScheduledPollTask(task, future)); } } finally { diff --git a/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothPlugin.java b/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothPlugin.java index 160368152..4929c7f15 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothPlugin.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothPlugin.java @@ -77,7 +77,7 @@ abstract class BluetoothPlugin implements DuplexPlugin, EventListener { final BluetoothConnectionLimiter connectionLimiter; final BluetoothConnectionFactory connectionFactory; - private final Executor ioExecutor; + private final Executor ioExecutor, wakefulIoExecutor; private final SecureRandom secureRandom; private final Backoff backoff; private final PluginCallback callback; @@ -124,6 +124,7 @@ abstract class BluetoothPlugin implements DuplexPlugin, EventListener { BluetoothPlugin(BluetoothConnectionLimiter connectionLimiter, BluetoothConnectionFactory connectionFactory, Executor ioExecutor, + Executor wakefulIoExecutor, SecureRandom secureRandom, Backoff backoff, PluginCallback callback, @@ -132,6 +133,7 @@ abstract class BluetoothPlugin implements DuplexPlugin, EventListener { this.connectionLimiter = connectionLimiter; this.connectionFactory = connectionFactory; this.ioExecutor = ioExecutor; + this.wakefulIoExecutor = wakefulIoExecutor; this.secureRandom = secureRandom; this.backoff = backoff; this.callback = callback; @@ -355,7 +357,7 @@ abstract class BluetoothPlugin implements DuplexPlugin, EventListener { if (isNullOrEmpty(address)) return; String uuid = p.get(PROP_UUID); if (isNullOrEmpty(uuid)) return; - ioExecutor.execute(() -> { + wakefulIoExecutor.execute(() -> { DuplexTransportConnection d = createConnection(p); if (d != null) { backoff.reset(); diff --git a/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/LanTcpPlugin.java b/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/LanTcpPlugin.java index 800643c75..87706b7e5 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/LanTcpPlugin.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/LanTcpPlugin.java @@ -88,10 +88,15 @@ class LanTcpPlugin extends TcpPlugin { } } - LanTcpPlugin(Executor ioExecutor, Backoff backoff, PluginCallback callback, - int maxLatency, int maxIdleTime, int connectionTimeout) { - super(ioExecutor, backoff, callback, maxLatency, maxIdleTime, - connectionTimeout); + LanTcpPlugin(Executor ioExecutor, + Executor wakefulIoExecutor, + Backoff backoff, + PluginCallback callback, + int maxLatency, + int maxIdleTime, + int connectionTimeout) { + super(ioExecutor, wakefulIoExecutor, backoff, callback, maxLatency, + maxIdleTime, connectionTimeout); } @Override diff --git a/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/LanTcpPluginFactory.java b/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/LanTcpPluginFactory.java index 6528a6571..c93afcf54 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/LanTcpPluginFactory.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/LanTcpPluginFactory.java @@ -26,13 +26,16 @@ public class LanTcpPluginFactory implements DuplexPluginFactory { private static final int MAX_POLLING_INTERVAL = 600_000; // 10 mins private static final double BACKOFF_BASE = 1.2; - private final Executor ioExecutor; + private final Executor ioExecutor, wakefulIoExecutor; private final EventBus eventBus; private final BackoffFactory backoffFactory; - public LanTcpPluginFactory(Executor ioExecutor, EventBus eventBus, + public LanTcpPluginFactory(Executor ioExecutor, + Executor wakefulIoExecutor, + EventBus eventBus, BackoffFactory backoffFactory) { this.ioExecutor = ioExecutor; + this.wakefulIoExecutor = wakefulIoExecutor; this.eventBus = eventBus; this.backoffFactory = backoffFactory; } @@ -51,8 +54,9 @@ public class LanTcpPluginFactory implements DuplexPluginFactory { public DuplexPlugin createPlugin(PluginCallback callback) { Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL, MAX_POLLING_INTERVAL, BACKOFF_BASE); - LanTcpPlugin plugin = new LanTcpPlugin(ioExecutor, backoff, callback, MAX_LATENCY, - MAX_IDLE_TIME, CONNECTION_TIMEOUT); + LanTcpPlugin plugin = new LanTcpPlugin(ioExecutor, wakefulIoExecutor, + backoff, callback, MAX_LATENCY, MAX_IDLE_TIME, + CONNECTION_TIMEOUT); eventBus.addListener(plugin); return plugin; } diff --git a/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/TcpPlugin.java b/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/TcpPlugin.java index 2130c69cc..f381832dc 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/TcpPlugin.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/TcpPlugin.java @@ -66,7 +66,7 @@ abstract class TcpPlugin implements DuplexPlugin, EventListener { private static final Pattern DOTTED_QUAD = Pattern.compile("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$"); - protected final Executor ioExecutor, bindExecutor; + protected final Executor ioExecutor, wakefulIoExecutor, bindExecutor; protected final Backoff backoff; protected final PluginCallback callback; protected final int maxLatency, maxIdleTime; @@ -107,9 +107,15 @@ abstract class TcpPlugin implements DuplexPlugin, EventListener { */ protected abstract boolean isEnabledByDefault(); - TcpPlugin(Executor ioExecutor, Backoff backoff, PluginCallback callback, - int maxLatency, int maxIdleTime, int connectionTimeout) { + TcpPlugin(Executor ioExecutor, + Executor wakefulIoExecutor, + Backoff backoff, + PluginCallback callback, + int maxLatency, + int maxIdleTime, + int connectionTimeout) { this.ioExecutor = ioExecutor; + this.wakefulIoExecutor = wakefulIoExecutor; this.backoff = backoff; this.callback = callback; this.maxLatency = maxLatency; @@ -245,7 +251,7 @@ abstract class TcpPlugin implements DuplexPlugin, EventListener { } private void connect(TransportProperties p, ConnectionHandler h) { - ioExecutor.execute(() -> { + wakefulIoExecutor.execute(() -> { DuplexTransportConnection d = createConnection(p); if (d != null) { backoff.reset(); diff --git a/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/WanTcpPlugin.java b/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/WanTcpPlugin.java index d466e87b0..64bc90517 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/WanTcpPlugin.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/WanTcpPlugin.java @@ -30,11 +30,16 @@ class WanTcpPlugin extends TcpPlugin { private volatile MappingResult mappingResult; - WanTcpPlugin(Executor ioExecutor, Backoff backoff, PortMapper portMapper, - PluginCallback callback, int maxLatency, int maxIdleTime, + WanTcpPlugin(Executor ioExecutor, + Executor wakefulIoExecutor, + Backoff backoff, + PortMapper portMapper, + PluginCallback callback, + int maxLatency, + int maxIdleTime, int connectionTimeout) { - super(ioExecutor, backoff, callback, maxLatency, maxIdleTime, - connectionTimeout); + super(ioExecutor, wakefulIoExecutor, backoff, callback, maxLatency, + maxIdleTime, connectionTimeout); this.portMapper = portMapper; } diff --git a/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/WanTcpPluginFactory.java b/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/WanTcpPluginFactory.java index db0f2c2a8..9b6459db8 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/WanTcpPluginFactory.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/WanTcpPluginFactory.java @@ -27,14 +27,18 @@ public class WanTcpPluginFactory implements DuplexPluginFactory { private static final int MAX_POLLING_INTERVAL = 600_000; // 10 mins private static final double BACKOFF_BASE = 1.2; - private final Executor ioExecutor; + private final Executor ioExecutor, wakefulIoExecutor; private final EventBus eventBus; private final BackoffFactory backoffFactory; private final ShutdownManager shutdownManager; - public WanTcpPluginFactory(Executor ioExecutor, EventBus eventBus, - BackoffFactory backoffFactory, ShutdownManager shutdownManager) { + public WanTcpPluginFactory(Executor ioExecutor, + Executor wakefulIoExecutor, + EventBus eventBus, + BackoffFactory backoffFactory, + ShutdownManager shutdownManager) { this.ioExecutor = ioExecutor; + this.wakefulIoExecutor = wakefulIoExecutor; this.eventBus = eventBus; this.backoffFactory = backoffFactory; this.shutdownManager = shutdownManager; @@ -54,9 +58,10 @@ public class WanTcpPluginFactory implements DuplexPluginFactory { public DuplexPlugin createPlugin(PluginCallback callback) { Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL, MAX_POLLING_INTERVAL, BACKOFF_BASE); - WanTcpPlugin plugin = new WanTcpPlugin(ioExecutor, backoff, - new PortMapperImpl(shutdownManager), callback, MAX_LATENCY, - MAX_IDLE_TIME, CONNECTION_TIMEOUT); + PortMapper portMapper = new PortMapperImpl(shutdownManager); + WanTcpPlugin plugin = new WanTcpPlugin(ioExecutor, wakefulIoExecutor, + backoff, portMapper, callback, MAX_LATENCY, MAX_IDLE_TIME, + CONNECTION_TIMEOUT); eventBus.addListener(plugin); return plugin; } diff --git a/bramble-core/src/main/java/org/briarproject/bramble/plugin/tor/TorPlugin.java b/bramble-core/src/main/java/org/briarproject/bramble/plugin/tor/TorPlugin.java index dbe63e774..24816197b 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/plugin/tor/TorPlugin.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/plugin/tor/TorPlugin.java @@ -118,7 +118,8 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener { private static final Pattern ONION_V2 = Pattern.compile("[a-z2-7]{16}"); private static final Pattern ONION_V3 = Pattern.compile("[a-z2-7]{56}"); - private final Executor ioExecutor, connectionStatusExecutor; + private final Executor ioExecutor, wakefulIoExecutor; + private final Executor connectionStatusExecutor; private final NetworkManager networkManager; private final LocationUtils locationUtils; private final SocketFactory torSocketFactory; @@ -145,15 +146,24 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener { protected abstract long getLastUpdateTime(); - TorPlugin(Executor ioExecutor, NetworkManager networkManager, - LocationUtils locationUtils, SocketFactory torSocketFactory, - Clock clock, ResourceProvider resourceProvider, + TorPlugin(Executor ioExecutor, + Executor wakefulIoExecutor, + NetworkManager networkManager, + LocationUtils locationUtils, + SocketFactory torSocketFactory, + Clock clock, + ResourceProvider resourceProvider, CircumventionProvider circumventionProvider, - BatteryManager batteryManager, Backoff backoff, + BatteryManager batteryManager, + Backoff backoff, TorRendezvousCrypto torRendezvousCrypto, - PluginCallback callback, String architecture, int maxLatency, - int maxIdleTime, File torDirectory) { + PluginCallback callback, + String architecture, + int maxLatency, + int maxIdleTime, + File torDirectory) { this.ioExecutor = ioExecutor; + this.wakefulIoExecutor = wakefulIoExecutor; this.networkManager = networkManager; this.locationUtils = locationUtils; this.torSocketFactory = torSocketFactory; @@ -620,7 +630,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener { } private void connect(TransportProperties p, ConnectionHandler h) { - ioExecutor.execute(() -> { + wakefulIoExecutor.execute(() -> { DuplexTransportConnection d = createConnection(p); if (d != null) { backoff.reset(); diff --git a/bramble-core/src/main/java/org/briarproject/bramble/system/DefaultWakefulIoExecutorModule.java b/bramble-core/src/main/java/org/briarproject/bramble/system/DefaultWakefulIoExecutorModule.java new file mode 100644 index 000000000..5f78b2d88 --- /dev/null +++ b/bramble-core/src/main/java/org/briarproject/bramble/system/DefaultWakefulIoExecutorModule.java @@ -0,0 +1,23 @@ +package org.briarproject.bramble.system; + +import org.briarproject.bramble.api.lifecycle.IoExecutor; +import org.briarproject.bramble.api.system.WakefulIoExecutor; + +import java.util.concurrent.Executor; + +import dagger.Module; +import dagger.Provides; + +/** + * Provides a default implementation of {@link WakefulIoExecutor} for systems + * without wake locks. + */ +@Module +public class DefaultWakefulIoExecutorModule { + + @Provides + @WakefulIoExecutor + Executor provideWakefulIoExecutor(@IoExecutor Executor ioExecutor) { + return ioExecutor; + } +} diff --git a/bramble-core/src/test/java/org/briarproject/bramble/plugin/PollerImplTest.java b/bramble-core/src/test/java/org/briarproject/bramble/plugin/PollerImplTest.java index 99fb4b4ec..5d5db5497 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/plugin/PollerImplTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/plugin/PollerImplTest.java @@ -58,7 +58,7 @@ public class PollerImplTest extends BrambleMockTestCase { private final Future future = context.mock(Future.class); private final SecureRandom random; - private final Executor ioExecutor = new ImmediateExecutor(); + private final Executor wakefulIoExecutor = new ImmediateExecutor(); private final TransportId transportId = getTransportId(); private final ContactId contactId = getContactId(); private final TransportProperties properties = new TransportProperties(); @@ -74,7 +74,7 @@ public class PollerImplTest extends BrambleMockTestCase { @Before public void setUp() { - poller = new PollerImpl(ioExecutor, scheduler, connectionManager, + poller = new PollerImpl(wakefulIoExecutor, scheduler, connectionManager, connectionRegistry, pluginManager, transportPropertyManager, random, clock); } @@ -234,7 +234,7 @@ public class PollerImplTest extends BrambleMockTestCase { oneOf(clock).currentTimeMillis(); will(returnValue(now)); oneOf(scheduler).schedule(with(any(Runnable.class)), - with(ioExecutor), with((long) pollingInterval), + with(wakefulIoExecutor), with((long) pollingInterval), with(MILLISECONDS)); will(returnValue(future)); }}); @@ -263,7 +263,7 @@ public class PollerImplTest extends BrambleMockTestCase { oneOf(clock).currentTimeMillis(); will(returnValue(now)); oneOf(scheduler).schedule(with(any(Runnable.class)), - with(ioExecutor), with((long) pollingInterval), + with(wakefulIoExecutor), with((long) pollingInterval), with(MILLISECONDS)); will(returnValue(future)); // Second event @@ -306,7 +306,7 @@ public class PollerImplTest extends BrambleMockTestCase { oneOf(clock).currentTimeMillis(); will(returnValue(now)); oneOf(scheduler).schedule(with(any(Runnable.class)), - with(ioExecutor), with((long) pollingInterval), + with(wakefulIoExecutor), with((long) pollingInterval), with(MILLISECONDS)); will(returnValue(future)); // Second event @@ -323,7 +323,7 @@ public class PollerImplTest extends BrambleMockTestCase { will(returnValue(now + 1)); oneOf(future).cancel(false); oneOf(scheduler).schedule(with(any(Runnable.class)), - with(ioExecutor), with((long) pollingInterval - 2), + with(wakefulIoExecutor), with((long) pollingInterval - 2), with(MILLISECONDS)); }}); @@ -350,7 +350,7 @@ public class PollerImplTest extends BrambleMockTestCase { oneOf(clock).currentTimeMillis(); will(returnValue(now)); oneOf(scheduler).schedule(with(any(Runnable.class)), - with(ioExecutor), with(0L), with(MILLISECONDS)); + with(wakefulIoExecutor), with(0L), with(MILLISECONDS)); will(returnValue(future)); will(new RunAction()); // Running the polling task schedules the next polling task @@ -361,7 +361,7 @@ public class PollerImplTest extends BrambleMockTestCase { oneOf(clock).currentTimeMillis(); will(returnValue(now)); oneOf(scheduler).schedule(with(any(Runnable.class)), - with(ioExecutor), with((long) (pollingInterval * 0.5)), + with(wakefulIoExecutor), with((long) (pollingInterval * 0.5)), with(MILLISECONDS)); will(returnValue(future)); // Get the transport properties and connected contacts @@ -394,7 +394,7 @@ public class PollerImplTest extends BrambleMockTestCase { oneOf(clock).currentTimeMillis(); will(returnValue(now)); oneOf(scheduler).schedule(with(any(Runnable.class)), - with(ioExecutor), with(0L), with(MILLISECONDS)); + with(wakefulIoExecutor), with(0L), with(MILLISECONDS)); will(returnValue(future)); will(new RunAction()); // Running the polling task schedules the next polling task @@ -405,7 +405,7 @@ public class PollerImplTest extends BrambleMockTestCase { oneOf(clock).currentTimeMillis(); will(returnValue(now)); oneOf(scheduler).schedule(with(any(Runnable.class)), - with(ioExecutor), with((long) (pollingInterval * 0.5)), + with(wakefulIoExecutor), with((long) (pollingInterval * 0.5)), with(MILLISECONDS)); will(returnValue(future)); // Get the transport properties and connected contacts @@ -436,7 +436,7 @@ public class PollerImplTest extends BrambleMockTestCase { oneOf(clock).currentTimeMillis(); will(returnValue(now)); oneOf(scheduler).schedule(with(any(Runnable.class)), - with(ioExecutor), with(0L), with(MILLISECONDS)); + with(wakefulIoExecutor), with(0L), with(MILLISECONDS)); will(returnValue(future)); // The plugin is deactivated before the task runs - cancel the task oneOf(future).cancel(false); @@ -460,7 +460,7 @@ public class PollerImplTest extends BrambleMockTestCase { oneOf(clock).currentTimeMillis(); will(returnValue(now)); oneOf(scheduler).schedule(with(any(Runnable.class)), - with(ioExecutor), with((long) pollingInterval), + with(wakefulIoExecutor), with((long) pollingInterval), with(MILLISECONDS)); will(returnValue(future)); }}); diff --git a/bramble-core/src/test/java/org/briarproject/bramble/plugin/tcp/LanTcpPluginTest.java b/bramble-core/src/test/java/org/briarproject/bramble/plugin/tcp/LanTcpPluginTest.java index 909a2f858..cd560e3a6 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/plugin/tcp/LanTcpPluginTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/plugin/tcp/LanTcpPluginTest.java @@ -53,7 +53,8 @@ public class LanTcpPluginTest extends BrambleTestCase { @Before public void setUp() { callback = new Callback(); - plugin = new LanTcpPlugin(ioExecutor, backoff, callback, 0, 0, 1000) { + plugin = new LanTcpPlugin(ioExecutor, ioExecutor, backoff, callback, + 0, 0, 1000) { @Override protected boolean canConnectToOwnAddress() { return true; diff --git a/bramble-core/src/test/java/org/briarproject/bramble/test/BrambleCoreIntegrationTestModule.java b/bramble-core/src/test/java/org/briarproject/bramble/test/BrambleCoreIntegrationTestModule.java index 184f5f6aa..8b631802e 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/test/BrambleCoreIntegrationTestModule.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/test/BrambleCoreIntegrationTestModule.java @@ -4,6 +4,7 @@ import org.briarproject.bramble.api.FeatureFlags; import org.briarproject.bramble.battery.DefaultBatteryManagerModule; import org.briarproject.bramble.event.DefaultEventExecutorModule; import org.briarproject.bramble.system.DefaultTaskSchedulerModule; +import org.briarproject.bramble.system.DefaultWakefulIoExecutorModule; import dagger.Module; import dagger.Provides; @@ -12,6 +13,7 @@ import dagger.Provides; DefaultBatteryManagerModule.class, DefaultEventExecutorModule.class, DefaultTaskSchedulerModule.class, + DefaultWakefulIoExecutorModule.class, TestDatabaseConfigModule.class, TestPluginConfigModule.class, TestSecureRandomModule.class diff --git a/bramble-java/src/main/java/org/briarproject/bramble/plugin/DesktopPluginModule.java b/bramble-java/src/main/java/org/briarproject/bramble/plugin/DesktopPluginModule.java index f7a5168b9..9a7cf3940 100644 --- a/bramble-java/src/main/java/org/briarproject/bramble/plugin/DesktopPluginModule.java +++ b/bramble-java/src/main/java/org/briarproject/bramble/plugin/DesktopPluginModule.java @@ -13,6 +13,7 @@ import org.briarproject.bramble.api.plugin.TransportId; import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory; import org.briarproject.bramble.api.plugin.simplex.SimplexPluginFactory; import org.briarproject.bramble.api.reliability.ReliabilityLayerFactory; +import org.briarproject.bramble.api.system.WakefulIoExecutor; import org.briarproject.bramble.plugin.bluetooth.JavaBluetoothPluginFactory; import org.briarproject.bramble.plugin.modem.ModemPluginFactory; import org.briarproject.bramble.plugin.tcp.LanTcpPluginFactory; @@ -37,18 +38,22 @@ public class DesktopPluginModule extends PluginModule { @Provides PluginConfig getPluginConfig(@IoExecutor Executor ioExecutor, - SecureRandom random, BackoffFactory backoffFactory, + @WakefulIoExecutor Executor wakefulIoExecutor, + SecureRandom random, + BackoffFactory backoffFactory, ReliabilityLayerFactory reliabilityFactory, - ShutdownManager shutdownManager, EventBus eventBus, + ShutdownManager shutdownManager, + EventBus eventBus, TimeoutMonitor timeoutMonitor) { DuplexPluginFactory bluetooth = new JavaBluetoothPluginFactory( - ioExecutor, random, eventBus, timeoutMonitor, backoffFactory); + ioExecutor, wakefulIoExecutor, random, eventBus, + timeoutMonitor, backoffFactory); DuplexPluginFactory modem = new ModemPluginFactory(ioExecutor, reliabilityFactory); - DuplexPluginFactory lan = new LanTcpPluginFactory(ioExecutor, eventBus, - backoffFactory); - DuplexPluginFactory wan = new WanTcpPluginFactory(ioExecutor, eventBus, - backoffFactory, shutdownManager); + DuplexPluginFactory lan = new LanTcpPluginFactory(ioExecutor, + wakefulIoExecutor, eventBus, backoffFactory); + DuplexPluginFactory wan = new WanTcpPluginFactory(ioExecutor, + wakefulIoExecutor, eventBus, backoffFactory, shutdownManager); Collection duplex = asList(bluetooth, modem, lan, wan); @NotNullByDefault diff --git a/bramble-java/src/main/java/org/briarproject/bramble/plugin/bluetooth/JavaBluetoothPlugin.java b/bramble-java/src/main/java/org/briarproject/bramble/plugin/bluetooth/JavaBluetoothPlugin.java index 9115900ad..ccc93fb2c 100644 --- a/bramble-java/src/main/java/org/briarproject/bramble/plugin/bluetooth/JavaBluetoothPlugin.java +++ b/bramble-java/src/main/java/org/briarproject/bramble/plugin/bluetooth/JavaBluetoothPlugin.java @@ -36,10 +36,16 @@ class JavaBluetoothPlugin JavaBluetoothPlugin(BluetoothConnectionLimiter connectionManager, BluetoothConnectionFactory connectionFactory, - Executor ioExecutor, SecureRandom secureRandom, Backoff backoff, - PluginCallback callback, int maxLatency, int maxIdleTime) { - super(connectionManager, connectionFactory, ioExecutor, secureRandom, - backoff, callback, maxLatency, maxIdleTime); + Executor ioExecutor, + Executor wakefulIoExecutor, + SecureRandom secureRandom, + Backoff backoff, + PluginCallback callback, + int maxLatency, + int maxIdleTime) { + super(connectionManager, connectionFactory, ioExecutor, + wakefulIoExecutor, secureRandom, backoff, callback, + maxLatency, maxIdleTime); } @Override diff --git a/bramble-java/src/main/java/org/briarproject/bramble/plugin/bluetooth/JavaBluetoothPluginFactory.java b/bramble-java/src/main/java/org/briarproject/bramble/plugin/bluetooth/JavaBluetoothPluginFactory.java index 4f1bb93fd..6a1b97295 100644 --- a/bramble-java/src/main/java/org/briarproject/bramble/plugin/bluetooth/JavaBluetoothPluginFactory.java +++ b/bramble-java/src/main/java/org/briarproject/bramble/plugin/bluetooth/JavaBluetoothPluginFactory.java @@ -28,16 +28,20 @@ public class JavaBluetoothPluginFactory implements DuplexPluginFactory { private static final int MAX_POLLING_INTERVAL = 10 * 60 * 1000; // 10 mins private static final double BACKOFF_BASE = 1.2; - private final Executor ioExecutor; + private final Executor ioExecutor, wakefulIoExecutor; private final SecureRandom secureRandom; private final EventBus eventBus; private final TimeoutMonitor timeoutMonitor; private final BackoffFactory backoffFactory; public JavaBluetoothPluginFactory(Executor ioExecutor, - SecureRandom secureRandom, EventBus eventBus, - TimeoutMonitor timeoutMonitor, BackoffFactory backoffFactory) { + Executor wakefulIoExecutor, + SecureRandom secureRandom, + EventBus eventBus, + TimeoutMonitor timeoutMonitor, + BackoffFactory backoffFactory) { this.ioExecutor = ioExecutor; + this.wakefulIoExecutor = wakefulIoExecutor; this.secureRandom = secureRandom; this.eventBus = eventBus; this.timeoutMonitor = timeoutMonitor; @@ -64,8 +68,8 @@ public class JavaBluetoothPluginFactory implements DuplexPluginFactory { Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL, MAX_POLLING_INTERVAL, BACKOFF_BASE); JavaBluetoothPlugin plugin = new JavaBluetoothPlugin(connectionLimiter, - connectionFactory, ioExecutor, secureRandom, backoff, callback, - MAX_LATENCY, MAX_IDLE_TIME); + connectionFactory, ioExecutor, wakefulIoExecutor, secureRandom, + backoff, callback, MAX_LATENCY, MAX_IDLE_TIME); eventBus.addListener(plugin); return plugin; } diff --git a/bramble-java/src/main/java/org/briarproject/bramble/plugin/tor/JavaTorPlugin.java b/bramble-java/src/main/java/org/briarproject/bramble/plugin/tor/JavaTorPlugin.java index 9b213cba7..d9d5ddf89 100644 --- a/bramble-java/src/main/java/org/briarproject/bramble/plugin/tor/JavaTorPlugin.java +++ b/bramble-java/src/main/java/org/briarproject/bramble/plugin/tor/JavaTorPlugin.java @@ -20,17 +20,26 @@ import javax.net.SocketFactory; @NotNullByDefault abstract class JavaTorPlugin extends TorPlugin { - JavaTorPlugin(Executor ioExecutor, NetworkManager networkManager, - LocationUtils locationUtils, SocketFactory torSocketFactory, - Clock clock, ResourceProvider resourceProvider, + JavaTorPlugin(Executor ioExecutor, + Executor wakefulIoExecutor, + NetworkManager networkManager, + LocationUtils locationUtils, + SocketFactory torSocketFactory, + Clock clock, + ResourceProvider resourceProvider, CircumventionProvider circumventionProvider, - BatteryManager batteryManager, Backoff backoff, + BatteryManager batteryManager, + Backoff backoff, TorRendezvousCrypto torRendezvousCrypto, - PluginCallback callback, String architecture, int maxLatency, - int maxIdleTime, File torDirectory) { - super(ioExecutor, networkManager, locationUtils, torSocketFactory, - clock, resourceProvider, circumventionProvider, batteryManager, - backoff, torRendezvousCrypto, callback, architecture, + PluginCallback callback, + String architecture, + int maxLatency, + int maxIdleTime, + File torDirectory) { + super(ioExecutor, wakefulIoExecutor, networkManager, locationUtils, + torSocketFactory, clock, resourceProvider, + circumventionProvider, batteryManager, backoff, + torRendezvousCrypto, callback, architecture, maxLatency, maxIdleTime, torDirectory); } diff --git a/bramble-java/src/main/java/org/briarproject/bramble/plugin/tor/UnixTorPlugin.java b/bramble-java/src/main/java/org/briarproject/bramble/plugin/tor/UnixTorPlugin.java index e57ea83f3..f7ee9134e 100644 --- a/bramble-java/src/main/java/org/briarproject/bramble/plugin/tor/UnixTorPlugin.java +++ b/bramble-java/src/main/java/org/briarproject/bramble/plugin/tor/UnixTorPlugin.java @@ -20,17 +20,26 @@ import javax.net.SocketFactory; @NotNullByDefault class UnixTorPlugin extends JavaTorPlugin { - UnixTorPlugin(Executor ioExecutor, NetworkManager networkManager, - LocationUtils locationUtils, SocketFactory torSocketFactory, - Clock clock, ResourceProvider resourceProvider, + UnixTorPlugin(Executor ioExecutor, + Executor wakefulIoExecutor, + NetworkManager networkManager, + LocationUtils locationUtils, + SocketFactory torSocketFactory, + Clock clock, + ResourceProvider resourceProvider, CircumventionProvider circumventionProvider, - BatteryManager batteryManager, Backoff backoff, + BatteryManager batteryManager, + Backoff backoff, TorRendezvousCrypto torRendezvousCrypto, - PluginCallback callback, String architecture, int maxLatency, - int maxIdleTime, File torDirectory) { - super(ioExecutor, networkManager, locationUtils, torSocketFactory, - clock, resourceProvider, circumventionProvider, batteryManager, - backoff, torRendezvousCrypto, callback, architecture, + PluginCallback callback, + String architecture, + int maxLatency, + int maxIdleTime, + File torDirectory) { + super(ioExecutor, wakefulIoExecutor, networkManager, locationUtils, + torSocketFactory, clock, resourceProvider, + circumventionProvider, batteryManager, backoff, + torRendezvousCrypto, callback, architecture, maxLatency, maxIdleTime, torDirectory); } diff --git a/bramble-java/src/main/java/org/briarproject/bramble/plugin/tor/UnixTorPluginFactory.java b/bramble-java/src/main/java/org/briarproject/bramble/plugin/tor/UnixTorPluginFactory.java index 07cbead56..ee7df5512 100644 --- a/bramble-java/src/main/java/org/briarproject/bramble/plugin/tor/UnixTorPluginFactory.java +++ b/bramble-java/src/main/java/org/briarproject/bramble/plugin/tor/UnixTorPluginFactory.java @@ -38,7 +38,7 @@ public class UnixTorPluginFactory implements DuplexPluginFactory { private static final int MAX_POLLING_INTERVAL = 10 * 60 * 1000; // 10 mins private static final double BACKOFF_BASE = 1.2; - private final Executor ioExecutor; + private final Executor ioExecutor, wakefulIoExecutor; private final NetworkManager networkManager; private final LocationUtils locationUtils; private final EventBus eventBus; @@ -51,12 +51,19 @@ public class UnixTorPluginFactory implements DuplexPluginFactory { private final File torDirectory; public UnixTorPluginFactory(Executor ioExecutor, - NetworkManager networkManager, LocationUtils locationUtils, - EventBus eventBus, SocketFactory torSocketFactory, - BackoffFactory backoffFactory, ResourceProvider resourceProvider, + Executor wakefulIoExecutor, + NetworkManager networkManager, + LocationUtils locationUtils, + EventBus eventBus, + SocketFactory torSocketFactory, + BackoffFactory backoffFactory, + ResourceProvider resourceProvider, CircumventionProvider circumventionProvider, - BatteryManager batteryManager, Clock clock, File torDirectory) { + BatteryManager batteryManager, + Clock clock, + File torDirectory) { this.ioExecutor = ioExecutor; + this.wakefulIoExecutor = wakefulIoExecutor; this.networkManager = networkManager; this.locationUtils = locationUtils; this.eventBus = eventBus; @@ -97,11 +104,11 @@ public class UnixTorPluginFactory implements DuplexPluginFactory { Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL, MAX_POLLING_INTERVAL, BACKOFF_BASE); TorRendezvousCrypto torRendezvousCrypto = new TorRendezvousCryptoImpl(); - UnixTorPlugin plugin = new UnixTorPlugin(ioExecutor, networkManager, - locationUtils, torSocketFactory, clock, resourceProvider, - circumventionProvider, batteryManager, backoff, - torRendezvousCrypto, callback, architecture, MAX_LATENCY, - MAX_IDLE_TIME, torDirectory); + UnixTorPlugin plugin = new UnixTorPlugin(ioExecutor, wakefulIoExecutor, + networkManager, locationUtils, torSocketFactory, clock, + resourceProvider, circumventionProvider, batteryManager, + backoff, torRendezvousCrypto, callback, architecture, + MAX_LATENCY, MAX_IDLE_TIME, torDirectory); eventBus.addListener(plugin); return plugin; } diff --git a/bramble-java/src/test/java/org/briarproject/bramble/plugin/tor/BridgeTest.java b/bramble-java/src/test/java/org/briarproject/bramble/plugin/tor/BridgeTest.java index 78ce1eca1..c06715d8b 100644 --- a/bramble-java/src/test/java/org/briarproject/bramble/plugin/tor/BridgeTest.java +++ b/bramble-java/src/test/java/org/briarproject/bramble/plugin/tor/BridgeTest.java @@ -10,6 +10,7 @@ import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin; import org.briarproject.bramble.api.system.Clock; import org.briarproject.bramble.api.system.LocationUtils; import org.briarproject.bramble.api.system.ResourceProvider; +import org.briarproject.bramble.api.system.WakefulIoExecutor; import org.briarproject.bramble.test.BrambleJavaIntegrationTestComponent; import org.briarproject.bramble.test.BrambleTestCase; import org.briarproject.bramble.test.DaggerBrambleJavaIntegrationTestComponent; @@ -60,6 +61,9 @@ public class BridgeTest extends BrambleTestCase { @IoExecutor Executor ioExecutor; @Inject + @WakefulIoExecutor + Executor wakefulIoExecutor; + @Inject NetworkManager networkManager; @Inject ResourceProvider resourceProvider; @@ -121,10 +125,10 @@ public class BridgeTest extends BrambleTestCase { return singletonList(bridge); } }; - factory = new UnixTorPluginFactory(ioExecutor, networkManager, - locationUtils, eventBus, torSocketFactory, backoffFactory, - resourceProvider, bridgeProvider, batteryManager, clock, - torDir); + factory = new UnixTorPluginFactory(ioExecutor, wakefulIoExecutor, + networkManager, locationUtils, eventBus, torSocketFactory, + backoffFactory, resourceProvider, bridgeProvider, + batteryManager, clock, torDir); } @After diff --git a/briar-android/src/main/java/org/briarproject/briar/android/AppModule.java b/briar-android/src/main/java/org/briarproject/briar/android/AppModule.java index 4e094b782..d818ceb7d 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/AppModule.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/AppModule.java @@ -32,6 +32,7 @@ import org.briarproject.bramble.api.system.AndroidWakeLockManager; import org.briarproject.bramble.api.system.Clock; import org.briarproject.bramble.api.system.LocationUtils; import org.briarproject.bramble.api.system.ResourceProvider; +import org.briarproject.bramble.api.system.WakefulIoExecutor; import org.briarproject.bramble.plugin.bluetooth.AndroidBluetoothPluginFactory; import org.briarproject.bramble.plugin.tcp.AndroidLanTcpPluginFactory; import org.briarproject.bramble.plugin.tor.AndroidTorPluginFactory; @@ -124,6 +125,7 @@ public class AppModule { @Provides PluginConfig providePluginConfig(@IoExecutor Executor ioExecutor, + @WakefulIoExecutor Executor wakefulIoExecutor, AndroidExecutor androidExecutor, SecureRandom random, SocketFactory torSocketFactory, @@ -140,14 +142,15 @@ public class AppModule { TimeoutMonitor timeoutMonitor) { Context appContext = app.getApplicationContext(); DuplexPluginFactory bluetooth = new AndroidBluetoothPluginFactory( - ioExecutor, androidExecutor, wakeLockManager, appContext, - random, eventBus, clock, timeoutMonitor, backoffFactory); + ioExecutor, wakefulIoExecutor, androidExecutor, + wakeLockManager, appContext, random, eventBus, clock, + timeoutMonitor, backoffFactory); DuplexPluginFactory tor = new AndroidTorPluginFactory(ioExecutor, - appContext, networkManager, locationUtils, eventBus, - torSocketFactory, backoffFactory, resourceProvider, + wakefulIoExecutor, appContext, networkManager, locationUtils, + eventBus, torSocketFactory, backoffFactory, resourceProvider, circumventionProvider, batteryManager, wakeLockManager, clock); DuplexPluginFactory lan = new AndroidLanTcpPluginFactory(ioExecutor, - eventBus, backoffFactory, appContext); + wakefulIoExecutor, eventBus, backoffFactory, appContext); Collection duplex = asList(bluetooth, tor, lan); @NotNullByDefault PluginConfig pluginConfig = new PluginConfig() { diff --git a/briar-headless/src/main/java/org/briarproject/briar/headless/HeadlessModule.kt b/briar-headless/src/main/java/org/briarproject/briar/headless/HeadlessModule.kt index 2baf75b9f..5f3ba6e86 100644 --- a/briar-headless/src/main/java/org/briarproject/briar/headless/HeadlessModule.kt +++ b/briar-headless/src/main/java/org/briarproject/briar/headless/HeadlessModule.kt @@ -18,6 +18,7 @@ import org.briarproject.bramble.api.plugin.simplex.SimplexPluginFactory import org.briarproject.bramble.api.system.Clock import org.briarproject.bramble.api.system.LocationUtils import org.briarproject.bramble.api.system.ResourceProvider +import org.briarproject.bramble.api.system.WakefulIoExecutor import org.briarproject.bramble.battery.DefaultBatteryManagerModule import org.briarproject.bramble.event.DefaultEventExecutorModule import org.briarproject.bramble.network.JavaNetworkModule @@ -26,6 +27,7 @@ import org.briarproject.bramble.plugin.tor.CircumventionProvider import org.briarproject.bramble.plugin.tor.UnixTorPluginFactory import org.briarproject.bramble.socks.SocksModule import org.briarproject.bramble.system.DefaultTaskSchedulerModule +import org.briarproject.bramble.system.DefaultWakefulIoExecutorModule import org.briarproject.bramble.system.DesktopSecureRandomModule import org.briarproject.bramble.system.JavaSystemModule import org.briarproject.bramble.util.OsUtils.isLinux @@ -48,6 +50,7 @@ import javax.net.SocketFactory DefaultBatteryManagerModule::class, DefaultEventExecutorModule::class, DefaultTaskSchedulerModule::class, + DefaultWakefulIoExecutorModule::class, DesktopSecureRandomModule::class, HeadlessBlogModule::class, HeadlessContactModule::class, @@ -75,16 +78,32 @@ internal class HeadlessModule(private val appDir: File) { @Provides internal fun providePluginConfig( - @IoExecutor ioExecutor: Executor, torSocketFactory: SocketFactory, - backoffFactory: BackoffFactory, networkManager: NetworkManager, - locationUtils: LocationUtils, eventBus: EventBus, resourceProvider: ResourceProvider, - circumventionProvider: CircumventionProvider, batteryManager: BatteryManager, clock: Clock + @IoExecutor ioExecutor: Executor, + @WakefulIoExecutor wakefulIoExecutor: Executor, + torSocketFactory: SocketFactory, + backoffFactory: BackoffFactory, + networkManager: NetworkManager, + locationUtils: LocationUtils, + eventBus: EventBus, + resourceProvider: ResourceProvider, + circumventionProvider: CircumventionProvider, + batteryManager: BatteryManager, + clock: Clock ): PluginConfig { val duplex: List = if (isLinux() || isMac()) { val torDirectory = File(appDir, "tor") val tor = UnixTorPluginFactory( - ioExecutor, networkManager, locationUtils, eventBus, torSocketFactory, - backoffFactory, resourceProvider, circumventionProvider, batteryManager, clock, + ioExecutor, + wakefulIoExecutor, + networkManager, + locationUtils, + eventBus, + torSocketFactory, + backoffFactory, + resourceProvider, + circumventionProvider, + batteryManager, + clock, torDirectory ) listOf(tor) diff --git a/briar-headless/src/test/java/org/briarproject/briar/headless/HeadlessTestModule.kt b/briar-headless/src/test/java/org/briarproject/briar/headless/HeadlessTestModule.kt index ffb163fc0..24c3e07a0 100644 --- a/briar-headless/src/test/java/org/briarproject/briar/headless/HeadlessTestModule.kt +++ b/briar-headless/src/test/java/org/briarproject/briar/headless/HeadlessTestModule.kt @@ -15,6 +15,7 @@ import org.briarproject.bramble.network.JavaNetworkModule import org.briarproject.bramble.plugin.tor.CircumventionModule import org.briarproject.bramble.socks.SocksModule import org.briarproject.bramble.system.DefaultTaskSchedulerModule +import org.briarproject.bramble.system.DefaultWakefulIoExecutorModule import org.briarproject.bramble.system.JavaSystemModule import org.briarproject.bramble.test.TestSecureRandomModule import org.briarproject.briar.headless.blogs.HeadlessBlogModule @@ -34,6 +35,7 @@ import javax.inject.Singleton CircumventionModule::class, DefaultEventExecutorModule::class, DefaultTaskSchedulerModule::class, + DefaultWakefulIoExecutorModule::class, SocksModule::class, TestSecureRandomModule::class, HeadlessBlogModule::class,