diff --git a/api/net/sf/briar/api/plugins/BatchPluginFactory.java b/api/net/sf/briar/api/plugins/BatchPluginFactory.java index 1a6d6a0799058a4a24481dc8d886b2f3bef5141c..3b60ba013941f24e7825d16176de08bbf6db52e4 100644 --- a/api/net/sf/briar/api/plugins/BatchPluginFactory.java +++ b/api/net/sf/briar/api/plugins/BatchPluginFactory.java @@ -4,5 +4,5 @@ import java.util.concurrent.Executor; public interface BatchPluginFactory { - BatchPlugin createPlugin(Executor executor, BatchPluginCallback callback); + BatchPlugin createPlugin(Executor e, BatchPluginCallback callback); } diff --git a/api/net/sf/briar/api/plugins/PluginExecutor.java b/api/net/sf/briar/api/plugins/PluginExecutor.java new file mode 100644 index 0000000000000000000000000000000000000000..4012baf740bc8d33dd0b268ebf5d7558f48bebcc --- /dev/null +++ b/api/net/sf/briar/api/plugins/PluginExecutor.java @@ -0,0 +1,15 @@ +package net.sf.briar.api.plugins; + +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import com.google.inject.BindingAnnotation; + +/** Annotation for injecting the executor used by transport plugins. */ +@BindingAnnotation +@Target({ PARAMETER }) +@Retention(RUNTIME) +public @interface PluginExecutor {} \ No newline at end of file diff --git a/api/net/sf/briar/api/plugins/StreamPluginFactory.java b/api/net/sf/briar/api/plugins/StreamPluginFactory.java index f8a0388b70ad44184b37990eb1a38f3ac641ec2e..3d48a1d2c80e3439d56f7ca07eb0df16353058ec 100644 --- a/api/net/sf/briar/api/plugins/StreamPluginFactory.java +++ b/api/net/sf/briar/api/plugins/StreamPluginFactory.java @@ -4,5 +4,5 @@ import java.util.concurrent.Executor; public interface StreamPluginFactory { - StreamPlugin createPlugin(Executor executor, StreamPluginCallback callback); + StreamPlugin createPlugin(Executor e, StreamPluginCallback callback); } diff --git a/api/net/sf/briar/api/transport/ConnectionRecogniserExecutor.java b/api/net/sf/briar/api/transport/ConnectionRecogniserExecutor.java new file mode 100644 index 0000000000000000000000000000000000000000..8251c48a40d70695f194e0a5d333d51d5400dd3e --- /dev/null +++ b/api/net/sf/briar/api/transport/ConnectionRecogniserExecutor.java @@ -0,0 +1,17 @@ +package net.sf.briar.api.transport; + +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import com.google.inject.BindingAnnotation; + +/** + * Annotation for injecting the executor for recognising incoming connections. + */ +@BindingAnnotation +@Target({ PARAMETER }) +@Retention(RUNTIME) +public @interface ConnectionRecogniserExecutor {} \ No newline at end of file diff --git a/components/net/sf/briar/plugins/AbstractPlugin.java b/components/net/sf/briar/plugins/AbstractPlugin.java index 6a779ef7a8857dfd88eb577d31c491b7f1a6f51d..546c6b2d2893c1bd02fa1fe9fbccdf96025ade84 100644 --- a/components/net/sf/briar/plugins/AbstractPlugin.java +++ b/components/net/sf/briar/plugins/AbstractPlugin.java @@ -4,15 +4,16 @@ import java.io.IOException; import java.util.concurrent.Executor; import net.sf.briar.api.plugins.Plugin; +import net.sf.briar.api.plugins.PluginExecutor; public abstract class AbstractPlugin implements Plugin { - protected final Executor executor; + protected final Executor pluginExecutor; protected boolean started = false; // Locking: this - protected AbstractPlugin(Executor executor) { - this.executor = executor; + protected AbstractPlugin(@PluginExecutor Executor pluginExecutor) { + this.pluginExecutor = pluginExecutor; } public synchronized void start() throws IOException { diff --git a/components/net/sf/briar/plugins/PluginManagerImpl.java b/components/net/sf/briar/plugins/PluginManagerImpl.java index 391ce8b189786129878fe5c89b822ecf8aca81ba..a4bb45f61ba9813016a8f1867599596200bfa02a 100644 --- a/components/net/sf/briar/plugins/PluginManagerImpl.java +++ b/components/net/sf/briar/plugins/PluginManagerImpl.java @@ -21,6 +21,7 @@ import net.sf.briar.api.plugins.BatchPluginCallback; import net.sf.briar.api.plugins.BatchPluginFactory; import net.sf.briar.api.plugins.Plugin; import net.sf.briar.api.plugins.PluginCallback; +import net.sf.briar.api.plugins.PluginExecutor; import net.sf.briar.api.plugins.PluginManager; import net.sf.briar.api.plugins.StreamPlugin; import net.sf.briar.api.plugins.StreamPluginCallback; @@ -51,7 +52,7 @@ class PluginManagerImpl implements PluginManager { }; private final DatabaseComponent db; - private final Executor executor; + private final Executor pluginExecutor; private final Poller poller; private final ConnectionDispatcher dispatcher; private final UiCallback uiCallback; @@ -59,10 +60,11 @@ class PluginManagerImpl implements PluginManager { private final List<StreamPlugin> streamPlugins; // Locking: this @Inject - PluginManagerImpl(DatabaseComponent db, Executor executor, Poller poller, + PluginManagerImpl(DatabaseComponent db, + @PluginExecutor Executor pluginExecutor, Poller poller, ConnectionDispatcher dispatcher, UiCallback uiCallback) { this.db = db; - this.executor = executor; + this.pluginExecutor = pluginExecutor; this.poller = poller; this.dispatcher = dispatcher; this.uiCallback = uiCallback; @@ -83,11 +85,13 @@ class PluginManagerImpl implements PluginManager { BatchPluginFactory factory = (BatchPluginFactory) c.newInstance(); BatchCallback callback = new BatchCallback(); - BatchPlugin plugin = factory.createPlugin(executor, callback); + BatchPlugin plugin = factory.createPlugin(pluginExecutor, + callback); if(plugin == null) { - if(LOG.isLoggable(Level.INFO)) + if(LOG.isLoggable(Level.INFO)) { LOG.info(factory.getClass().getSimpleName() + " did not create a plugin"); + } continue; } TransportId id = plugin.getId(); @@ -121,11 +125,13 @@ class PluginManagerImpl implements PluginManager { StreamPluginFactory factory = (StreamPluginFactory) c.newInstance(); StreamCallback callback = new StreamCallback(); - StreamPlugin plugin = factory.createPlugin(executor, callback); + StreamPlugin plugin = factory.createPlugin(pluginExecutor, + callback); if(plugin == null) { - if(LOG.isLoggable(Level.INFO)) + if(LOG.isLoggable(Level.INFO)) { LOG.info(factory.getClass().getSimpleName() + " did not create a plugin"); + } continue; } TransportId id = plugin.getId(); diff --git a/components/net/sf/briar/plugins/PluginsModule.java b/components/net/sf/briar/plugins/PluginsModule.java index 5cb124da21a14756769647d6e81d318ef5b1113c..c725cd36ce487c42e108c0d140ef680b15827fb6 100644 --- a/components/net/sf/briar/plugins/PluginsModule.java +++ b/components/net/sf/briar/plugins/PluginsModule.java @@ -1,5 +1,9 @@ package net.sf.briar.plugins; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +import net.sf.briar.api.plugins.PluginExecutor; import net.sf.briar.api.plugins.PluginManager; import com.google.inject.AbstractModule; @@ -9,6 +13,8 @@ public class PluginsModule extends AbstractModule { @Override protected void configure() { + bind(Executor.class).annotatedWith(PluginExecutor.class).toInstance( + Executors.newCachedThreadPool()); bind(PluginManager.class).to( PluginManagerImpl.class).in(Singleton.class); bind(Poller.class).to(PollerImpl.class); diff --git a/components/net/sf/briar/plugins/bluetooth/BluetoothPlugin.java b/components/net/sf/briar/plugins/bluetooth/BluetoothPlugin.java index b6c53a4b224fd947eccc434d16a416787dd9530e..4b2d7d80e7cdc6f7e95a77be4f3320177ab7468d 100644 --- a/components/net/sf/briar/plugins/bluetooth/BluetoothPlugin.java +++ b/components/net/sf/briar/plugins/bluetooth/BluetoothPlugin.java @@ -19,6 +19,7 @@ import javax.microedition.io.StreamConnectionNotifier; import net.sf.briar.api.ContactId; import net.sf.briar.api.TransportProperties; +import net.sf.briar.api.plugins.PluginExecutor; import net.sf.briar.api.plugins.StreamPlugin; import net.sf.briar.api.plugins.StreamPluginCallback; import net.sf.briar.api.protocol.TransportId; @@ -44,9 +45,9 @@ class BluetoothPlugin extends AbstractPlugin implements StreamPlugin { private LocalDevice localDevice = null; // Locking: this private StreamConnectionNotifier socket = null; // Locking: this - BluetoothPlugin(Executor executor, StreamPluginCallback callback, - long pollingInterval) { - super(executor); + BluetoothPlugin(@PluginExecutor Executor pluginExecutor, + StreamPluginCallback callback, long pollingInterval) { + super(pluginExecutor); this.callback = callback; this.pollingInterval = pollingInterval; } @@ -69,7 +70,7 @@ class BluetoothPlugin extends AbstractPlugin implements StreamPlugin { } catch(UnsatisfiedLinkError e) { // On Linux the user may need to install libbluetooth-dev if(OsUtils.isLinux()) { - executor.execute(new Runnable() { + pluginExecutor.execute(new Runnable() { public void run() { callback.showMessage("BLUETOOTH_INSTALL_LIBS"); } @@ -77,7 +78,7 @@ class BluetoothPlugin extends AbstractPlugin implements StreamPlugin { } throw new IOException(e.getMessage()); } - executor.execute(createContactSocketBinder()); + pluginExecutor.execute(createContactSocketBinder()); } @Override @@ -199,7 +200,7 @@ class BluetoothPlugin extends AbstractPlugin implements StreamPlugin { public synchronized void poll() { if(!started) return; - executor.execute(createConnectors()); + pluginExecutor.execute(createConnectors()); } private Runnable createConnectors() { diff --git a/components/net/sf/briar/plugins/bluetooth/BluetoothPluginFactory.java b/components/net/sf/briar/plugins/bluetooth/BluetoothPluginFactory.java index ed9dfa8657e9a55df795648faa9e9a3c5cc908e7..a7a55fa1c3fefc0db6cfb19c93ad2190e640672e 100644 --- a/components/net/sf/briar/plugins/bluetooth/BluetoothPluginFactory.java +++ b/components/net/sf/briar/plugins/bluetooth/BluetoothPluginFactory.java @@ -2,16 +2,17 @@ package net.sf.briar.plugins.bluetooth; import java.util.concurrent.Executor; -import net.sf.briar.api.plugins.StreamPluginCallback; +import net.sf.briar.api.plugins.PluginExecutor; import net.sf.briar.api.plugins.StreamPlugin; +import net.sf.briar.api.plugins.StreamPluginCallback; import net.sf.briar.api.plugins.StreamPluginFactory; public class BluetoothPluginFactory implements StreamPluginFactory { private static final long POLLING_INTERVAL = 3L * 60L * 1000L; // 3 mins - public StreamPlugin createPlugin(Executor executor, + public StreamPlugin createPlugin(@PluginExecutor Executor pluginExecutor, StreamPluginCallback callback) { - return new BluetoothPlugin(executor, callback, POLLING_INTERVAL); + return new BluetoothPlugin(pluginExecutor, callback, POLLING_INTERVAL); } } diff --git a/components/net/sf/briar/plugins/file/FilePlugin.java b/components/net/sf/briar/plugins/file/FilePlugin.java index de1b0466be21d2f8a3505ab31a6ef30e30e0990b..40f579ff13b38ad29ed6c7d937ebca16f5b441ed 100644 --- a/components/net/sf/briar/plugins/file/FilePlugin.java +++ b/components/net/sf/briar/plugins/file/FilePlugin.java @@ -13,6 +13,7 @@ import java.util.logging.Logger; import net.sf.briar.api.ContactId; import net.sf.briar.api.plugins.BatchPlugin; import net.sf.briar.api.plugins.BatchPluginCallback; +import net.sf.briar.api.plugins.PluginExecutor; import net.sf.briar.api.transport.BatchTransportReader; import net.sf.briar.api.transport.BatchTransportWriter; import net.sf.briar.api.transport.TransportConstants; @@ -35,8 +36,9 @@ abstract class FilePlugin extends AbstractPlugin implements BatchPlugin { protected abstract void writerFinished(File f); protected abstract void readerFinished(File f); - protected FilePlugin(Executor executor, BatchPluginCallback callback) { - super(executor); + protected FilePlugin(@PluginExecutor Executor pluginExecutor, + BatchPluginCallback callback) { + super(pluginExecutor); this.callback = callback; } @@ -85,7 +87,7 @@ abstract class FilePlugin extends AbstractPlugin implements BatchPlugin { protected synchronized void createReaderFromFile(final File f) { if(!started) return; - executor.execute(new ReaderCreator(f)); + pluginExecutor.execute(new ReaderCreator(f)); } public BatchTransportWriter sendInvitation(int code, long timeout) { diff --git a/components/net/sf/briar/plugins/file/RemovableDrivePlugin.java b/components/net/sf/briar/plugins/file/RemovableDrivePlugin.java index 3126e66e013df237fd8586730c8eabdf90e8e428..b2412c46d0ce63aac2d470e3de607c0830cf3c3f 100644 --- a/components/net/sf/briar/plugins/file/RemovableDrivePlugin.java +++ b/components/net/sf/briar/plugins/file/RemovableDrivePlugin.java @@ -11,6 +11,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import net.sf.briar.api.plugins.BatchPluginCallback; +import net.sf.briar.api.plugins.PluginExecutor; import net.sf.briar.api.protocol.TransportId; import net.sf.briar.util.StringUtils; @@ -28,9 +29,10 @@ implements RemovableDriveMonitor.Callback { private final RemovableDriveFinder finder; private final RemovableDriveMonitor monitor; - RemovableDrivePlugin(Executor executor, BatchPluginCallback callback, - RemovableDriveFinder finder, RemovableDriveMonitor monitor) { - super(executor, callback); + RemovableDrivePlugin(@PluginExecutor Executor pluginExecutor, + BatchPluginCallback callback, RemovableDriveFinder finder, + RemovableDriveMonitor monitor) { + super(pluginExecutor, callback); this.finder = finder; this.monitor = monitor; } diff --git a/components/net/sf/briar/plugins/file/RemovableDrivePluginFactory.java b/components/net/sf/briar/plugins/file/RemovableDrivePluginFactory.java index 92850a4e03a89e22ad96fb1b6fb99e5b341d6e25..c8d906d689b433ca471bb6614dc16104bd7fec0c 100644 --- a/components/net/sf/briar/plugins/file/RemovableDrivePluginFactory.java +++ b/components/net/sf/briar/plugins/file/RemovableDrivePluginFactory.java @@ -2,16 +2,17 @@ package net.sf.briar.plugins.file; import java.util.concurrent.Executor; -import net.sf.briar.api.plugins.BatchPluginCallback; import net.sf.briar.api.plugins.BatchPlugin; +import net.sf.briar.api.plugins.BatchPluginCallback; import net.sf.briar.api.plugins.BatchPluginFactory; +import net.sf.briar.api.plugins.PluginExecutor; import net.sf.briar.util.OsUtils; public class RemovableDrivePluginFactory implements BatchPluginFactory { private static final long POLLING_INTERVAL = 10L * 1000L; // 10 seconds - public BatchPlugin createPlugin(Executor executor, + public BatchPlugin createPlugin(@PluginExecutor Executor pluginExecutor, BatchPluginCallback callback) { RemovableDriveFinder finder; RemovableDriveMonitor monitor; @@ -33,6 +34,7 @@ public class RemovableDrivePluginFactory implements BatchPluginFactory { } else { return null; } - return new RemovableDrivePlugin(executor, callback, finder, monitor); + return new RemovableDrivePlugin(pluginExecutor, callback, finder, + monitor); } } diff --git a/components/net/sf/briar/plugins/socket/LanSocketPlugin.java b/components/net/sf/briar/plugins/socket/LanSocketPlugin.java index 181a9b8ff3c73c69438cf164f36106976706c209..751d0f1fb60e738c6cb2eb7d90461c0f94e5ddfe 100644 --- a/components/net/sf/briar/plugins/socket/LanSocketPlugin.java +++ b/components/net/sf/briar/plugins/socket/LanSocketPlugin.java @@ -14,6 +14,7 @@ import java.util.concurrent.Executor; import java.util.logging.Level; import java.util.logging.Logger; +import net.sf.briar.api.plugins.PluginExecutor; import net.sf.briar.api.plugins.StreamPluginCallback; import net.sf.briar.api.transport.StreamTransportConnection; import net.sf.briar.util.ByteUtils; @@ -24,9 +25,9 @@ class LanSocketPlugin extends SimpleSocketPlugin { private static final Logger LOG = Logger.getLogger(LanSocketPlugin.class.getName()); - LanSocketPlugin(Executor executor, StreamPluginCallback callback, - long pollingInterval) { - super(executor, callback, pollingInterval); + LanSocketPlugin(@PluginExecutor Executor pluginExecutor, + StreamPluginCallback callback, long pollingInterval) { + super(pluginExecutor, callback, pollingInterval); } @Override diff --git a/components/net/sf/briar/plugins/socket/SimpleSocketPlugin.java b/components/net/sf/briar/plugins/socket/SimpleSocketPlugin.java index e50db5675a88dd0c0b1da14dbf73f6681df73fdb..c887005a6ee5f40c6bf3443bc8caaacc3cc5c6b8 100644 --- a/components/net/sf/briar/plugins/socket/SimpleSocketPlugin.java +++ b/components/net/sf/briar/plugins/socket/SimpleSocketPlugin.java @@ -15,6 +15,7 @@ import java.util.logging.Logger; import net.sf.briar.api.ContactId; import net.sf.briar.api.TransportProperties; +import net.sf.briar.api.plugins.PluginExecutor; import net.sf.briar.api.plugins.StreamPluginCallback; import net.sf.briar.api.protocol.TransportId; import net.sf.briar.api.transport.StreamTransportConnection; @@ -32,9 +33,9 @@ class SimpleSocketPlugin extends SocketPlugin { private final long pollingInterval; - SimpleSocketPlugin(Executor executor, StreamPluginCallback callback, - long pollingInterval) { - super(executor, callback); + SimpleSocketPlugin(@PluginExecutor Executor pluginExecutor, + StreamPluginCallback callback, long pollingInterval) { + super(pluginExecutor, callback); this.pollingInterval = pollingInterval; } diff --git a/components/net/sf/briar/plugins/socket/SimpleSocketPluginFactory.java b/components/net/sf/briar/plugins/socket/SimpleSocketPluginFactory.java index c96de3c196b15c1a3590a2f0f1711a1986243791..a34e21ead8d1c00e6800c70b653155007d4e9e94 100644 --- a/components/net/sf/briar/plugins/socket/SimpleSocketPluginFactory.java +++ b/components/net/sf/briar/plugins/socket/SimpleSocketPluginFactory.java @@ -2,16 +2,18 @@ package net.sf.briar.plugins.socket; import java.util.concurrent.Executor; -import net.sf.briar.api.plugins.StreamPluginCallback; +import net.sf.briar.api.plugins.PluginExecutor; import net.sf.briar.api.plugins.StreamPlugin; +import net.sf.briar.api.plugins.StreamPluginCallback; import net.sf.briar.api.plugins.StreamPluginFactory; public class SimpleSocketPluginFactory implements StreamPluginFactory { private static final long POLLING_INTERVAL = 5L * 60L * 1000L; // 5 mins - public StreamPlugin createPlugin(Executor executor, + public StreamPlugin createPlugin(@PluginExecutor Executor pluginExecutor, StreamPluginCallback callback) { - return new SimpleSocketPlugin(executor, callback, POLLING_INTERVAL); + return new SimpleSocketPlugin(pluginExecutor, callback, + POLLING_INTERVAL); } } diff --git a/components/net/sf/briar/plugins/socket/SocketPlugin.java b/components/net/sf/briar/plugins/socket/SocketPlugin.java index ed06bf7429166f00c8db61fbbf41fc084f5f67a7..c5863ebfa2eea42f9591c93841c5db72b42420f9 100644 --- a/components/net/sf/briar/plugins/socket/SocketPlugin.java +++ b/components/net/sf/briar/plugins/socket/SocketPlugin.java @@ -9,8 +9,9 @@ import java.util.logging.Level; import java.util.logging.Logger; import net.sf.briar.api.ContactId; -import net.sf.briar.api.plugins.StreamPluginCallback; +import net.sf.briar.api.plugins.PluginExecutor; import net.sf.briar.api.plugins.StreamPlugin; +import net.sf.briar.api.plugins.StreamPluginCallback; import net.sf.briar.api.transport.StreamTransportConnection; import net.sf.briar.plugins.AbstractPlugin; @@ -32,15 +33,16 @@ abstract class SocketPlugin extends AbstractPlugin implements StreamPlugin { protected abstract SocketAddress getLocalSocketAddress(); protected abstract SocketAddress getRemoteSocketAddress(ContactId c); - protected SocketPlugin(Executor executor, StreamPluginCallback callback) { - super(executor); + protected SocketPlugin(@PluginExecutor Executor pluginExecutor, + StreamPluginCallback callback) { + super(pluginExecutor); this.callback = callback; } @Override public synchronized void start() throws IOException { super.start(); - executor.execute(createBinder()); + pluginExecutor.execute(createBinder()); } private Runnable createBinder() { @@ -140,7 +142,7 @@ abstract class SocketPlugin extends AbstractPlugin implements StreamPlugin { if(!shouldPoll()) throw new UnsupportedOperationException(); if(!started) return; for(ContactId c : callback.getRemoteProperties().keySet()) { - executor.execute(createConnector(c)); + pluginExecutor.execute(createConnector(c)); } } diff --git a/components/net/sf/briar/protocol/batch/OutgoingBatchConnection.java b/components/net/sf/briar/protocol/batch/OutgoingBatchConnection.java index e9811587fd3e6e0dbbb38cc06d0d08d7d73f2483..1d22b832009cf1ab72349745866f081fb086df1d 100644 --- a/components/net/sf/briar/protocol/batch/OutgoingBatchConnection.java +++ b/components/net/sf/briar/protocol/batch/OutgoingBatchConnection.java @@ -2,6 +2,7 @@ package net.sf.briar.protocol.batch; import static net.sf.briar.api.protocol.ProtocolConstants.MAX_PACKET_LENGTH; +import java.io.EOFException; import java.io.IOException; import java.io.OutputStream; import java.util.logging.Level; @@ -57,7 +58,7 @@ class OutgoingBatchConnection { ProtocolWriter writer = protoFactory.createProtocolWriter(out); // There should be enough space for a packet long capacity = conn.getRemainingCapacity(); - if(capacity < MAX_PACKET_LENGTH) throw new IOException(); + if(capacity < MAX_PACKET_LENGTH) throw new EOFException(); // Write a transport update TransportUpdate t = db.generateTransportUpdate(contactId); if(t != null) writer.writeTransportUpdate(t); diff --git a/components/net/sf/briar/transport/ConnectionRecogniserImpl.java b/components/net/sf/briar/transport/ConnectionRecogniserImpl.java index 08c2ed3de2792bc1d2ffaa3bfa36882223c67263..4a2e31e8da61be8fbe0dc4235e6dd146cbdba8ae 100644 --- a/components/net/sf/briar/transport/ConnectionRecogniserImpl.java +++ b/components/net/sf/briar/transport/ConnectionRecogniserImpl.java @@ -33,6 +33,7 @@ import net.sf.briar.api.protocol.TransportId; import net.sf.briar.api.protocol.TransportIndex; import net.sf.briar.api.transport.ConnectionContext; import net.sf.briar.api.transport.ConnectionRecogniser; +import net.sf.briar.api.transport.ConnectionRecogniserExecutor; import net.sf.briar.api.transport.ConnectionWindow; import net.sf.briar.util.ByteUtils; @@ -44,9 +45,9 @@ DatabaseListener { private static final Logger LOG = Logger.getLogger(ConnectionRecogniserImpl.class.getName()); - private final CryptoComponent crypto; - private final DatabaseComponent db; private final Executor executor; + private final DatabaseComponent db; + private final CryptoComponent crypto; private final Cipher tagCipher; // Locking: this private final Set<TransportId> localTransportIds; // Locking: this private final Map<Bytes, Context> expected; // Locking: this @@ -54,11 +55,11 @@ DatabaseListener { private boolean initialised = false; // Locking: this @Inject - ConnectionRecogniserImpl(CryptoComponent crypto, DatabaseComponent db, - Executor executor) { - this.crypto = crypto; - this.db = db; + ConnectionRecogniserImpl(@ConnectionRecogniserExecutor Executor executor, + DatabaseComponent db, CryptoComponent crypto) { this.executor = executor; + this.db = db; + this.crypto = crypto; tagCipher = crypto.getTagCipher(); localTransportIds = new HashSet<TransportId>(); expected = new HashMap<Bytes, Context>(); diff --git a/components/net/sf/briar/transport/TransportModule.java b/components/net/sf/briar/transport/TransportModule.java index 3af8ef144be11de9d0776329ace96fac02b698bc..1e9debcd77c2a65ec8301b3ebf35406d7126abd7 100644 --- a/components/net/sf/briar/transport/TransportModule.java +++ b/components/net/sf/briar/transport/TransportModule.java @@ -1,13 +1,18 @@ package net.sf.briar.transport; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + import net.sf.briar.api.transport.ConnectionContextFactory; import net.sf.briar.api.transport.ConnectionDispatcher; import net.sf.briar.api.transport.ConnectionReaderFactory; import net.sf.briar.api.transport.ConnectionRecogniser; +import net.sf.briar.api.transport.ConnectionRecogniserExecutor; import net.sf.briar.api.transport.ConnectionWindowFactory; import net.sf.briar.api.transport.ConnectionWriterFactory; import com.google.inject.AbstractModule; +import com.google.inject.Singleton; public class TransportModule extends AbstractModule { @@ -18,10 +23,14 @@ public class TransportModule extends AbstractModule { bind(ConnectionDispatcher.class).to(ConnectionDispatcherImpl.class); bind(ConnectionReaderFactory.class).to( ConnectionReaderFactoryImpl.class); - bind(ConnectionRecogniser.class).to(ConnectionRecogniserImpl.class); + bind(ConnectionRecogniser.class).to(ConnectionRecogniserImpl.class).in( + Singleton.class); bind(ConnectionWindowFactory.class).to( ConnectionWindowFactoryImpl.class); bind(ConnectionWriterFactory.class).to( ConnectionWriterFactoryImpl.class); + bind(Executor.class).annotatedWith( + ConnectionRecogniserExecutor.class).toInstance( + Executors.newCachedThreadPool()); } } diff --git a/test/net/sf/briar/ProtocolIntegrationTest.java b/test/net/sf/briar/ProtocolIntegrationTest.java index 8d22479fe74229d49dc0ad18f1aedcaca27dd400..6e4559538598bd72afe5df527c698d1072af8121 100644 --- a/test/net/sf/briar/ProtocolIntegrationTest.java +++ b/test/net/sf/briar/ProtocolIntegrationTest.java @@ -16,8 +16,6 @@ import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import java.util.Random; -import java.util.concurrent.Executor; -import java.util.concurrent.Executors; import junit.framework.TestCase; import net.sf.briar.api.crypto.CryptoComponent; @@ -59,10 +57,8 @@ import net.sf.briar.transport.TransportModule; import org.junit.Test; -import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Injector; -import com.google.inject.Module; public class ProtocolIntegrationTest extends TestCase { @@ -87,14 +83,7 @@ public class ProtocolIntegrationTest extends TestCase { public ProtocolIntegrationTest() throws Exception { super(); - Module testModule = new AbstractModule() { - @Override - public void configure() { - bind(Executor.class).toInstance( - Executors.newCachedThreadPool()); - } - }; - Injector i = Guice.createInjector(testModule, new CryptoModule(), + Injector i = Guice.createInjector(new CryptoModule(), new DatabaseModule(), new LifecycleModule(), new ProtocolModule(), new SerialModule(), new TestDatabaseModule(), new ProtocolBatchModule(), diff --git a/test/net/sf/briar/db/H2DatabaseTest.java b/test/net/sf/briar/db/H2DatabaseTest.java index 56e441b0972c01e28c6e838b1788e24a2dcf7d2d..869963226fbe4b2d83c5c36db2dc3aab15ad2da0 100644 --- a/test/net/sf/briar/db/H2DatabaseTest.java +++ b/test/net/sf/briar/db/H2DatabaseTest.java @@ -13,8 +13,6 @@ import java.util.Iterator; import java.util.Map; import java.util.Random; import java.util.concurrent.CountDownLatch; -import java.util.concurrent.Executor; -import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; @@ -56,10 +54,8 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; -import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Injector; -import com.google.inject.Module; public class H2DatabaseTest extends TestCase { @@ -97,14 +93,7 @@ public class H2DatabaseTest extends TestCase { public H2DatabaseTest() throws Exception { super(); - Module testModule = new AbstractModule() { - @Override - public void configure() { - bind(Executor.class).toInstance( - new ScheduledThreadPoolExecutor(5)); - } - }; - Injector i = Guice.createInjector(testModule, new CryptoModule(), + Injector i = Guice.createInjector(new CryptoModule(), new DatabaseModule(), new LifecycleModule(), new ProtocolModule(), new SerialModule(), new ProtocolBatchModule(), new TransportModule(), diff --git a/test/net/sf/briar/protocol/batch/BatchConnectionReadWriteTest.java b/test/net/sf/briar/protocol/batch/BatchConnectionReadWriteTest.java index 10af1dcb99f89b5e636dad05b45df445895f76d7..e753cac020cf52b72e2d4d1a26c83b4abdb55338 100644 --- a/test/net/sf/briar/protocol/batch/BatchConnectionReadWriteTest.java +++ b/test/net/sf/briar/protocol/batch/BatchConnectionReadWriteTest.java @@ -5,14 +5,10 @@ import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.InputStream; -import java.io.OutputStream; import java.util.Collection; import java.util.Collections; import java.util.Random; import java.util.concurrent.CountDownLatch; -import java.util.concurrent.Executor; -import java.util.concurrent.ScheduledThreadPoolExecutor; import junit.framework.TestCase; import net.sf.briar.TestDatabaseModule; @@ -31,8 +27,6 @@ import net.sf.briar.api.protocol.Transport; import net.sf.briar.api.protocol.TransportId; import net.sf.briar.api.protocol.TransportIndex; import net.sf.briar.api.protocol.TransportUpdate; -import net.sf.briar.api.transport.BatchTransportReader; -import net.sf.briar.api.transport.BatchTransportWriter; import net.sf.briar.api.transport.ConnectionContext; import net.sf.briar.api.transport.ConnectionReaderFactory; import net.sf.briar.api.transport.ConnectionRecogniser; @@ -43,9 +37,6 @@ import net.sf.briar.db.DatabaseModule; import net.sf.briar.lifecycle.LifecycleModule; import net.sf.briar.plugins.ImmediateExecutor; import net.sf.briar.protocol.ProtocolModule; -import net.sf.briar.protocol.batch.IncomingBatchConnection; -import net.sf.briar.protocol.batch.OutgoingBatchConnection; -import net.sf.briar.protocol.batch.ProtocolBatchModule; import net.sf.briar.protocol.stream.ProtocolStreamModule; import net.sf.briar.serial.SerialModule; import net.sf.briar.transport.TransportModule; @@ -54,10 +45,8 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; -import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Injector; -import com.google.inject.Module; public class BatchConnectionReadWriteTest extends TestCase { @@ -90,16 +79,8 @@ public class BatchConnectionReadWriteTest extends TestCase { } private Injector createInjector(File dir) { - Module testModule = new AbstractModule() { - @Override - public void configure() { - bind(Executor.class).toInstance( - new ScheduledThreadPoolExecutor(5)); - } - }; - return Guice.createInjector(testModule, new CryptoModule(), - new DatabaseModule(), new LifecycleModule(), - new ProtocolModule(), new SerialModule(), + return Guice.createInjector(new CryptoModule(), new DatabaseModule(), + new LifecycleModule(), new ProtocolModule(), new SerialModule(), new TestDatabaseModule(dir), new ProtocolBatchModule(), new TransportModule(), new ProtocolStreamModule()); } @@ -133,12 +114,14 @@ public class BatchConnectionReadWriteTest extends TestCase { alice.getInstance(ConnectionWriterFactory.class); ProtocolWriterFactory protoFactory = alice.getInstance(ProtocolWriterFactory.class); - BatchTransportWriter transport = new TestBatchTransportWriter(out); + TestBatchTransportWriter transport = new TestBatchTransportWriter(out, + Long.MAX_VALUE); OutgoingBatchConnection batchOut = new OutgoingBatchConnection(db, connFactory, protoFactory, contactId, transportIndex, transport); // Write whatever needs to be written batchOut.write(); + assertTrue(transport.getSuccess()); // Close Alice's database db.close(); // Return the contents of the batch connection @@ -187,14 +170,15 @@ public class BatchConnectionReadWriteTest extends TestCase { bob.getInstance(ConnectionReaderFactory.class); ProtocolReaderFactory protoFactory = bob.getInstance(ProtocolReaderFactory.class); - BatchTransportReader reader = new TestBatchTransportReader(in); + TestBatchTransportReader transport = new TestBatchTransportReader(in); IncomingBatchConnection batchIn = new IncomingBatchConnection( new ImmediateExecutor(), new ImmediateExecutor(), db, - connFactory, protoFactory, ctx, reader, tag); + connFactory, protoFactory, ctx, transport, tag); // No messages should have been added yet assertFalse(listener.messagesAdded); // Read whatever needs to be read batchIn.read(); + assertTrue(transport.getSuccess()); // The private message from Alice should have been added assertTrue(listener.messagesAdded); // Close Bob's database @@ -215,46 +199,6 @@ public class BatchConnectionReadWriteTest extends TestCase { } } - private static class TestBatchTransportWriter - implements BatchTransportWriter { - - private final OutputStream out; - - private TestBatchTransportWriter(OutputStream out) { - this.out = out; - } - - public long getCapacity() { - return Long.MAX_VALUE; - } - - public OutputStream getOutputStream() { - return out; - } - - public void dispose(boolean success) { - assertTrue(success); - } - } - - private static class TestBatchTransportReader - implements BatchTransportReader { - - private final InputStream in; - - private TestBatchTransportReader(InputStream in) { - this.in = in; - } - - public InputStream getInputStream() { - return in; - } - - public void dispose(boolean success) { - assertTrue(success); - } - } - private static class TestCallback implements Callback { private final CountDownLatch latch = new CountDownLatch(1); diff --git a/test/net/sf/briar/protocol/batch/TestBatchTransportReader.java b/test/net/sf/briar/protocol/batch/TestBatchTransportReader.java new file mode 100644 index 0000000000000000000000000000000000000000..080339a7835fa524fccd16c4f58589509c884604 --- /dev/null +++ b/test/net/sf/briar/protocol/batch/TestBatchTransportReader.java @@ -0,0 +1,29 @@ +package net.sf.briar.protocol.batch; + +import java.io.InputStream; + +import net.sf.briar.api.transport.BatchTransportReader; + +class TestBatchTransportReader +implements BatchTransportReader { + + private final InputStream in; + + private boolean success = false; + + TestBatchTransportReader(InputStream in) { + this.in = in; + } + + public InputStream getInputStream() { + return in; + } + + public void dispose(boolean success) { + this.success = success; + } + + boolean getSuccess() { + return success; + } +} \ No newline at end of file diff --git a/test/net/sf/briar/protocol/batch/TestBatchTransportWriter.java b/test/net/sf/briar/protocol/batch/TestBatchTransportWriter.java new file mode 100644 index 0000000000000000000000000000000000000000..c37aa73b5b94a49aa44ff0dbd8642660e442fdc9 --- /dev/null +++ b/test/net/sf/briar/protocol/batch/TestBatchTransportWriter.java @@ -0,0 +1,37 @@ +package net.sf.briar.protocol.batch; + +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; + +import net.sf.briar.api.transport.BatchTransportWriter; + +class TestBatchTransportWriter +implements BatchTransportWriter { + + private final ByteArrayOutputStream out; + private final long capacity; + + private boolean success = false; + + TestBatchTransportWriter(ByteArrayOutputStream out, + long capacity) { + this.out = out; + this.capacity = capacity; + } + + public long getCapacity() { + return capacity - out.size(); + } + + public OutputStream getOutputStream() { + return out; + } + + public void dispose(boolean success) { + this.success = success; + } + + boolean getSuccess() { + return success; + } +} \ No newline at end of file diff --git a/test/net/sf/briar/transport/ConnectionRecogniserImplTest.java b/test/net/sf/briar/transport/ConnectionRecogniserImplTest.java index 2ff78152bb3957160a18e5d38783b9f078063f03..be5482a6bd9c832db6a13c635e1a199c487a3b99 100644 --- a/test/net/sf/briar/transport/ConnectionRecogniserImplTest.java +++ b/test/net/sf/briar/transport/ConnectionRecogniserImplTest.java @@ -81,8 +81,8 @@ public class ConnectionRecogniserImplTest extends TestCase { will(returnValue(window)); }}); Executor executor = new ImmediateExecutor(); - ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db, - executor); + ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(executor, db, + crypto); assertNull(c.acceptConnection(transportId, new byte[TAG_LENGTH])); context.assertIsSatisfied(); } @@ -109,8 +109,8 @@ public class ConnectionRecogniserImplTest extends TestCase { oneOf(db).setConnectionWindow(contactId, remoteIndex, window); }}); Executor executor = new ImmediateExecutor(); - ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db, - executor); + ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(executor, db, + crypto); byte[] tag = calculateTag(); // The tag should not be expected by the wrong transport TransportId wrong = new TransportId(TestUtils.getRandomId()); @@ -150,8 +150,8 @@ public class ConnectionRecogniserImplTest extends TestCase { will(returnValue(window)); }}); Executor executor = new ImmediateExecutor(); - ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db, - executor); + ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(executor, db, + crypto); byte[] tag = calculateTag(); // Ensure the recogniser is initialised assertFalse(c.isInitialised()); @@ -177,8 +177,8 @@ public class ConnectionRecogniserImplTest extends TestCase { will(returnValue(Collections.emptyList())); }}); Executor executor = new ImmediateExecutor(); - ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db, - executor); + ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(executor, db, + crypto); byte[] tag = calculateTag(); // Remove the contact c.eventOccurred(new ContactRemovedEvent(contactId)); @@ -214,8 +214,8 @@ public class ConnectionRecogniserImplTest extends TestCase { oneOf(db).setConnectionWindow(contactId, remoteIndex, window); }}); Executor executor = new ImmediateExecutor(); - ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db, - executor); + ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(executor, db, + crypto); byte[] tag = calculateTag(); // The tag should not be expected assertFalse(c.isInitialised()); @@ -262,8 +262,8 @@ public class ConnectionRecogniserImplTest extends TestCase { oneOf(db).setConnectionWindow(contactId, remoteIndex, window); }}); Executor executor = new ImmediateExecutor(); - ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db, - executor); + ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(executor, db, + crypto); byte[] tag = calculateTag(); // Add the transport c.eventOccurred(new TransportAddedEvent(transportId)); @@ -309,8 +309,8 @@ public class ConnectionRecogniserImplTest extends TestCase { oneOf(db).setConnectionWindow(contactId, remoteIndex, window); }}); Executor executor = new ImmediateExecutor(); - ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db, - executor); + ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(executor, db, + crypto); byte[] tag = calculateTag(); // The tag should not be expected assertFalse(c.isInitialised()); @@ -358,8 +358,8 @@ public class ConnectionRecogniserImplTest extends TestCase { oneOf(db).setConnectionWindow(contactId, remoteIndex, window); }}); Executor executor = new ImmediateExecutor(); - ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db, - executor); + ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(executor, db, + crypto); byte[] tag = calculateTag(); // Update the contact c.eventOccurred(new RemoteTransportsUpdatedEvent(contactId, @@ -401,8 +401,8 @@ public class ConnectionRecogniserImplTest extends TestCase { will(returnValue(window)); }}); Executor executor = new ImmediateExecutor(); - ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db, - executor); + ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(executor, db, + crypto); byte[] tag = calculateTag(); // Ensure the recogniser is initialised assertFalse(c.isInitialised()); @@ -431,8 +431,8 @@ public class ConnectionRecogniserImplTest extends TestCase { will(returnValue(null)); }}); Executor executor = new ImmediateExecutor(); - ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db, - executor); + ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(executor, db, + crypto); byte[] tag = calculateTag(); // Update the contact c.eventOccurred(new RemoteTransportsUpdatedEvent(contactId, @@ -497,8 +497,8 @@ public class ConnectionRecogniserImplTest extends TestCase { oneOf(db).setConnectionWindow(contactId, remoteIndex, window); }}); Executor executor = new ImmediateExecutor(); - ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db, - executor); + ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(executor, db, + crypto); byte[] tag = calculateTag(); // Ensure the recogniser is initialised assertFalse(c.isInitialised()); @@ -574,8 +574,8 @@ public class ConnectionRecogniserImplTest extends TestCase { oneOf(db).setConnectionWindow(contactId, remoteIndex, window); }}); Executor executor = new ImmediateExecutor(); - ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db, - executor); + ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(executor, db, + crypto); byte[] tag = calculateTag(); // Update the contact c.eventOccurred(new RemoteTransportsUpdatedEvent(contactId, diff --git a/test/net/sf/briar/transport/ConnectionWriterTest.java b/test/net/sf/briar/transport/ConnectionWriterTest.java index 9e01fd3c0836b50c8a38203e91bb264e299120b5..0dc41851e556948806f9523360a869873e1c9aff 100644 --- a/test/net/sf/briar/transport/ConnectionWriterTest.java +++ b/test/net/sf/briar/transport/ConnectionWriterTest.java @@ -5,8 +5,6 @@ import static net.sf.briar.api.transport.TransportConstants.MIN_CONNECTION_LENGT import java.io.ByteArrayOutputStream; import java.util.Random; -import java.util.concurrent.Executor; -import java.util.concurrent.ScheduledThreadPoolExecutor; import junit.framework.TestCase; import net.sf.briar.TestDatabaseModule; @@ -22,10 +20,8 @@ import net.sf.briar.serial.SerialModule; import org.junit.Test; -import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Injector; -import com.google.inject.Module; public class ConnectionWriterTest extends TestCase { @@ -34,14 +30,7 @@ public class ConnectionWriterTest extends TestCase { public ConnectionWriterTest() throws Exception { super(); - Module testModule = new AbstractModule() { - @Override - public void configure() { - bind(Executor.class).toInstance( - new ScheduledThreadPoolExecutor(5)); - } - }; - Injector i = Guice.createInjector(testModule, new CryptoModule(), + Injector i = Guice.createInjector(new CryptoModule(), new DatabaseModule(), new LifecycleModule(), new ProtocolModule(), new SerialModule(), new TestDatabaseModule(), new ProtocolBatchModule(),