Synchronize access to superclass members.

This commit is contained in:
akwizgran
2011-10-06 18:16:08 +01:00
parent 42e586abc4
commit 7a994114ff
3 changed files with 28 additions and 21 deletions

View File

@@ -17,6 +17,7 @@ public abstract class AbstractPlugin implements TransportPlugin {
protected final Executor executor; protected final Executor executor;
// These fields should be accessed with this's lock held
protected Map<String, String> localProperties = null; protected Map<String, String> localProperties = null;
protected Map<ContactId, Map<String, String>> remoteProperties = null; protected Map<ContactId, Map<String, String>> remoteProperties = null;
protected Map<String, String> config = null; protected Map<String, String> config = null;
@@ -27,7 +28,7 @@ public abstract class AbstractPlugin implements TransportPlugin {
this.executor = executor; 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<ContactId, Map<String, String>> remoteProperties,
Map<String, String> config) throws InvalidPropertiesException, Map<String, String> config) throws InvalidPropertiesException,
InvalidConfigException { InvalidConfigException {

View File

@@ -39,33 +39,18 @@ public class SimpleSocketPlugin extends SocketPlugin {
@Override @Override
protected SocketAddress getLocalSocketAddress() { protected SocketAddress getLocalSocketAddress() {
assert localProperties != null; assert started && localProperties != null;
return createSocketAddress(localProperties); return createSocketAddress(localProperties);
} }
@Override @Override
protected SocketAddress getSocketAddress(ContactId c) { protected SocketAddress getSocketAddress(ContactId c) {
assert remoteProperties != null; assert started && remoteProperties != null;
Map<String, String> properties = remoteProperties.get(c); Map<String, String> properties = remoteProperties.get(c);
if(properties == null) return null; if(properties == null) return null;
return createSocketAddress(properties); 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) { private SocketAddress createSocketAddress(Map<String, String> properties) {
assert properties != null; assert properties != null;
String host = properties.get("host"); String host = properties.get("host");
@@ -80,13 +65,30 @@ public class SimpleSocketPlugin extends SocketPlugin {
return new InetSocketAddress(host, port); 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 @Override
protected Socket createClientSocket() throws IOException { protected Socket createClientSocket() throws IOException {
assert started;
return new Socket(); return new Socket();
} }
@Override @Override
protected ServerSocket createServerSocket() throws IOException { protected ServerSocket createServerSocket() throws IOException {
assert started;
return new ServerSocket(); return new ServerSocket();
} }
} }

View File

@@ -18,6 +18,7 @@ import net.sf.briar.plugins.AbstractPlugin;
abstract class SocketPlugin extends AbstractPlugin abstract class SocketPlugin extends AbstractPlugin
implements StreamTransportPlugin { implements StreamTransportPlugin {
// These fields should be accessed with this's lock held
protected StreamTransportCallback callback = null; protected StreamTransportCallback callback = null;
protected ServerSocket socket = null; protected ServerSocket socket = null;
@@ -135,7 +136,7 @@ implements StreamTransportPlugin {
public synchronized void poll() { public synchronized void poll() {
if(!shouldPoll()) throw new UnsupportedOperationException(); if(!shouldPoll()) throw new UnsupportedOperationException();
if(!started) throw new IllegalStateException(); if(!started) return;
for(ContactId c : remoteProperties.keySet()) { for(ContactId c : remoteProperties.keySet()) {
executor.execute(createConnector(c)); executor.execute(createConnector(c));
} }
@@ -177,6 +178,9 @@ implements StreamTransportPlugin {
} }
public StreamTransportConnection createConnection(ContactId c) { public StreamTransportConnection createConnection(ContactId c) {
return started ? createAndConnectSocket(c) : null; synchronized(this) {
if(!started) return null;
}
return createAndConnectSocket(c);
} }
} }