Minor concurrency cleanups.

This commit is contained in:
akwizgran
2011-10-12 17:11:31 +01:00
parent 3a07d1b882
commit c5d9d9fa64
2 changed files with 20 additions and 23 deletions

View File

@@ -89,17 +89,13 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
private void bind() { private void bind() {
String uuid; String uuid;
LocalDevice ld;
synchronized(this) { synchronized(this) {
if(!started) return; if(!started) return;
TransportConfig c = callback.getConfig(); uuid = getUuid();
uuid = c.get("uuid");
if(uuid == null) uuid = createAndSetUuid(c);
ld = localDevice;
} }
// Try to make the device discoverable (requires root on Linux) // Try to make the device discoverable (requires root on Linux)
try { try {
ld.setDiscoverable(DiscoveryAgent.GIAC); localDevice.setDiscoverable(DiscoveryAgent.GIAC);
} catch(BluetoothStateException e) { } catch(BluetoothStateException e) {
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage()); if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
} }
@@ -123,22 +119,27 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
return; return;
} }
streamConnectionNotifier = scn; streamConnectionNotifier = scn;
startListener(); setLocalBluetoothAddress(localDevice.getBluetoothAddress());
} }
setLocalBluetoothAddress(ld.getBluetoothAddress()); startListener();
} }
private synchronized String createAndSetUuid(TransportConfig c) { private synchronized String getUuid() {
byte[] b = new byte[16]; assert started;
new Random().nextBytes(b); // FIXME: Use a SecureRandom? TransportConfig c = callback.getConfig();
String uuid = StringUtils.toHexString(b); String uuid = c.get("uuid");
c.put("uuid", uuid); if(uuid == null) {
callback.setConfig(c); // 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; return uuid;
} }
private void startListener() { private void startListener() {
assert started;
new Thread() { new Thread() {
@Override @Override
public void run() { public void run() {
@@ -168,7 +169,7 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
} }
private synchronized void setLocalBluetoothAddress(String address) { private synchronized void setLocalBluetoothAddress(String address) {
if(!started) return; assert started;
TransportProperties p = callback.getLocalProperties(); TransportProperties p = callback.getLocalProperties();
p.put("address", address); p.put("address", address);
callback.setLocalProperties(p); callback.setLocalProperties(p);
@@ -211,7 +212,6 @@ class BluetoothPlugin extends AbstractPlugin implements StreamTransportPlugin {
Map<ContactId, String> uuids; Map<ContactId, String> uuids;
synchronized(this) { synchronized(this) {
if(!started) return Collections.emptyMap(); if(!started) return Collections.emptyMap();
if(localDevice == null) return Collections.emptyMap();
discoveryAgent = localDevice.getDiscoveryAgent(); discoveryAgent = localDevice.getDiscoveryAgent();
addresses = new HashMap<String, ContactId>(); addresses = new HashMap<String, ContactId>();
uuids = new HashMap<ContactId, String>(); uuids = new HashMap<ContactId, String>();

View File

@@ -85,8 +85,8 @@ implements StreamTransportPlugin {
} }
socket = ss; socket = ss;
setLocalSocketAddress(ss.getLocalSocketAddress()); setLocalSocketAddress(ss.getLocalSocketAddress());
startListener();
} }
startListener();
} }
private void startListener() { private void startListener() {
@@ -103,7 +103,7 @@ implements StreamTransportPlugin {
ServerSocket ss; ServerSocket ss;
Socket s; Socket s;
synchronized(this) { synchronized(this) {
if(!started || socket == null) return; if(!started) return;
ss = socket; ss = socket;
} }
try { try {
@@ -120,10 +120,7 @@ implements StreamTransportPlugin {
@Override @Override
public synchronized void stop() throws IOException { public synchronized void stop() throws IOException {
super.stop(); super.stop();
if(socket != null) { if(socket != null) socket.close();
socket.close();
socket = null;
}
} }
public synchronized void poll() { public synchronized void poll() {