Modified:
/trunk/bluecove-emu/src/test/java/net/sf/bluecove/RFCOMMConnectTest.java
=======================================
---
/trunk/bluecove-emu/src/test/java/net/sf/bluecove/RFCOMMConnectTest.java
Fri Mar 13 10:16:12 2009
+++
/trunk/bluecove-emu/src/test/java/net/sf/bluecove/RFCOMMConnectTest.java
Mon Sep 14 10:29:22 2009
@@ -45,133 +45,144 @@
*
*/
public class RFCOMMConnectTest extends TestCase {
- private static final UUID uuid = new UUID(0x2108);
-
- private Thread serverThread;
-
- private EchoServerRunnable srv;
-
- private static final String echoGreeting = "I echo";
-
- protected void setUp() throws Exception {
- super.setUp();
- EmulatorTestsHelper.startInProcessServer();
- EmulatorTestsHelper.useThreadLocalEmulator();
- serverThread = EmulatorTestsHelper.runNewEmulatorStack(srv = new
EchoServerRunnable());
- }
-
- protected void tearDown() throws Exception {
- super.tearDown();
- if (srv != null) {
- srv.stop = true;
- }
- if ((serverThread != null) && (serverThread.isAlive())) {
- serverThread.interrupt();
- serverThread.join();
- }
- EmulatorTestsHelper.stopInProcessServer();
- }
-
- private class EchoServerConnectionThread extends Thread {
-
- private StreamConnection conn;
-
- private EchoServerConnectionThread(StreamConnection conn) {
- super("EchoServerConnectionThread");
- this.conn = conn;
- }
-
- public void run() {
- try {
- DataOutputStream dos = conn.openDataOutputStream();
- DataInputStream dis = conn.openDataInputStream();
-
- dos.writeUTF(echoGreeting);
- dos.flush();
-
- String received = dis.readUTF();
- System.out.println("Server received:" + received);
-
- dos.writeUTF(received);
- dos.flush();
-
- dos.close();
- dis.close();
-
- conn.close();
- } catch (Throwable e) {
- // System.err.print(e.toString());
- // e.printStackTrace();
- } finally {
- if (conn != null) {
- try {
- conn.close();
- } catch (IOException ignore) {
- }
- }
- }
- }
-
- }
-
- private class EchoServerRunnable implements Runnable {
-
- boolean stop = false;
-
- public void run() {
-
- StreamConnectionNotifier service = null;
-
- try {
- String url = "btspp://localhost:" + uuid.toString() + ";name=TServer";
- service = (StreamConnectionNotifier) Connector.open(url);
-
- while (!stop) {
- StreamConnection conn = (StreamConnection) service.acceptAndOpen();
- System.out.println("Server received connection");
- EchoServerConnectionThread t = new EchoServerConnectionThread(conn);
- t.setDaemon(true);
- t.start();
- }
-
- } catch (Throwable e) {
- if (!stop) {
- System.err.print(e.toString());
- e.printStackTrace();
- }
- } finally {
- if (service != null) {
- try {
- service.close();
- } catch (IOException ignore) {
- }
- }
- }
- }
- }
-
- public void testTwoConnections() throws Exception {
- DiscoveryAgent discoveryAgent =
LocalDevice.getLocalDevice().getDiscoveryAgent();
- // Find service
- String serverURL = discoveryAgent.selectService(uuid,
ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
- Assert.assertNotNull("service not found", serverURL);
-
- StreamConnection conn = (StreamConnection) Connector.open(serverURL);
-
- StreamConnection conn2 = null;
- try {
- conn2 = (StreamConnection) Connector.open(serverURL);
- Assert.fail("Should not accpet the second connection to the same port");
- } catch (IOException e) {
-
- } finally {
- if (conn2 != null) {
- try {
- conn2.close();
- } catch (IOException ignore) {
- }
- }
- }
- conn.close();
- }
-}
+
+ private static final UUID uuid = new UUID(0x2108);
+
+ private Thread serverThread;
+
+ private EchoServerRunnable srv;
+
+ private static final String echoGreeting = "I echo";
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ EmulatorTestsHelper.startInProcessServer();
+ EmulatorTestsHelper.useThreadLocalEmulator();
+ serverThread = EmulatorTestsHelper.runNewEmulatorStack(srv = new
EchoServerRunnable());
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ if (srv != null) {
+ srv.stop = true;
+ }
+ if ((serverThread != null) && (serverThread.isAlive())) {
+ serverThread.interrupt();
+ serverThread.join();
+ }
+ EmulatorTestsHelper.stopInProcessServer();
+ }
+
+ private class EchoServerConnectionThread extends Thread {
+
+ private StreamConnection conn;
+
+ private EchoServerConnectionThread(StreamConnection conn) {
+ super("EchoServerConnectionThread");
+ this.conn = conn;
+ }
+
+ public void run() {
+ try {
+ DataOutputStream dos = conn.openDataOutputStream();
+ DataInputStream dis = conn.openDataInputStream();
+
+ dos.writeUTF(echoGreeting);
+ dos.flush();
+
+ String received = dis.readUTF();
+ System.out.println("Server received:" + received);
+
+ dos.writeUTF(received);
+ dos.flush();
+
+ dos.close();
+ dis.close();
+
+ conn.close();
+ } catch (Throwable e) {
+ // System.err.print(e.toString());
+ // e.printStackTrace();
+ } finally {
+ if (conn != null) {
+ try {
+ conn.close();
+ } catch (IOException ignore) {
+ }
+ }
+ }
+ }
+
+ }
+
+ private class EchoServerRunnable implements Runnable {
+
+ boolean stop = false;
+
+ public void run() {
+
+ StreamConnectionNotifier service = null;
+
+ try {
+ String url = "btspp://localhost:" + uuid.toString()
+ ";name=TServer";
+ service = (StreamConnectionNotifier) Connector.open(url);
+
+ while (!stop) {
+ StreamConnection conn = (StreamConnection)
service.acceptAndOpen();
+ System.out.println("Server received connection");
+ EchoServerConnectionThread t = new
EchoServerConnectionThread(conn);
+ t.setDaemon(true);
+ t.start();
+ }
+
+ } catch (Throwable e) {
+ if (!stop) {
+ System.err.print(e.toString());
+ e.printStackTrace();
+ }
+ } finally {
+ if (service != null) {
+ try {
+ service.close();
+ } catch (IOException ignore) {
+ }
+ }
+ }
+ }
+ }
+
+ public void testTwoConnections() throws Exception {
+ DiscoveryAgent discoveryAgent =
LocalDevice.getLocalDevice().getDiscoveryAgent();
+ // Find service
+ String serverURL = null;
+ // Let the server start in a separate thread.
+ int tryCount = 0;
+ while ((serverURL == null) && (tryCount <= 3)) {
+ if (tryCount > 0) {
+ Thread.sleep(300);
+ }
+ tryCount++;
+ serverURL = discoveryAgent.selectService(uuid,
ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
+
+ }
+ Assert.assertNotNull("service not found", serverURL);
+
+ StreamConnection conn = (StreamConnection)
Connector.open(serverURL);
+
+ StreamConnection conn2 = null;
+ try {
+ conn2 = (StreamConnection) Connector.open(serverURL);
+ Assert.fail("Should not accpet the second connection to the
same port");
+ } catch (IOException e) {
+
+ } finally {
+ if (conn2 != null) {
+ try {
+ conn2.close();
+ } catch (IOException ignore) {
+ }
+ }
+ }
+ conn.close();
+ }
+}