mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 04:39:54 +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
|
@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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user