No need to poll plugins before they're enabled.

This commit is contained in:
akwizgran
2016-05-04 15:38:20 +01:00
parent 1ae98ef2b5
commit 036c5d6753
4 changed files with 27 additions and 31 deletions

View File

@@ -142,10 +142,12 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
} }
} }
@Override
public Plugin getPlugin(TransportId t) { public Plugin getPlugin(TransportId t) {
return plugins.get(t); return plugins.get(t);
} }
@Override
public Collection<DuplexPlugin> getInvitationPlugins() { public Collection<DuplexPlugin> getInvitationPlugins() {
List<DuplexPlugin> supported = new ArrayList<DuplexPlugin>(); List<DuplexPlugin> supported = new ArrayList<DuplexPlugin>();
for (DuplexPlugin d : duplexPlugins) for (DuplexPlugin d : duplexPlugins)
@@ -153,6 +155,7 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
return Collections.unmodifiableList(supported); return Collections.unmodifiableList(supported);
} }
@Override
public Collection<DuplexPlugin> getKeyAgreementPlugins() { public Collection<DuplexPlugin> getKeyAgreementPlugins() {
List<DuplexPlugin> supported = new ArrayList<DuplexPlugin>(); List<DuplexPlugin> supported = new ArrayList<DuplexPlugin>();
for (DuplexPlugin d : duplexPlugins) for (DuplexPlugin d : duplexPlugins)
@@ -194,6 +197,7 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
private void connectToContact(final ContactId c, final SimplexPlugin p) { private void connectToContact(final ContactId c, final SimplexPlugin p) {
ioExecutor.execute(new Runnable() { ioExecutor.execute(new Runnable() {
@Override
public void run() { public void run() {
TransportId t = p.getId(); TransportId t = p.getId();
if (!connectionRegistry.isConnected(c, t)) { if (!connectionRegistry.isConnected(c, t)) {
@@ -207,6 +211,7 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
private void connectToContact(final ContactId c, final DuplexPlugin p) { private void connectToContact(final ContactId c, final DuplexPlugin p) {
ioExecutor.execute(new Runnable() { ioExecutor.execute(new Runnable() {
@Override
public void run() { public void run() {
TransportId t = p.getId(); TransportId t = p.getId();
if (!connectionRegistry.isConnected(c, t)) { if (!connectionRegistry.isConnected(c, t)) {
@@ -229,6 +234,7 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
this.latch = latch; this.latch = latch;
} }
@Override
public void run() { public void run() {
try { try {
TransportId id = factory.getId(); TransportId id = factory.getId();
@@ -248,7 +254,6 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
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 " +
@@ -281,6 +286,7 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
this.latch = latch; this.latch = latch;
} }
@Override
public void run() { public void run() {
try { try {
TransportId id = factory.getId(); TransportId id = factory.getId();
@@ -300,7 +306,6 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
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 " +
@@ -332,6 +337,7 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
this.latch = latch; this.latch = latch;
} }
@Override
public void run() { public void run() {
try { try {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
@@ -357,6 +363,7 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
this.id = id; this.id = id;
} }
@Override
public Settings getSettings() { public Settings getSettings() {
try { try {
return settingsManager.getSettings(id.getString()); return settingsManager.getSettings(id.getString());
@@ -366,6 +373,7 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
} }
} }
@Override
public TransportProperties getLocalProperties() { public TransportProperties getLocalProperties() {
try { try {
TransportProperties p = TransportProperties p =
@@ -377,6 +385,7 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
} }
} }
@Override
public Map<ContactId, TransportProperties> getRemoteProperties() { public Map<ContactId, TransportProperties> getRemoteProperties() {
try { try {
return transportPropertyManager.getRemoteProperties(id); return transportPropertyManager.getRemoteProperties(id);
@@ -386,6 +395,7 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
} }
} }
@Override
public void mergeSettings(Settings s) { public void mergeSettings(Settings s) {
try { try {
settingsManager.mergeSettings(s, id.getString()); settingsManager.mergeSettings(s, id.getString());
@@ -394,6 +404,7 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
} }
} }
@Override
public void mergeLocalProperties(TransportProperties p) { public void mergeLocalProperties(TransportProperties p) {
try { try {
transportPropertyManager.mergeLocalProperties(id, p); transportPropertyManager.mergeLocalProperties(id, p);
@@ -402,24 +413,29 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
} }
} }
@Override
public int showChoice(String[] options, String... message) { public int showChoice(String[] options, String... message) {
return uiCallback.showChoice(options, message); return uiCallback.showChoice(options, message);
} }
@Override
public boolean showConfirmationMessage(String... message) { public boolean showConfirmationMessage(String... message) {
return uiCallback.showConfirmationMessage(message); return uiCallback.showConfirmationMessage(message);
} }
@Override
public void showMessage(String... message) { public void showMessage(String... message) {
uiCallback.showMessage(message); uiCallback.showMessage(message);
} }
@Override
public void transportEnabled() { public void transportEnabled() {
eventBus.broadcast(new TransportEnabledEvent(id)); eventBus.broadcast(new TransportEnabledEvent(id));
Plugin p = plugins.get(id); Plugin p = plugins.get(id);
if (p != null) poller.pollNow(p); if (p != null) poller.pollNow(p);
} }
@Override
public void transportDisabled() { public void transportDisabled() {
eventBus.broadcast(new TransportDisabledEvent(id)); eventBus.broadcast(new TransportDisabledEvent(id));
} }
@@ -432,10 +448,12 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
super(id); super(id);
} }
@Override
public void readerCreated(TransportConnectionReader r) { public void readerCreated(TransportConnectionReader r) {
connectionManager.manageIncomingConnection(id, r); connectionManager.manageIncomingConnection(id, r);
} }
@Override
public void writerCreated(ContactId c, TransportConnectionWriter w) { public void writerCreated(ContactId c, TransportConnectionWriter w) {
connectionManager.manageOutgoingConnection(c, id, w); connectionManager.manageOutgoingConnection(c, id, w);
} }
@@ -448,10 +466,12 @@ class PluginManagerImpl implements PluginManager, Service, EventListener {
super(id); super(id);
} }
@Override
public void incomingConnectionCreated(DuplexTransportConnection d) { public void incomingConnectionCreated(DuplexTransportConnection d) {
connectionManager.manageIncomingConnection(id, d); connectionManager.manageIncomingConnection(id, d);
} }
@Override
public void outgoingConnectionCreated(ContactId c, public void outgoingConnectionCreated(ContactId c,
DuplexTransportConnection d) { DuplexTransportConnection d) {
connectionManager.manageOutgoingConnection(c, id, d); connectionManager.manageOutgoingConnection(c, id, d);

View File

@@ -4,9 +4,6 @@ import org.briarproject.api.plugins.Plugin;
interface Poller { interface Poller {
/** 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. */ /** Tells the poller to poll the given plugin immediately. */
void pollNow(Plugin p); void pollNow(Plugin p);

View File

@@ -39,25 +39,17 @@ class PollerImpl implements Poller {
tasks = new ConcurrentHashMap<TransportId, PollTask>(); tasks = new ConcurrentHashMap<TransportId, PollTask>();
} }
@Override
public void stop() { public void stop() {
timer.cancel(); timer.cancel();
} }
public void addPlugin(Plugin p) { @Override
// Randomise first polling interval
if (p.shouldPoll())
schedule(p, randomise(p.getPollingInterval()), false);
}
public void pollNow(Plugin p) { public void pollNow(Plugin p) {
// Randomise next polling interval // Randomise next polling interval
if (p.shouldPoll()) schedule(p, 0, true); if (p.shouldPoll()) schedule(p, 0, true);
} }
private int randomise(int interval) {
return (int) (interval * random.nextDouble());
}
private void schedule(Plugin p, int interval, boolean randomiseNext) { private void schedule(Plugin p, int interval, boolean randomiseNext) {
// Replace any previously scheduled task for this plugin // Replace any previously scheduled task for this plugin
PollTask task = new PollTask(p, randomiseNext); PollTask task = new PollTask(p, randomiseNext);
@@ -68,6 +60,7 @@ class PollerImpl implements Poller {
private void poll(final Plugin p) { private void poll(final Plugin p) {
ioExecutor.execute(new Runnable() { ioExecutor.execute(new Runnable() {
@Override
public void run() { public void run() {
if (LOG.isLoggable(INFO)) if (LOG.isLoggable(INFO))
LOG.info("Polling " + p.getClass().getSimpleName()); LOG.info("Polling " + p.getClass().getSimpleName());
@@ -90,7 +83,8 @@ class PollerImpl implements Poller {
public void run() { public void run() {
tasks.remove(plugin.getId()); tasks.remove(plugin.getId());
int interval = plugin.getPollingInterval(); int interval = plugin.getPollingInterval();
if (randomiseNext) interval = randomise(interval); if (randomiseNext)
interval = (int) (interval * random.nextDouble());
schedule(plugin, interval, false); schedule(plugin, interval, false);
poll(plugin); poll(plugin);
} }

View File

@@ -84,9 +84,6 @@ public class PluginManagerImplTest extends BriarTestCase {
will(returnValue(simplexPlugin)); // Created will(returnValue(simplexPlugin)); // Created
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));
@@ -105,8 +102,6 @@ public class PluginManagerImplTest extends BriarTestCase {
will(returnValue(duplexPlugin)); // Created will(returnValue(duplexPlugin)); // Created
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));
@@ -193,9 +188,6 @@ public class PluginManagerImplTest extends BriarTestCase {
will(returnValue(simplexPlugin)); // Created will(returnValue(simplexPlugin)); // Created
oneOf(simplexPlugin).start(); oneOf(simplexPlugin).start();
will(returnValue(true)); // Started will(returnValue(true)); // Started
oneOf(simplexPlugin).shouldPoll();
will(returnValue(true)); // Should poll
oneOf(poller).addPlugin(simplexPlugin);
// Second simplex plugin // Second simplex plugin
oneOf(simplexFactory1).getId(); oneOf(simplexFactory1).getId();
will(returnValue(simplexId1)); will(returnValue(simplexId1));
@@ -204,8 +196,6 @@ public class PluginManagerImplTest extends BriarTestCase {
will(returnValue(simplexPlugin1)); // Created will(returnValue(simplexPlugin1)); // Created
oneOf(simplexPlugin1).start(); oneOf(simplexPlugin1).start();
will(returnValue(true)); // Started will(returnValue(true)); // Started
oneOf(simplexPlugin1).shouldPoll();
will(returnValue(false)); // Should not poll
// First duplex plugin // First duplex plugin
oneOf(pluginConfig).getDuplexFactories(); oneOf(pluginConfig).getDuplexFactories();
will(returnValue(Arrays.asList(duplexFactory, duplexFactory1))); will(returnValue(Arrays.asList(duplexFactory, duplexFactory1)));
@@ -216,9 +206,6 @@ public class PluginManagerImplTest extends BriarTestCase {
will(returnValue(duplexPlugin)); // Created will(returnValue(duplexPlugin)); // Created
oneOf(duplexPlugin).start(); oneOf(duplexPlugin).start();
will(returnValue(true)); // Started will(returnValue(true)); // Started
oneOf(duplexPlugin).shouldPoll();
will(returnValue(true)); // Should poll
oneOf(poller).addPlugin(duplexPlugin);
// Second duplex plugin // Second duplex plugin
oneOf(duplexFactory1).getId(); oneOf(duplexFactory1).getId();
will(returnValue(duplexId1)); will(returnValue(duplexId1));
@@ -227,8 +214,6 @@ public class PluginManagerImplTest extends BriarTestCase {
will(returnValue(duplexPlugin1)); // Created will(returnValue(duplexPlugin1)); // Created
oneOf(duplexPlugin1).start(); oneOf(duplexPlugin1).start();
will(returnValue(true)); // Started will(returnValue(true)); // Started
oneOf(duplexPlugin1).shouldPoll();
will(returnValue(false)); // Should not poll
// Start listening for events // Start listening for events
oneOf(eventBus).addListener(with(any(EventListener.class))); oneOf(eventBus).addListener(with(any(EventListener.class)));
// eventOccurred() // eventOccurred()