Skip to content
Snippets Groups Projects
Commit c5d9d9fa authored by akwizgran's avatar akwizgran
Browse files

Minor concurrency cleanups.

parent 3a07d1b8
No related branches found
No related tags found
No related merge requests found
......@@ -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>();
......
......@@ -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() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment