Added getMaxLatency() method to transport plugins.

This commit is contained in:
akwizgran
2013-02-01 20:19:23 +00:00
parent 14177e51f6
commit a73d9d05f2
24 changed files with 101 additions and 51 deletions

View File

@@ -14,6 +14,9 @@ public interface Plugin {
/** Returns a label for looking up the plugin's translated name. */
String getName();
/** Returns the transport's maximum latency in milliseconds. */
long getMaxLatency();
/** Starts the plugin and returns true if it started successfully. */
boolean start() throws IOException;

View File

@@ -50,7 +50,7 @@ class BluetoothPlugin implements DuplexPlugin {
private final Executor pluginExecutor;
private final Clock clock;
private final DuplexPluginCallback callback;
private final long pollingInterval;
private final long maxLatency, pollingInterval;
private final Object discoveryLock = new Object();
private final ScheduledExecutorService scheduler;
@@ -61,10 +61,12 @@ class BluetoothPlugin implements DuplexPlugin {
private volatile LocalDevice localDevice = null;
BluetoothPlugin(@PluginExecutor Executor pluginExecutor, Clock clock,
DuplexPluginCallback callback, long pollingInterval) {
DuplexPluginCallback callback, long maxLatency,
long pollingInterval) {
this.pluginExecutor = pluginExecutor;
this.clock = clock;
this.callback = callback;
this.maxLatency = maxLatency;
this.pollingInterval = pollingInterval;
scheduler = Executors.newScheduledThreadPool(0);
}
@@ -77,6 +79,10 @@ class BluetoothPlugin implements DuplexPlugin {
return "BLUETOOTH_PLUGIN_NAME";
}
public long getMaxLatency() {
return maxLatency;
}
public boolean start() throws IOException {
// Initialise the Bluetooth stack
try {

View File

@@ -12,7 +12,8 @@ import net.sf.briar.api.plugins.duplex.DuplexPluginFactory;
public class BluetoothPluginFactory implements DuplexPluginFactory {
private static final long POLLING_INTERVAL = 3L * 60L * 1000L; // 3 mins
private static final long MAX_LATENCY = 60L * 1000L; // 1 minute
private static final long POLLING_INTERVAL = 3L * 60L * 1000L; // 3 minutes
private final Executor pluginExecutor;
private final Clock clock;
@@ -28,6 +29,6 @@ public class BluetoothPluginFactory implements DuplexPluginFactory {
public DuplexPlugin createPlugin(DuplexPluginCallback callback) {
return new BluetoothPlugin(pluginExecutor, clock, callback,
POLLING_INTERVAL);
MAX_LATENCY, POLLING_INTERVAL);
}
}

View File

@@ -60,7 +60,7 @@ class DroidtoothPlugin implements DuplexPlugin {
private final AndroidExecutor androidExecutor;
private final Context appContext;
private final DuplexPluginCallback callback;
private final long pollingInterval;
private final long maxLatency, pollingInterval;
private volatile boolean running = false;
private volatile BluetoothServerSocket socket = null;
@@ -70,11 +70,13 @@ class DroidtoothPlugin implements DuplexPlugin {
DroidtoothPlugin(@PluginExecutor Executor pluginExecutor,
AndroidExecutor androidExecutor, Context appContext,
DuplexPluginCallback callback, long pollingInterval) {
DuplexPluginCallback callback, long maxLatency,
long pollingInterval) {
this.pluginExecutor = pluginExecutor;
this.androidExecutor = androidExecutor;
this.appContext = appContext;
this.callback = callback;
this.maxLatency = maxLatency;
this.pollingInterval = pollingInterval;
}
@@ -87,6 +89,10 @@ class DroidtoothPlugin implements DuplexPlugin {
return "BLUETOOTH_PLUGIN_NAME";
}
public long getMaxLatency() {
return maxLatency;
}
public boolean start() throws IOException {
// BluetoothAdapter.getDefaultAdapter() must be called on a thread
// with a message queue, so submit it to the AndroidExecutor

View File

@@ -12,7 +12,8 @@ import android.content.Context;
public class DroidtoothPluginFactory implements DuplexPluginFactory {
private static final long POLLING_INTERVAL = 3L * 60L * 1000L; // 3 mins
private static final long MAX_LATENCY = 60L * 1000L; // 1 minute
private static final long POLLING_INTERVAL = 3L * 60L * 1000L; // 3 minutes
private final Executor pluginExecutor;
private final AndroidExecutor androidExecutor;
@@ -31,6 +32,6 @@ public class DroidtoothPluginFactory implements DuplexPluginFactory {
public DuplexPlugin createPlugin(DuplexPluginCallback callback) {
return new DroidtoothPlugin(pluginExecutor, androidExecutor, appContext,
callback, POLLING_INTERVAL);
callback, MAX_LATENCY, POLLING_INTERVAL);
}
}

View File

@@ -31,13 +31,15 @@ implements RemovableDriveMonitor.Callback {
private final RemovableDriveFinder finder;
private final RemovableDriveMonitor monitor;
private final long maxLatency;
RemovableDrivePlugin(@PluginExecutor Executor pluginExecutor,
SimplexPluginCallback callback, RemovableDriveFinder finder,
RemovableDriveMonitor monitor) {
RemovableDriveMonitor monitor, long maxLatency) {
super(pluginExecutor, callback);
this.finder = finder;
this.monitor = monitor;
this.maxLatency = maxLatency;
}
public TransportId getId() {
@@ -48,6 +50,10 @@ implements RemovableDriveMonitor.Callback {
return "REMOVABLE_DRIVE_PLUGIN_NAME";
}
public long getMaxLatency() {
return maxLatency;
}
public boolean start() throws IOException {
running = true;
monitor.start(this);

View File

@@ -11,6 +11,8 @@ import net.sf.briar.util.OsUtils;
public class RemovableDrivePluginFactory implements SimplexPluginFactory {
// Maximum latency 14 days (Royal Mail or lackadaisical carrier pigeon)
private static final long MAX_LATENCY = 14L * 24L * 60L * 60L * 1000L;
private static final long POLLING_INTERVAL = 10L * 1000L; // 10 seconds
private final Executor pluginExecutor;
@@ -46,6 +48,6 @@ public class RemovableDrivePluginFactory implements SimplexPluginFactory {
return null;
}
return new RemovableDrivePlugin(pluginExecutor, callback, finder,
monitor);
monitor, MAX_LATENCY);
}
}

View File

@@ -41,7 +41,7 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
private final ModemFactory modemFactory;
private final SerialPortList serialPortList;
private final DuplexPluginCallback callback;
private final long pollingInterval;
private final long maxLatency, pollingInterval;
private final boolean shuffle; // Used to disable shuffling for testing
private volatile boolean running = false;
@@ -49,12 +49,13 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
ModemPlugin(@PluginExecutor Executor pluginExecutor,
ModemFactory modemFactory, SerialPortList serialPortList,
DuplexPluginCallback callback, long pollingInterval,
boolean shuffle) {
DuplexPluginCallback callback, long maxLatency,
long pollingInterval, boolean shuffle) {
this.pluginExecutor = pluginExecutor;
this.modemFactory = modemFactory;
this.serialPortList = serialPortList;
this.callback = callback;
this.maxLatency = maxLatency;
this.pollingInterval = pollingInterval;
this.shuffle = shuffle;
}
@@ -67,6 +68,10 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
return "MODEM_PLUGIN_NAME";
}
public long getMaxLatency() {
return maxLatency;
}
public boolean start() {
for(String portName : serialPortList.getPortNames()) {
if(LOG.isLoggable(INFO))

View File

@@ -12,6 +12,7 @@ import net.sf.briar.util.StringUtils;
public class ModemPluginFactory implements DuplexPluginFactory {
private static final long MAX_LATENCY = 60L * 1000L; // 1 minute
private static final long POLLING_INTERVAL = 60L * 60L * 1000L; // 1 hour
private final Executor pluginExecutor;
@@ -34,6 +35,6 @@ public class ModemPluginFactory implements DuplexPluginFactory {
String enabled = callback.getConfig().get("enabled");
if(StringUtils.isNullOrEmpty(enabled)) return null;
return new ModemPlugin(pluginExecutor, modemFactory, serialPortList,
callback, POLLING_INTERVAL, true);
callback, MAX_LATENCY, POLLING_INTERVAL, true);
}
}

View File

@@ -46,8 +46,9 @@ class LanTcpPlugin extends TcpPlugin {
private final Clock clock;
LanTcpPlugin(@PluginExecutor Executor pluginExecutor, Clock clock,
DuplexPluginCallback callback, long pollingInterval) {
super(pluginExecutor, callback, pollingInterval);
DuplexPluginCallback callback, long maxLatency,
long pollingInterval) {
super(pluginExecutor, callback, maxLatency, pollingInterval);
this.clock = clock;
}

View File

@@ -12,6 +12,7 @@ import net.sf.briar.api.plugins.duplex.DuplexPluginFactory;
public class LanTcpPluginFactory implements DuplexPluginFactory {
private static final long MAX_LATENCY = 60L * 1000L; // 1 minute
private static final long POLLING_INTERVAL = 60L * 1000L; // 1 minute
private final Executor pluginExecutor;
@@ -27,7 +28,7 @@ public class LanTcpPluginFactory implements DuplexPluginFactory {
}
public DuplexPlugin createPlugin(DuplexPluginCallback callback) {
return new LanTcpPlugin(pluginExecutor, clock, callback,
return new LanTcpPlugin(pluginExecutor, clock, callback, MAX_LATENCY,
POLLING_INTERVAL);
}
}

View File

@@ -31,7 +31,7 @@ abstract class TcpPlugin implements DuplexPlugin {
protected final Executor pluginExecutor;
protected final DuplexPluginCallback callback;
protected final long pollingInterval;
protected final long maxLatency, pollingInterval;
protected volatile boolean running = false;
private volatile ServerSocket socket = null;
@@ -43,12 +43,18 @@ abstract class TcpPlugin implements DuplexPlugin {
protected abstract List<SocketAddress> getLocalSocketAddresses();
protected TcpPlugin(@PluginExecutor Executor pluginExecutor,
DuplexPluginCallback callback, long pollingInterval) {
DuplexPluginCallback callback, long maxLatency,
long pollingInterval) {
this.pluginExecutor = pluginExecutor;
this.callback = callback;
this.maxLatency = maxLatency;
this.pollingInterval = pollingInterval;
}
public long getMaxLatency() {
return maxLatency;
}
public boolean start() {
running = true;
pluginExecutor.execute(new Runnable() {

View File

@@ -38,9 +38,9 @@ class WanTcpPlugin extends TcpPlugin {
private volatile MappingResult mappingResult;
WanTcpPlugin(@PluginExecutor Executor pluginExecutor,
DuplexPluginCallback callback, long pollingInterval,
PortMapper portMapper) {
super(pluginExecutor, callback, pollingInterval);
DuplexPluginCallback callback, long maxLatency,
long pollingInterval, PortMapper portMapper) {
super(pluginExecutor, callback, maxLatency, pollingInterval);
this.portMapper = portMapper;
}

View File

@@ -11,6 +11,7 @@ import net.sf.briar.api.plugins.duplex.DuplexPluginFactory;
public class WanTcpPluginFactory implements DuplexPluginFactory {
private static final long MAX_LATENCY = 1L * 60L; // 1 minute
private static final long POLLING_INTERVAL = 5L * 60L * 1000L; // 5 minutes
private final Executor pluginExecutor;
@@ -27,7 +28,7 @@ public class WanTcpPluginFactory implements DuplexPluginFactory {
}
public DuplexPlugin createPlugin(DuplexPluginCallback callback) {
return new WanTcpPlugin(pluginExecutor, callback, POLLING_INTERVAL,
new PortMapperImpl(shutdownManager));
return new WanTcpPlugin(pluginExecutor, callback, MAX_LATENCY,
POLLING_INTERVAL, new PortMapperImpl(shutdownManager));
}
}

View File

@@ -46,16 +46,18 @@ class TorPlugin implements DuplexPlugin {
private final Executor pluginExecutor;
private final DuplexPluginCallback callback;
private final long pollingInterval;
private final long maxLatency, pollingInterval;
private boolean running = false, connected = false; // Locking: this
private NetLayer netLayer = null; // Locking: this
private NetServerSocket socket = null; // Locking: this
TorPlugin(@PluginExecutor Executor pluginExecutor,
DuplexPluginCallback callback, long pollingInterval) {
DuplexPluginCallback callback, long maxLatency,
long pollingInterval) {
this.pluginExecutor = pluginExecutor;
this.callback = callback;
this.maxLatency = maxLatency;
this.pollingInterval = pollingInterval;
}
@@ -67,6 +69,10 @@ class TorPlugin implements DuplexPlugin {
return "TOR_PLUGIN_NAME";
}
public long getMaxLatency() {
return maxLatency;
}
public boolean start() {
synchronized(this) {
running = true;

View File

@@ -11,6 +11,7 @@ import net.sf.briar.util.StringUtils;
public class TorPluginFactory implements DuplexPluginFactory {
private static final long MAX_LATENCY = 5L * 60L * 1000L; // 5 minutes
private static final long POLLING_INTERVAL = 15L * 60L * 1000L; // 15 mins
private final Executor pluginExecutor;
@@ -27,6 +28,7 @@ public class TorPluginFactory implements DuplexPluginFactory {
// This plugin is not enabled by default
String enabled = callback.getConfig().get("enabled");
if(StringUtils.isNullOrEmpty(enabled)) return null;
return new TorPlugin(pluginExecutor, callback, POLLING_INTERVAL);
return new TorPlugin(pluginExecutor, callback, MAX_LATENCY,
POLLING_INTERVAL);
}
}

View File

@@ -26,7 +26,8 @@ public class BluetoothClientTest extends DuplexClientTest {
// Create the plugin
callback = new ClientCallback(new TransportConfig(),
new TransportProperties(), remote);
plugin = new BluetoothPlugin(executor, new SystemClock(), callback, 0L);
plugin = new BluetoothPlugin(executor, new SystemClock(), callback, 0L,
0L);
}
public static void main(String[] args) throws Exception {

View File

@@ -21,7 +21,8 @@ public class BluetoothServerTest extends DuplexServerTest {
// Create the plugin
callback = new ServerCallback(new TransportConfig(), local,
Collections.singletonMap(contactId, new TransportProperties()));
plugin = new BluetoothPlugin(executor, new SystemClock(), callback, 0L);
plugin = new BluetoothPlugin(executor, new SystemClock(), callback, 0L,
0L);
}
public static void main(String[] args) throws Exception {

View File

@@ -54,7 +54,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
callback, finder, monitor);
callback, finder, monitor, 0L);
plugin.start();
assertNull(plugin.createWriter(contactId));
@@ -89,7 +89,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
callback, finder, monitor);
callback, finder, monitor, 0L);
plugin.start();
assertNull(plugin.createWriter(contactId));
@@ -126,7 +126,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
callback, finder, monitor);
callback, finder, monitor, 0L);
plugin.start();
assertNull(plugin.createWriter(contactId));
@@ -165,7 +165,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
callback, finder, monitor);
callback, finder, monitor, 0L);
plugin.start();
assertNull(plugin.createWriter(contactId));
@@ -204,7 +204,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
callback, finder, monitor);
callback, finder, monitor, 0L);
plugin.start();
assertNotNull(plugin.createWriter(contactId));
@@ -247,7 +247,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
callback, finder, monitor);
callback, finder, monitor, 0L);
plugin.start();
SimplexTransportWriter writer = plugin.createWriter(contactId);
@@ -286,7 +286,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
callback, finder, monitor);
callback, finder, monitor, 0L);
plugin.start();
plugin.driveInserted(testDir);
@@ -306,7 +306,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
context.mock(RemovableDriveMonitor.class);
RemovableDrivePlugin plugin = new RemovableDrivePlugin(executor,
callback, finder, monitor);
callback, finder, monitor, 0L);
assertFalse(plugin.isPossibleConnectionFilename("abcdefg.dat"));
assertFalse(plugin.isPossibleConnectionFilename("abcdefghi.dat"));
@@ -334,7 +334,7 @@ public class RemovableDrivePluginTest extends BriarTestCase {
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(
new ImmediateExecutor(), callback, finder, monitor);
new ImmediateExecutor(), callback, finder, monitor, 0L);
plugin.start();
File f = new File(testDir, "abcdefgh.dat");

View File

@@ -37,7 +37,7 @@ public class ModemPluginTest extends BriarTestCase {
final SerialPortList serialPortList =
context.mock(SerialPortList.class);
final ModemPlugin plugin = new ModemPlugin(null, modemFactory,
serialPortList, null, 0L, true);
serialPortList, null, 0L, 0L, true);
final Modem modem = context.mock(Modem.class);
context.checking(new Expectations() {{
oneOf(serialPortList).getPortNames();
@@ -71,7 +71,7 @@ public class ModemPluginTest extends BriarTestCase {
final DuplexPluginCallback callback =
context.mock(DuplexPluginCallback.class);
final ModemPlugin plugin = new ModemPlugin(null, modemFactory,
serialPortList, callback, 0L, true);
serialPortList, callback, 0L, 0L, true);
final Modem modem = context.mock(Modem.class);
final TransportProperties local = new TransportProperties();
local.put("iso3166", ISO_1336);
@@ -112,7 +112,7 @@ public class ModemPluginTest extends BriarTestCase {
final DuplexPluginCallback callback =
context.mock(DuplexPluginCallback.class);
final ModemPlugin plugin = new ModemPlugin(null, modemFactory,
serialPortList, callback, 0L, true);
serialPortList, callback, 0L, 0L, true);
final Modem modem = context.mock(Modem.class);
final TransportProperties local = new TransportProperties();
local.put("iso3166", ISO_1336);
@@ -153,7 +153,7 @@ public class ModemPluginTest extends BriarTestCase {
final DuplexPluginCallback callback =
context.mock(DuplexPluginCallback.class);
final ModemPlugin plugin = new ModemPlugin(null, modemFactory,
serialPortList, callback, 0L, true);
serialPortList, callback, 0L, 0L, true);
final Modem modem = context.mock(Modem.class);
final TransportProperties local = new TransportProperties();
local.put("iso3166", ISO_1336);
@@ -204,7 +204,7 @@ public class ModemPluginTest extends BriarTestCase {
context.mock(DuplexPluginCallback.class);
// Disable shuffling for this test, it confuses jMock
final ModemPlugin plugin = new ModemPlugin(pluginExecutor, modemFactory,
serialPortList, callback, 0L, false);
serialPortList, callback, 0L, 0L, false);
final Modem modem = context.mock(Modem.class);
final TransportProperties local = new TransportProperties();
local.put("iso3166", ISO_1336);

View File

@@ -27,7 +27,7 @@ public class LanTcpClientTest extends DuplexClientTest {
// Create the plugin
callback = new ClientCallback(new TransportConfig(),
new TransportProperties(), remote);
plugin = new LanTcpPlugin(executor, new SystemClock(), callback, 0L);
plugin = new LanTcpPlugin(executor, new SystemClock(), callback, 0, 0);
}
public static void main(String[] args) throws Exception {

View File

@@ -36,7 +36,7 @@ public class LanTcpPluginTest extends BriarTestCase {
callback.local.put("port", "0");
Executor executor = Executors.newCachedThreadPool();
Clock clock = new SystemClock();
DuplexPlugin plugin = new LanTcpPlugin(executor, clock, callback, 0L);
DuplexPlugin plugin = new LanTcpPlugin(executor, clock, callback, 0, 0);
plugin.start();
// The plugin should have bound a socket and stored the port number
assertTrue(callback.propertiesLatch.await(5, SECONDS));
@@ -62,7 +62,7 @@ public class LanTcpPluginTest extends BriarTestCase {
Callback callback = new Callback();
Executor executor = Executors.newCachedThreadPool();
Clock clock = new SystemClock();
DuplexPlugin plugin = new LanTcpPlugin(executor, clock, callback, 0L);
DuplexPlugin plugin = new LanTcpPlugin(executor, clock, callback, 0, 0);
plugin.start();
// Listen on a local port
final ServerSocket ss = new ServerSocket();

View File

@@ -18,7 +18,7 @@ public class LanTcpServerTest extends DuplexServerTest {
callback = new ServerCallback(new TransportConfig(),
new TransportProperties(),
Collections.singletonMap(contactId, new TransportProperties()));
plugin = new LanTcpPlugin(executor, new SystemClock(), callback, 0L);
plugin = new LanTcpPlugin(executor, new SystemClock(), callback, 0, 0);
}
public static void main(String[] args) throws Exception {

View File

@@ -31,7 +31,7 @@ public class TorPluginTest extends BriarTestCase {
try {
// Create a plugin instance for the server
Callback serverCallback = new Callback();
serverPlugin = new TorPlugin(e, serverCallback, 0L);
serverPlugin = new TorPlugin(e, serverCallback, 0, 0);
System.out.println("Starting server plugin");
serverPlugin.start();
// The plugin should create a hidden service... eventually
@@ -46,7 +46,7 @@ public class TorPluginTest extends BriarTestCase {
TransportProperties p = new TransportProperties();
p.put("onion", onion);
clientCallback.remote.put(contactId, p);
clientPlugin = new TorPlugin(e, clientCallback, 0L);
clientPlugin = new TorPlugin(e, clientCallback, 0, 0);
System.out.println("Starting client plugin");
clientPlugin.start();
// The plugin should start without creating a hidden service
@@ -87,7 +87,7 @@ public class TorPluginTest extends BriarTestCase {
try {
// Start a plugin instance with no private key
Callback callback = new Callback();
plugin = new TorPlugin(e, callback, 0L);
plugin = new TorPlugin(e, callback, 0, 0);
System.out.println("Starting plugin without private key");
plugin.start();
// The plugin should create a hidden service... eventually
@@ -106,7 +106,7 @@ public class TorPluginTest extends BriarTestCase {
// Start another instance, reusing the private key
callback = new Callback();
callback.config.put("privateKey", privateKey);
plugin = new TorPlugin(e, callback, 0L);
plugin = new TorPlugin(e, callback, 0, 0);
System.out.println("Starting plugin with private key");
plugin.start();
// The plugin should create a hidden service... eventually