Author: tchemit Date: 2013-10-23 09:16:16 +0200 (Wed, 23 Oct 2013) New Revision: 687 Url: http://nuiton.org/projects/sandbox/repository/revisions/687 Log: cleantest Removed: bigfin/src/test/java/com/ Modified: bigfin/src/test/java/org/nuiton/bigfin/BigFinClientTest.java Modified: bigfin/src/test/java/org/nuiton/bigfin/BigFinClientTest.java =================================================================== --- bigfin/src/test/java/org/nuiton/bigfin/BigFinClientTest.java 2013-10-13 06:31:24 UTC (rev 686) +++ bigfin/src/test/java/org/nuiton/bigfin/BigFinClientTest.java 2013-10-23 07:16:16 UTC (rev 687) @@ -13,6 +13,7 @@ import javax.microedition.io.Connector; import javax.microedition.io.StreamConnection; import java.io.DataInputStream; +import java.io.DataOutputStream; import java.io.IOException; import java.util.List; @@ -43,80 +44,129 @@ Assume.assumeTrue("No remote device found, stop test.", !devices.isEmpty()); - for (RemoteDevice device : devices) { + // use first device + RemoteDevice device = devices.get(0); - if (log.isInfoEnabled()) { - log.info("found remote device: " + device); - } + if (log.isInfoEnabled()) { + log.info("found remote device: " + device); + } - Multimap<Integer, ServiceRecord> services = - ArrayListMultimap.create(); + Multimap<Integer, ServiceRecord> services = ArrayListMultimap.create(); - int[] indexes = {3, 256, 4353}; + int[] indexes = {3, 256, 4353}; - for (int index : indexes) { - List<ServiceRecord> serviceRecords = client.discoverServiceUrls(new UUID(index), device); + for (int index : indexes) { + List<ServiceRecord> serviceRecords = client.discoverServiceUrls(new UUID(index), device); - if (!serviceRecords.isEmpty()) { - if (log.isInfoEnabled()) { - log.info("Found some services for index: " + index); - } - services.putAll(index, serviceRecords); + if (!serviceRecords.isEmpty()) { + if (log.isInfoEnabled()) { + log.info("Found some services for index: " + index); } + services.putAll(index, serviceRecords); } + } -// int index = 0x0004; -// for (int i = 0; i < 20000; i++) { -// -// if (log.isInfoEnabled()) { -// log.info("Try to discover with service id: " + index); -// } -// -// List<ServiceRecord> serviceRecords = client.discoverServiceUrls(new UUID(index), device); -// -// if (!serviceRecords.isEmpty()) { -// if (log.isInfoEnabled()) { -// log.info("Found some services for index: " + index); + // take first service + Integer serviceIndex = services.keySet().iterator().next(); + + // take first service record + ServiceRecord serviceRecord = services.get(serviceIndex).iterator().next(); + + // get connection url + String url = serviceRecord.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, true); + + String serviceName = BigFinClient.getServiceName(serviceRecord); + if (log.isInfoEnabled()) { + log.info("Found service(" + serviceIndex + "): " + url + (serviceName == null ? "" : ", name: " + serviceName)); + } + + StreamConnection connection = (StreamConnection) Connector.open(url, 2); + +// { +// RemoteDevice rd = RemoteDevice.getRemoteDevice(connection); +// boolean authenticate = rd.authenticate(); +// if (log.isInfoEnabled()) { +// log.info("Authenticate response: " + authenticate); +// } // } -// services.putAll(index, serviceRecords); -// -// } -// index++; -// } - for (Integer serviceIndex : services.keySet()) { + // start a reading stream + ReadingRunnable readingRunnable = new ReadingRunnable(connection); - for (ServiceRecord serviceRecord : services.get(serviceIndex)) { - String url = serviceRecord.getConnectionURL(ServiceRecord.AUTHENTICATE_NOENCRYPT, true); + Thread readingThread = new Thread(readingRunnable); - String serviceName = BigFinClient.getServiceName(serviceRecord); - if (log.isInfoEnabled()) { - log.info("Found service(" + serviceIndex + "): " + url + (serviceName == null ? "" : ", name: " + serviceName)); - } - StreamConnection connection = (StreamConnection) Connector.open(url); + readingThread.start(); - { - RemoteDevice rd = RemoteDevice.getRemoteDevice(connection); - boolean authenticate = rd.authenticate(); - if (log.isInfoEnabled()) { - log.info("Authenticate response: " + authenticate); - } - } + synchronized (readingRunnable.lock) { + readingRunnable.lock.wait(); + } - DataInputStream dataInputStream = connection.openDataInputStream(); + if (log.isInfoEnabled()) { + log.info("Will write to device..."); + } + DataOutputStream dataOutputStream = connection.openDataOutputStream(); + dataOutputStream.writeChar('a'); + dataOutputStream.flush(); + dataOutputStream.writeChar('b'); + dataOutputStream.flush(); + while(true) {} + } - if (log.isInfoEnabled()) { - log.info("Open data stream: "+dataInputStream); - } - while (true) { + public static class ReadingRunnable implements Runnable { - String c = dataInputStream.readUTF(); + protected final StreamConnection connection; - if (log.isInfoEnabled()) { - log.info("Read: " + c); - } + protected boolean stop; + + protected boolean ready; + + protected final Object lock = new Object(); + + public boolean isReady() { + return ready; + } + + public void setStop(boolean stop) { + this.stop = stop; + } + + public ReadingRunnable(StreamConnection connection) { + this.connection = connection; + } + + @Override + public void run() { + try { + + if (log.isInfoEnabled()) { + log.info("Start " + this); + } + DataInputStream dataInputStream = connection.openDataInputStream(); + + if (log.isInfoEnabled()) { + log.info("Open data stream: " + dataInputStream); + } + + synchronized (lock) { + lock.notifyAll(); + } + + while (!stop) { + + String c = dataInputStream.readUTF(); + + if (log.isInfoEnabled()) { + log.info("Read: " + c); } } + + if (log.isInfoEnabled()) { + log.info("Will stop " + this); + } + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Reading error", e); + } } } }