mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 20:29:52 +01:00
Cancel outstanding tasks and shut down the executor.
This commit is contained in:
@@ -76,7 +76,7 @@ class PluginManagerImpl implements PluginManager {
|
||||
return batchPlugins.size() + streamPlugins.size();
|
||||
}
|
||||
|
||||
public synchronized int startPlugins() {
|
||||
public synchronized int start() {
|
||||
Set<TransportId> ids = new HashSet<TransportId>();
|
||||
// Instantiate and start the batch plugins
|
||||
for(String s : BATCH_FACTORIES) {
|
||||
@@ -162,12 +162,12 @@ class PluginManagerImpl implements PluginManager {
|
||||
List<Plugin> plugins = new ArrayList<Plugin>();
|
||||
plugins.addAll(batchPlugins);
|
||||
plugins.addAll(streamPlugins);
|
||||
poller.startPolling(Collections.unmodifiableList(plugins));
|
||||
poller.start(Collections.unmodifiableList(plugins));
|
||||
// Return the number of plugins successfully started
|
||||
return batchPlugins.size() + streamPlugins.size();
|
||||
}
|
||||
|
||||
public synchronized int stopPlugins() {
|
||||
public synchronized int stop() {
|
||||
int stopped = 0;
|
||||
// Stop the batch plugins
|
||||
for(BatchPlugin plugin : batchPlugins) {
|
||||
@@ -189,6 +189,10 @@ class PluginManagerImpl implements PluginManager {
|
||||
}
|
||||
}
|
||||
streamPlugins.clear();
|
||||
// Stop the poller
|
||||
poller.stop();
|
||||
// Shut down the executor service
|
||||
pluginExecutor.shutdown();
|
||||
// Return the number of plugins successfully stopped
|
||||
return stopped;
|
||||
}
|
||||
|
||||
@@ -7,8 +7,8 @@ import net.sf.briar.api.plugins.Plugin;
|
||||
interface Poller {
|
||||
|
||||
/** Starts a new thread to poll the given collection of plugins. */
|
||||
void startPolling(Collection<Plugin> plugins);
|
||||
void start(Collection<Plugin> plugins);
|
||||
|
||||
/** Tells the poller thread to exit. */
|
||||
void stopPolling();
|
||||
void stop();
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ class PollerImpl implements Poller, Runnable {
|
||||
pollTimes = new TreeSet<PollTime>();
|
||||
}
|
||||
|
||||
public synchronized void startPolling(Collection<Plugin> plugins) {
|
||||
public synchronized void start(Collection<Plugin> plugins) {
|
||||
for(Plugin plugin : plugins) schedule(plugin);
|
||||
new Thread(this).start();
|
||||
}
|
||||
@@ -39,7 +39,7 @@ class PollerImpl implements Poller, Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void stopPolling() {
|
||||
public synchronized void stop() {
|
||||
pollTimes.clear();
|
||||
notifyAll();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.sf.briar.plugins.bluetooth;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
@@ -8,6 +9,7 @@ import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
@@ -43,6 +45,7 @@ class BluetoothPlugin implements StreamPlugin {
|
||||
private final StreamPluginCallback callback;
|
||||
private final long pollingInterval;
|
||||
private final Object discoveryLock = new Object();
|
||||
private final Collection<ScheduledFuture<?>> socketClosers; // Locking: this
|
||||
|
||||
private boolean running = false; // Locking: this
|
||||
private LocalDevice localDevice = null; // Locking: this
|
||||
@@ -53,6 +56,7 @@ class BluetoothPlugin implements StreamPlugin {
|
||||
this.pluginExecutor = pluginExecutor;
|
||||
this.callback = callback;
|
||||
this.pollingInterval = pollingInterval;
|
||||
socketClosers = new ArrayList<ScheduledFuture<?>>();
|
||||
}
|
||||
|
||||
public TransportId getId() {
|
||||
@@ -167,6 +171,7 @@ class BluetoothPlugin implements StreamPlugin {
|
||||
|
||||
public synchronized void stop() throws IOException {
|
||||
running = false;
|
||||
for(ScheduledFuture<?> close : socketClosers) close.cancel(false);
|
||||
localDevice = null;
|
||||
if(socket != null) {
|
||||
socket.close();
|
||||
@@ -377,11 +382,15 @@ class BluetoothPlugin implements StreamPlugin {
|
||||
return;
|
||||
}
|
||||
// Close the socket when the invitation times out
|
||||
pluginExecutor.schedule(new Runnable() {
|
||||
Runnable close = new Runnable() {
|
||||
public void run() {
|
||||
tryToClose(scn);
|
||||
}
|
||||
}, c.getTimeout(), TimeUnit.MILLISECONDS);
|
||||
};
|
||||
synchronized(this) {
|
||||
socketClosers.add(pluginExecutor.schedule(close, c.getTimeout(),
|
||||
TimeUnit.MILLISECONDS));
|
||||
}
|
||||
try {
|
||||
StreamConnection s = scn.acceptAndOpen();
|
||||
c.addConnection(s);
|
||||
|
||||
Reference in New Issue
Block a user