Converted plugin executor to ScheduledExecutorService.

No Thread.sleep() till Brooklyn.
This commit is contained in:
akwizgran
2011-12-09 21:02:36 +00:00
parent 2014235b86
commit cd068e89c0
17 changed files with 55 additions and 53 deletions

View File

@@ -7,7 +7,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -51,7 +51,7 @@ class PluginManagerImpl implements PluginManager {
"net.sf.briar.plugins.socket.SimpleSocketPluginFactory"
};
private final Executor pluginExecutor;
private final ScheduledExecutorService pluginExecutor;
private final DatabaseComponent db;
private final Poller poller;
private final ConnectionDispatcher dispatcher;
@@ -60,7 +60,7 @@ class PluginManagerImpl implements PluginManager {
private final List<StreamPlugin> streamPlugins; // Locking: this
@Inject
PluginManagerImpl(@PluginExecutor Executor pluginExecutor,
PluginManagerImpl(@PluginExecutor ScheduledExecutorService pluginExecutor,
DatabaseComponent db, Poller poller,
ConnectionDispatcher dispatcher, UiCallback uiCallback) {
this.pluginExecutor = pluginExecutor;

View File

@@ -1,7 +1,7 @@
package net.sf.briar.plugins;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import net.sf.briar.api.plugins.PluginExecutor;
import net.sf.briar.api.plugins.PluginManager;
@@ -13,8 +13,9 @@ public class PluginsModule extends AbstractModule {
@Override
protected void configure() {
bind(Executor.class).annotatedWith(PluginExecutor.class).toInstance(
Executors.newCachedThreadPool());
bind(ScheduledExecutorService.class).annotatedWith(
PluginExecutor.class).toInstance(
Executors.newScheduledThreadPool(1));
bind(PluginManager.class).to(
PluginManagerImpl.class).in(Singleton.class);
bind(Poller.class).to(PollerImpl.class);

View File

@@ -7,7 +7,8 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -38,7 +39,7 @@ class BluetoothPlugin implements StreamPlugin {
private static final Logger LOG =
Logger.getLogger(BluetoothPlugin.class.getName());
private final Executor pluginExecutor;
private final ScheduledExecutorService pluginExecutor;
private final StreamPluginCallback callback;
private final long pollingInterval;
private final Object discoveryLock = new Object();
@@ -47,7 +48,7 @@ class BluetoothPlugin implements StreamPlugin {
private LocalDevice localDevice = null; // Locking: this
private StreamConnectionNotifier socket = null; // Locking: this
BluetoothPlugin(@PluginExecutor Executor pluginExecutor,
BluetoothPlugin(@PluginExecutor ScheduledExecutorService pluginExecutor,
StreamPluginCallback callback, long pollingInterval) {
this.pluginExecutor = pluginExecutor;
this.callback = callback;
@@ -376,18 +377,11 @@ class BluetoothPlugin implements StreamPlugin {
return;
}
// Close the socket when the invitation times out
pluginExecutor.execute(new Runnable() {
pluginExecutor.schedule(new Runnable() {
public void run() {
try {
Thread.sleep(c.getTimeout());
} catch(InterruptedException e) {
if(LOG.isLoggable(Level.INFO))
LOG.info("Interrupted while waiting for invitation");
Thread.currentThread().interrupt();
}
tryToClose(scn);
}
});
}, c.getTimeout(), TimeUnit.MILLISECONDS);
try {
StreamConnection s = scn.acceptAndOpen();
c.addConnection(s);

View File

@@ -1,6 +1,6 @@
package net.sf.briar.plugins.bluetooth;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import net.sf.briar.api.plugins.PluginExecutor;
import net.sf.briar.api.plugins.StreamPlugin;
@@ -11,7 +11,8 @@ public class BluetoothPluginFactory implements StreamPluginFactory {
private static final long POLLING_INTERVAL = 3L * 60L * 1000L; // 3 mins
public StreamPlugin createPlugin(@PluginExecutor Executor pluginExecutor,
public StreamPlugin createPlugin(
@PluginExecutor ScheduledExecutorService pluginExecutor,
StreamPluginCallback callback) {
return new BluetoothPlugin(pluginExecutor, callback, POLLING_INTERVAL);
}

View File

@@ -1,6 +1,6 @@
package net.sf.briar.plugins.file;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import net.sf.briar.api.plugins.BatchPlugin;
import net.sf.briar.api.plugins.BatchPluginCallback;
@@ -12,7 +12,8 @@ public class RemovableDrivePluginFactory implements BatchPluginFactory {
private static final long POLLING_INTERVAL = 10L * 1000L; // 10 seconds
public BatchPlugin createPlugin(@PluginExecutor Executor pluginExecutor,
public BatchPlugin createPlugin(
@PluginExecutor ScheduledExecutorService pluginExecutor,
BatchPluginCallback callback) {
RemovableDriveFinder finder;
RemovableDriveMonitor monitor;

View File

@@ -10,7 +10,7 @@ import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.Random;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -25,7 +25,7 @@ class LanSocketPlugin extends SimpleSocketPlugin {
private static final Logger LOG =
Logger.getLogger(LanSocketPlugin.class.getName());
LanSocketPlugin(@PluginExecutor Executor pluginExecutor,
LanSocketPlugin(@PluginExecutor ScheduledExecutorService pluginExecutor,
StreamPluginCallback callback, long pollingInterval) {
super(pluginExecutor, callback, pollingInterval);
}

View File

@@ -9,7 +9,7 @@ import java.net.Socket;
import java.net.SocketAddress;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -31,7 +31,7 @@ class SimpleSocketPlugin extends SocketPlugin {
private static final Logger LOG =
Logger.getLogger(SimpleSocketPlugin.class.getName());
SimpleSocketPlugin(@PluginExecutor Executor pluginExecutor,
SimpleSocketPlugin(@PluginExecutor ScheduledExecutorService pluginExecutor,
StreamPluginCallback callback, long pollingInterval) {
super(pluginExecutor, callback, pollingInterval);
}

View File

@@ -1,6 +1,6 @@
package net.sf.briar.plugins.socket;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import net.sf.briar.api.plugins.PluginExecutor;
import net.sf.briar.api.plugins.StreamPlugin;
@@ -11,7 +11,8 @@ public class SimpleSocketPluginFactory implements StreamPluginFactory {
private static final long POLLING_INTERVAL = 5L * 60L * 1000L; // 5 mins
public StreamPlugin createPlugin(@PluginExecutor Executor pluginExecutor,
public StreamPlugin createPlugin(
@PluginExecutor ScheduledExecutorService pluginExecutor,
StreamPluginCallback callback) {
return new SimpleSocketPlugin(pluginExecutor, callback,
POLLING_INTERVAL);

View File

@@ -6,7 +6,7 @@ import java.net.Socket;
import java.net.SocketAddress;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -22,7 +22,7 @@ abstract class SocketPlugin implements StreamPlugin {
private static final Logger LOG =
Logger.getLogger(SocketPlugin.class.getName());
protected final Executor pluginExecutor;
protected final ScheduledExecutorService pluginExecutor;
protected final StreamPluginCallback callback;
private final long pollingInterval;
@@ -37,7 +37,8 @@ abstract class SocketPlugin implements StreamPlugin {
protected abstract SocketAddress getLocalSocketAddress();
protected abstract SocketAddress getRemoteSocketAddress(ContactId c);
protected SocketPlugin(@PluginExecutor Executor pluginExecutor,
protected SocketPlugin(
@PluginExecutor ScheduledExecutorService pluginExecutor,
StreamPluginCallback callback, long pollingInterval) {
this.pluginExecutor = pluginExecutor;
this.callback = callback;