View Javadoc

1   /**
2    *  BlueCove - Java library for Bluetooth
3    *  Copyright (C) 2008 Michael Lifshits
4    *  Copyright (C) 2008 Vlad Skarzhevskyy
5    *
6    *  Licensed to the Apache Software Foundation (ASF) under one
7    *  or more contributor license agreements.  See the NOTICE file
8    *  distributed with this work for additional information
9    *  regarding copyright ownership.  The ASF licenses this file
10   *  to you under the Apache License, Version 2.0 (the
11   *  "License"); you may not use this file except in compliance
12   *  with the License.  You may obtain a copy of the License at
13   *
14   *    http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing,
17   *  software distributed under the License is distributed on an
18   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19   *  KIND, either express or implied.  See the License for the
20   *  specific language governing permissions and limitations
21   *  under the License.
22   *
23   *  @author vlads
24   *  @version $Id: EmulatorTestsHelper.java 2471 2008-12-01 03:44:20Z skarzhevskyy $
25   */
26  package com.intel.bluetooth;
27  
28  import javax.bluetooth.BluetoothStateException;
29  
30  /**
31   * 
32   */
33  public class EmulatorTestsHelper {
34  
35  	private static int threadNumber;
36  
37  	private static synchronized int nextThreadNum() {
38  		return threadNumber++;
39  	}
40  
41  	/**
42  	 * Start air simulator server as in process server.
43  	 */
44  	public static void startInProcessServer() {
45  		BlueCoveImpl.setConfigProperty(BlueCoveConfigProperties.PROPERTY_EMULATOR_PORT, "0");
46  	}
47  
48  	/**
49  	 * Shutdown all running Stacks and air simulator server.
50  	 */
51  	public static void stopInProcessServer() {
52  		BlueCoveImpl.shutdown();
53  		EmulatorHelper.getService().shutdown();
54  	}
55  
56  	/**
57  	 * API that enables the use of Multiple Bluetooth Adapters in parallel in
58  	 * the same JVM. Each thread can call this function to initialize new
59  	 * adapter.
60  	 * 
61  	 * @see com.intel.bluetooth.BlueCoveImpl#useThreadLocalBluetoothStack()
62  	 * @throws BluetoothStateException
63  	 *             if the Bluetooth system emulator could not be initialized
64  	 */
65  	public static void useThreadLocalEmulator() throws BluetoothStateException {
66  		useThreadLocalEmulator(null, null);
67  	}
68  
69  	/**
70  	 * API that enables the use of Multiple Bluetooth Adapters in parallel in
71  	 * the same JVM. Each thread can call this function to initialize new
72  	 * adapter.
73  	 * 
74  	 * @see com.intel.bluetooth.BlueCoveImpl#useThreadLocalBluetoothStack()
75  	 * 
76  	 * @param deviceID
77  	 *            select bluetooth adapter by its system ID, can be
78  	 *            <code>null</code>
79  	 * @param localAddress
80  	 *            select bluetooth adapter by its Address, can be
81  	 *            <code>null</code>
82  	 * @throws BluetoothStateException
83  	 *             if the Bluetooth system emulator could not be initialized
84  	 */
85  	public static void useThreadLocalEmulator(String deviceID, String localAddress) throws BluetoothStateException {
86  		BlueCoveImpl.useThreadLocalBluetoothStack();
87  		BlueCoveImpl.setConfigProperty(BlueCoveConfigProperties.PROPERTY_STACK, BlueCoveImpl.STACK_EMULATOR);
88  		BlueCoveImpl.setConfigProperty(BlueCoveConfigProperties.PROPERTY_EMULATOR_PORT, "0");
89  		BlueCoveImpl.setConfigProperty(BlueCoveConfigProperties.PROPERTY_LOCAL_DEVICE_ID, deviceID);
90  		BlueCoveImpl.setConfigProperty(BlueCoveConfigProperties.PROPERTY_LOCAL_DEVICE_ADDRESS, localAddress);
91  		BlueCoveImpl.getThreadBluetoothStackID();
92  	}
93  
94  	private static class RunBefore implements Runnable {
95  
96  		private Runnable runnable;
97  
98  		private Object startedEvent = new Object();
99  
100 		private boolean started = false;
101 
102 		private BluetoothStateException startException;
103 
104 		RunBefore(Runnable runnable) {
105 			this.runnable = runnable;
106 		}
107 
108 		public void run() {
109 			try {
110 				useThreadLocalEmulator();
111 			} catch (BluetoothStateException e) {
112 				startException = e;
113 			} finally {
114 				started = true;
115 				synchronized (startedEvent) {
116 					startedEvent.notifyAll();
117 				}
118 			}
119 			runnable.run();
120 		}
121 	}
122 
123 	/**
124 	 * Helper function to execute code using different Bluetooth address
125 	 * 
126 	 * @param runnable
127 	 *            to be executed using another stack
128 	 * @return created and running Thread that will execute runnable in a new
129 	 *         ThreadGroup
130 	 * @throws BluetoothStateException
131 	 *             if the Bluetooth system emulator could not be initialized
132 	 */
133 	public static Thread runNewEmulatorStack(Runnable runnable) throws BluetoothStateException {
134 		RunBefore r = new RunBefore(runnable);
135 		int id = nextThreadNum();
136 		ThreadGroup g = new ThreadGroup("TestHelperThreadGroup-" + id);
137 		Thread t = new Thread(g, r, "TestHelperThread-" + id);
138 		synchronized (r.startedEvent) {
139 			t.start();
140 			while (!r.started) {
141 				try {
142 					r.startedEvent.wait();
143 				} catch (InterruptedException e) {
144 					throw (BluetoothStateException) UtilsJavaSE.initCause(new BluetoothStateException(e.getMessage()),
145 							e);
146 				}
147 				if (r.startException != null) {
148 					throw r.startException;
149 				}
150 			}
151 		}
152 		return t;
153 	}
154 }