mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
Moved locking to the superclass to simplify subclasses.
This commit is contained in:
@@ -36,20 +36,12 @@ public class SimpleSocketPlugin extends SocketPlugin {
|
||||
|
||||
@Override
|
||||
protected SocketAddress getLocalSocketAddress() {
|
||||
Map<String, String> properties;
|
||||
synchronized(this) {
|
||||
properties = localProperties;
|
||||
}
|
||||
if(properties == null) return null;
|
||||
return createSocketAddress(properties);
|
||||
return createSocketAddress(localProperties);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SocketAddress getSocketAddress(ContactId c) {
|
||||
Map<String, String> properties;
|
||||
synchronized(this) {
|
||||
properties = remoteProperties.get(c);
|
||||
}
|
||||
Map<String, String> properties = remoteProperties.get(c);
|
||||
if(properties == null) return null;
|
||||
return createSocketAddress(properties);
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ abstract class SocketPlugin implements StreamTransportPlugin {
|
||||
|
||||
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 getSocketAddress(ContactId c);
|
||||
protected abstract Socket createClientSocket();
|
||||
@@ -49,9 +50,14 @@ abstract class SocketPlugin implements StreamTransportPlugin {
|
||||
protected Runnable createBinder() {
|
||||
return new Runnable() {
|
||||
public void run() {
|
||||
SocketAddress addr = getLocalSocketAddress();
|
||||
if(addr == null) return;
|
||||
Socket s = createServerSocket();
|
||||
SocketAddress addr;
|
||||
Socket s;
|
||||
synchronized(SocketPlugin.this) {
|
||||
if(!started) return;
|
||||
addr = getLocalSocketAddress();
|
||||
s = createServerSocket();
|
||||
}
|
||||
if(addr == null || s == null) return;
|
||||
try {
|
||||
s.bind(addr);
|
||||
} catch(IOException e) {
|
||||
@@ -102,16 +108,15 @@ abstract class SocketPlugin implements StreamTransportPlugin {
|
||||
};
|
||||
}
|
||||
|
||||
public StreamTransportConnection createConnection(ContactId c) {
|
||||
if(!started) throw new IllegalStateException();
|
||||
return createAndConnectSocket(c);
|
||||
}
|
||||
|
||||
private StreamTransportConnection createAndConnectSocket(ContactId c) {
|
||||
if(!started) return null;
|
||||
SocketAddress addr = getSocketAddress(c);
|
||||
if(addr == null) return null;
|
||||
Socket s = createClientSocket();
|
||||
protected StreamTransportConnection createAndConnectSocket(ContactId c) {
|
||||
SocketAddress addr;
|
||||
Socket s;
|
||||
synchronized(this) {
|
||||
if(!started) return null;
|
||||
addr = getSocketAddress(c);
|
||||
s = createClientSocket();
|
||||
}
|
||||
if(addr == null || s == null) return null;
|
||||
try {
|
||||
s.connect(addr);
|
||||
} catch(IOException e) {
|
||||
@@ -119,4 +124,9 @@ abstract class SocketPlugin implements StreamTransportPlugin {
|
||||
}
|
||||
return new SocketTransportConnection(s);
|
||||
}
|
||||
|
||||
public StreamTransportConnection createConnection(ContactId c) {
|
||||
if(!started) throw new IllegalStateException();
|
||||
return createAndConnectSocket(c);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user