From 11b571fd8f7aa6cbbe7114edfe3207a7ca8d705a Mon Sep 17 00:00:00 2001
From: akwizgran <akwizgran@users.sourceforge.net>
Date: Thu, 6 Oct 2011 09:34:15 +0100
Subject: [PATCH] Moved locking to the superclass to simplify subclasses.

---
 .../plugins/socket/SimpleSocketPlugin.java    | 12 ++-----
 .../sf/briar/plugins/socket/SocketPlugin.java | 36 ++++++++++++-------
 2 files changed, 25 insertions(+), 23 deletions(-)

diff --git a/components/net/sf/briar/plugins/socket/SimpleSocketPlugin.java b/components/net/sf/briar/plugins/socket/SimpleSocketPlugin.java
index 2734a484a3..e8eef01c11 100644
--- a/components/net/sf/briar/plugins/socket/SimpleSocketPlugin.java
+++ b/components/net/sf/briar/plugins/socket/SimpleSocketPlugin.java
@@ -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);
 	}
diff --git a/components/net/sf/briar/plugins/socket/SocketPlugin.java b/components/net/sf/briar/plugins/socket/SocketPlugin.java
index b857fa866f..a7a6c2ae04 100644
--- a/components/net/sf/briar/plugins/socket/SocketPlugin.java
+++ b/components/net/sf/briar/plugins/socket/SocketPlugin.java
@@ -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);
+	}
 }
-- 
GitLab