From c5d9d9fa64a566132708c33fe29584ac59c38fa0 Mon Sep 17 00:00:00 2001
From: akwizgran <akwizgran@users.sourceforge.net>
Date: Wed, 12 Oct 2011 17:11:31 +0100
Subject: [PATCH] Minor concurrency cleanups.

---
 .../plugins/bluetooth/BluetoothPlugin.java    | 34 +++++++++----------
 .../sf/briar/plugins/socket/SocketPlugin.java |  9 ++---
 2 files changed, 20 insertions(+), 23 deletions(-)

diff --git a/components/net/sf/briar/plugins/bluetooth/BluetoothPlugin.java b/components/net/sf/briar/plugins/bluetooth/BluetoothPlugin.java
index f493728227..6390b353fe 100644
--- a/components/net/sf/briar/plugins/bluetooth/BluetoothPlugin.java
+++ b/components/net/sf/briar/plugins/bluetooth/BluetoothPlugin.java
@@ -89,17 +89,13 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
 
 	private void bind() {
 		String uuid;
-		LocalDevice ld;
 		synchronized(this) {
 			if(!started) return;
-			TransportConfig c = callback.getConfig();
-			uuid = c.get("uuid");
-			if(uuid == null) uuid = createAndSetUuid(c);
-			ld = localDevice;
+			uuid = getUuid();
 		}
 		// Try to make the device discoverable (requires root on Linux)
 		try {
-			ld.setDiscoverable(DiscoveryAgent.GIAC);
+			localDevice.setDiscoverable(DiscoveryAgent.GIAC);
 		} catch(BluetoothStateException e) {
 			if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
 		}
@@ -123,22 +119,27 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
 				return;
 			}
 			streamConnectionNotifier = scn;
-			startListener();
+			setLocalBluetoothAddress(localDevice.getBluetoothAddress());
 		}
-		setLocalBluetoothAddress(ld.getBluetoothAddress());
+		startListener();
 	}
 
-	private synchronized String createAndSetUuid(TransportConfig c) {
-		byte[] b = new byte[16];
-		new Random().nextBytes(b); // FIXME: Use a SecureRandom?
-		String uuid = StringUtils.toHexString(b);
-		c.put("uuid", uuid);
-		callback.setConfig(c);
+	private synchronized String getUuid() {
+		assert started;
+		TransportConfig c = callback.getConfig();
+		String uuid = c.get("uuid");
+		if(uuid == null) {
+			// Generate a (weakly) random UUID and store it
+			byte[] b = new byte[16];
+			new Random().nextBytes(b);
+			uuid = StringUtils.toHexString(b);
+			c.put("uuid", uuid);
+			callback.setConfig(c);
+		}
 		return uuid;
 	}
 
 	private void startListener() {
-		assert started;
 		new Thread() {
 			@Override
 			public void run() {
@@ -168,7 +169,7 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
 	}
 
 	private synchronized void setLocalBluetoothAddress(String address) {
-		if(!started) return;
+		assert started;
 		TransportProperties p = callback.getLocalProperties();
 		p.put("address", address);
 		callback.setLocalProperties(p);
@@ -211,7 +212,6 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
 		Map<ContactId, String> uuids;
 		synchronized(this) {
 			if(!started) return Collections.emptyMap();
-			if(localDevice == null) return Collections.emptyMap();
 			discoveryAgent = localDevice.getDiscoveryAgent();
 			addresses = new HashMap<String, ContactId>();
 			uuids = new HashMap<ContactId, String>();
diff --git a/components/net/sf/briar/plugins/socket/SocketPlugin.java b/components/net/sf/briar/plugins/socket/SocketPlugin.java
index a9debf74e5..61eab794c6 100644
--- a/components/net/sf/briar/plugins/socket/SocketPlugin.java
+++ b/components/net/sf/briar/plugins/socket/SocketPlugin.java
@@ -85,8 +85,8 @@ implements StreamTransportPlugin {
 			}
 			socket = ss;
 			setLocalSocketAddress(ss.getLocalSocketAddress());
-			startListener();
 		}
+		startListener();
 	}
 
 	private void startListener() {
@@ -103,7 +103,7 @@ implements StreamTransportPlugin {
 			ServerSocket ss;
 			Socket s;
 			synchronized(this) {
-				if(!started || socket == null) return;
+				if(!started) return;
 				ss = socket;
 			}
 			try {
@@ -120,10 +120,7 @@ implements StreamTransportPlugin {
 	@Override
 	public synchronized void stop() throws IOException {
 		super.stop();
-		if(socket != null) {
-			socket.close();
-			socket = null;
-		}
+		if(socket != null) socket.close();
 	}
 
 	public synchronized void poll() {
-- 
GitLab