diff --git a/briar-api/src/org/briarproject/api/lifecycle/Service.java b/briar-api/src/org/briarproject/api/lifecycle/Service.java index 88bbb6b4e28a5496cf8b747c80f6ddd207ed5089..3c80e6cf68835e01558a94bbe664d86bd610d20b 100644 --- a/briar-api/src/org/briarproject/api/lifecycle/Service.java +++ b/briar-api/src/org/briarproject/api/lifecycle/Service.java @@ -2,9 +2,15 @@ package org.briarproject.api.lifecycle; public interface Service { - /** Starts the service and returns true if it started successfully. */ + /** + * Starts the service and returns true if it started successfully. + * This method must not be called concurrently with {@link #stop()}. + */ public boolean start(); - /** Stops the service and returns true if it stopped successfully. */ + /** + * Stops the service and returns true if it stopped successfully. + * This method must not be called concurrently with {@link #start()}. + */ public boolean stop(); } diff --git a/briar-core/src/org/briarproject/plugins/PluginManagerImpl.java b/briar-core/src/org/briarproject/plugins/PluginManagerImpl.java index 620698c4a9abf991e0c1fef2d88f2326f70a254e..e6ea8587783f9cc7829c94db5d9183873244cf58 100644 --- a/briar-core/src/org/briarproject/plugins/PluginManagerImpl.java +++ b/briar-core/src/org/briarproject/plugins/PluginManagerImpl.java @@ -42,8 +42,6 @@ import org.briarproject.api.plugins.simplex.SimplexPluginFactory; import org.briarproject.api.system.Clock; import org.briarproject.api.ui.UiCallback; -// FIXME: Don't make alien calls with a lock held (that includes waiting on a -// latch that depends on an alien call) class PluginManagerImpl implements PluginManager { private static final Logger LOG = @@ -63,7 +61,7 @@ class PluginManagerImpl implements PluginManager { @Inject PluginManagerImpl(@IoExecutor Executor ioExecutor, - SimplexPluginConfig simplexPluginConfig, + SimplexPluginConfig simplexPluginConfig, DuplexPluginConfig duplexPluginConfig, Clock clock, DatabaseComponent db, Poller poller, ConnectionManager connectionManager, UiCallback uiCallback) { @@ -80,7 +78,7 @@ class PluginManagerImpl implements PluginManager { duplexPlugins = new CopyOnWriteArrayList<DuplexPlugin>(); } - public synchronized boolean start() { + public boolean start() { // Instantiate and start the simplex plugins LOG.info("Starting simplex plugins"); Collection<SimplexPluginFactory> sFactories = @@ -104,14 +102,10 @@ class PluginManagerImpl implements PluginManager { Thread.currentThread().interrupt(); return false; } - // Start the poller - LOG.info("Starting poller"); - List<Plugin> start = new ArrayList<Plugin>(plugins.values()); - poller.start(Collections.unmodifiableList(start)); return true; } - public synchronized boolean stop() { + public boolean stop() { // Stop the poller LOG.info("Stopping poller"); poller.stop(); @@ -190,6 +184,7 @@ class PluginManagerImpl implements PluginManager { if(started) { plugins.put(id, plugin); simplexPlugins.add(plugin); + if(plugin.shouldPoll()) poller.addPlugin(plugin); if(LOG.isLoggable(INFO)) { String name = plugin.getClass().getSimpleName(); LOG.info("Starting " + name + " took " + @@ -252,6 +247,7 @@ class PluginManagerImpl implements PluginManager { if(started) { plugins.put(id, plugin); duplexPlugins.add(plugin); + if(plugin.shouldPoll()) poller.addPlugin(plugin); if(LOG.isLoggable(INFO)) { String name = plugin.getClass().getSimpleName(); LOG.info("Starting " + name + " took " + diff --git a/briar-core/src/org/briarproject/plugins/Poller.java b/briar-core/src/org/briarproject/plugins/Poller.java index ae57caebae456ed70ec8ac2001e00327c2d4f38a..22de0148949ee7003dfec2ef87546579f603af48 100644 --- a/briar-core/src/org/briarproject/plugins/Poller.java +++ b/briar-core/src/org/briarproject/plugins/Poller.java @@ -1,17 +1,15 @@ package org.briarproject.plugins; -import java.util.Collection; - import org.briarproject.api.plugins.Plugin; interface Poller { - /** Starts a poller for the given collection of plugins. */ - void start(Collection<Plugin> plugins); - - /** Stops the poller. */ - void stop(); + /** Adds the given plugin to the collection of plugins to be polled. */ + void addPlugin(Plugin p); /** Tells the poller to poll the given plugin immediately. */ void pollNow(Plugin p); + + /** Stops the poller. */ + void stop(); } diff --git a/briar-core/src/org/briarproject/plugins/PollerImpl.java b/briar-core/src/org/briarproject/plugins/PollerImpl.java index bd0f03a273c1a8e48465fe3e863e78ecbf2eb347..1d3ede42ce944a79205ce5ee5d085ddec99962ed 100644 --- a/briar-core/src/org/briarproject/plugins/PollerImpl.java +++ b/briar-core/src/org/briarproject/plugins/PollerImpl.java @@ -2,7 +2,6 @@ package org.briarproject.plugins; import static java.util.logging.Level.INFO; -import java.util.Collection; import java.util.TimerTask; import java.util.concurrent.Executor; import java.util.logging.Logger; @@ -31,21 +30,19 @@ class PollerImpl implements Poller { this.timer = timer; } - public void start(Collection<Plugin> plugins) { - for(Plugin plugin : plugins) schedule(plugin, true); + public void stop() { + timer.cancel(); } - private void schedule(Plugin plugin, boolean randomise) { - if(plugin.shouldPoll()) { - long interval = plugin.getPollingInterval(); - // Randomise intervals at startup to spread out connection attempts - if(randomise) interval = (long) (interval * Math.random()); - timer.schedule(new PollTask(plugin), interval); - } + public void addPlugin(Plugin p) { + schedule(p, true); } - public void stop() { - timer.cancel(); + private void schedule(Plugin plugin, boolean randomise) { + long interval = plugin.getPollingInterval(); + // Randomise intervals at startup to spread out connection attempts + if(randomise) interval = (long) (interval * Math.random()); + timer.schedule(new PollTask(plugin), interval); } public void pollNow(final Plugin p) { diff --git a/briar-tests/src/org/briarproject/plugins/PluginManagerImplTest.java b/briar-tests/src/org/briarproject/plugins/PluginManagerImplTest.java index 02afd959029612a6dbbbec0bfc35e59a657ef1cd..2e088fa3066b09da872c8c1adb5fb9db60ed8efa 100644 --- a/briar-tests/src/org/briarproject/plugins/PluginManagerImplTest.java +++ b/briar-tests/src/org/briarproject/plugins/PluginManagerImplTest.java @@ -76,6 +76,9 @@ public class PluginManagerImplTest extends BriarTestCase { will(returnValue(true)); oneOf(simplexPlugin).start(); will(returnValue(true)); // Started + oneOf(simplexPlugin).shouldPoll(); + will(returnValue(true)); + oneOf(poller).addPlugin(simplexPlugin); // Second simplex plugin oneOf(simplexFailFactory).getId(); will(returnValue(simplexFailId)); @@ -102,14 +105,14 @@ public class PluginManagerImplTest extends BriarTestCase { will(returnValue(true)); oneOf(duplexPlugin).start(); will(returnValue(true)); // Started + oneOf(duplexPlugin).shouldPoll(); + will(returnValue(false)); // Second duplex plugin oneOf(duplexFailFactory).getId(); will(returnValue(duplexFailId)); oneOf(duplexFailFactory).createPlugin(with(any( DuplexPluginCallback.class))); will(returnValue(null)); // Failed to create a plugin - // Start the poller - oneOf(poller).start(Arrays.asList(simplexPlugin, duplexPlugin)); // Stop the poller oneOf(poller).stop(); // Stop the plugins