Moved callback initialisation from start() to constructor so it can be

accessed outside the lock.
This commit is contained in:
akwizgran
2011-10-08 12:46:35 +01:00
parent 7eaefd97cb
commit 5f40015ec2
13 changed files with 88 additions and 101 deletions

View File

@@ -26,9 +26,9 @@ public abstract class AbstractPlugin implements TransportPlugin {
this.executor = executor;
}
protected synchronized void start(Map<String, String> localProperties,
public synchronized void start(Map<String, String> localProperties,
Map<ContactId, Map<String, String>> remoteProperties,
Map<String, String> config) {
Map<String, String> config) throws IOException {
if(started) throw new IllegalStateException();
started = true;
this.localProperties = Collections.unmodifiableMap(localProperties);

View File

@@ -36,14 +36,16 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
private static final Logger LOG =
Logger.getLogger(BluetoothPlugin.class.getName());
private final StreamTransportCallback callback;
private final long pollingInterval;
private StreamTransportCallback callback = null;
private LocalDevice localDevice = null;
private StreamConnectionNotifier streamConnectionNotifier = null;
BluetoothPlugin(Executor executor, long pollingInterval) {
BluetoothPlugin(Executor executor, StreamTransportCallback callback,
long pollingInterval) {
super(executor);
this.callback = callback;
this.pollingInterval = pollingInterval;
}
@@ -51,12 +53,11 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
return id;
}
@Override
public synchronized void start(Map<String, String> localProperties,
Map<ContactId, Map<String, String>> remoteProperties,
Map<String, String> config, StreamTransportCallback callback)
throws IOException {
Map<String, String> config) throws IOException {
super.start(localProperties, remoteProperties, config);
this.callback = callback;
// Initialise the Bluetooth stack
try {
localDevice = LocalDevice.getLocalDevice();
@@ -69,6 +70,7 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
executor.execute(createBinder());
}
@Override
public synchronized void stop() throws IOException {
super.stop();
if(streamConnectionNotifier != null) {

View File

@@ -5,7 +5,6 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -26,19 +25,14 @@ implements BatchTransportPlugin {
private static final Logger LOG =
Logger.getLogger(FilePlugin.class.getName());
protected final BatchTransportCallback callback;
protected abstract File chooseOutputDirectory();
protected abstract void writerFinished(File f);
protected abstract void readerFinished(File f);
protected FilePlugin(Executor executor) {
protected FilePlugin(Executor executor, BatchTransportCallback callback) {
super(executor);
}
public synchronized void start(Map<String, String> localProperties,
Map<ContactId, Map<String, String>> remoteProperties,
Map<String, String> config, BatchTransportCallback callback)
throws IOException {
super.start(localProperties, remoteProperties, config);
this.callback = callback;
}

View File

@@ -24,9 +24,9 @@ implements RemovableDriveMonitor.Callback {
private final RemovableDriveFinder finder;
private final RemovableDriveMonitor monitor;
RemovableDrivePlugin(Executor executor, RemovableDriveFinder finder,
RemovableDriveMonitor monitor) {
super(executor);
RemovableDrivePlugin(Executor executor, BatchTransportCallback callback,
RemovableDriveFinder finder, RemovableDriveMonitor monitor) {
super(executor, callback);
this.finder = finder;
this.monitor = monitor;
}
@@ -38,9 +38,8 @@ implements RemovableDriveMonitor.Callback {
@Override
public void start(Map<String, String> localProperties,
Map<ContactId, Map<String, String>> remoteProperties,
Map<String, String> config, BatchTransportCallback callback)
throws IOException {
super.start(localProperties, remoteProperties, config, callback);
Map<String, String> config) throws IOException {
super.start(localProperties, remoteProperties, config);
monitor.start(this);
}

View File

@@ -11,6 +11,7 @@ import java.util.concurrent.Executor;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportId;
import net.sf.briar.api.transport.stream.StreamTransportCallback;
class SimpleSocketPlugin extends SocketPlugin {
@@ -20,8 +21,9 @@ class SimpleSocketPlugin extends SocketPlugin {
private final long pollingInterval;
SimpleSocketPlugin(Executor executor, long pollingInterval) {
super(executor);
SimpleSocketPlugin(Executor executor, StreamTransportCallback callback,
long pollingInterval) {
super(executor, callback);
this.pollingInterval = pollingInterval;
}

View File

@@ -21,8 +21,9 @@ implements StreamTransportPlugin {
private static final Logger LOG =
Logger.getLogger(SocketPlugin.class.getName());
// These fields should be accessed with this's lock held
protected StreamTransportCallback callback = null;
protected final StreamTransportCallback callback;
// This field should be accessed with this's lock held
protected ServerSocket socket = null;
// These methods should be called with this's lock held and started == true
@@ -32,15 +33,17 @@ implements StreamTransportPlugin {
protected abstract Socket createClientSocket() throws IOException;
protected abstract ServerSocket createServerSocket() throws IOException;
protected SocketPlugin(Executor executor) {
protected SocketPlugin(Executor executor,
StreamTransportCallback callback) {
super(executor);
this.callback = callback;
}
@Override
public synchronized void start(Map<String, String> localProperties,
Map<ContactId, Map<String, String>> remoteProperties,
Map<String, String> config, StreamTransportCallback callback) {
Map<String, String> config) throws IOException {
super.start(localProperties, remoteProperties, config);
this.callback = callback;
executor.execute(createBinder());
}
@@ -128,6 +131,7 @@ implements StreamTransportPlugin {
}
}
@Override
public synchronized void stop() throws IOException {
super.stop();
if(socket != null) {
@@ -136,6 +140,7 @@ implements StreamTransportPlugin {
}
}
@Override
public synchronized void setLocalProperties(
Map<String, String> properties) {
super.setLocalProperties(properties);