Moved locking to the superclass to simplify subclasses.

This commit is contained in:
akwizgran
2011-10-06 09:34:15 +01:00
parent ffe10e1502
commit 11b571fd8f
2 changed files with 25 additions and 23 deletions

View File

@@ -36,20 +36,12 @@ public class SimpleSocketPlugin extends SocketPlugin {
@Override @Override
protected SocketAddress getLocalSocketAddress() { protected SocketAddress getLocalSocketAddress() {
Map<String, String> properties; return createSocketAddress(localProperties);
synchronized(this) {
properties = localProperties;
}
if(properties == null) return null;
return createSocketAddress(properties);
} }
@Override @Override
protected SocketAddress getSocketAddress(ContactId c) { protected SocketAddress getSocketAddress(ContactId c) {
Map<String, String> properties; Map<String, String> properties = remoteProperties.get(c);
synchronized(this) {
properties = remoteProperties.get(c);
}
if(properties == null) return null; if(properties == null) return null;
return createSocketAddress(properties); return createSocketAddress(properties);
} }

View File

@@ -24,6 +24,7 @@ abstract class SocketPlugin implements StreamTransportPlugin {
private volatile boolean started = false; private volatile boolean started = false;
// These methods should be called with this's lock held and started == true
protected abstract SocketAddress getLocalSocketAddress(); protected abstract SocketAddress getLocalSocketAddress();
protected abstract SocketAddress getSocketAddress(ContactId c); protected abstract SocketAddress getSocketAddress(ContactId c);
protected abstract Socket createClientSocket(); protected abstract Socket createClientSocket();
@@ -49,9 +50,14 @@ abstract class SocketPlugin implements StreamTransportPlugin {
protected Runnable createBinder() { protected Runnable createBinder() {
return new Runnable() { return new Runnable() {
public void run() { public void run() {
SocketAddress addr = getLocalSocketAddress(); SocketAddress addr;
if(addr == null) return; Socket s;
Socket s = createServerSocket(); synchronized(SocketPlugin.this) {
if(!started) return;
addr = getLocalSocketAddress();
s = createServerSocket();
}
if(addr == null || s == null) return;
try { try {
s.bind(addr); s.bind(addr);
} catch(IOException e) { } catch(IOException e) {
@@ -102,16 +108,15 @@ abstract class SocketPlugin implements StreamTransportPlugin {
}; };
} }
public StreamTransportConnection createConnection(ContactId c) { protected StreamTransportConnection createAndConnectSocket(ContactId c) {
if(!started) throw new IllegalStateException(); SocketAddress addr;
return createAndConnectSocket(c); Socket s;
} synchronized(this) {
if(!started) return null;
private StreamTransportConnection createAndConnectSocket(ContactId c) { addr = getSocketAddress(c);
if(!started) return null; s = createClientSocket();
SocketAddress addr = getSocketAddress(c); }
if(addr == null) return null; if(addr == null || s == null) return null;
Socket s = createClientSocket();
try { try {
s.connect(addr); s.connect(addr);
} catch(IOException e) { } catch(IOException e) {
@@ -119,4 +124,9 @@ abstract class SocketPlugin implements StreamTransportPlugin {
} }
return new SocketTransportConnection(s); return new SocketTransportConnection(s);
} }
public StreamTransportConnection createConnection(ContactId c) {
if(!started) throw new IllegalStateException();
return createAndConnectSocket(c);
}
} }