Refactored PluginManager and Poller to remove non-open calls. Bug #15.

This commit is contained in:
akwizgran
2014-11-08 15:40:51 +00:00
parent c2d6e9afde
commit 4009561996
5 changed files with 33 additions and 33 deletions

View File

@@ -2,9 +2,15 @@ package org.briarproject.api.lifecycle;
public interface Service { 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(); 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(); public boolean stop();
} }

View File

@@ -42,8 +42,6 @@ import org.briarproject.api.plugins.simplex.SimplexPluginFactory;
import org.briarproject.api.system.Clock; import org.briarproject.api.system.Clock;
import org.briarproject.api.ui.UiCallback; 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 { class PluginManagerImpl implements PluginManager {
private static final Logger LOG = private static final Logger LOG =
@@ -80,7 +78,7 @@ class PluginManagerImpl implements PluginManager {
duplexPlugins = new CopyOnWriteArrayList<DuplexPlugin>(); duplexPlugins = new CopyOnWriteArrayList<DuplexPlugin>();
} }
public synchronized boolean start() { public boolean start() {
// Instantiate and start the simplex plugins // Instantiate and start the simplex plugins
LOG.info("Starting simplex plugins"); LOG.info("Starting simplex plugins");
Collection<SimplexPluginFactory> sFactories = Collection<SimplexPluginFactory> sFactories =
@@ -104,14 +102,10 @@ class PluginManagerImpl implements PluginManager {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
return false; return false;
} }
// Start the poller
LOG.info("Starting poller");
List<Plugin> start = new ArrayList<Plugin>(plugins.values());
poller.start(Collections.unmodifiableList(start));
return true; return true;
} }
public synchronized boolean stop() { public boolean stop() {
// Stop the poller // Stop the poller
LOG.info("Stopping poller"); LOG.info("Stopping poller");
poller.stop(); poller.stop();
@@ -190,6 +184,7 @@ class PluginManagerImpl implements PluginManager {
if(started) { if(started) {
plugins.put(id, plugin); plugins.put(id, plugin);
simplexPlugins.add(plugin); simplexPlugins.add(plugin);
if(plugin.shouldPoll()) poller.addPlugin(plugin);
if(LOG.isLoggable(INFO)) { if(LOG.isLoggable(INFO)) {
String name = plugin.getClass().getSimpleName(); String name = plugin.getClass().getSimpleName();
LOG.info("Starting " + name + " took " + LOG.info("Starting " + name + " took " +
@@ -252,6 +247,7 @@ class PluginManagerImpl implements PluginManager {
if(started) { if(started) {
plugins.put(id, plugin); plugins.put(id, plugin);
duplexPlugins.add(plugin); duplexPlugins.add(plugin);
if(plugin.shouldPoll()) poller.addPlugin(plugin);
if(LOG.isLoggable(INFO)) { if(LOG.isLoggable(INFO)) {
String name = plugin.getClass().getSimpleName(); String name = plugin.getClass().getSimpleName();
LOG.info("Starting " + name + " took " + LOG.info("Starting " + name + " took " +

View File

@@ -1,17 +1,15 @@
package org.briarproject.plugins; package org.briarproject.plugins;
import java.util.Collection;
import org.briarproject.api.plugins.Plugin; import org.briarproject.api.plugins.Plugin;
interface Poller { interface Poller {
/** Starts a poller for the given collection of plugins. */ /** Adds the given plugin to the collection of plugins to be polled. */
void start(Collection<Plugin> plugins); void addPlugin(Plugin p);
/** Stops the poller. */
void stop();
/** Tells the poller to poll the given plugin immediately. */ /** Tells the poller to poll the given plugin immediately. */
void pollNow(Plugin p); void pollNow(Plugin p);
/** Stops the poller. */
void stop();
} }

View File

@@ -2,7 +2,6 @@ package org.briarproject.plugins;
import static java.util.logging.Level.INFO; import static java.util.logging.Level.INFO;
import java.util.Collection;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.logging.Logger; import java.util.logging.Logger;
@@ -31,21 +30,19 @@ class PollerImpl implements Poller {
this.timer = timer; this.timer = timer;
} }
public void start(Collection<Plugin> plugins) { public void stop() {
for(Plugin plugin : plugins) schedule(plugin, true); timer.cancel();
}
public void addPlugin(Plugin p) {
schedule(p, true);
} }
private void schedule(Plugin plugin, boolean randomise) { private void schedule(Plugin plugin, boolean randomise) {
if(plugin.shouldPoll()) { long interval = plugin.getPollingInterval();
long interval = plugin.getPollingInterval(); // Randomise intervals at startup to spread out connection attempts
// Randomise intervals at startup to spread out connection attempts if(randomise) interval = (long) (interval * Math.random());
if(randomise) interval = (long) (interval * Math.random()); timer.schedule(new PollTask(plugin), interval);
timer.schedule(new PollTask(plugin), interval);
}
}
public void stop() {
timer.cancel();
} }
public void pollNow(final Plugin p) { public void pollNow(final Plugin p) {

View File

@@ -76,6 +76,9 @@ public class PluginManagerImplTest extends BriarTestCase {
will(returnValue(true)); will(returnValue(true));
oneOf(simplexPlugin).start(); oneOf(simplexPlugin).start();
will(returnValue(true)); // Started will(returnValue(true)); // Started
oneOf(simplexPlugin).shouldPoll();
will(returnValue(true));
oneOf(poller).addPlugin(simplexPlugin);
// Second simplex plugin // Second simplex plugin
oneOf(simplexFailFactory).getId(); oneOf(simplexFailFactory).getId();
will(returnValue(simplexFailId)); will(returnValue(simplexFailId));
@@ -102,14 +105,14 @@ public class PluginManagerImplTest extends BriarTestCase {
will(returnValue(true)); will(returnValue(true));
oneOf(duplexPlugin).start(); oneOf(duplexPlugin).start();
will(returnValue(true)); // Started will(returnValue(true)); // Started
oneOf(duplexPlugin).shouldPoll();
will(returnValue(false));
// Second duplex plugin // Second duplex plugin
oneOf(duplexFailFactory).getId(); oneOf(duplexFailFactory).getId();
will(returnValue(duplexFailId)); will(returnValue(duplexFailId));
oneOf(duplexFailFactory).createPlugin(with(any( oneOf(duplexFailFactory).createPlugin(with(any(
DuplexPluginCallback.class))); DuplexPluginCallback.class)));
will(returnValue(null)); // Failed to create a plugin will(returnValue(null)); // Failed to create a plugin
// Start the poller
oneOf(poller).start(Arrays.asList(simplexPlugin, duplexPlugin));
// Stop the poller // Stop the poller
oneOf(poller).stop(); oneOf(poller).stop();
// Stop the plugins // Stop the plugins