mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
Merge branch '1142-scheduler-interface' into 'master'
Use TaskScheduler for scheduling tasks See merge request briar/briar!1266
This commit is contained in:
@@ -16,6 +16,8 @@
|
||||
android:label="@string/app_name"
|
||||
android:supportsRtl="true">
|
||||
|
||||
<receiver android:name=".system.AlarmReceiver" />
|
||||
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
|
||||
@@ -6,6 +6,7 @@ import org.briarproject.bramble.plugin.tor.CircumventionModule;
|
||||
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 dagger.Module;
|
||||
|
||||
@@ -13,6 +14,7 @@ import dagger.Module;
|
||||
AndroidBatteryModule.class,
|
||||
AndroidNetworkModule.class,
|
||||
AndroidSystemModule.class,
|
||||
AndroidTaskSchedulerModule.class,
|
||||
CircumventionModule.class,
|
||||
ReportingModule.class,
|
||||
SocksModule.class
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package org.briarproject.bramble;
|
||||
|
||||
import org.briarproject.bramble.api.system.AlarmListener;
|
||||
|
||||
public interface BrambleAppComponent {
|
||||
|
||||
AlarmListener alarmListener();
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package org.briarproject.bramble;
|
||||
|
||||
public interface BrambleApplication {
|
||||
|
||||
BrambleAppComponent getBrambleAppComponent();
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.briarproject.bramble.api.system;
|
||||
|
||||
import android.content.Intent;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface AlarmListener {
|
||||
|
||||
void onAlarm(Intent intent);
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package org.briarproject.bramble.system;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
@NotNullByDefault
|
||||
interface AlarmConstants {
|
||||
|
||||
/**
|
||||
* Request code for the broadcast intent attached to the periodic alarm.
|
||||
*/
|
||||
int REQUEST_ALARM = 1;
|
||||
|
||||
/**
|
||||
* Key for storing the process ID in the extras of the periodic alarm's
|
||||
* intent. This allows us to ignore alarms scheduled by dead processes.
|
||||
*/
|
||||
String EXTRA_PID = "org.briarproject.bramble.EXTRA_PID";
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.briarproject.bramble.system;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
import org.briarproject.bramble.BrambleApplication;
|
||||
|
||||
public class AlarmReceiver extends BroadcastReceiver {
|
||||
|
||||
@Override
|
||||
public void onReceive(Context ctx, Intent intent) {
|
||||
BrambleApplication app =
|
||||
(BrambleApplication) ctx.getApplicationContext();
|
||||
app.getBrambleAppComponent().alarmListener().onAlarm(intent);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
package org.briarproject.bramble.system;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.app.AlarmManager;
|
||||
import android.app.Application;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Intent;
|
||||
import android.os.Process;
|
||||
import android.os.SystemClock;
|
||||
|
||||
import org.briarproject.bramble.api.lifecycle.Service;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.system.AlarmListener;
|
||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.FutureTask;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.concurrent.GuardedBy;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
import static android.app.AlarmManager.ELAPSED_REALTIME_WAKEUP;
|
||||
import static android.app.AlarmManager.INTERVAL_FIFTEEN_MINUTES;
|
||||
import static android.app.PendingIntent.FLAG_CANCEL_CURRENT;
|
||||
import static android.content.Context.ALARM_SERVICE;
|
||||
import static android.os.Build.VERSION.SDK_INT;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.system.AlarmConstants.EXTRA_PID;
|
||||
import static org.briarproject.bramble.system.AlarmConstants.REQUEST_ALARM;
|
||||
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
class AndroidTaskScheduler implements TaskScheduler, Service, AlarmListener {
|
||||
|
||||
private static final Logger LOG =
|
||||
getLogger(AndroidTaskScheduler.class.getName());
|
||||
|
||||
private static final long TICK_MS = SECONDS.toMillis(10);
|
||||
private static final long ALARM_MS = INTERVAL_FIFTEEN_MINUTES;
|
||||
|
||||
private final Application app;
|
||||
private final ScheduledExecutorService scheduledExecutorService;
|
||||
private final AlarmManager alarmManager;
|
||||
|
||||
private final Object lock = new Object();
|
||||
@GuardedBy("lock")
|
||||
private final Queue<ScheduledTask> tasks = new PriorityQueue<>();
|
||||
|
||||
AndroidTaskScheduler(Application app,
|
||||
ScheduledExecutorService scheduledExecutorService) {
|
||||
this.app = app;
|
||||
this.scheduledExecutorService = scheduledExecutorService;
|
||||
alarmManager = (AlarmManager)
|
||||
requireNonNull(app.getSystemService(ALARM_SERVICE));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startService() {
|
||||
scheduledExecutorService.scheduleAtFixedRate(this::runDueTasks,
|
||||
TICK_MS, TICK_MS, MILLISECONDS);
|
||||
scheduleAlarm();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stopService() {
|
||||
cancelAlarm();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<?> schedule(Runnable task, long delay, TimeUnit unit) {
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
long dueMillis = now + MILLISECONDS.convert(delay, unit);
|
||||
ScheduledTask s = new ScheduledTask(task, dueMillis);
|
||||
if (dueMillis <= now) {
|
||||
scheduledExecutorService.execute(s);
|
||||
} else {
|
||||
synchronized (lock) {
|
||||
tasks.add(s);
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<?> scheduleWithFixedDelay(Runnable task, long delay,
|
||||
long interval, TimeUnit unit) {
|
||||
Runnable wrapped = () -> {
|
||||
task.run();
|
||||
scheduleWithFixedDelay(task, interval, interval, unit);
|
||||
};
|
||||
return schedule(wrapped, delay, unit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAlarm(Intent intent) {
|
||||
int extraPid = intent.getIntExtra(EXTRA_PID, -1);
|
||||
int currentPid = Process.myPid();
|
||||
if (extraPid == currentPid) {
|
||||
LOG.info("Alarm");
|
||||
rescheduleAlarm();
|
||||
runDueTasks();
|
||||
} else if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Ignoring alarm with PID " + extraPid
|
||||
+ ", current PID is " + currentPid);
|
||||
}
|
||||
}
|
||||
|
||||
private void runDueTasks() {
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
List<ScheduledTask> due = new ArrayList<>();
|
||||
synchronized (lock) {
|
||||
while (true) {
|
||||
ScheduledTask s = tasks.peek();
|
||||
if (s == null || s.dueMillis > now) break;
|
||||
due.add(tasks.remove());
|
||||
}
|
||||
}
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Running " + due.size() + " due tasks");
|
||||
}
|
||||
for (ScheduledTask s : due) {
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Task is " + (now - s.dueMillis) + " ms overdue");
|
||||
}
|
||||
s.run();
|
||||
}
|
||||
}
|
||||
|
||||
private void scheduleAlarm() {
|
||||
if (SDK_INT >= 23) scheduleIdleAlarm();
|
||||
else scheduleInexactRepeatingAlarm();
|
||||
}
|
||||
|
||||
private void rescheduleAlarm() {
|
||||
// If SDK_INT < 23 the alarm repeats automatically
|
||||
if (SDK_INT >= 23) scheduleIdleAlarm();
|
||||
}
|
||||
|
||||
private void cancelAlarm() {
|
||||
alarmManager.cancel(getAlarmPendingIntent());
|
||||
}
|
||||
|
||||
private void scheduleInexactRepeatingAlarm() {
|
||||
alarmManager.setInexactRepeating(ELAPSED_REALTIME_WAKEUP,
|
||||
SystemClock.elapsedRealtime() + ALARM_MS, ALARM_MS,
|
||||
getAlarmPendingIntent());
|
||||
}
|
||||
|
||||
@TargetApi(23)
|
||||
private void scheduleIdleAlarm() {
|
||||
alarmManager.setAndAllowWhileIdle(ELAPSED_REALTIME_WAKEUP,
|
||||
SystemClock.elapsedRealtime() + ALARM_MS,
|
||||
getAlarmPendingIntent());
|
||||
}
|
||||
|
||||
private PendingIntent getAlarmPendingIntent() {
|
||||
Intent i = new Intent(app, AlarmReceiver.class);
|
||||
i.putExtra(EXTRA_PID, android.os.Process.myPid());
|
||||
return PendingIntent.getBroadcast(app, REQUEST_ALARM, i,
|
||||
FLAG_CANCEL_CURRENT);
|
||||
}
|
||||
|
||||
private static class ScheduledTask extends FutureTask<Void>
|
||||
implements Comparable<ScheduledTask> {
|
||||
|
||||
private final long dueMillis;
|
||||
|
||||
public ScheduledTask(Runnable runnable, long dueMillis) {
|
||||
super(runnable, null);
|
||||
this.dueMillis = dueMillis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(ScheduledTask s) {
|
||||
//noinspection UseCompareMethod
|
||||
if (dueMillis < s.dueMillis) return -1;
|
||||
if (dueMillis > s.dueMillis) return 1;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package org.briarproject.bramble.system;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.bramble.api.system.AlarmListener;
|
||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
||||
|
||||
import java.util.concurrent.RejectedExecutionHandler;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
|
||||
@Module
|
||||
public class AndroidTaskSchedulerModule {
|
||||
|
||||
public static class EagerSingletons {
|
||||
@Inject
|
||||
AndroidTaskScheduler scheduler;
|
||||
}
|
||||
|
||||
private final ScheduledExecutorService scheduledExecutorService;
|
||||
|
||||
public AndroidTaskSchedulerModule() {
|
||||
// Discard tasks that are submitted during shutdown
|
||||
RejectedExecutionHandler policy =
|
||||
new ScheduledThreadPoolExecutor.DiscardPolicy();
|
||||
scheduledExecutorService = new ScheduledThreadPoolExecutor(1, policy);
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
AndroidTaskScheduler provideAndroidTaskScheduler(
|
||||
LifecycleManager lifecycleManager, Application app) {
|
||||
lifecycleManager.registerForShutdown(scheduledExecutorService);
|
||||
AndroidTaskScheduler scheduler =
|
||||
new AndroidTaskScheduler(app, scheduledExecutorService);
|
||||
lifecycleManager.registerService(scheduler);
|
||||
return scheduler;
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
AlarmListener provideAlarmListener(AndroidTaskScheduler scheduler) {
|
||||
return scheduler;
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
TaskScheduler provideTaskScheduler(AndroidTaskScheduler scheduler) {
|
||||
return scheduler;
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,9 @@ 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.Future;
|
||||
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 Future<?> 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,28 @@
|
||||
package org.briarproject.bramble.api.system;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
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)}.
|
||||
*/
|
||||
Future<?> schedule(Runnable task, long delay, TimeUnit unit);
|
||||
|
||||
/**
|
||||
* See {@link ScheduledExecutorService#scheduleWithFixedDelay(Runnable, long, long, TimeUnit)}.
|
||||
*/
|
||||
Future<?> scheduleWithFixedDelay(Runnable task, long delay,
|
||||
long interval, TimeUnit unit);
|
||||
}
|
||||
@@ -9,7 +9,6 @@ import org.briarproject.bramble.plugin.PluginModule;
|
||||
import org.briarproject.bramble.properties.PropertiesModule;
|
||||
import org.briarproject.bramble.rendezvous.RendezvousModule;
|
||||
import org.briarproject.bramble.sync.validation.ValidationModule;
|
||||
import org.briarproject.bramble.system.SystemModule;
|
||||
import org.briarproject.bramble.transport.TransportModule;
|
||||
import org.briarproject.bramble.versioning.VersioningModule;
|
||||
|
||||
@@ -31,8 +30,6 @@ public interface BrambleCoreEagerSingletons {
|
||||
|
||||
void inject(RendezvousModule.EagerSingletons init);
|
||||
|
||||
void inject(SystemModule.EagerSingletons init);
|
||||
|
||||
void inject(TransportModule.EagerSingletons init);
|
||||
|
||||
void inject(ValidationModule.EagerSingletons init);
|
||||
@@ -50,7 +47,6 @@ public interface BrambleCoreEagerSingletons {
|
||||
c.inject(new RendezvousModule.EagerSingletons());
|
||||
c.inject(new PluginModule.EagerSingletons());
|
||||
c.inject(new PropertiesModule.EagerSingletons());
|
||||
c.inject(new SystemModule.EagerSingletons());
|
||||
c.inject(new TransportModule.EagerSingletons());
|
||||
c.inject(new ValidationModule.EagerSingletons());
|
||||
c.inject(new VersioningModule.EagerSingletons());
|
||||
|
||||
@@ -21,7 +21,7 @@ import org.briarproject.bramble.rendezvous.RendezvousModule;
|
||||
import org.briarproject.bramble.settings.SettingsModule;
|
||||
import org.briarproject.bramble.sync.SyncModule;
|
||||
import org.briarproject.bramble.sync.validation.ValidationModule;
|
||||
import org.briarproject.bramble.system.SystemModule;
|
||||
import org.briarproject.bramble.system.ClockModule;
|
||||
import org.briarproject.bramble.transport.TransportModule;
|
||||
import org.briarproject.bramble.versioning.VersioningModule;
|
||||
|
||||
@@ -29,6 +29,7 @@ import dagger.Module;
|
||||
|
||||
@Module(includes = {
|
||||
ClientModule.class,
|
||||
ClockModule.class,
|
||||
ConnectionModule.class,
|
||||
ContactModule.class,
|
||||
CryptoModule.class,
|
||||
@@ -48,7 +49,6 @@ import dagger.Module;
|
||||
RendezvousModule.class,
|
||||
SettingsModule.class,
|
||||
SyncModule.class,
|
||||
SystemModule.class,
|
||||
TransportModule.class,
|
||||
ValidationModule.class,
|
||||
VersioningModule.class
|
||||
|
||||
@@ -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;
|
||||
@@ -188,7 +189,7 @@ 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(() ->
|
||||
Future<?> future = scheduler.schedule(() ->
|
||||
ioExecutor.execute(task), delay, MILLISECONDS);
|
||||
tasks.put(t, new ScheduledPollTask(task, future));
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
@@ -144,7 +143,7 @@ class RendezvousPollerImpl implements RendezvousPoller, Service, EventListener {
|
||||
} catch (DbException e) {
|
||||
throw new ServiceException(e);
|
||||
}
|
||||
scheduler.scheduleAtFixedRate(this::poll, POLLING_INTERVAL_MS,
|
||||
scheduler.scheduleWithFixedDelay(this::poll, POLLING_INTERVAL_MS,
|
||||
POLLING_INTERVAL_MS, MILLISECONDS);
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.briarproject.bramble.system;
|
||||
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
|
||||
@Module
|
||||
public class ClockModule {
|
||||
|
||||
@Provides
|
||||
Clock provideClock() {
|
||||
return new SystemClock();
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,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;
|
||||
@@ -15,34 +14,26 @@ import dagger.Module;
|
||||
import dagger.Provides;
|
||||
|
||||
@Module
|
||||
public class SystemModule {
|
||||
public class DefaultTaskSchedulerModule {
|
||||
|
||||
public static class EagerSingletons {
|
||||
@Inject
|
||||
@Scheduler
|
||||
ScheduledExecutorService scheduledExecutorService;
|
||||
TaskScheduler scheduler;
|
||||
}
|
||||
|
||||
private final ScheduledExecutorService scheduler;
|
||||
private final ScheduledExecutorService scheduledExecutorService;
|
||||
|
||||
public SystemModule() {
|
||||
public DefaultTaskSchedulerModule() {
|
||||
// Discard tasks that are submitted during shutdown
|
||||
RejectedExecutionHandler policy =
|
||||
new ScheduledThreadPoolExecutor.DiscardPolicy();
|
||||
scheduler = new ScheduledThreadPoolExecutor(1, policy);
|
||||
}
|
||||
|
||||
@Provides
|
||||
Clock provideClock() {
|
||||
return new SystemClock();
|
||||
scheduledExecutorService = new ScheduledThreadPoolExecutor(1, policy);
|
||||
}
|
||||
|
||||
@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,36 @@
|
||||
package org.briarproject.bramble.system;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.system.TaskScheduler;
|
||||
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
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 Future<?> schedule(Runnable task, long delay, TimeUnit unit) {
|
||||
return delegate.schedule(task, delay, unit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<?> 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;
|
||||
@@ -196,7 +198,7 @@ class TransportKeyManagerImpl implements TransportKeyManager {
|
||||
|
||||
private void scheduleKeyUpdate(long now) {
|
||||
long delay = timePeriodLength - now % timePeriodLength;
|
||||
scheduler.schedule((Runnable) this::updateKeys, delay, MILLISECONDS);
|
||||
scheduler.schedule(this::updateKeys, delay, MILLISECONDS);
|
||||
}
|
||||
|
||||
private void updateKeys() {
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package org.briarproject.bramble;
|
||||
|
||||
import org.briarproject.bramble.system.DefaultTaskSchedulerModule;
|
||||
|
||||
public interface BrambleCoreIntegrationTestEagerSingletons
|
||||
extends BrambleCoreEagerSingletons {
|
||||
|
||||
void inject(DefaultTaskSchedulerModule.EagerSingletons init);
|
||||
|
||||
class Helper {
|
||||
|
||||
public static void injectEagerSingletons(
|
||||
BrambleCoreIntegrationTestEagerSingletons c) {
|
||||
BrambleCoreEagerSingletons.Helper.injectEagerSingletons(c);
|
||||
c.inject(new DefaultTaskSchedulerModule.EagerSingletons());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package org.briarproject.bramble.contact;
|
||||
|
||||
import org.briarproject.bramble.BrambleCoreEagerSingletons;
|
||||
import org.briarproject.bramble.BrambleCoreIntegrationTestEagerSingletons;
|
||||
import org.briarproject.bramble.api.Pair;
|
||||
import org.briarproject.bramble.api.contact.Contact;
|
||||
import org.briarproject.bramble.api.contact.ContactManager;
|
||||
@@ -62,11 +62,13 @@ public class ContactExchangeIntegrationTest extends BrambleTestCase {
|
||||
alice = DaggerContactExchangeIntegrationTestComponent.builder()
|
||||
.testDatabaseConfigModule(
|
||||
new TestDatabaseConfigModule(aliceDir)).build();
|
||||
BrambleCoreEagerSingletons.Helper.injectEagerSingletons(alice);
|
||||
BrambleCoreIntegrationTestEagerSingletons.Helper
|
||||
.injectEagerSingletons(alice);
|
||||
bob = DaggerContactExchangeIntegrationTestComponent.builder()
|
||||
.testDatabaseConfigModule(new TestDatabaseConfigModule(bobDir))
|
||||
.build();
|
||||
BrambleCoreEagerSingletons.Helper.injectEagerSingletons(bob);
|
||||
BrambleCoreIntegrationTestEagerSingletons.Helper
|
||||
.injectEagerSingletons(bob);
|
||||
// Set up the devices and get the identities
|
||||
aliceIdentity = setUp(alice, "Alice");
|
||||
bobIdentity = setUp(bob, "Bob");
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package org.briarproject.bramble.contact;
|
||||
|
||||
import org.briarproject.bramble.BrambleCoreEagerSingletons;
|
||||
import org.briarproject.bramble.BrambleCoreIntegrationTestEagerSingletons;
|
||||
import org.briarproject.bramble.BrambleCoreModule;
|
||||
import org.briarproject.bramble.api.connection.ConnectionManager;
|
||||
import org.briarproject.bramble.api.contact.ContactExchangeManager;
|
||||
@@ -23,7 +23,7 @@ import dagger.Component;
|
||||
BrambleCoreModule.class
|
||||
})
|
||||
interface ContactExchangeIntegrationTestComponent
|
||||
extends BrambleCoreEagerSingletons {
|
||||
extends BrambleCoreIntegrationTestEagerSingletons {
|
||||
|
||||
ConnectionManager getConnectionManager();
|
||||
|
||||
|
||||
@@ -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,8 +31,7 @@ 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 java.util.concurrent.Future;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.Collections.emptyList;
|
||||
@@ -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,7 @@ 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 Future<?> future = context.mock(Future.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);
|
||||
@@ -123,7 +122,7 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
|
||||
PendingContactStateChangedEvent.class, e ->
|
||||
e.getPendingContactState() == OFFLINE)));
|
||||
// Capture the poll task
|
||||
oneOf(scheduler).scheduleAtFixedRate(with(any(Runnable.class)),
|
||||
oneOf(scheduler).scheduleWithFixedDelay(with(any(Runnable.class)),
|
||||
with(POLLING_INTERVAL_MS), with(POLLING_INTERVAL_MS),
|
||||
with(MILLISECONDS));
|
||||
will(new CaptureArgumentAction<>(capturePollTask, Runnable.class,
|
||||
@@ -159,7 +158,7 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
|
||||
PendingContactStateChangedEvent.class, e ->
|
||||
e.getPendingContactState() == FAILED)));
|
||||
// Schedule the poll task
|
||||
oneOf(scheduler).scheduleAtFixedRate(with(any(Runnable.class)),
|
||||
oneOf(scheduler).scheduleWithFixedDelay(with(any(Runnable.class)),
|
||||
with(POLLING_INTERVAL_MS), with(POLLING_INTERVAL_MS),
|
||||
with(MILLISECONDS));
|
||||
}});
|
||||
@@ -468,7 +467,7 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
|
||||
oneOf(db).getPendingContacts(txn);
|
||||
will(returnValue(emptyList()));
|
||||
// Capture the poll task
|
||||
oneOf(scheduler).scheduleAtFixedRate(with(any(Runnable.class)),
|
||||
oneOf(scheduler).scheduleWithFixedDelay(with(any(Runnable.class)),
|
||||
with(POLLING_INTERVAL_MS), with(POLLING_INTERVAL_MS),
|
||||
with(MILLISECONDS));
|
||||
will(new CaptureArgumentAction<>(capturePollTask, Runnable.class,
|
||||
@@ -545,7 +544,7 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
|
||||
PendingContactStateChangedEvent.class, e ->
|
||||
e.getPendingContactState() == OFFLINE)));
|
||||
// Capture the poll task
|
||||
oneOf(scheduler).scheduleAtFixedRate(with(any(Runnable.class)),
|
||||
oneOf(scheduler).scheduleWithFixedDelay(with(any(Runnable.class)),
|
||||
with(POLLING_INTERVAL_MS), with(POLLING_INTERVAL_MS),
|
||||
with(MILLISECONDS));
|
||||
will(new CaptureArgumentAction<>(capturePollTask, Runnable.class,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package org.briarproject.bramble.sync;
|
||||
|
||||
import org.briarproject.bramble.BrambleCoreEagerSingletons;
|
||||
import org.briarproject.bramble.BrambleCoreIntegrationTestEagerSingletons;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.crypto.TransportCrypto;
|
||||
@@ -73,7 +73,8 @@ public class SyncIntegrationTest extends BrambleTestCase {
|
||||
|
||||
SyncIntegrationTestComponent component =
|
||||
DaggerSyncIntegrationTestComponent.builder().build();
|
||||
BrambleCoreEagerSingletons.Helper.injectEagerSingletons(component);
|
||||
BrambleCoreIntegrationTestEagerSingletons.Helper
|
||||
.injectEagerSingletons(component);
|
||||
component.inject(this);
|
||||
|
||||
contactId = getContactId();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package org.briarproject.bramble.sync;
|
||||
|
||||
import org.briarproject.bramble.BrambleCoreEagerSingletons;
|
||||
import org.briarproject.bramble.BrambleCoreIntegrationTestEagerSingletons;
|
||||
import org.briarproject.bramble.BrambleCoreModule;
|
||||
import org.briarproject.bramble.test.BrambleCoreIntegrationTestModule;
|
||||
|
||||
@@ -13,7 +13,8 @@ import dagger.Component;
|
||||
BrambleCoreIntegrationTestModule.class,
|
||||
BrambleCoreModule.class
|
||||
})
|
||||
interface SyncIntegrationTestComponent extends BrambleCoreEagerSingletons {
|
||||
interface SyncIntegrationTestComponent extends
|
||||
BrambleCoreIntegrationTestEagerSingletons {
|
||||
|
||||
void inject(SyncIntegrationTest testCase);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.briarproject.bramble.test;
|
||||
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 dagger.Module;
|
||||
import dagger.Provides;
|
||||
@@ -10,6 +11,7 @@ import dagger.Provides;
|
||||
@Module(includes = {
|
||||
DefaultBatteryManagerModule.class,
|
||||
DefaultEventExecutorModule.class,
|
||||
DefaultTaskSchedulerModule.class,
|
||||
TestDatabaseConfigModule.class,
|
||||
TestPluginConfigModule.class,
|
||||
TestSecureRandomModule.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();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package org.briarproject.bramble.plugin.tor;
|
||||
|
||||
import org.briarproject.bramble.BrambleCoreEagerSingletons;
|
||||
import org.briarproject.bramble.BrambleCoreIntegrationTestEagerSingletons;
|
||||
import org.briarproject.bramble.api.battery.BatteryManager;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||
@@ -47,7 +47,8 @@ public class BridgeTest extends BrambleTestCase {
|
||||
public static Iterable<String> data() {
|
||||
BrambleJavaIntegrationTestComponent component =
|
||||
DaggerBrambleJavaIntegrationTestComponent.builder().build();
|
||||
BrambleCoreEagerSingletons.Helper.injectEagerSingletons(component);
|
||||
BrambleCoreIntegrationTestEagerSingletons.Helper
|
||||
.injectEagerSingletons(component);
|
||||
return component.getCircumventionProvider().getBridges(false);
|
||||
}
|
||||
|
||||
@@ -92,7 +93,8 @@ public class BridgeTest extends BrambleTestCase {
|
||||
|
||||
BrambleJavaIntegrationTestComponent component =
|
||||
DaggerBrambleJavaIntegrationTestComponent.builder().build();
|
||||
BrambleCoreEagerSingletons.Helper.injectEagerSingletons(component);
|
||||
BrambleCoreIntegrationTestEagerSingletons.Helper
|
||||
.injectEagerSingletons(component);
|
||||
component.inject(this);
|
||||
|
||||
LocationUtils locationUtils = () -> "US";
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package org.briarproject.bramble.test;
|
||||
|
||||
import org.briarproject.bramble.BrambleCoreEagerSingletons;
|
||||
import org.briarproject.bramble.BrambleCoreIntegrationTestEagerSingletons;
|
||||
import org.briarproject.bramble.BrambleCoreModule;
|
||||
import org.briarproject.bramble.BrambleJavaModule;
|
||||
import org.briarproject.bramble.plugin.tor.BridgeTest;
|
||||
@@ -17,7 +17,7 @@ import dagger.Component;
|
||||
BrambleJavaModule.class
|
||||
})
|
||||
public interface BrambleJavaIntegrationTestComponent
|
||||
extends BrambleCoreEagerSingletons {
|
||||
extends BrambleCoreIntegrationTestEagerSingletons {
|
||||
|
||||
void inject(BridgeTest init);
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.briarproject.briar.android;
|
||||
|
||||
import org.briarproject.bramble.BrambleAndroidEagerSingletons;
|
||||
import org.briarproject.bramble.BrambleAndroidModule;
|
||||
import org.briarproject.bramble.BrambleAppComponent;
|
||||
import org.briarproject.bramble.BrambleCoreEagerSingletons;
|
||||
import org.briarproject.bramble.BrambleCoreModule;
|
||||
import org.briarproject.bramble.account.BriarAccountModule;
|
||||
@@ -73,7 +74,7 @@ import dagger.Component;
|
||||
})
|
||||
public interface AndroidComponent
|
||||
extends BrambleCoreEagerSingletons, BrambleAndroidEagerSingletons,
|
||||
BriarCoreEagerSingletons, AndroidEagerSingletons {
|
||||
BriarCoreEagerSingletons, AndroidEagerSingletons, BrambleAppComponent {
|
||||
|
||||
// Exposed objects
|
||||
@CryptoExecutor
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.briarproject.briar.android;
|
||||
import android.app.Activity;
|
||||
import android.content.SharedPreferences;
|
||||
|
||||
import org.briarproject.bramble.BrambleApplication;
|
||||
import org.briarproject.briar.android.navdrawer.NavDrawerActivity;
|
||||
|
||||
import java.util.Collection;
|
||||
@@ -12,7 +13,7 @@ import java.util.logging.LogRecord;
|
||||
* This exists so that the Application object will not necessarily be cast
|
||||
* directly to the Briar application object.
|
||||
*/
|
||||
public interface BriarApplication {
|
||||
public interface BriarApplication extends BrambleApplication {
|
||||
|
||||
Class<? extends Activity> ENTRY_ACTIVITY = NavDrawerActivity.class;
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ import org.acra.ACRA;
|
||||
import org.acra.ReportingInteractionMode;
|
||||
import org.acra.annotation.ReportsCrashes;
|
||||
import org.briarproject.bramble.BrambleAndroidEagerSingletons;
|
||||
import org.briarproject.bramble.BrambleAppComponent;
|
||||
import org.briarproject.bramble.BrambleCoreEagerSingletons;
|
||||
import org.briarproject.briar.BriarCoreEagerSingletons;
|
||||
import org.briarproject.briar.BuildConfig;
|
||||
@@ -170,6 +171,11 @@ public class BriarApplicationImpl extends Application
|
||||
StrictMode.setVmPolicy(vmPolicy.build());
|
||||
}
|
||||
|
||||
@Override
|
||||
public BrambleAppComponent getBrambleAppComponent() {
|
||||
return applicationComponent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<LogRecord> getRecentLogRecords() {
|
||||
return logHandler.getRecentLogRecords();
|
||||
|
||||
@@ -8,6 +8,7 @@ import com.vanniktech.emoji.EmojiManager;
|
||||
import com.vanniktech.emoji.google.GoogleEmojiProvider;
|
||||
|
||||
import org.briarproject.bramble.BrambleAndroidEagerSingletons;
|
||||
import org.briarproject.bramble.BrambleAppComponent;
|
||||
import org.briarproject.bramble.BrambleCoreEagerSingletons;
|
||||
import org.briarproject.briar.BriarCoreEagerSingletons;
|
||||
|
||||
@@ -53,6 +54,11 @@ public class TestBriarApplication extends Application
|
||||
EmojiManager.install(new GoogleEmojiProvider());
|
||||
}
|
||||
|
||||
@Override
|
||||
public BrambleAppComponent getBrambleAppComponent() {
|
||||
return applicationComponent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<LogRecord> getRecentLogRecords() {
|
||||
return emptyList();
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package org.briarproject.briar.feed;
|
||||
|
||||
import org.briarproject.bramble.BrambleCoreEagerSingletons;
|
||||
import org.briarproject.bramble.BrambleCoreIntegrationTestEagerSingletons;
|
||||
import org.briarproject.bramble.BrambleCoreModule;
|
||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||
@@ -27,7 +27,7 @@ import dagger.Component;
|
||||
TestSocksModule.class,
|
||||
})
|
||||
interface FeedManagerIntegrationTestComponent
|
||||
extends BrambleCoreEagerSingletons {
|
||||
extends BrambleCoreIntegrationTestEagerSingletons {
|
||||
|
||||
void inject(FeedManagerIntegrationTest testCase);
|
||||
|
||||
@@ -47,7 +47,8 @@ interface FeedManagerIntegrationTestComponent
|
||||
|
||||
public static void injectEagerSingletons(
|
||||
FeedManagerIntegrationTestComponent c) {
|
||||
BrambleCoreEagerSingletons.Helper.injectEagerSingletons(c);
|
||||
BrambleCoreIntegrationTestEagerSingletons.Helper
|
||||
.injectEagerSingletons(c);
|
||||
c.inject(new BlogModule.EagerSingletons());
|
||||
c.inject(new FeedModule.EagerSingletons());
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package org.briarproject.briar.messaging;
|
||||
|
||||
import org.briarproject.bramble.BrambleCoreEagerSingletons;
|
||||
import org.briarproject.bramble.BrambleCoreIntegrationTestEagerSingletons;
|
||||
import org.briarproject.bramble.BrambleCoreModule;
|
||||
import org.briarproject.bramble.test.BrambleCoreIntegrationTestModule;
|
||||
import org.briarproject.briar.client.BriarClientModule;
|
||||
@@ -19,7 +19,7 @@ import dagger.Component;
|
||||
MessagingModule.class
|
||||
})
|
||||
interface MessageSizeIntegrationTestComponent
|
||||
extends BrambleCoreEagerSingletons {
|
||||
extends BrambleCoreIntegrationTestEagerSingletons {
|
||||
|
||||
void inject(MessageSizeIntegrationTest testCase);
|
||||
|
||||
@@ -31,7 +31,8 @@ interface MessageSizeIntegrationTestComponent
|
||||
|
||||
public static void injectEagerSingletons(
|
||||
MessageSizeIntegrationTestComponent c) {
|
||||
BrambleCoreEagerSingletons.Helper.injectEagerSingletons(c);
|
||||
BrambleCoreIntegrationTestEagerSingletons.Helper
|
||||
.injectEagerSingletons(c);
|
||||
c.inject(new ForumModule.EagerSingletons());
|
||||
c.inject(new MessagingModule.EagerSingletons());
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package org.briarproject.briar.messaging;
|
||||
|
||||
import org.briarproject.bramble.BrambleCoreEagerSingletons;
|
||||
import org.briarproject.bramble.BrambleCoreIntegrationTestEagerSingletons;
|
||||
import org.briarproject.bramble.BrambleCoreModule;
|
||||
import org.briarproject.bramble.api.connection.ConnectionManager;
|
||||
import org.briarproject.bramble.api.contact.ContactManager;
|
||||
@@ -24,7 +24,7 @@ import dagger.Component;
|
||||
MessagingModule.class
|
||||
})
|
||||
interface SimplexMessagingIntegrationTestComponent
|
||||
extends BrambleCoreEagerSingletons {
|
||||
extends BrambleCoreIntegrationTestEagerSingletons {
|
||||
|
||||
void inject(MessagingModule.EagerSingletons init);
|
||||
|
||||
@@ -46,7 +46,8 @@ interface SimplexMessagingIntegrationTestComponent
|
||||
|
||||
public static void injectEagerSingletons(
|
||||
SimplexMessagingIntegrationTestComponent c) {
|
||||
BrambleCoreEagerSingletons.Helper.injectEagerSingletons(c);
|
||||
BrambleCoreIntegrationTestEagerSingletons.Helper
|
||||
.injectEagerSingletons(c);
|
||||
c.inject(new MessagingModule.EagerSingletons());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package org.briarproject.briar.test;
|
||||
|
||||
import org.briarproject.bramble.BrambleCoreEagerSingletons;
|
||||
import org.briarproject.bramble.BrambleCoreIntegrationTestEagerSingletons;
|
||||
import org.briarproject.bramble.BrambleCoreModule;
|
||||
import org.briarproject.bramble.api.client.ClientHelper;
|
||||
import org.briarproject.bramble.api.connection.ConnectionManager;
|
||||
@@ -51,7 +51,7 @@ import dagger.Component;
|
||||
SharingModule.class
|
||||
})
|
||||
public interface BriarIntegrationTestComponent
|
||||
extends BrambleCoreEagerSingletons {
|
||||
extends BrambleCoreIntegrationTestEagerSingletons {
|
||||
|
||||
void inject(BriarIntegrationTest<BriarIntegrationTestComponent> init);
|
||||
|
||||
@@ -115,7 +115,8 @@ public interface BriarIntegrationTestComponent
|
||||
|
||||
public static void injectEagerSingletons(
|
||||
BriarIntegrationTestComponent c) {
|
||||
BrambleCoreEagerSingletons.Helper.injectEagerSingletons(c);
|
||||
BrambleCoreIntegrationTestEagerSingletons.Helper
|
||||
.injectEagerSingletons(c);
|
||||
c.inject(new BlogModule.EagerSingletons());
|
||||
c.inject(new ForumModule.EagerSingletons());
|
||||
c.inject(new GroupInvitationModule.EagerSingletons());
|
||||
|
||||
@@ -3,8 +3,6 @@ package org.briarproject.briar.headless
|
||||
import dagger.Component
|
||||
import org.briarproject.bramble.BrambleCoreEagerSingletons
|
||||
import org.briarproject.bramble.BrambleCoreModule
|
||||
import org.briarproject.bramble.account.AccountModule
|
||||
import org.briarproject.bramble.system.DesktopSecureRandomModule
|
||||
import org.briarproject.briar.BriarCoreEagerSingletons
|
||||
import org.briarproject.briar.BriarCoreModule
|
||||
import java.security.SecureRandom
|
||||
@@ -14,8 +12,6 @@ import javax.inject.Singleton
|
||||
modules = [
|
||||
BrambleCoreModule::class,
|
||||
BriarCoreModule::class,
|
||||
DesktopSecureRandomModule::class,
|
||||
AccountModule::class,
|
||||
HeadlessModule::class
|
||||
]
|
||||
)
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.briarproject.briar.headless
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import org.briarproject.bramble.account.AccountModule
|
||||
import org.briarproject.bramble.api.FeatureFlags
|
||||
import org.briarproject.bramble.api.battery.BatteryManager
|
||||
import org.briarproject.bramble.api.db.DatabaseConfig
|
||||
@@ -24,6 +25,8 @@ import org.briarproject.bramble.plugin.tor.CircumventionModule
|
||||
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.DesktopSecureRandomModule
|
||||
import org.briarproject.bramble.system.JavaSystemModule
|
||||
import org.briarproject.bramble.util.OsUtils.isLinux
|
||||
import org.briarproject.bramble.util.OsUtils.isMac
|
||||
@@ -40,17 +43,20 @@ import javax.net.SocketFactory
|
||||
|
||||
@Module(
|
||||
includes = [
|
||||
JavaNetworkModule::class,
|
||||
JavaSystemModule::class,
|
||||
AccountModule::class,
|
||||
CircumventionModule::class,
|
||||
DefaultBatteryManagerModule::class,
|
||||
DefaultEventExecutorModule::class,
|
||||
SocksModule::class,
|
||||
DefaultTaskSchedulerModule::class,
|
||||
DesktopSecureRandomModule::class,
|
||||
HeadlessBlogModule::class,
|
||||
HeadlessContactModule::class,
|
||||
HeadlessEventModule::class,
|
||||
HeadlessForumModule::class,
|
||||
HeadlessMessagingModule::class
|
||||
HeadlessMessagingModule::class,
|
||||
JavaNetworkModule::class,
|
||||
JavaSystemModule::class,
|
||||
SocksModule::class
|
||||
]
|
||||
)
|
||||
internal class HeadlessModule(private val appDir: File) {
|
||||
|
||||
@@ -3,10 +3,7 @@ package org.briarproject.briar.headless
|
||||
import dagger.Component
|
||||
import org.briarproject.bramble.BrambleCoreEagerSingletons
|
||||
import org.briarproject.bramble.BrambleCoreModule
|
||||
import org.briarproject.bramble.account.AccountModule
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent
|
||||
import org.briarproject.bramble.event.DefaultEventExecutorModule
|
||||
import org.briarproject.bramble.test.TestSecureRandomModule
|
||||
import org.briarproject.briar.BriarCoreEagerSingletons
|
||||
import org.briarproject.briar.BriarCoreModule
|
||||
import org.briarproject.briar.api.test.TestDataCreator
|
||||
@@ -16,9 +13,6 @@ import javax.inject.Singleton
|
||||
modules = [
|
||||
BrambleCoreModule::class,
|
||||
BriarCoreModule::class,
|
||||
TestSecureRandomModule::class,
|
||||
AccountModule::class,
|
||||
DefaultEventExecutorModule::class,
|
||||
HeadlessTestModule::class
|
||||
]
|
||||
)
|
||||
|
||||
@@ -3,16 +3,20 @@ package org.briarproject.briar.headless
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import org.briarproject.bramble.account.AccountModule
|
||||
import org.briarproject.bramble.api.FeatureFlags
|
||||
import org.briarproject.bramble.api.db.DatabaseConfig
|
||||
import org.briarproject.bramble.api.plugin.PluginConfig
|
||||
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.event.DefaultEventExecutorModule
|
||||
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.JavaSystemModule
|
||||
import org.briarproject.bramble.test.TestSecureRandomModule
|
||||
import org.briarproject.briar.headless.blogs.HeadlessBlogModule
|
||||
import org.briarproject.briar.headless.contact.HeadlessContactModule
|
||||
import org.briarproject.briar.headless.event.HeadlessEventModule
|
||||
@@ -26,8 +30,12 @@ import javax.inject.Singleton
|
||||
includes = [
|
||||
JavaNetworkModule::class,
|
||||
JavaSystemModule::class,
|
||||
AccountModule::class,
|
||||
CircumventionModule::class,
|
||||
DefaultEventExecutorModule::class,
|
||||
DefaultTaskSchedulerModule::class,
|
||||
SocksModule::class,
|
||||
TestSecureRandomModule::class,
|
||||
HeadlessBlogModule::class,
|
||||
HeadlessContactModule::class,
|
||||
HeadlessEventModule::class,
|
||||
|
||||
Reference in New Issue
Block a user