This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository tutti. See http://git.codelutin.com/tutti.git commit 5977ed771a103fb846853c94fdf50095614dce5b Author: Tony CHEMIT <chemit@codelutin.com> Date: Sat Apr 4 21:36:29 2015 +0200 amélioration du code de démarrage de client ichtyomètre --- .../tutti/ichtyometer/IchtyometerClient.java | 192 ++++----------------- .../tutti/ichtyometer/RemoteDevicesFinder.java | 135 +++++++++++++++ .../tutti/ichtyometer/ServiceRecordsFinder.java | 120 +++++++++++++ .../tutti/ichtyometer/tool/FeedReaderTool.java | 2 +- .../tutti/ichtyometer/tool/SendCommandTool.java | 2 +- .../java/fr/ifremer/tutti/ichtyometer/BigFins.java | 10 +- .../tutti/ichtyometer/IchtyometerClientTest.java | 2 +- .../tutti/ichtyometer/feed/FeedReaderTest.java | 2 +- .../ichtyometer/interactive/CommandEngineTest.java | 2 +- 9 files changed, 301 insertions(+), 166 deletions(-) diff --git a/tutti-ichtyometer/src/main/java/fr/ifremer/tutti/ichtyometer/IchtyometerClient.java b/tutti-ichtyometer/src/main/java/fr/ifremer/tutti/ichtyometer/IchtyometerClient.java index cfd45da..87c96e5 100644 --- a/tutti-ichtyometer/src/main/java/fr/ifremer/tutti/ichtyometer/IchtyometerClient.java +++ b/tutti-ichtyometer/src/main/java/fr/ifremer/tutti/ichtyometer/IchtyometerClient.java @@ -25,7 +25,6 @@ package fr.ifremer.tutti.ichtyometer; */ import com.google.common.base.Preconditions; -import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import com.intel.bluetooth.BlueCoveImpl; @@ -33,14 +32,9 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import javax.bluetooth.BluetoothStateException; -import javax.bluetooth.DataElement; -import javax.bluetooth.DeviceClass; -import javax.bluetooth.DiscoveryAgent; -import javax.bluetooth.DiscoveryListener; import javax.bluetooth.LocalDevice; import javax.bluetooth.RemoteDevice; import javax.bluetooth.ServiceRecord; -import javax.bluetooth.UUID; import javax.microedition.io.Connector; import javax.microedition.io.StreamConnection; import java.io.Closeable; @@ -73,6 +67,12 @@ public class IchtyometerClient implements Closeable { */ protected static Map<String, String> REMOTE_CONNECTION_URL_CACHE; + private final int maximumNumberOfTryToConnect; + + public IchtyometerClient(int maximumNumberOfTryToConnect) { + this.maximumNumberOfTryToConnect = maximumNumberOfTryToConnect; + } + public static void clearRemoteDeviceCaches() { REMOTE_DEVICE_CACHE = null; REMOTE_CONNECTION_URL_CACHE = null; @@ -122,11 +122,8 @@ public class IchtyometerClient implements Closeable { // build map of remote devices - try { - REMOTE_DEVICE_CACHE = discoverDevices(); - } catch (Exception e) { - throw new RemoteDeviceNotFoundException("Could not detected devices", e); - } + REMOTE_DEVICE_CACHE = discoverRemoteDevices(); + } if (REMOTE_DEVICE_CACHE.isEmpty()) { @@ -154,32 +151,24 @@ public class IchtyometerClient implements Closeable { if (!REMOTE_CONNECTION_URL_CACHE.containsKey(name)) { int serviceIndex = 3; - List<ServiceRecord> serviceRecords; - try { - serviceRecords = discoverServiceUrls(new UUID(serviceIndex), remoteDevice); - } catch (Exception e) { - throw new RemoteDeviceNotFoundException("Could not read remote device services", e); - } - if (serviceRecords.isEmpty()) { - throw new RemoteDeviceServiceNotFoundException("No services detected."); - } + List<ServiceRecord> serviceRecords = discoverServiceRecords(serviceIndex); if (log.isInfoEnabled()) { log.info("Found some services for index: " + serviceIndex); } - serviceRecords.addAll(serviceRecords); - + // take first service record ServiceRecord serviceRecord = serviceRecords.get(0); // get connection url - String connectionUrl = serviceRecord.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, true); + String url = serviceRecord.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, true); if (log.isInfoEnabled()) { - log.info("Found service(" + serviceIndex + "): " + connectionUrl + ", name: " + name); + log.info("Found service(" + serviceIndex + "): " + url + ", name: " + name); } - REMOTE_CONNECTION_URL_CACHE.put(name, connectionUrl); + REMOTE_CONNECTION_URL_CACHE.put(name, url); + } // get connection url @@ -222,11 +211,6 @@ public class IchtyometerClient implements Closeable { connection.close(); } - public String getConnectionUrl() { - checkIsOpened(); - return connectionUrl; - } - public boolean isOpen() { return open; } @@ -242,151 +226,39 @@ public class IchtyometerClient implements Closeable { } } - protected Map<String, RemoteDevice> discoverDevices() throws BluetoothStateException, InterruptedException { + protected Map<String, RemoteDevice> discoverRemoteDevices() throws RemoteDeviceNotFoundException { - Map<String, RemoteDevice> devices = Maps.newTreeMap(); + RemoteDevicesFinder remoteDevicesFinder = new RemoteDevicesFinder(maximumNumberOfTryToConnect, this); - MyDiscoveryListener listener = new MyDiscoveryListener(this, devices, null); + Map<String, RemoteDevice> devices; + try { + devices = remoteDevicesFinder.findDevices(localDevice); - synchronized (this) { - boolean started = localDevice.getDiscoveryAgent().startInquiry(DiscoveryAgent.LIAC, listener); - if (started) { - if (log.isInfoEnabled()) { - log.info("wait for device inquiry to complete..."); - } - this.wait(); - if (log.isInfoEnabled()) { - log.info(devices.size() + " device(s) found"); - } - } + } catch (Exception e) { + throw new RemoteDeviceNotFoundException("Could not detected devices", e); } - return devices; - } - - protected List<ServiceRecord> discoverServiceUrls(UUID serviceUUID, RemoteDevice device) throws BluetoothStateException, InterruptedException { - - List<ServiceRecord> serviceRecords = Lists.newArrayList(); - - MyDiscoveryListener listener = new MyDiscoveryListener(this, - null, - serviceRecords); - - synchronized (this) { - - int[] attrIDs = new int[]{ - 0x0100 // Service name - }; - localDevice.getDiscoveryAgent().searchServices(attrIDs, - new UUID[]{serviceUUID}, - device, - listener); - this.wait(); - } + return devices; - return serviceRecords; } - protected static class MyDiscoveryListener implements DiscoveryListener { - - final Object lock; + protected List<ServiceRecord> discoverServiceRecords(int serviceIndex) throws RemoteDeviceNotFoundException, RemoteDeviceServiceNotFoundException { - final Map<String, RemoteDevice> devices; - - final List<ServiceRecord> serviceRecords; - - MyDiscoveryListener(Object lock, - Map<String, RemoteDevice> devices, - List<ServiceRecord> serviceRecords) { - this.lock = lock; - this.devices = devices; - this.serviceRecords = serviceRecords; - } - - - @Override - public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) { - if (log.isInfoEnabled()) { - log.info("Device " + btDevice.getBluetoothAddress() + " found"); - } - - String friendlyName = getName(btDevice); - if (friendlyName == null) { - - // retry - friendlyName = getName(btDevice); - - } - - if (friendlyName == null) { - throw new RemoteDeviceCantGetNameException(btDevice.getBluetoothAddress()); - } - - devices.put(friendlyName, btDevice); - } - - protected String getName(RemoteDevice btDevice) { - String friendlyName = null; - try { - friendlyName = btDevice.getFriendlyName(false); - if (log.isInfoEnabled()) { - log.info("Name: " + friendlyName); - } - } catch (IOException e) { - if (log.isDebugEnabled()) { - log.debug("Can't get name of remote", e); - } - - } - return friendlyName; + ServiceRecordsFinder serviceRecordsFinder = new ServiceRecordsFinder(maximumNumberOfTryToConnect, this); + List<ServiceRecord> serviceRecords; + try { + serviceRecords = serviceRecordsFinder.findServices(localDevice, remoteDevice, serviceIndex); + } catch (Exception e) { + throw new RemoteDeviceNotFoundException("Could not read remote device services", e); } - @Override - public void inquiryCompleted(int discType) { - if (log.isDebugEnabled()) { - log.debug("Device Inquiry completed!"); - } - synchronized (lock) { - lock.notifyAll(); - } + if (serviceRecords.isEmpty()) { + throw new RemoteDeviceServiceNotFoundException("No services detected."); } - @Override - public void serviceSearchCompleted(int transID, int respCode) { - if (log.isDebugEnabled()) { - log.debug("Service search completed!"); - } - synchronized (lock) { - lock.notifyAll(); - } - } + return serviceRecords; - @Override - public void servicesDiscovered(int transID, ServiceRecord[] servRecord) { - for (ServiceRecord aServRecord : servRecord) { - String url = aServRecord.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false); - if (url == null) { - continue; - } - serviceRecords.add(aServRecord); - String serviceName = getServiceName(aServRecord); - if (log.isDebugEnabled()) { - log.debug("service found " + url + (serviceName == null ? "" : ", name: " + serviceName)); - } - } - } } - public static String getServiceName(ServiceRecord serviceRecord) { - DataElement serviceName = serviceRecord.getAttributeValue(0x0100); - String result; - - if (serviceName == null) { - result = null; - } else { - result = String.valueOf(serviceName.getValue()); - } - return result; - } } diff --git a/tutti-ichtyometer/src/main/java/fr/ifremer/tutti/ichtyometer/RemoteDevicesFinder.java b/tutti-ichtyometer/src/main/java/fr/ifremer/tutti/ichtyometer/RemoteDevicesFinder.java new file mode 100644 index 0000000..e79f907 --- /dev/null +++ b/tutti-ichtyometer/src/main/java/fr/ifremer/tutti/ichtyometer/RemoteDevicesFinder.java @@ -0,0 +1,135 @@ +package fr.ifremer.tutti.ichtyometer; + +import com.google.common.collect.Maps; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import javax.bluetooth.BluetoothStateException; +import javax.bluetooth.DeviceClass; +import javax.bluetooth.DiscoveryAgent; +import javax.bluetooth.DiscoveryListener; +import javax.bluetooth.LocalDevice; +import javax.bluetooth.RemoteDevice; +import javax.bluetooth.ServiceRecord; +import java.io.IOException; +import java.util.Map; + +/** + * Created on 4/4/15. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.14.4 + */ +public class RemoteDevicesFinder { + + /** Logger. */ + private static final Log log = LogFactory.getLog(RemoteDevicesFinder.class); + + private final Map<String, RemoteDevice> devices; + + private final int maximumNumberOfTryToConnect; + + private final Object lock; + + public RemoteDevicesFinder(int maximumNumberOfTryToConnect, Object lock) { + this.maximumNumberOfTryToConnect = maximumNumberOfTryToConnect; + this.lock = lock; + this.devices = Maps.newTreeMap(); + } + + public Map<String, RemoteDevice> findDevices(LocalDevice localDevice) throws BluetoothStateException, InterruptedException { + + DevicesDiscoveryListener listener = new DevicesDiscoveryListener(); + + + synchronized (lock) { + + int tryRound = 0; + + while (tryRound < maximumNumberOfTryToConnect && devices.isEmpty()) { + + boolean started = localDevice.getDiscoveryAgent().startInquiry(DiscoveryAgent.LIAC, listener); + if (started) { + if (log.isInfoEnabled()) { + log.info("Wait for device inquiry to complete... (try " + tryRound + ")"); + } + lock.wait(); + if (log.isInfoEnabled()) { + log.info(devices.size() + " device(s) found"); + } + } + tryRound++; + } + + } + + return devices; + + } + + protected class DevicesDiscoveryListener implements DiscoveryListener { + + @Override + public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) { + if (log.isInfoEnabled()) { + log.info("Device " + btDevice.getBluetoothAddress() + " found"); + } + + String friendlyName = null; + + int tryRound = 0; + + while (tryRound < maximumNumberOfTryToConnect && friendlyName == null) { + + if (log.isInfoEnabled()) { + log.info("Try to get device friendlyName (try " + tryRound + ")"); + } + friendlyName = getName(btDevice); + + tryRound++; + } + + if (friendlyName == null) { + throw new RemoteDeviceCantGetNameException(btDevice.getBluetoothAddress()); + } + + devices.put(friendlyName, btDevice); + } + + protected String getName(RemoteDevice btDevice) { + String friendlyName = null; + try { + friendlyName = btDevice.getFriendlyName(false); + if (log.isInfoEnabled()) { + log.info("Name: " + friendlyName); + } + } catch (IOException e) { + if (log.isDebugEnabled()) { + log.debug("Can't get name of remote", e); + } + + } + return friendlyName; + } + + @Override + public void inquiryCompleted(int discType) { + if (log.isDebugEnabled()) { + log.debug("Device Inquiry completed!"); + } + synchronized (lock) { + lock.notifyAll(); + } + } + + @Override + public void serviceSearchCompleted(int transID, int respCode) { + } + + @Override + public void servicesDiscovered(int transID, ServiceRecord[] servRecord) { + } + + } + +} diff --git a/tutti-ichtyometer/src/main/java/fr/ifremer/tutti/ichtyometer/ServiceRecordsFinder.java b/tutti-ichtyometer/src/main/java/fr/ifremer/tutti/ichtyometer/ServiceRecordsFinder.java new file mode 100644 index 0000000..a4d39f0 --- /dev/null +++ b/tutti-ichtyometer/src/main/java/fr/ifremer/tutti/ichtyometer/ServiceRecordsFinder.java @@ -0,0 +1,120 @@ +package fr.ifremer.tutti.ichtyometer; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import javax.bluetooth.BluetoothStateException; +import javax.bluetooth.DataElement; +import javax.bluetooth.DeviceClass; +import javax.bluetooth.DiscoveryListener; +import javax.bluetooth.LocalDevice; +import javax.bluetooth.RemoteDevice; +import javax.bluetooth.ServiceRecord; +import javax.bluetooth.UUID; +import java.util.ArrayList; +import java.util.List; + +/** + * Created on 4/4/15. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.14.4 + */ +public class ServiceRecordsFinder { + + /** Logger. */ + private static final Log log = LogFactory.getLog(ServiceRecordsFinder.class); + + private final int maximumNumberOfTryToConnect; + + private final Object lock; + + private final List<ServiceRecord> serviceRecords; + + public ServiceRecordsFinder(int maximumNumberOfTryToConnect, Object lock) { + this.maximumNumberOfTryToConnect = maximumNumberOfTryToConnect; + this.lock = lock; + this.serviceRecords = new ArrayList<>(); + } + + public List<ServiceRecord> findServices(LocalDevice localDevice, RemoteDevice device, int serviceIndex) throws InterruptedException, BluetoothStateException { + + ServicesDiscoveryListener listener = new ServicesDiscoveryListener(); + int tryRound = 0; + + while (tryRound < maximumNumberOfTryToConnect && serviceRecords.isEmpty()) { + synchronized (lock) { + + int[] attrIDs = new int[]{ + 0x0100 // Service name + }; + + + if (log.isInfoEnabled()) { + log.info("Trying to get services (try " + tryRound + ")"); + } + localDevice.getDiscoveryAgent().searchServices(attrIDs, + new UUID[]{new UUID(serviceIndex)}, + device, + listener); + + lock.wait(); + } + + tryRound++; + } + + return serviceRecords; + + } + + protected class ServicesDiscoveryListener implements DiscoveryListener { + + @Override + public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) { + } + + @Override + public void inquiryCompleted(int discType) { + } + + @Override + public void serviceSearchCompleted(int transID, int respCode) { + if (log.isDebugEnabled()) { + log.debug("Service search completed!"); + } + synchronized (lock) { + lock.notifyAll(); + } + } + + @Override + public void servicesDiscovered(int transID, ServiceRecord[] servRecord) { + for (ServiceRecord aServRecord : servRecord) { + String url = aServRecord.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false); + if (url == null) { + continue; + } + serviceRecords.add(aServRecord); + String serviceName = getServiceName(aServRecord); + if (log.isDebugEnabled()) { + log.debug("service found " + url + (serviceName == null ? "" : ", name: " + serviceName)); + } + } + } + } + + protected String getServiceName(ServiceRecord serviceRecord) { + + DataElement serviceName = serviceRecord.getAttributeValue(0x0100); + String result; + + if (serviceName == null) { + result = null; + } else { + result = String.valueOf(serviceName.getValue()); + } + return result; + + } +} diff --git a/tutti-ichtyometer/src/main/java/fr/ifremer/tutti/ichtyometer/tool/FeedReaderTool.java b/tutti-ichtyometer/src/main/java/fr/ifremer/tutti/ichtyometer/tool/FeedReaderTool.java index 901dc4a..1b63b5c 100644 --- a/tutti-ichtyometer/src/main/java/fr/ifremer/tutti/ichtyometer/tool/FeedReaderTool.java +++ b/tutti-ichtyometer/src/main/java/fr/ifremer/tutti/ichtyometer/tool/FeedReaderTool.java @@ -72,7 +72,7 @@ public class FeedReaderTool { } }; - IchtyometerClient client = new IchtyometerClient(); + IchtyometerClient client = new IchtyometerClient(2); client.open(remoteDeviceChooser, true); diff --git a/tutti-ichtyometer/src/main/java/fr/ifremer/tutti/ichtyometer/tool/SendCommandTool.java b/tutti-ichtyometer/src/main/java/fr/ifremer/tutti/ichtyometer/tool/SendCommandTool.java index 8983978..fdbb2c6 100644 --- a/tutti-ichtyometer/src/main/java/fr/ifremer/tutti/ichtyometer/tool/SendCommandTool.java +++ b/tutti-ichtyometer/src/main/java/fr/ifremer/tutti/ichtyometer/tool/SendCommandTool.java @@ -80,7 +80,7 @@ public class SendCommandTool { } }; - IchtyometerClient client = new IchtyometerClient(); + IchtyometerClient client = new IchtyometerClient(2); client.open(remoteDeviceChooser, true); diff --git a/tutti-ichtyometer/src/test/java/fr/ifremer/tutti/ichtyometer/BigFins.java b/tutti-ichtyometer/src/test/java/fr/ifremer/tutti/ichtyometer/BigFins.java index 76ada4d..00c9c3b 100644 --- a/tutti-ichtyometer/src/test/java/fr/ifremer/tutti/ichtyometer/BigFins.java +++ b/tutti-ichtyometer/src/test/java/fr/ifremer/tutti/ichtyometer/BigFins.java @@ -24,6 +24,8 @@ package fr.ifremer.tutti.ichtyometer; * #L% */ +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.junit.Assume; import java.util.Set; @@ -36,6 +38,9 @@ import java.util.Set; */ public class BigFins { + /** Logger. */ + private static final Log log = LogFactory.getLog(BigFins.class); + private static final String BIG_FIN_V2_NAME_PREFIX = "BigFin DFS/2-"; public static void open(IchtyometerClient client) { @@ -45,7 +50,7 @@ public class BigFins { public String chooseRemoteDevice(Set<String> remoteDeviceNames) { String result = null; for (String remoteDeviceName : remoteDeviceNames) { - if (remoteDeviceName.startsWith(BIG_FIN_V2_NAME_PREFIX)) { + if (remoteDeviceName.startsWith(BIG_FIN_V2_NAME_PREFIX) || remoteDeviceName.equals("Yo")) { result = remoteDeviceName; break; } @@ -58,6 +63,9 @@ public class BigFins { client.open(remoteDeviceChooser, true); } catch (Exception e) { + if (log.isErrorEnabled()) { + log.error("Could not connect to remote device",e); + } Assume.assumeTrue("Could not connect to remote device", true); } diff --git a/tutti-ichtyometer/src/test/java/fr/ifremer/tutti/ichtyometer/IchtyometerClientTest.java b/tutti-ichtyometer/src/test/java/fr/ifremer/tutti/ichtyometer/IchtyometerClientTest.java index aeb93cb..127d25a 100644 --- a/tutti-ichtyometer/src/test/java/fr/ifremer/tutti/ichtyometer/IchtyometerClientTest.java +++ b/tutti-ichtyometer/src/test/java/fr/ifremer/tutti/ichtyometer/IchtyometerClientTest.java @@ -39,7 +39,7 @@ public class IchtyometerClientTest { @Test public void connect() throws IOException, InterruptedException { - try (IchtyometerClient client = new IchtyometerClient()) { + try (IchtyometerClient client = new IchtyometerClient(2)) { BigFins.open(client); } diff --git a/tutti-ichtyometer/src/test/java/fr/ifremer/tutti/ichtyometer/feed/FeedReaderTest.java b/tutti-ichtyometer/src/test/java/fr/ifremer/tutti/ichtyometer/feed/FeedReaderTest.java index 73c88c6..9c70d6c 100644 --- a/tutti-ichtyometer/src/test/java/fr/ifremer/tutti/ichtyometer/feed/FeedReaderTest.java +++ b/tutti-ichtyometer/src/test/java/fr/ifremer/tutti/ichtyometer/feed/FeedReaderTest.java @@ -52,7 +52,7 @@ public class FeedReaderTest { @Before public void setUp() throws Exception { - client = new IchtyometerClient(); + client = new IchtyometerClient(2); BigFins.open(client); } diff --git a/tutti-ichtyometer/src/test/java/fr/ifremer/tutti/ichtyometer/interactive/CommandEngineTest.java b/tutti-ichtyometer/src/test/java/fr/ifremer/tutti/ichtyometer/interactive/CommandEngineTest.java index 95db3dd..bac5bfe 100644 --- a/tutti-ichtyometer/src/test/java/fr/ifremer/tutti/ichtyometer/interactive/CommandEngineTest.java +++ b/tutti-ichtyometer/src/test/java/fr/ifremer/tutti/ichtyometer/interactive/CommandEngineTest.java @@ -92,7 +92,7 @@ These messages are sent on a user-triggered basis; the host does not poll for th @Before public void setUp() throws Exception { - client = new IchtyometerClient(); + client = new IchtyometerClient(2); BigFins.open(client); -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.