mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 03:39:05 +01:00
Plugins should use the executor rather than creating threads.
This commit is contained in:
@@ -126,7 +126,11 @@ class BluetoothPlugin extends AbstractPlugin implements StreamPlugin {
|
||||
}
|
||||
socket = scn;
|
||||
}
|
||||
startContactAccepterThread();
|
||||
pluginExecutor.execute(new Runnable() {
|
||||
public void run() {
|
||||
acceptContactConnections();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private synchronized String getUuid() {
|
||||
@@ -160,15 +164,6 @@ class BluetoothPlugin extends AbstractPlugin implements StreamPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
private void startContactAccepterThread() {
|
||||
new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
acceptContactConnections();
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
|
||||
private void acceptContactConnections() {
|
||||
while(true) {
|
||||
StreamConnectionNotifier scn;
|
||||
@@ -299,9 +294,17 @@ class BluetoothPlugin extends AbstractPlugin implements StreamPlugin {
|
||||
// The invitee's device may not be discoverable, so both parties must
|
||||
// try to initiate connections
|
||||
String uuid = convertInvitationCodeToUuid(code);
|
||||
ConnectionCallback c = new ConnectionCallback(uuid, timeout);
|
||||
startOutgoingInvitationThread(c);
|
||||
startIncomingInvitationThread(c);
|
||||
final ConnectionCallback c = new ConnectionCallback(uuid, timeout);
|
||||
pluginExecutor.execute(new Runnable() {
|
||||
public void run() {
|
||||
createInvitationConnection(c);
|
||||
}
|
||||
});
|
||||
pluginExecutor.execute(new Runnable() {
|
||||
public void run() {
|
||||
bindInvitationSocket(c);
|
||||
}
|
||||
});
|
||||
try {
|
||||
StreamConnection s = c.waitForConnection();
|
||||
return s == null ? null : new BluetoothTransportConnection(s);
|
||||
@@ -319,15 +322,6 @@ class BluetoothPlugin extends AbstractPlugin implements StreamPlugin {
|
||||
return StringUtils.toHexString(b);
|
||||
}
|
||||
|
||||
private void startOutgoingInvitationThread(final ConnectionCallback c) {
|
||||
new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
createInvitationConnection(c);
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
|
||||
private void createInvitationConnection(ConnectionCallback c) {
|
||||
DiscoveryAgent discoveryAgent;
|
||||
synchronized(this) {
|
||||
@@ -369,30 +363,25 @@ class BluetoothPlugin extends AbstractPlugin implements StreamPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
private void startIncomingInvitationThread(final ConnectionCallback c) {
|
||||
new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
bindInvitationSocket(c);
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
|
||||
private void bindInvitationSocket(ConnectionCallback c) {
|
||||
private void bindInvitationSocket(final ConnectionCallback c) {
|
||||
synchronized(this) {
|
||||
if(!started) return;
|
||||
makeDeviceDiscoverable();
|
||||
}
|
||||
// Bind the socket
|
||||
String url = "btspp://localhost:" + c.getUuid() + ";name=RFCOMM";
|
||||
StreamConnectionNotifier scn;
|
||||
final StreamConnectionNotifier scn;
|
||||
try {
|
||||
scn = (StreamConnectionNotifier) Connector.open(url);
|
||||
} catch(IOException e) {
|
||||
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
|
||||
return;
|
||||
}
|
||||
startInvitationAccepterThread(c, scn);
|
||||
pluginExecutor.execute(new Runnable() {
|
||||
public void run() {
|
||||
acceptInvitationConnection(c, scn);
|
||||
}
|
||||
});
|
||||
// Close the socket when the invitation times out
|
||||
try {
|
||||
Thread.sleep(c.getTimeout());
|
||||
@@ -408,16 +397,6 @@ class BluetoothPlugin extends AbstractPlugin implements StreamPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
private void startInvitationAccepterThread(final ConnectionCallback c,
|
||||
final StreamConnectionNotifier scn) {
|
||||
new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
acceptInvitationConnection(c, scn);
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
|
||||
private void acceptInvitationConnection(ConnectionCallback c,
|
||||
StreamConnectionNotifier scn) {
|
||||
synchronized(this) {
|
||||
|
||||
@@ -3,14 +3,18 @@ package net.sf.briar.plugins.file;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import net.sf.briar.api.plugins.PluginExecutor;
|
||||
|
||||
class PollingRemovableDriveMonitor implements RemovableDriveMonitor, Runnable {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(PollingRemovableDriveMonitor.class.getName());
|
||||
|
||||
private final Executor pluginExecutor;
|
||||
private final RemovableDriveFinder finder;
|
||||
private final long pollingInterval;
|
||||
private final Object pollingLock = new Object();
|
||||
@@ -19,8 +23,9 @@ class PollingRemovableDriveMonitor implements RemovableDriveMonitor, Runnable {
|
||||
private volatile Callback callback = null;
|
||||
private volatile IOException exception = null;
|
||||
|
||||
public PollingRemovableDriveMonitor(RemovableDriveFinder finder,
|
||||
long pollingInterval) {
|
||||
public PollingRemovableDriveMonitor(@PluginExecutor Executor pluginExecutor,
|
||||
RemovableDriveFinder finder, long pollingInterval) {
|
||||
this.pluginExecutor = pluginExecutor;
|
||||
this.finder = finder;
|
||||
this.pollingInterval = pollingInterval;
|
||||
}
|
||||
@@ -29,7 +34,7 @@ class PollingRemovableDriveMonitor implements RemovableDriveMonitor, Runnable {
|
||||
if(running) throw new IllegalStateException();
|
||||
running = true;
|
||||
this.callback = callback;
|
||||
new Thread(this).start();
|
||||
pluginExecutor.execute(this);
|
||||
}
|
||||
|
||||
public synchronized void stop() throws IOException {
|
||||
@@ -50,14 +55,7 @@ class PollingRemovableDriveMonitor implements RemovableDriveMonitor, Runnable {
|
||||
Collection<File> drives = finder.findRemovableDrives();
|
||||
while(running) {
|
||||
synchronized(pollingLock) {
|
||||
try {
|
||||
pollingLock.wait(pollingInterval);
|
||||
} catch(InterruptedException e) {
|
||||
if(LOG.isLoggable(Level.INFO))
|
||||
LOG.info("Interrupted while waiting to poll");
|
||||
Thread.currentThread().interrupt();
|
||||
return;
|
||||
}
|
||||
pollingLock.wait(pollingInterval);
|
||||
}
|
||||
if(!running) return;
|
||||
Collection<File> newDrives = finder.findRemovableDrives();
|
||||
@@ -66,6 +64,10 @@ class PollingRemovableDriveMonitor implements RemovableDriveMonitor, Runnable {
|
||||
}
|
||||
drives = newDrives;
|
||||
}
|
||||
} catch(InterruptedException e) {
|
||||
if(LOG.isLoggable(Level.INFO))
|
||||
LOG.info("Interrupted while waiting to poll");
|
||||
Thread.currentThread().interrupt();
|
||||
} catch(IOException e) {
|
||||
exception = e;
|
||||
}
|
||||
|
||||
@@ -25,11 +25,11 @@ public class RemovableDrivePluginFactory implements BatchPluginFactory {
|
||||
} else if(OsUtils.isMac()) {
|
||||
// JNotify requires OS X 10.5 or newer, so we have to poll
|
||||
finder = new MacRemovableDriveFinder();
|
||||
monitor = new PollingRemovableDriveMonitor(finder,
|
||||
monitor = new PollingRemovableDriveMonitor(pluginExecutor, finder,
|
||||
POLLING_INTERVAL);
|
||||
} else if(OsUtils.isWindows()) {
|
||||
finder = new WindowsRemovableDriveFinder();
|
||||
monitor = new PollingRemovableDriveMonitor(finder,
|
||||
monitor = new PollingRemovableDriveMonitor(pluginExecutor, finder,
|
||||
POLLING_INTERVAL);
|
||||
} else {
|
||||
return null;
|
||||
|
||||
@@ -42,15 +42,11 @@ abstract class SocketPlugin extends AbstractPlugin implements StreamPlugin {
|
||||
@Override
|
||||
public synchronized void start() throws IOException {
|
||||
super.start();
|
||||
pluginExecutor.execute(createBinder());
|
||||
}
|
||||
|
||||
private Runnable createBinder() {
|
||||
return new Runnable() {
|
||||
pluginExecutor.execute(new Runnable() {
|
||||
public void run() {
|
||||
bind();
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
private void bind() {
|
||||
@@ -93,25 +89,11 @@ abstract class SocketPlugin extends AbstractPlugin implements StreamPlugin {
|
||||
socket = ss;
|
||||
setLocalSocketAddress(ss.getLocalSocketAddress());
|
||||
}
|
||||
startListenerThread();
|
||||
}
|
||||
|
||||
private void startListenerThread() {
|
||||
new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
listen();
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
|
||||
private void listen() {
|
||||
// Accept connections until the socket is closed
|
||||
while(true) {
|
||||
ServerSocket ss;
|
||||
Socket s;
|
||||
synchronized(this) {
|
||||
if(!started) return;
|
||||
ss = socket;
|
||||
}
|
||||
try {
|
||||
s = ss.accept();
|
||||
|
||||
Reference in New Issue
Block a user