Merged shared code in listener classes and made casts safe.

This commit is contained in:
akwizgran
2011-10-28 15:48:30 +01:00
parent d2e1500ac1
commit 559cdfaeba
3 changed files with 81 additions and 73 deletions

View File

@@ -0,0 +1,49 @@
package net.sf.briar.plugins.bluetooth;
import java.util.concurrent.atomic.AtomicInteger;
import javax.bluetooth.DataElement;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
abstract class AbstractListener implements DiscoveryListener {
protected final DiscoveryAgent discoveryAgent;
protected final AtomicInteger searches = new AtomicInteger(1);
protected boolean finished = false; // Locking: this
protected AbstractListener(DiscoveryAgent discoveryAgent) {
this.discoveryAgent = discoveryAgent;
}
public void inquiryCompleted(int discoveryType) {
if(searches.decrementAndGet() == 0) {
synchronized(this) {
finished = true;
notifyAll();
}
}
}
public void serviceSearchCompleted(int transaction, int response) {
if(searches.decrementAndGet() == 0) {
synchronized(this) {
finished = true;
notifyAll();
}
}
}
protected Object getDataElementValue(Object o) {
if(o instanceof DataElement) {
// Bluecove throws an exception if the type is unknown
try {
return ((DataElement) o).getValue();
} catch(ClassCastException e) {
return null;
}
}
return null;
}
}

View File

@@ -4,7 +4,6 @@ import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -12,29 +11,24 @@ import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DataElement;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import net.sf.briar.api.ContactId;
class ContactListener implements DiscoveryListener {
class ContactListener extends AbstractListener {
private static final Logger LOG =
Logger.getLogger(ContactListener.class.getName());
private final AtomicInteger searches = new AtomicInteger(1);
private final DiscoveryAgent discoveryAgent;
private final Map<String, ContactId> addresses;
private final Map<ContactId, String> uuids;
private final Map<ContactId, String> urls;
private boolean finished = false; // Locking: this
ContactListener(DiscoveryAgent discoveryAgent,
Map<String, ContactId> addresses, Map<ContactId, String> uuids) {
this.discoveryAgent = discoveryAgent;
super(discoveryAgent);
this.addresses = addresses;
this.uuids = uuids;
urls = Collections.synchronizedMap(new HashMap<ContactId, String>());
@@ -68,15 +62,6 @@ class ContactListener implements DiscoveryListener {
searches.incrementAndGet();
}
public void inquiryCompleted(int discoveryType) {
if(searches.decrementAndGet() == 0) {
synchronized(this) {
finished = true;
notifyAll();
}
}
}
public void servicesDiscovered(int transaction, ServiceRecord[] services) {
for(ServiceRecord record : services) {
// Do we recognise the address?
@@ -92,26 +77,20 @@ class ContactListener implements DiscoveryListener {
if(serviceUrl == null) continue;
// Does this service have the UUID we're looking for?
DataElement classIds = record.getAttributeValue(0x1);
if(classIds == null) continue;
@SuppressWarnings("unchecked")
Enumeration<DataElement> e =
(Enumeration<DataElement>) classIds.getValue();
for(DataElement classId : Collections.list(e)) {
UUID serviceUuid = (UUID) classId.getValue();
if(uuid.equalsIgnoreCase(serviceUuid.toString())) {
// The UUID matches - store the URL
urls.put(c, serviceUrl);
Object o = getDataElementValue(classIds);
if(o instanceof Enumeration) {
Enumeration<?> e = (Enumeration<?>) o;
for(Object o1 : Collections.list(e)) {
Object o2 = getDataElementValue(o1);
if(o2 instanceof UUID) {
UUID serviceUuid = (UUID) o2;
if(uuid.equalsIgnoreCase(serviceUuid.toString())) {
// The UUID matches - store the URL
urls.put(c, serviceUrl);
}
}
}
}
}
}
public void serviceSearchCompleted(int transaction, int response) {
if(searches.decrementAndGet() == 0) {
synchronized(this) {
finished = true;
notifyAll();
}
}
}
}

View File

@@ -2,7 +2,6 @@ package net.sf.briar.plugins.bluetooth;
import java.util.Collections;
import java.util.Enumeration;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -10,25 +9,21 @@ import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DataElement;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
class InvitationListener implements DiscoveryListener {
class InvitationListener extends AbstractListener {
private static final Logger LOG =
Logger.getLogger(InvitationListener.class.getName());
private final AtomicInteger searches = new AtomicInteger(1);
private final DiscoveryAgent discoveryAgent;
private final String uuid;
private String url = null; // Locking: this
private boolean finished = false; // Locking: this
InvitationListener(DiscoveryAgent discoveryAgent, String uuid) {
this.discoveryAgent = discoveryAgent;
super(discoveryAgent);
this.uuid = uuid;
}
@@ -54,15 +49,6 @@ class InvitationListener implements DiscoveryListener {
}
}
public void inquiryCompleted(int discoveryType) {
if(searches.decrementAndGet() == 0) {
synchronized(this) {
finished = true;
notifyAll();
}
}
}
public void servicesDiscovered(int transaction, ServiceRecord[] services) {
for(ServiceRecord record : services) {
// Does this service have a URL?
@@ -71,31 +57,25 @@ class InvitationListener implements DiscoveryListener {
if(serviceUrl == null) continue;
// Does this service have the UUID we're looking for?
DataElement classIds = record.getAttributeValue(0x1);
if(classIds == null) continue;
@SuppressWarnings("unchecked")
Enumeration<DataElement> e =
(Enumeration<DataElement>) classIds.getValue();
for(DataElement classId : Collections.list(e)) {
UUID serviceUuid = (UUID) classId.getValue();
if(uuid.equalsIgnoreCase(serviceUuid.toString())) {
// The UUID matches - store the URL
synchronized(this) {
url = serviceUrl;
finished = true;
notifyAll();
Object o = getDataElementValue(classIds);
if(o instanceof Enumeration) {
Enumeration<?> e = (Enumeration<?>) o;
for(Object o1 : Collections.list(e)) {
Object o2 = getDataElementValue(o1);
if(o2 instanceof UUID) {
UUID serviceUuid = (UUID) o2;
if(uuid.equalsIgnoreCase(serviceUuid.toString())) {
// The UUID matches - store the URL
synchronized(this) {
url = serviceUrl;
finished = true;
notifyAll();
return;
}
}
}
return;
}
}
}
}
public void serviceSearchCompleted(int transaction, int response) {
if(searches.decrementAndGet() == 0) {
synchronized(this) {
finished = true;
notifyAll();
}
}
}
}