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() {
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>();

View File

@@ -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() {