New polling logic for LAN (and WAN). #252

Same exponential backoff approach as Bluetooth.
This commit is contained in:
akwizgran
2016-02-12 16:15:48 +00:00
parent c081c08ff5
commit 5ce8927477
12 changed files with 103 additions and 135 deletions

View File

@@ -46,7 +46,7 @@ public class AndroidPluginsModule extends PluginsModule {
DuplexPluginFactory tor = new TorPluginFactory(ioExecutor, appContext,
locationUtils, eventBus);
DuplexPluginFactory lan = new AndroidLanTcpPluginFactory(ioExecutor,
appContext);
backoffFactory, appContext);
final Collection<DuplexPluginFactory> factories =
Arrays.asList(bluetooth, tor, lan);
return new DuplexPluginConfig() {

View File

@@ -1,14 +1,5 @@
package org.briarproject.plugins.tcp;
import static android.content.Context.CONNECTIVITY_SERVICE;
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
import static android.net.ConnectivityManager.TYPE_WIFI;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -16,6 +7,16 @@ import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import org.briarproject.api.plugins.Backoff;
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
import static android.content.Context.CONNECTIVITY_SERVICE;
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
import static android.net.ConnectivityManager.TYPE_WIFI;
class AndroidLanTcpPlugin extends LanTcpPlugin {
private static final Logger LOG =
@@ -25,10 +26,10 @@ class AndroidLanTcpPlugin extends LanTcpPlugin {
private volatile BroadcastReceiver networkStateReceiver = null;
AndroidLanTcpPlugin(Executor ioExecutor, Context appContext,
DuplexPluginCallback callback, int maxLatency,
int maxIdleTime, int pollingInterval) {
super(ioExecutor, callback, maxLatency, maxIdleTime, pollingInterval);
AndroidLanTcpPlugin(Executor ioExecutor, Backoff backoff,
Context appContext, DuplexPluginCallback callback, int maxLatency,
int maxIdleTime) {
super(ioExecutor, backoff, callback, maxLatency, maxIdleTime);
this.appContext = appContext;
}

View File

@@ -1,25 +1,32 @@
package org.briarproject.plugins.tcp;
import java.util.concurrent.Executor;
import android.content.Context;
import org.briarproject.api.TransportId;
import org.briarproject.api.plugins.Backoff;
import org.briarproject.api.plugins.BackoffFactory;
import org.briarproject.api.plugins.duplex.DuplexPlugin;
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
import org.briarproject.api.plugins.duplex.DuplexPluginFactory;
import android.content.Context;
import java.util.concurrent.Executor;
public class AndroidLanTcpPluginFactory implements DuplexPluginFactory {
private static final int MAX_LATENCY = 30 * 1000; // 30 seconds
private static final int MAX_IDLE_TIME = 30 * 1000; // 30 seconds
private static final int POLLING_INTERVAL = 3 * 60 * 1000; // 3 minutes
private static final int MIN_POLLING_INTERVAL = 2 * 60 * 1000; // 2 minutes
private static final int MAX_POLLING_INTERVAL = 60 * 60 * 1000; // 1 hour
private static final double BACKOFF_BASE = 1.2;
private final Executor ioExecutor;
private final BackoffFactory backoffFactory;
private final Context appContext;
public AndroidLanTcpPluginFactory(Executor ioExecutor, Context appContext) {
public AndroidLanTcpPluginFactory(Executor ioExecutor,
BackoffFactory backoffFactory, Context appContext) {
this.ioExecutor = ioExecutor;
this.backoffFactory = backoffFactory;
this.appContext = appContext;
}
@@ -28,7 +35,9 @@ public class AndroidLanTcpPluginFactory implements DuplexPluginFactory {
}
public DuplexPlugin createPlugin(DuplexPluginCallback callback) {
return new AndroidLanTcpPlugin(ioExecutor, appContext, callback,
MAX_LATENCY, MAX_IDLE_TIME, POLLING_INTERVAL);
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
MAX_POLLING_INTERVAL, BACKOFF_BASE);
return new AndroidLanTcpPlugin(ioExecutor, backoff, appContext,
callback, MAX_LATENCY, MAX_IDLE_TIME);
}
}

View File

@@ -1,6 +1,7 @@
package org.briarproject.plugins.tcp;
import org.briarproject.api.TransportId;
import org.briarproject.api.plugins.Backoff;
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
import org.briarproject.api.properties.TransportProperties;
@@ -16,9 +17,9 @@ class LanTcpPlugin extends TcpPlugin {
static final TransportId ID = new TransportId("lan");
LanTcpPlugin(Executor ioExecutor, DuplexPluginCallback callback,
int maxLatency, int maxIdleTime, int pollingInterval) {
super(ioExecutor, callback, maxLatency, maxIdleTime, pollingInterval);
LanTcpPlugin(Executor ioExecutor, Backoff backoff,
DuplexPluginCallback callback, int maxLatency, int maxIdleTime) {
super(ioExecutor, backoff, callback, maxLatency, maxIdleTime);
}
public TransportId getId() {

View File

@@ -1,22 +1,29 @@
package org.briarproject.plugins.tcp;
import java.util.concurrent.Executor;
import org.briarproject.api.TransportId;
import org.briarproject.api.plugins.Backoff;
import org.briarproject.api.plugins.BackoffFactory;
import org.briarproject.api.plugins.duplex.DuplexPlugin;
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
import org.briarproject.api.plugins.duplex.DuplexPluginFactory;
import java.util.concurrent.Executor;
public class LanTcpPluginFactory implements DuplexPluginFactory {
private static final int MAX_LATENCY = 30 * 1000; // 30 seconds
private static final int MAX_IDLE_TIME = 30 * 1000; // 30 seconds
private static final int POLLING_INTERVAL = 3 * 60 * 1000; // 3 minutes
private static final int MIN_POLLING_INTERVAL = 2 * 60 * 1000; // 2 minutes
private static final int MAX_POLLING_INTERVAL = 60 * 60 * 1000; // 1 hour
private static final double BACKOFF_BASE = 1.2;
private final Executor ioExecutor;
private final BackoffFactory backoffFactory;
public LanTcpPluginFactory(Executor ioExecutor) {
public LanTcpPluginFactory(Executor ioExecutor,
BackoffFactory backoffFactory) {
this.ioExecutor = ioExecutor;
this.backoffFactory = backoffFactory;
}
public TransportId getId() {
@@ -24,7 +31,9 @@ public class LanTcpPluginFactory implements DuplexPluginFactory {
}
public DuplexPlugin createPlugin(DuplexPluginCallback callback) {
return new LanTcpPlugin(ioExecutor, callback, MAX_LATENCY,
MAX_IDLE_TIME, POLLING_INTERVAL);
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
MAX_POLLING_INTERVAL, BACKOFF_BASE);
return new LanTcpPlugin(ioExecutor, backoff, callback, MAX_LATENCY,
MAX_IDLE_TIME);
}
}

View File

@@ -2,6 +2,7 @@ package org.briarproject.plugins.tcp;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.crypto.PseudoRandom;
import org.briarproject.api.plugins.Backoff;
import org.briarproject.api.plugins.duplex.DuplexPlugin;
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
import org.briarproject.api.plugins.duplex.DuplexTransportConnection;
@@ -36,8 +37,9 @@ abstract class TcpPlugin implements DuplexPlugin {
Logger.getLogger(TcpPlugin.class.getName());
protected final Executor ioExecutor;
protected final Backoff backoff;
protected final DuplexPluginCallback callback;
protected final int maxLatency, maxIdleTime, pollingInterval, socketTimeout;
protected final int maxLatency, maxIdleTime, socketTimeout;
protected volatile boolean running = false;
protected volatile ServerSocket socket = null;
@@ -51,13 +53,13 @@ abstract class TcpPlugin implements DuplexPlugin {
/** Returns true if connections to the given address can be attempted. */
protected abstract boolean isConnectable(InetSocketAddress remote);
protected TcpPlugin(Executor ioExecutor, DuplexPluginCallback callback,
int maxLatency, int maxIdleTime, int pollingInterval) {
protected TcpPlugin(Executor ioExecutor, Backoff backoff,
DuplexPluginCallback callback, int maxLatency, int maxIdleTime) {
this.ioExecutor = ioExecutor;
this.backoff = backoff;
this.callback = callback;
this.maxLatency = maxLatency;
this.maxIdleTime = maxIdleTime;
this.pollingInterval = pollingInterval;
if (maxIdleTime > Integer.MAX_VALUE / 2)
socketTimeout = Integer.MAX_VALUE;
else socketTimeout = maxIdleTime * 2;
@@ -102,6 +104,7 @@ abstract class TcpPlugin implements DuplexPlugin {
return;
}
socket = ss;
backoff.reset();
SocketAddress local = ss.getLocalSocketAddress();
setLocalSocketAddress((InetSocketAddress) local);
if (LOG.isLoggable(INFO)) LOG.info("Listening on " + local);
@@ -147,6 +150,7 @@ abstract class TcpPlugin implements DuplexPlugin {
}
if (LOG.isLoggable(INFO))
LOG.info("Connection from " + s.getRemoteSocketAddress());
backoff.reset();
TcpTransportConnection conn = new TcpTransportConnection(this, s);
callback.incomingConnectionCreated(conn);
}
@@ -166,11 +170,12 @@ abstract class TcpPlugin implements DuplexPlugin {
}
public int getPollingInterval() {
return pollingInterval;
return backoff.getPollingInterval();
}
public void poll(Collection<ContactId> connected) {
if (!isRunning()) return;
backoff.increment();
for (ContactId c : callback.getRemoteProperties().keySet())
if (!connected.contains(c)) connectAndCallBack(c);
}
@@ -179,7 +184,10 @@ abstract class TcpPlugin implements DuplexPlugin {
ioExecutor.execute(new Runnable() {
public void run() {
DuplexTransportConnection d = createConnection(c);
if (d != null) callback.outgoingConnectionCreated(c, d);
if (d != null) {
backoff.reset();
callback.outgoingConnectionCreated(c, d);
}
}
});
}

View File

@@ -1,6 +1,7 @@
package org.briarproject.plugins.tcp;
import org.briarproject.api.TransportId;
import org.briarproject.api.plugins.Backoff;
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
import org.briarproject.api.properties.TransportProperties;
@@ -20,10 +21,9 @@ class WanTcpPlugin extends TcpPlugin {
private volatile MappingResult mappingResult;
WanTcpPlugin(Executor ioExecutor, PortMapper portMapper,
DuplexPluginCallback callback, int maxLatency, int maxIdleTime,
int pollingInterval) {
super(ioExecutor, callback, maxLatency, maxIdleTime, pollingInterval);
WanTcpPlugin(Executor ioExecutor, Backoff backoff, PortMapper portMapper,
DuplexPluginCallback callback, int maxLatency, int maxIdleTime) {
super(ioExecutor, backoff, callback, maxLatency, maxIdleTime);
this.portMapper = portMapper;
}

View File

@@ -1,25 +1,31 @@
package org.briarproject.plugins.tcp;
import java.util.concurrent.Executor;
import org.briarproject.api.TransportId;
import org.briarproject.api.lifecycle.ShutdownManager;
import org.briarproject.api.plugins.Backoff;
import org.briarproject.api.plugins.BackoffFactory;
import org.briarproject.api.plugins.duplex.DuplexPlugin;
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
import org.briarproject.api.plugins.duplex.DuplexPluginFactory;
import java.util.concurrent.Executor;
public class WanTcpPluginFactory implements DuplexPluginFactory {
private static final int MAX_LATENCY = 30 * 1000; // 30 seconds
private static final int MAX_IDLE_TIME = 30 * 1000; // 30 seconds
private static final int POLLING_INTERVAL = 5 * 60 * 1000; // 5 minutes
private static final int MIN_POLLING_INTERVAL = 2 * 60 * 1000; // 2 minutes
private static final int MAX_POLLING_INTERVAL = 60 * 60 * 1000; // 1 hour
private static final double BACKOFF_BASE = 1.2;
private final Executor ioExecutor;
private final BackoffFactory backoffFactory;
private final ShutdownManager shutdownManager;
public WanTcpPluginFactory(Executor ioExecutor,
ShutdownManager shutdownManager) {
BackoffFactory backoffFactory, ShutdownManager shutdownManager) {
this.ioExecutor = ioExecutor;
this.backoffFactory = backoffFactory;
this.shutdownManager = shutdownManager;
}
@@ -28,7 +34,10 @@ public class WanTcpPluginFactory implements DuplexPluginFactory {
}
public DuplexPlugin createPlugin(DuplexPluginCallback callback) {
return new WanTcpPlugin(ioExecutor, new PortMapperImpl(shutdownManager),
callback, MAX_LATENCY, MAX_IDLE_TIME, POLLING_INTERVAL);
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
MAX_POLLING_INTERVAL, BACKOFF_BASE);
return new WanTcpPlugin(ioExecutor, backoff,
new PortMapperImpl(shutdownManager), callback, MAX_LATENCY,
MAX_IDLE_TIME);
}
}

View File

@@ -47,9 +47,10 @@ public class DesktopPluginsModule extends PluginsModule {
random, backoffFactory);
DuplexPluginFactory modem = new ModemPluginFactory(ioExecutor,
reliabilityFactory);
DuplexPluginFactory lan = new LanTcpPluginFactory(ioExecutor);
DuplexPluginFactory lan = new LanTcpPluginFactory(ioExecutor,
backoffFactory);
DuplexPluginFactory wan = new WanTcpPluginFactory(ioExecutor,
shutdownManager);
backoffFactory, shutdownManager);
final Collection<DuplexPluginFactory> factories =
Arrays.asList(bluetooth, modem, lan, wan);
return new DuplexPluginConfig() {

View File

@@ -1,49 +0,0 @@
package org.briarproject.plugins.tcp;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.properties.TransportProperties;
import org.briarproject.api.settings.Settings;
import org.briarproject.plugins.DuplexClientTest;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
// This is not a JUnit test - it has to be run manually while the server test
// is running on another machine
public class LanTcpClientTest extends DuplexClientTest {
private static final int MAX_LATENCY = 60 * 1000;
private static final int MAX_IDLE_TIME = 30 * 1000;
private static final int POLLING_INTERVAL = 60 * 1000;
private LanTcpClientTest(Executor executor, String serverAddress,
String serverPort) {
// Store the server's internal address and port
TransportProperties p = new TransportProperties();
p.put("address", serverAddress);
p.put("port", serverPort);
Map<ContactId, TransportProperties> remote =
Collections.singletonMap(contactId, p);
// Create the plugin
callback = new ClientCallback(new Settings(),
new TransportProperties(), remote);
plugin = new LanTcpPlugin(executor, callback, MAX_LATENCY,
MAX_IDLE_TIME, POLLING_INTERVAL);
}
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.err.println("Please specify the server's address and port");
System.exit(1);
}
ExecutorService executor = Executors.newCachedThreadPool();
try {
new LanTcpClientTest(executor, args[0], args[1]).run();
} finally {
executor.shutdown();
}
}
}

View File

@@ -2,6 +2,7 @@ package org.briarproject.plugins.tcp;
import org.briarproject.BriarTestCase;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.plugins.Backoff;
import org.briarproject.api.plugins.duplex.DuplexPlugin;
import org.briarproject.api.plugins.duplex.DuplexPluginCallback;
import org.briarproject.api.plugins.duplex.DuplexTransportConnection;
@@ -32,10 +33,11 @@ import static org.junit.Assert.assertTrue;
public class LanTcpPluginTest extends BriarTestCase {
private final ContactId contactId = new ContactId(234);
private final Backoff backoff = new TestBackoff();
@Test
public void testAddressesAreOnSameLan() {
LanTcpPlugin plugin = new LanTcpPlugin(null, null, 0, 0, 0);
LanTcpPlugin plugin = new LanTcpPlugin(null, null, null, 0, 0);
// Local and remote in 10.0.0.0/8 should return true
assertTrue(plugin.addressesAreOnSameLan(makeAddress(10, 0, 0, 0),
makeAddress(10, 255, 255, 255)));
@@ -84,7 +86,8 @@ public class LanTcpPluginTest extends BriarTestCase {
}
Callback callback = new Callback();
Executor executor = Executors.newCachedThreadPool();
DuplexPlugin plugin = new LanTcpPlugin(executor, callback, 0, 0, 0);
DuplexPlugin plugin = new LanTcpPlugin(executor, backoff, callback,
0, 0);
plugin.start();
// The plugin should have bound a socket and stored the port number
assertTrue(callback.propertiesLatch.await(5, SECONDS));
@@ -116,7 +119,8 @@ public class LanTcpPluginTest extends BriarTestCase {
}
Callback callback = new Callback();
Executor executor = Executors.newCachedThreadPool();
DuplexPlugin plugin = new LanTcpPlugin(executor, callback, 0, 0, 0);
DuplexPlugin plugin = new LanTcpPlugin(executor, backoff, callback,
0, 0);
plugin.start();
// The plugin should have bound a socket and stored the port number
assertTrue(callback.propertiesLatch.await(5, SECONDS));
@@ -216,4 +220,15 @@ public class LanTcpPluginTest extends BriarTestCase {
public void transportDisabled() {}
}
private static class TestBackoff implements Backoff {
public int getPollingInterval() {
return 60 * 1000;
}
public void increment() {}
public void reset() {}
}
}

View File

@@ -1,36 +0,0 @@
package org.briarproject.plugins.tcp;
import org.briarproject.api.properties.TransportProperties;
import org.briarproject.api.settings.Settings;
import org.briarproject.plugins.DuplexServerTest;
import java.util.Collections;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
// This is not a JUnit test - it has to be run manually while the client test
// is running on another machine
public class LanTcpServerTest extends DuplexServerTest {
private static final int MAX_LATENCY = 60 * 1000;
private static final int MAX_IDLE_TIME = 30 * 1000;
private static final int POLLING_INTERVAL = 60 * 1000;
private LanTcpServerTest(Executor executor) {
callback = new ServerCallback(new Settings(),
new TransportProperties(),
Collections.singletonMap(contactId, new TransportProperties()));
plugin = new LanTcpPlugin(executor, callback, MAX_LATENCY,
MAX_IDLE_TIME, POLLING_INTERVAL);
}
public static void main(String[] args) throws Exception {
ExecutorService executor = Executors.newCachedThreadPool();
try {
new LanTcpServerTest(executor).run();
} finally {
executor.shutdown();
}
}
}