diff --git a/components/net/sf/briar/plugins/AbstractPlugin.java b/components/net/sf/briar/plugins/AbstractPlugin.java index babc90c0f6ecbacb18e922d6ae26f8e937ae7b7d..c0259d8b0ea8f7bf3f3ed80102648eca940403c8 100644 --- a/components/net/sf/briar/plugins/AbstractPlugin.java +++ b/components/net/sf/briar/plugins/AbstractPlugin.java @@ -17,6 +17,7 @@ public abstract class AbstractPlugin implements TransportPlugin { protected final Executor executor; + // These fields should be accessed with this's lock held protected Map<String, String> localProperties = null; protected Map<ContactId, Map<String, String>> remoteProperties = null; protected Map<String, String> config = null; @@ -27,7 +28,7 @@ public abstract class AbstractPlugin implements TransportPlugin { this.executor = executor; } - protected void start(Map<String, String> localProperties, + protected synchronized void start(Map<String, String> localProperties, Map<ContactId, Map<String, String>> remoteProperties, Map<String, String> config) throws InvalidPropertiesException, InvalidConfigException { diff --git a/components/net/sf/briar/plugins/socket/SimpleSocketPlugin.java b/components/net/sf/briar/plugins/socket/SimpleSocketPlugin.java index 9233c006b9de78383ddbd09f68374d9b92debad6..be8d73dfe5599bbd8d4dc1115800f3f97915c694 100644 --- a/components/net/sf/briar/plugins/socket/SimpleSocketPlugin.java +++ b/components/net/sf/briar/plugins/socket/SimpleSocketPlugin.java @@ -24,7 +24,7 @@ public class SimpleSocketPlugin extends SocketPlugin { super(executor); this.pollingInterval = pollingInterval; } - + public TransportId getId() { return id; } @@ -39,33 +39,18 @@ public class SimpleSocketPlugin extends SocketPlugin { @Override protected SocketAddress getLocalSocketAddress() { - assert localProperties != null; + assert started && localProperties != null; return createSocketAddress(localProperties); } @Override protected SocketAddress getSocketAddress(ContactId c) { - assert remoteProperties != null; + assert started && remoteProperties != null; Map<String, String> properties = remoteProperties.get(c); if(properties == null) return null; return createSocketAddress(properties); } - @Override - protected void setLocalSocketAddress(SocketAddress s) { - assert localProperties != null; - 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? - Map<String, String> m = new TreeMap<String, String>(localProperties); - m.put("host", host); - m.put("port", port); - callback.setLocalProperties(m); - } - private SocketAddress createSocketAddress(Map<String, String> properties) { assert properties != null; String host = properties.get("host"); @@ -80,13 +65,30 @@ public class SimpleSocketPlugin extends SocketPlugin { return new InetSocketAddress(host, port); } + @Override + protected void setLocalSocketAddress(SocketAddress s) { + assert started && localProperties != null; + 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? + Map<String, String> m = new TreeMap<String, String>(localProperties); + m.put("host", host); + m.put("port", port); + callback.setLocalProperties(m); + } + @Override protected Socket createClientSocket() throws IOException { + assert started; return new Socket(); } @Override protected ServerSocket createServerSocket() throws IOException { + assert started; return new ServerSocket(); } } diff --git a/components/net/sf/briar/plugins/socket/SocketPlugin.java b/components/net/sf/briar/plugins/socket/SocketPlugin.java index ad6560069a28191c9df1376aab1bbfea6e814c15..ef693baa726bf4aa02293a3e7ade3668c7e8ce68 100644 --- a/components/net/sf/briar/plugins/socket/SocketPlugin.java +++ b/components/net/sf/briar/plugins/socket/SocketPlugin.java @@ -18,6 +18,7 @@ import net.sf.briar.plugins.AbstractPlugin; abstract class SocketPlugin extends AbstractPlugin implements StreamTransportPlugin { + // These fields should be accessed with this's lock held protected StreamTransportCallback callback = null; protected ServerSocket socket = null; @@ -135,7 +136,7 @@ implements StreamTransportPlugin { public synchronized void poll() { if(!shouldPoll()) throw new UnsupportedOperationException(); - if(!started) throw new IllegalStateException(); + if(!started) return; for(ContactId c : remoteProperties.keySet()) { executor.execute(createConnector(c)); } @@ -177,6 +178,9 @@ implements StreamTransportPlugin { } public StreamTransportConnection createConnection(ContactId c) { - return started ? createAndConnectSocket(c) : null; + synchronized(this) { + if(!started) return null; + } + return createAndConnectSocket(c); } }