View Javadoc

1   /**
2    *  BlueCove - Java library for Bluetooth
3    *  Copyright (C) 2008-2009 Vlad Skarzhevskyy
4    *
5    *  Licensed to the Apache Software Foundation (ASF) under one
6    *  or more contributor license agreements.  See the NOTICE file
7    *  distributed with this work for additional information
8    *  regarding copyright ownership.  The ASF licenses this file
9    *  to you under the Apache License, Version 2.0 (the
10   *  "License"); you may not use this file except in compliance
11   *  with the License.  You may obtain a copy of the License at
12   *
13   *    http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing,
16   *  software distributed under the License is distributed on an
17   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   *  KIND, either express or implied.  See the License for the
19   *  specific language governing permissions and limitations
20   *  under the License.
21   *
22   *  @author vlads
23   *  @version $Id: RFCOMMConnectTest.java 3024 2009-09-15 02:17:25Z skarzhevskyy $
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                 // System.err.print(e.toString());
105                 // e.printStackTrace();
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         // Find service
157         String serverURL = null;
158         // Let the server start in a separate thread.
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 }