mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-11 18:29:05 +01:00
Wrap scheduler in an interface.
This commit is contained in:
@@ -15,10 +15,9 @@ import org.briarproject.bramble.api.network.NetworkStatus;
|
||||
import org.briarproject.bramble.api.network.event.NetworkStatusEvent;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.api.system.Scheduler;
|
||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
||||
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
@@ -50,7 +49,7 @@ class AndroidNetworkManager implements NetworkManager, Service {
|
||||
private static final String WIFI_AP_STATE_CHANGED_ACTION =
|
||||
"android.net.wifi.WIFI_AP_STATE_CHANGED";
|
||||
|
||||
private final ScheduledExecutorService scheduler;
|
||||
private final TaskScheduler scheduler;
|
||||
private final EventBus eventBus;
|
||||
private final Context appContext;
|
||||
private final AtomicReference<Future<?>> connectivityCheck =
|
||||
@@ -60,8 +59,8 @@ class AndroidNetworkManager implements NetworkManager, Service {
|
||||
private volatile BroadcastReceiver networkStateReceiver = null;
|
||||
|
||||
@Inject
|
||||
AndroidNetworkManager(@Scheduler ScheduledExecutorService scheduler,
|
||||
EventBus eventBus, Application app) {
|
||||
AndroidNetworkManager(TaskScheduler scheduler, EventBus eventBus,
|
||||
Application app) {
|
||||
this.scheduler = scheduler;
|
||||
this.eventBus = eventBus;
|
||||
this.appContext = app.getApplicationContext();
|
||||
|
||||
@@ -18,6 +18,7 @@ import org.briarproject.bramble.api.plugin.PluginException;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.bramble.api.system.AndroidExecutor;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
||||
import org.briarproject.bramble.util.AndroidUtils;
|
||||
import org.briarproject.bramble.util.IoUtils;
|
||||
|
||||
@@ -31,7 +32,6 @@ import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
@@ -67,7 +67,7 @@ class AndroidBluetoothPlugin extends BluetoothPlugin<BluetoothServerSocket> {
|
||||
|
||||
private static final int MAX_DISCOVERY_MS = 10_000;
|
||||
|
||||
private final ScheduledExecutorService scheduler;
|
||||
private final TaskScheduler scheduler;
|
||||
private final AndroidExecutor androidExecutor;
|
||||
private final Context appContext;
|
||||
private final Clock clock;
|
||||
@@ -79,10 +79,16 @@ class AndroidBluetoothPlugin extends BluetoothPlugin<BluetoothServerSocket> {
|
||||
private volatile BluetoothAdapter adapter = null;
|
||||
|
||||
AndroidBluetoothPlugin(BluetoothConnectionLimiter connectionLimiter,
|
||||
TimeoutMonitor timeoutMonitor, Executor ioExecutor,
|
||||
SecureRandom secureRandom, ScheduledExecutorService scheduler,
|
||||
AndroidExecutor androidExecutor, Context appContext, Clock clock,
|
||||
Backoff backoff, PluginCallback callback, int maxLatency,
|
||||
TimeoutMonitor timeoutMonitor,
|
||||
Executor ioExecutor,
|
||||
SecureRandom secureRandom,
|
||||
TaskScheduler scheduler,
|
||||
AndroidExecutor androidExecutor,
|
||||
Context appContext,
|
||||
Clock clock,
|
||||
Backoff backoff,
|
||||
PluginCallback callback,
|
||||
int maxLatency,
|
||||
int maxIdleTime) {
|
||||
super(connectionLimiter, timeoutMonitor, ioExecutor, secureRandom,
|
||||
backoff, callback, maxLatency, maxIdleTime);
|
||||
|
||||
@@ -13,10 +13,10 @@ import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
|
||||
import org.briarproject.bramble.api.system.AndroidExecutor;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
@@ -33,7 +33,7 @@ public class AndroidBluetoothPluginFactory implements DuplexPluginFactory {
|
||||
private static final double BACKOFF_BASE = 1.2;
|
||||
|
||||
private final Executor ioExecutor;
|
||||
private final ScheduledExecutorService scheduler;
|
||||
private final TaskScheduler scheduler;
|
||||
private final AndroidExecutor androidExecutor;
|
||||
private final Context appContext;
|
||||
private final SecureRandom secureRandom;
|
||||
@@ -43,10 +43,14 @@ public class AndroidBluetoothPluginFactory implements DuplexPluginFactory {
|
||||
private final BackoffFactory backoffFactory;
|
||||
|
||||
public AndroidBluetoothPluginFactory(Executor ioExecutor,
|
||||
ScheduledExecutorService scheduler,
|
||||
AndroidExecutor androidExecutor, Context appContext,
|
||||
SecureRandom secureRandom, EventBus eventBus, Clock clock,
|
||||
TimeoutMonitor timeoutMonitor, BackoffFactory backoffFactory) {
|
||||
TaskScheduler scheduler,
|
||||
AndroidExecutor androidExecutor,
|
||||
Context appContext,
|
||||
SecureRandom secureRandom,
|
||||
EventBus eventBus,
|
||||
Clock clock,
|
||||
TimeoutMonitor timeoutMonitor,
|
||||
BackoffFactory backoffFactory) {
|
||||
this.ioExecutor = ioExecutor;
|
||||
this.scheduler = scheduler;
|
||||
this.androidExecutor = androidExecutor;
|
||||
|
||||
@@ -8,12 +8,12 @@ import org.briarproject.bramble.api.io.TimeoutMonitor;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.Plugin;
|
||||
import org.briarproject.bramble.api.plugin.duplex.AbstractDuplexTransportConnection;
|
||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
||||
import org.briarproject.bramble.util.RenewableWakeLock;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
import static android.content.Context.POWER_SERVICE;
|
||||
import static android.os.PowerManager.PARTIAL_WAKE_LOCK;
|
||||
@@ -34,9 +34,10 @@ class AndroidBluetoothTransportConnection
|
||||
|
||||
AndroidBluetoothTransportConnection(Plugin plugin,
|
||||
BluetoothConnectionLimiter connectionLimiter,
|
||||
TimeoutMonitor timeoutMonitor, Context appContext,
|
||||
ScheduledExecutorService scheduler, BluetoothSocket socket)
|
||||
throws IOException {
|
||||
TimeoutMonitor timeoutMonitor,
|
||||
Context appContext,
|
||||
TaskScheduler scheduler,
|
||||
BluetoothSocket socket) throws IOException {
|
||||
super(plugin);
|
||||
this.connectionLimiter = connectionLimiter;
|
||||
this.socket = socket;
|
||||
|
||||
@@ -15,11 +15,11 @@ import org.briarproject.bramble.api.plugin.PluginCallback;
|
||||
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.TaskScheduler;
|
||||
import org.briarproject.bramble.util.RenewableWakeLock;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
import javax.net.SocketFactory;
|
||||
|
||||
@@ -36,18 +36,26 @@ class AndroidTorPlugin extends TorPlugin {
|
||||
private final Context appContext;
|
||||
private final RenewableWakeLock wakeLock;
|
||||
|
||||
AndroidTorPlugin(Executor ioExecutor, ScheduledExecutorService scheduler,
|
||||
Context appContext, NetworkManager networkManager,
|
||||
LocationUtils locationUtils, SocketFactory torSocketFactory,
|
||||
Clock clock, ResourceProvider resourceProvider,
|
||||
AndroidTorPlugin(Executor ioExecutor,
|
||||
TaskScheduler scheduler,
|
||||
Context appContext,
|
||||
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,
|
||||
PluginCallback callback,
|
||||
String architecture,
|
||||
int maxLatency,
|
||||
int maxIdleTime) {
|
||||
super(ioExecutor, networkManager, locationUtils, torSocketFactory,
|
||||
clock, resourceProvider, circumventionProvider, batteryManager,
|
||||
backoff, torRendezvousCrypto, callback, architecture, maxLatency, maxIdleTime,
|
||||
backoff, torRendezvousCrypto, callback, architecture,
|
||||
maxLatency, maxIdleTime,
|
||||
appContext.getDir("tor", MODE_PRIVATE));
|
||||
this.appContext = appContext;
|
||||
PowerManager pm = (PowerManager)
|
||||
|
||||
@@ -16,10 +16,10 @@ import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
|
||||
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.TaskScheduler;
|
||||
import org.briarproject.bramble.util.AndroidUtils;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
@@ -39,7 +39,7 @@ public class AndroidTorPluginFactory implements DuplexPluginFactory {
|
||||
private static final double BACKOFF_BASE = 1.2;
|
||||
|
||||
private final Executor ioExecutor;
|
||||
private final ScheduledExecutorService scheduler;
|
||||
private final TaskScheduler scheduler;
|
||||
private final Context appContext;
|
||||
private final NetworkManager networkManager;
|
||||
private final LocationUtils locationUtils;
|
||||
@@ -52,12 +52,17 @@ public class AndroidTorPluginFactory implements DuplexPluginFactory {
|
||||
private final Clock clock;
|
||||
|
||||
public AndroidTorPluginFactory(Executor ioExecutor,
|
||||
ScheduledExecutorService scheduler, Context appContext,
|
||||
NetworkManager networkManager, LocationUtils locationUtils,
|
||||
EventBus eventBus, SocketFactory torSocketFactory,
|
||||
BackoffFactory backoffFactory, ResourceProvider resourceProvider,
|
||||
TaskScheduler scheduler,
|
||||
Context appContext,
|
||||
NetworkManager networkManager,
|
||||
LocationUtils locationUtils,
|
||||
EventBus eventBus,
|
||||
SocketFactory torSocketFactory,
|
||||
BackoffFactory backoffFactory,
|
||||
ResourceProvider resourceProvider,
|
||||
CircumventionProvider circumventionProvider,
|
||||
BatteryManager batteryManager, Clock clock) {
|
||||
BatteryManager batteryManager,
|
||||
Clock clock) {
|
||||
this.ioExecutor = ioExecutor;
|
||||
this.scheduler = scheduler;
|
||||
this.appContext = appContext;
|
||||
|
||||
@@ -3,8 +3,8 @@ package org.briarproject.bramble.util;
|
||||
import android.os.PowerManager;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
||||
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Logger;
|
||||
@@ -29,7 +29,7 @@ public class RenewableWakeLock {
|
||||
private static final int SAFETY_MARGIN_MS = 10_000;
|
||||
|
||||
private final PowerManager powerManager;
|
||||
private final ScheduledExecutorService scheduler;
|
||||
private final TaskScheduler scheduler;
|
||||
private final int levelAndFlags;
|
||||
private final String tag;
|
||||
private final long durationMs;
|
||||
@@ -39,10 +39,10 @@ public class RenewableWakeLock {
|
||||
@Nullable
|
||||
private PowerManager.WakeLock wakeLock; // Locking: lock
|
||||
@Nullable
|
||||
private ScheduledFuture future; // Locking: lock
|
||||
private ScheduledFuture<?> future; // Locking: lock
|
||||
|
||||
public RenewableWakeLock(PowerManager powerManager,
|
||||
ScheduledExecutorService scheduler, int levelAndFlags, String tag,
|
||||
TaskScheduler scheduler, int levelAndFlags, String tag,
|
||||
long duration, TimeUnit timeUnit) {
|
||||
this.powerManager = powerManager;
|
||||
this.scheduler = scheduler;
|
||||
|
||||
@@ -1,25 +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 a scheduled executor service
|
||||
* 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
|
||||
@Target({FIELD, METHOD, PARAMETER})
|
||||
@Retention(RUNTIME)
|
||||
public @interface Scheduler {
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package org.briarproject.bramble.api.system;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* A service that can be used to schedule the execution of tasks.
|
||||
* <p>
|
||||
* The service should only be used for running tasks on other executors
|
||||
* at scheduled times. No significant work should be run by the service itself.
|
||||
*/
|
||||
@NotNullByDefault
|
||||
public interface TaskScheduler {
|
||||
|
||||
/**
|
||||
* See {@link ScheduledExecutorService#schedule(Runnable, long, TimeUnit)}.
|
||||
*/
|
||||
ScheduledFuture<?> schedule(Runnable task, long delay, TimeUnit unit);
|
||||
|
||||
/**
|
||||
* See {@link ScheduledExecutorService#scheduleAtFixedRate(Runnable, long, long, TimeUnit)}.
|
||||
*/
|
||||
ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long delay,
|
||||
long interval, TimeUnit unit);
|
||||
|
||||
/**
|
||||
* See {@link ScheduledExecutorService#scheduleWithFixedDelay(Runnable, long, long, TimeUnit)}.
|
||||
*/
|
||||
ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, long delay,
|
||||
long interval, TimeUnit unit);
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package org.briarproject.bramble.io;
|
||||
import org.briarproject.bramble.api.io.TimeoutMonitor;
|
||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.bramble.api.system.Scheduler;
|
||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -11,7 +11,6 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.concurrent.GuardedBy;
|
||||
@@ -30,7 +29,7 @@ class TimeoutMonitorImpl implements TimeoutMonitor {
|
||||
|
||||
private static final long CHECK_INTERVAL_MS = SECONDS.toMillis(10);
|
||||
|
||||
private final ScheduledExecutorService scheduler;
|
||||
private final TaskScheduler scheduler;
|
||||
private final Executor ioExecutor;
|
||||
private final Clock clock;
|
||||
private final Object lock = new Object();
|
||||
@@ -41,7 +40,7 @@ class TimeoutMonitorImpl implements TimeoutMonitor {
|
||||
private Future<?> task = null;
|
||||
|
||||
@Inject
|
||||
TimeoutMonitorImpl(@Scheduler ScheduledExecutorService scheduler,
|
||||
TimeoutMonitorImpl(TaskScheduler scheduler,
|
||||
@IoExecutor Executor ioExecutor, Clock clock) {
|
||||
this.scheduler = scheduler;
|
||||
this.ioExecutor = ioExecutor;
|
||||
@@ -74,7 +73,7 @@ class TimeoutMonitorImpl implements TimeoutMonitor {
|
||||
if (toCancel != null) toCancel.cancel(false);
|
||||
}
|
||||
|
||||
@Scheduler
|
||||
// Scheduler
|
||||
private void checkTimeouts() {
|
||||
ioExecutor.execute(() -> {
|
||||
List<TimeoutInputStream> snapshot;
|
||||
|
||||
@@ -26,7 +26,7 @@ import org.briarproject.bramble.api.plugin.simplex.SimplexPlugin;
|
||||
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.Scheduler;
|
||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
@@ -36,7 +36,6 @@ import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
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.ReentrantLock;
|
||||
import java.util.logging.Logger;
|
||||
@@ -58,7 +57,7 @@ class PollerImpl implements Poller, EventListener {
|
||||
private static final Logger LOG = getLogger(PollerImpl.class.getName());
|
||||
|
||||
private final Executor ioExecutor;
|
||||
private final ScheduledExecutorService scheduler;
|
||||
private final TaskScheduler scheduler;
|
||||
private final ConnectionManager connectionManager;
|
||||
private final ConnectionRegistry connectionRegistry;
|
||||
private final PluginManager pluginManager;
|
||||
@@ -71,11 +70,13 @@ class PollerImpl implements Poller, EventListener {
|
||||
|
||||
@Inject
|
||||
PollerImpl(@IoExecutor Executor ioExecutor,
|
||||
@Scheduler ScheduledExecutorService scheduler,
|
||||
TaskScheduler scheduler,
|
||||
ConnectionManager connectionManager,
|
||||
ConnectionRegistry connectionRegistry, PluginManager pluginManager,
|
||||
ConnectionRegistry connectionRegistry,
|
||||
PluginManager pluginManager,
|
||||
TransportPropertyManager transportPropertyManager,
|
||||
SecureRandom random, Clock clock) {
|
||||
SecureRandom random,
|
||||
Clock clock) {
|
||||
this.ioExecutor = ioExecutor;
|
||||
this.scheduler = scheduler;
|
||||
this.connectionManager = connectionManager;
|
||||
|
||||
@@ -41,7 +41,7 @@ import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionClosedE
|
||||
import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionOpenedEvent;
|
||||
import org.briarproject.bramble.api.rendezvous.event.RendezvousPollEvent;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.bramble.api.system.Scheduler;
|
||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
||||
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.ArrayList;
|
||||
@@ -52,7 +52,6 @@ import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -80,7 +79,7 @@ class RendezvousPollerImpl implements RendezvousPoller, Service, EventListener {
|
||||
private static final Logger LOG =
|
||||
getLogger(RendezvousPollerImpl.class.getName());
|
||||
|
||||
private final ScheduledExecutorService scheduler;
|
||||
private final TaskScheduler scheduler;
|
||||
private final DatabaseComponent db;
|
||||
private final IdentityManager identityManager;
|
||||
private final TransportCrypto transportCrypto;
|
||||
@@ -105,7 +104,7 @@ class RendezvousPollerImpl implements RendezvousPoller, Service, EventListener {
|
||||
|
||||
@Inject
|
||||
RendezvousPollerImpl(@IoExecutor Executor ioExecutor,
|
||||
@Scheduler ScheduledExecutorService scheduler,
|
||||
TaskScheduler scheduler,
|
||||
DatabaseComponent db,
|
||||
IdentityManager identityManager,
|
||||
TransportCrypto transportCrypto,
|
||||
@@ -205,7 +204,7 @@ class RendezvousPollerImpl implements RendezvousPoller, Service, EventListener {
|
||||
return plugin.createRendezvousEndpoint(k, cs.alice, h);
|
||||
}
|
||||
|
||||
@Scheduler
|
||||
// Scheduler
|
||||
private void poll() {
|
||||
worker.execute(() -> {
|
||||
removeExpiredPendingContacts();
|
||||
|
||||
@@ -2,7 +2,7 @@ package org.briarproject.bramble.system;
|
||||
|
||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.bramble.api.system.Scheduler;
|
||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
||||
|
||||
import java.util.concurrent.RejectedExecutionHandler;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
@@ -19,17 +19,16 @@ public class SystemModule {
|
||||
|
||||
public static class EagerSingletons {
|
||||
@Inject
|
||||
@Scheduler
|
||||
ScheduledExecutorService scheduledExecutorService;
|
||||
TaskScheduler scheduler;
|
||||
}
|
||||
|
||||
private final ScheduledExecutorService scheduler;
|
||||
private final ScheduledExecutorService scheduledExecutorService;
|
||||
|
||||
public SystemModule() {
|
||||
// Discard tasks that are submitted during shutdown
|
||||
RejectedExecutionHandler policy =
|
||||
new ScheduledThreadPoolExecutor.DiscardPolicy();
|
||||
scheduler = new ScheduledThreadPoolExecutor(1, policy);
|
||||
scheduledExecutorService = new ScheduledThreadPoolExecutor(1, policy);
|
||||
}
|
||||
|
||||
@Provides
|
||||
@@ -39,10 +38,8 @@ public class SystemModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
@Scheduler
|
||||
ScheduledExecutorService provideScheduledExecutorService(
|
||||
LifecycleManager lifecycleManager) {
|
||||
lifecycleManager.registerForShutdown(scheduler);
|
||||
return scheduler;
|
||||
TaskScheduler provideTaskScheduler(LifecycleManager lifecycleManager) {
|
||||
lifecycleManager.registerForShutdown(scheduledExecutorService);
|
||||
return new TaskSchedulerImpl(scheduledExecutorService);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package org.briarproject.bramble.system;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
||||
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
/**
|
||||
* A {@link TaskScheduler} that delegates all calls to a
|
||||
* {@link ScheduledExecutorService}.
|
||||
*/
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
class TaskSchedulerImpl implements TaskScheduler {
|
||||
|
||||
private final ScheduledExecutorService delegate;
|
||||
|
||||
TaskSchedulerImpl(ScheduledExecutorService delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScheduledFuture<?> schedule(Runnable task, long delay,
|
||||
TimeUnit unit) {
|
||||
return delegate.schedule(task, delay, unit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long delay,
|
||||
long interval, TimeUnit unit) {
|
||||
return delegate.scheduleAtFixedRate(task, delay, interval, unit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, long delay,
|
||||
long interval, TimeUnit unit) {
|
||||
return delegate.scheduleWithFixedDelay(task, delay, interval, unit);
|
||||
}
|
||||
}
|
||||
@@ -6,10 +6,9 @@ import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.bramble.api.system.Scheduler;
|
||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
import javax.inject.Inject;
|
||||
@@ -22,14 +21,15 @@ class TransportKeyManagerFactoryImpl implements
|
||||
private final DatabaseComponent db;
|
||||
private final TransportCrypto transportCrypto;
|
||||
private final Executor dbExecutor;
|
||||
private final ScheduledExecutorService scheduler;
|
||||
private final TaskScheduler scheduler;
|
||||
private final Clock clock;
|
||||
|
||||
@Inject
|
||||
TransportKeyManagerFactoryImpl(DatabaseComponent db,
|
||||
TransportCrypto transportCrypto,
|
||||
@DatabaseExecutor Executor dbExecutor,
|
||||
@Scheduler ScheduledExecutorService scheduler, Clock clock) {
|
||||
TaskScheduler scheduler,
|
||||
Clock clock) {
|
||||
this.db = db;
|
||||
this.transportCrypto = transportCrypto;
|
||||
this.dbExecutor = dbExecutor;
|
||||
|
||||
@@ -11,7 +11,7 @@ import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.bramble.api.system.Scheduler;
|
||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
||||
import org.briarproject.bramble.api.transport.KeySetId;
|
||||
import org.briarproject.bramble.api.transport.StreamContext;
|
||||
import org.briarproject.bramble.api.transport.TransportKeySet;
|
||||
@@ -24,7 +24,6 @@ import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.logging.Logger;
|
||||
@@ -53,7 +52,7 @@ class TransportKeyManagerImpl implements TransportKeyManager {
|
||||
private final DatabaseComponent db;
|
||||
private final TransportCrypto transportCrypto;
|
||||
private final Executor dbExecutor;
|
||||
private final ScheduledExecutorService scheduler;
|
||||
private final TaskScheduler scheduler;
|
||||
private final Clock clock;
|
||||
private final TransportId transportId;
|
||||
private final long timePeriodLength;
|
||||
@@ -72,9 +71,12 @@ class TransportKeyManagerImpl implements TransportKeyManager {
|
||||
pendingContactOutContexts = new HashMap<>();
|
||||
|
||||
TransportKeyManagerImpl(DatabaseComponent db,
|
||||
TransportCrypto transportCrypto, Executor dbExecutor,
|
||||
@Scheduler ScheduledExecutorService scheduler, Clock clock,
|
||||
TransportId transportId, long maxLatency) {
|
||||
TransportCrypto transportCrypto,
|
||||
Executor dbExecutor,
|
||||
TaskScheduler scheduler,
|
||||
Clock clock,
|
||||
TransportId transportId,
|
||||
long maxLatency) {
|
||||
this.db = db;
|
||||
this.transportCrypto = transportCrypto;
|
||||
this.dbExecutor = dbExecutor;
|
||||
|
||||
@@ -19,6 +19,7 @@ import org.briarproject.bramble.api.plugin.simplex.SimplexPlugin;
|
||||
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.test.BrambleMockTestCase;
|
||||
import org.briarproject.bramble.test.ImmediateExecutor;
|
||||
import org.briarproject.bramble.test.RunAction;
|
||||
@@ -30,7 +31,6 @@ import org.junit.Test;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
@@ -45,8 +45,7 @@ import static org.briarproject.bramble.test.TestUtils.getTransportId;
|
||||
|
||||
public class PollerImplTest extends BrambleMockTestCase {
|
||||
|
||||
private final ScheduledExecutorService scheduler =
|
||||
context.mock(ScheduledExecutorService.class);
|
||||
private final TaskScheduler scheduler = context.mock(TaskScheduler.class);
|
||||
private final ConnectionManager connectionManager =
|
||||
context.mock(ConnectionManager.class);
|
||||
private final ConnectionRegistry connectionRegistry =
|
||||
@@ -56,7 +55,8 @@ public class PollerImplTest extends BrambleMockTestCase {
|
||||
private final TransportPropertyManager transportPropertyManager =
|
||||
context.mock(TransportPropertyManager.class);
|
||||
private final Clock clock = context.mock(Clock.class);
|
||||
private final ScheduledFuture future = context.mock(ScheduledFuture.class);
|
||||
private final ScheduledFuture<?> future =
|
||||
context.mock(ScheduledFuture.class);
|
||||
private final SecureRandom random;
|
||||
|
||||
private final Executor ioExecutor = new ImmediateExecutor();
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionClosedE
|
||||
import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionOpenedEvent;
|
||||
import org.briarproject.bramble.api.rendezvous.event.RendezvousPollEvent;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
import org.briarproject.bramble.test.CaptureArgumentAction;
|
||||
import org.briarproject.bramble.test.DbExpectations;
|
||||
@@ -37,7 +38,6 @@ import org.junit.Test;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
@@ -60,8 +60,7 @@ import static org.briarproject.bramble.test.TestUtils.getTransportProperties;
|
||||
|
||||
public class RendezvousPollerImplTest extends BrambleMockTestCase {
|
||||
|
||||
private final ScheduledExecutorService scheduler =
|
||||
context.mock(ScheduledExecutorService.class);
|
||||
private final TaskScheduler scheduler = context.mock(TaskScheduler.class);
|
||||
private final DatabaseComponent db = context.mock(DatabaseComponent.class);
|
||||
private final IdentityManager identityManager =
|
||||
context.mock(IdentityManager.class);
|
||||
|
||||
@@ -8,6 +8,7 @@ import org.briarproject.bramble.api.db.DatabaseComponent;
|
||||
import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
||||
import org.briarproject.bramble.api.transport.IncomingKeys;
|
||||
import org.briarproject.bramble.api.transport.KeySetId;
|
||||
import org.briarproject.bramble.api.transport.OutgoingKeys;
|
||||
@@ -29,7 +30,6 @@ import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.Collections.singletonList;
|
||||
@@ -55,8 +55,7 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
|
||||
private final TransportCrypto transportCrypto =
|
||||
context.mock(TransportCrypto.class);
|
||||
private final Executor dbExecutor = context.mock(Executor.class);
|
||||
private final ScheduledExecutorService scheduler =
|
||||
context.mock(ScheduledExecutorService.class);
|
||||
private final TaskScheduler scheduler = context.mock(TaskScheduler.class);
|
||||
private final Clock clock = context.mock(Clock.class);
|
||||
|
||||
private final TransportId transportId = getTransportId();
|
||||
|
||||
@@ -31,7 +31,7 @@ import org.briarproject.bramble.api.system.AndroidExecutor;
|
||||
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.Scheduler;
|
||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
||||
import org.briarproject.bramble.plugin.bluetooth.AndroidBluetoothPluginFactory;
|
||||
import org.briarproject.bramble.plugin.tcp.AndroidLanTcpPluginFactory;
|
||||
import org.briarproject.bramble.plugin.tor.AndroidTorPluginFactory;
|
||||
@@ -55,7 +55,6 @@ import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
@@ -125,14 +124,19 @@ public class AppModule {
|
||||
|
||||
@Provides
|
||||
PluginConfig providePluginConfig(@IoExecutor Executor ioExecutor,
|
||||
@Scheduler ScheduledExecutorService scheduler,
|
||||
AndroidExecutor androidExecutor, SecureRandom random,
|
||||
SocketFactory torSocketFactory, BackoffFactory backoffFactory,
|
||||
Application app, NetworkManager networkManager,
|
||||
LocationUtils locationUtils, EventBus eventBus,
|
||||
TaskScheduler scheduler,
|
||||
AndroidExecutor androidExecutor,
|
||||
SecureRandom random,
|
||||
SocketFactory torSocketFactory,
|
||||
BackoffFactory backoffFactory,
|
||||
Application app,
|
||||
NetworkManager networkManager,
|
||||
LocationUtils locationUtils,
|
||||
EventBus eventBus,
|
||||
ResourceProvider resourceProvider,
|
||||
CircumventionProvider circumventionProvider,
|
||||
BatteryManager batteryManager, Clock clock,
|
||||
BatteryManager batteryManager,
|
||||
Clock clock,
|
||||
TimeoutMonitor timeoutMonitor) {
|
||||
Context appContext = app.getApplicationContext();
|
||||
DuplexPluginFactory bluetooth = new AndroidBluetoothPluginFactory(
|
||||
|
||||
@@ -29,7 +29,7 @@ import org.briarproject.bramble.api.plugin.event.TransportInactiveEvent;
|
||||
import org.briarproject.bramble.api.sync.Group;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.bramble.api.system.Scheduler;
|
||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
import org.briarproject.briar.api.blog.Blog;
|
||||
import org.briarproject.briar.api.blog.BlogManager;
|
||||
@@ -48,7 +48,6 @@ import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -85,7 +84,7 @@ class FeedManagerImpl implements FeedManager, EventListener, OpenDatabaseHook,
|
||||
|
||||
private static final int CONNECT_TIMEOUT = 60 * 1000; // Milliseconds
|
||||
|
||||
private final ScheduledExecutorService scheduler;
|
||||
private final TaskScheduler scheduler;
|
||||
private final Executor ioExecutor;
|
||||
private final DatabaseComponent db;
|
||||
private final ContactGroupFactory contactGroupFactory;
|
||||
@@ -101,13 +100,17 @@ class FeedManagerImpl implements FeedManager, EventListener, OpenDatabaseHook,
|
||||
private volatile boolean torActive = false;
|
||||
|
||||
@Inject
|
||||
FeedManagerImpl(@Scheduler ScheduledExecutorService scheduler,
|
||||
@IoExecutor Executor ioExecutor, DatabaseComponent db,
|
||||
ContactGroupFactory contactGroupFactory, ClientHelper clientHelper,
|
||||
BlogManager blogManager, BlogPostFactory blogPostFactory,
|
||||
FeedFactory feedFactory, SocketFactory torSocketFactory,
|
||||
Clock clock, Dns noDnsLookups) {
|
||||
|
||||
FeedManagerImpl(TaskScheduler scheduler,
|
||||
@IoExecutor Executor ioExecutor,
|
||||
DatabaseComponent db,
|
||||
ContactGroupFactory contactGroupFactory,
|
||||
ClientHelper clientHelper,
|
||||
BlogManager blogManager,
|
||||
BlogPostFactory blogPostFactory,
|
||||
FeedFactory feedFactory,
|
||||
SocketFactory torSocketFactory,
|
||||
Clock clock,
|
||||
Dns noDnsLookups) {
|
||||
this.scheduler = scheduler;
|
||||
this.ioExecutor = ioExecutor;
|
||||
this.db = db;
|
||||
@@ -285,7 +288,7 @@ class FeedManagerImpl implements FeedManager, EventListener, OpenDatabaseHook,
|
||||
* This method is called periodically from a background service.
|
||||
* It fetches all available feeds and posts new entries to the respective
|
||||
* blog.
|
||||
*
|
||||
* <p>
|
||||
* We can not do this within one database {@link Transaction},
|
||||
* because fetching can take a long time
|
||||
* and we can not block the database that long.
|
||||
@@ -491,9 +494,9 @@ class FeedManagerImpl implements FeedManager, EventListener, OpenDatabaseHook,
|
||||
private Comparator<SyndEntry> getEntryComparator() {
|
||||
return (e1, e2) -> {
|
||||
Date d1 = e1.getPublishedDate() != null ? e1.getPublishedDate() :
|
||||
e1.getUpdatedDate();
|
||||
e1.getUpdatedDate();
|
||||
Date d2 = e2.getPublishedDate() != null ? e2.getPublishedDate() :
|
||||
e2.getUpdatedDate();
|
||||
e2.getUpdatedDate();
|
||||
if (d1 == null && d2 == null) return 0;
|
||||
if (d1 == null) return -1;
|
||||
if (d2 == null) return 1;
|
||||
|
||||
@@ -15,6 +15,7 @@ import org.briarproject.bramble.api.sync.Group;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.bramble.api.sync.Message;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
import org.briarproject.bramble.test.ImmediateExecutor;
|
||||
import org.briarproject.briar.api.blog.Blog;
|
||||
@@ -30,7 +31,6 @@ import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
import javax.net.SocketFactory;
|
||||
|
||||
@@ -45,8 +45,7 @@ import static org.briarproject.briar.api.feed.FeedManager.MAJOR_VERSION;
|
||||
|
||||
public class FeedManagerImplTest extends BrambleMockTestCase {
|
||||
|
||||
private final ScheduledExecutorService scheduler =
|
||||
context.mock(ScheduledExecutorService.class);
|
||||
private final TaskScheduler scheduler = context.mock(TaskScheduler.class);
|
||||
private final Executor ioExecutor = new ImmediateExecutor();
|
||||
private final DatabaseComponent db = context.mock(DatabaseComponent.class);
|
||||
private final ContactGroupFactory contactGroupFactory =
|
||||
@@ -92,7 +91,7 @@ public class FeedManagerImplTest extends BrambleMockTestCase {
|
||||
|
||||
@Test
|
||||
public void testFetchFeedsIoException() throws Exception {
|
||||
BdfDictionary feedDict= new BdfDictionary();
|
||||
BdfDictionary feedDict = new BdfDictionary();
|
||||
BdfList feedList = BdfList.of(feedDict);
|
||||
|
||||
expectGetFeeds(feedList);
|
||||
|
||||
Reference in New Issue
Block a user