Plugins don't need their own copies of configs and properties.

This commit is contained in:
akwizgran
2011-10-12 16:52:39 +01:00
parent 631f4e74b5
commit 3a07d1b882
14 changed files with 226 additions and 311 deletions

View File

@@ -1,56 +1,28 @@
package net.sf.briar.plugins;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.Executor;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.plugins.TransportPlugin;
public abstract class AbstractPlugin implements TransportPlugin {
protected final Executor executor;
// These fields should be accessed with this's lock held
protected TransportProperties localProperties = null;
protected Map<ContactId, TransportProperties> remoteProperties = null;
protected TransportConfig config = null;
// This field must only be accessed with this's lock held
protected boolean started = false;
protected AbstractPlugin(Executor executor) {
this.executor = executor;
}
public synchronized void start(TransportProperties localProperties,
Map<ContactId, TransportProperties> remoteProperties,
TransportConfig config) throws IOException {
public synchronized void start() throws IOException {
if(started) throw new IllegalStateException();
started = true;
this.localProperties = localProperties;
this.remoteProperties = remoteProperties;
this.config = config;
}
public synchronized void stop() throws IOException {
if(!started) throw new IllegalStateException();
started = false;
}
public synchronized void setLocalProperties(TransportProperties p) {
if(!started) throw new IllegalStateException();
localProperties = p;
}
public synchronized void setRemoteProperties(ContactId c,
TransportProperties p) {
if(!started) throw new IllegalStateException();
remoteProperties.put(c, p);
}
public synchronized void setConfig(TransportConfig c) {
if(!started) throw new IllegalStateException();
this.config = c;
}
}

View File

@@ -1,7 +1,6 @@
package net.sf.briar.plugins.bluetooth;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -55,13 +54,11 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
}
@Override
public void start(TransportProperties localProperties,
Map<ContactId, TransportProperties> remoteProperties,
TransportConfig config) throws IOException {
public void start() throws IOException {
// Initialise the Bluetooth stack
try {
synchronized(this) {
super.start(localProperties, remoteProperties, config);
super.start();
localDevice = LocalDevice.getLocalDevice();
}
} catch(UnsatisfiedLinkError e) {
@@ -95,10 +92,11 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
LocalDevice ld;
synchronized(this) {
if(!started) return;
uuid = config.get("uuid");
TransportConfig c = callback.getConfig();
uuid = c.get("uuid");
if(uuid == null) uuid = createAndSetUuid(c);
ld = localDevice;
}
if(uuid == null) uuid = createAndSetUuid();
// Try to make the device discoverable (requires root on Linux)
try {
ld.setDiscoverable(DiscoveryAgent.GIAC);
@@ -130,15 +128,10 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
setLocalBluetoothAddress(ld.getBluetoothAddress());
}
private String createAndSetUuid() {
private synchronized String createAndSetUuid(TransportConfig c) {
byte[] b = new byte[16];
new Random().nextBytes(b); // FIXME: Use a SecureRandom?
String uuid = StringUtils.toHexString(b);
TransportConfig c;
synchronized(this) {
if(!started) return uuid;
c = new TransportConfig(config);
}
c.put("uuid", uuid);
callback.setConfig(c);
return uuid;
@@ -174,12 +167,9 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
}
}
private void setLocalBluetoothAddress(String address) {
TransportProperties p;
synchronized(this) {
if(!started) return;
p = new TransportProperties(localProperties);
}
private synchronized void setLocalBluetoothAddress(String address) {
if(!started) return;
TransportProperties p = callback.getLocalProperties();
p.put("address", address);
callback.setLocalProperties(p);
}
@@ -194,28 +184,28 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
public synchronized void poll() {
if(!started) return;
executor.execute(createConnectors(remoteProperties.keySet()));
executor.execute(createConnectors());
}
private Runnable createConnectors(final Collection<ContactId> contacts) {
private Runnable createConnectors() {
return new Runnable() {
public void run() {
connectAndCallBack(contacts);
connectAndCallBack();
}
};
}
private void connectAndCallBack(Collection<ContactId> contacts) {
Map<ContactId, String> discovered = discover(contacts);
private void connectAndCallBack() {
Map<ContactId, String> discovered = discover();
for(Entry<ContactId, String> e : discovered.entrySet()) {
ContactId c = e.getKey();
String url = e.getValue();
StreamTransportConnection conn = createConnection(c, url);
StreamTransportConnection conn = connect(c, url);
if(conn != null) callback.outgoingConnectionCreated(c, conn);
}
}
private Map<ContactId, String> discover(Collection<ContactId> contacts) {
private Map<ContactId, String> discover() {
DiscoveryAgent discoveryAgent;
Map<String, ContactId> addresses;
Map<ContactId, String> uuids;
@@ -225,12 +215,13 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
discoveryAgent = localDevice.getDiscoveryAgent();
addresses = new HashMap<String, ContactId>();
uuids = new HashMap<ContactId, String>();
for(Entry<ContactId, TransportProperties> e
: remoteProperties.entrySet()) {
Map<ContactId, TransportProperties> remote =
callback.getRemoteProperties();
for(Entry<ContactId, TransportProperties> e : remote.entrySet()) {
ContactId c = e.getKey();
TransportProperties properties = e.getValue();
String address = properties.get("address");
String uuid = properties.get("uuid");
TransportProperties p = e.getValue();
String address = p.get("address");
String uuid = p.get("uuid");
if(address != null && uuid != null) {
addresses.put(address, c);
uuids.put(c, uuid);
@@ -250,12 +241,13 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
return listener.getUrls();
}
private StreamTransportConnection createConnection(ContactId c,
String url) {
private StreamTransportConnection connect(ContactId c, String url) {
try {
synchronized(this) {
if(!started) return null;
if(!remoteProperties.containsKey(c)) return null;
Map<ContactId, TransportProperties> remote =
callback.getRemoteProperties();
if(!remote.containsKey(c)) return null;
}
StreamConnection s = (StreamConnection) Connector.open(url);
return new BluetoothTransportConnection(s);
@@ -266,8 +258,8 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
}
public StreamTransportConnection createConnection(ContactId c) {
Map<ContactId, String> discovered = discover(Collections.singleton(c));
Map<ContactId, String> discovered = discover();
String url = discovered.get(c);
return url == null ? null : createConnection(c, url);
return url == null ? null : connect(c, url);
}
}

View File

@@ -3,15 +3,11 @@ package net.sf.briar.plugins.file;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportId;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.plugins.BatchTransportCallback;
class RemovableDrivePlugin extends FilePlugin
@@ -38,15 +34,13 @@ implements RemovableDriveMonitor.Callback {
}
@Override
public void start(TransportProperties localProperties,
Map<ContactId, TransportProperties> remoteProperties,
TransportConfig config) throws IOException {
super.start(localProperties, remoteProperties, config);
public synchronized void start() throws IOException {
super.start();
monitor.start(this);
}
@Override
public void stop() throws IOException {
public synchronized void stop() throws IOException {
super.stop();
monitor.stop();
}

View File

@@ -39,23 +39,36 @@ class SimpleSocketPlugin extends SocketPlugin {
}
@Override
protected SocketAddress getLocalSocketAddress() {
protected Socket createClientSocket() throws IOException {
assert started;
return createSocketAddress(localProperties);
return new Socket();
}
@Override
protected SocketAddress getSocketAddress(ContactId c) {
protected ServerSocket createServerSocket() throws IOException {
assert started;
TransportProperties properties = remoteProperties.get(c);
if(properties == null) return null;
return createSocketAddress(properties);
return new ServerSocket();
}
private SocketAddress createSocketAddress(TransportProperties properties) {
assert properties != null;
String host = properties.get("host");
String portString = properties.get("port");
@Override
protected synchronized SocketAddress getLocalSocketAddress() {
assert started;
return createSocketAddress(callback.getLocalProperties());
}
@Override
protected synchronized SocketAddress getRemoteSocketAddress(ContactId c) {
assert started;
TransportProperties p = callback.getRemoteProperties().get(c);
return p == null ? null : createSocketAddress(p);
}
private synchronized SocketAddress createSocketAddress(
TransportProperties p) {
assert started;
assert p != null;
String host = p.get("host");
String portString = p.get("port");
if(host == null || portString == null) return null;
int port;
try {
@@ -67,32 +80,17 @@ class SimpleSocketPlugin extends SocketPlugin {
}
@Override
protected void setLocalSocketAddress(SocketAddress s) {
TransportProperties p;
synchronized(this) {
if(!started) return;
p = new TransportProperties(localProperties);
}
protected synchronized void setLocalSocketAddress(SocketAddress s) {
assert started;
if(!(s instanceof InetSocketAddress))
throw new IllegalArgumentException();
InetSocketAddress i = (InetSocketAddress) s;
String host = i.getAddress().getHostAddress();
String port = String.valueOf(i.getPort());
// FIXME: Special handling for private IP addresses?
TransportProperties p = callback.getLocalProperties();
p.put("host", host);
p.put("port", port);
callback.setLocalProperties(p);
}
@Override
protected Socket createClientSocket() throws IOException {
assert started;
return new Socket();
}
@Override
protected ServerSocket createServerSocket() throws IOException {
assert started;
return new ServerSocket();
}
}

View File

@@ -4,14 +4,11 @@ import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.plugins.StreamTransportCallback;
import net.sf.briar.api.plugins.StreamTransportPlugin;
import net.sf.briar.api.transport.StreamTransportConnection;
@@ -25,16 +22,17 @@ implements StreamTransportPlugin {
protected final StreamTransportCallback callback;
// This field should be accessed with this's lock held
// This field must only be accessed with this's lock held
protected ServerSocket socket = null;
protected abstract void setLocalSocketAddress(SocketAddress s);
// These methods should be called with this's lock held and started == true
protected abstract SocketAddress getLocalSocketAddress();
protected abstract SocketAddress getSocketAddress(ContactId c);
// These methods must only be called with this's lock held and
// started == true
protected abstract Socket createClientSocket() throws IOException;
protected abstract ServerSocket createServerSocket() throws IOException;
protected abstract SocketAddress getLocalSocketAddress();
protected abstract SocketAddress getRemoteSocketAddress(ContactId c);
protected SocketPlugin(Executor executor,
StreamTransportCallback callback) {
@@ -43,10 +41,8 @@ implements StreamTransportPlugin {
}
@Override
public synchronized void start(TransportProperties localProperties,
Map<ContactId, TransportProperties> remoteProperties,
TransportConfig config) throws IOException {
super.start(localProperties, remoteProperties, config);
public synchronized void start() throws IOException {
super.start();
executor.execute(createBinder());
}
@@ -130,30 +126,11 @@ implements StreamTransportPlugin {
}
}
@Override
public synchronized void setLocalProperties(TransportProperties p) {
super.setLocalProperties(p);
// Close and reopen the socket if its address has changed
if(socket != null) {
SocketAddress addr = socket.getLocalSocketAddress();
if(!getLocalSocketAddress().equals(addr)) {
try {
socket.close();
} catch(IOException e) {
if(LOG.isLoggable(Level.WARNING))
LOG.warning(e.getMessage());
}
socket = null;
executor.execute(createBinder());
}
}
}
public synchronized void poll() {
// Subclasses may not support polling
if(!shouldPoll()) throw new UnsupportedOperationException();
if(!started) return;
for(ContactId c : remoteProperties.keySet()) {
for(ContactId c : callback.getRemoteProperties().keySet()) {
executor.execute(createConnector(c));
}
}
@@ -177,7 +154,7 @@ implements StreamTransportPlugin {
try {
synchronized(this) {
if(!started) return null;
addr = getSocketAddress(c);
addr = getRemoteSocketAddress(c);
s = createClientSocket();
}
if(addr == null || s == null) return null;