1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package net.sf.bluecove;
26
27 import java.io.DataInputStream;
28 import java.io.DataOutputStream;
29 import java.io.IOException;
30
31 import javax.bluetooth.DiscoveryAgent;
32 import javax.bluetooth.LocalDevice;
33 import javax.bluetooth.ServiceRecord;
34 import javax.bluetooth.UUID;
35 import javax.microedition.io.Connector;
36 import javax.microedition.io.StreamConnection;
37 import javax.microedition.io.StreamConnectionNotifier;
38
39 import junit.framework.Assert;
40 import junit.framework.TestCase;
41
42 import com.intel.bluetooth.EmulatorTestsHelper;
43
44
45
46
47 public class RFCOMMConnectTest extends TestCase {
48
49 private static final UUID uuid = new UUID(0x2108);
50
51 private Thread serverThread;
52
53 private EchoServerRunnable srv;
54
55 private static final String echoGreeting = "I echo";
56
57 protected void setUp() throws Exception {
58 super.setUp();
59 EmulatorTestsHelper.startInProcessServer();
60 EmulatorTestsHelper.useThreadLocalEmulator();
61 serverThread = EmulatorTestsHelper.runNewEmulatorStack(srv = new EchoServerRunnable());
62 }
63
64 protected void tearDown() throws Exception {
65 super.tearDown();
66 if (srv != null) {
67 srv.stop = true;
68 }
69 if ((serverThread != null) && (serverThread.isAlive())) {
70 serverThread.interrupt();
71 serverThread.join();
72 }
73 EmulatorTestsHelper.stopInProcessServer();
74 }
75
76 private class EchoServerConnectionThread extends Thread {
77
78 private StreamConnection conn;
79
80 private EchoServerConnectionThread(StreamConnection conn) {
81 super("EchoServerConnectionThread");
82 this.conn = conn;
83 }
84
85 public void run() {
86 try {
87 DataOutputStream dos = conn.openDataOutputStream();
88 DataInputStream dis = conn.openDataInputStream();
89
90 dos.writeUTF(echoGreeting);
91 dos.flush();
92
93 String received = dis.readUTF();
94 System.out.println("Server received:" + received);
95
96 dos.writeUTF(received);
97 dos.flush();
98
99 dos.close();
100 dis.close();
101
102 conn.close();
103 } catch (Throwable e) {
104
105
106 } finally {
107 if (conn != null) {
108 try {
109 conn.close();
110 } catch (IOException ignore) {
111 }
112 }
113 }
114 }
115
116 }
117
118 private class EchoServerRunnable implements Runnable {
119
120 boolean stop = false;
121
122 public void run() {
123
124 StreamConnectionNotifier service = null;
125
126 try {
127 String url = "btspp://localhost:" + uuid.toString() + ";name=TServer";
128 service = (StreamConnectionNotifier) Connector.open(url);
129
130 while (!stop) {
131 StreamConnection conn = (StreamConnection) service.acceptAndOpen();
132 System.out.println("Server received connection");
133 EchoServerConnectionThread t = new EchoServerConnectionThread(conn);
134 t.setDaemon(true);
135 t.start();
136 }
137
138 } catch (Throwable e) {
139 if (!stop) {
140 System.err.print(e.toString());
141 e.printStackTrace();
142 }
143 } finally {
144 if (service != null) {
145 try {
146 service.close();
147 } catch (IOException ignore) {
148 }
149 }
150 }
151 }
152 }
153
154 public void testTwoConnections() throws Exception {
155 DiscoveryAgent discoveryAgent = LocalDevice.getLocalDevice().getDiscoveryAgent();
156
157 String serverURL = null;
158
159 int tryCount = 0;
160 while ((serverURL == null) && (tryCount <= 3)) {
161 if (tryCount > 0) {
162 Thread.sleep(700);
163 }
164 tryCount++;
165 serverURL = discoveryAgent.selectService(uuid, ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
166
167 }
168 Assert.assertNotNull("service not found", serverURL);
169
170 StreamConnection conn = (StreamConnection) Connector.open(serverURL);
171
172 StreamConnection conn2 = null;
173 try {
174 conn2 = (StreamConnection) Connector.open(serverURL);
175 Assert.fail("Should not accpet the second connection to the same port");
176 } catch (IOException e) {
177
178 } finally {
179 if (conn2 != null) {
180 try {
181 conn2.close();
182 } catch (IOException ignore) {
183 }
184 }
185 }
186 conn.close();
187 }
188 }