View Javadoc

1   /**
2    *  BlueCove - Java library for Bluetooth
3    *  Copyright (C) 2006-2007 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: BlueCoveTestCanvas.java 2655 2008-12-24 16:04:37Z skarzhevskyy $
24   */
25  package net.sf.bluecove;
26  
27  import java.util.Enumeration;
28  
29  import javax.microedition.lcdui.Command;
30  import javax.microedition.lcdui.CommandListener;
31  import javax.microedition.lcdui.Displayable;
32  
33  import net.sf.bluecove.util.StorageRMS;
34  
35  import org.bluecove.tester.log.Logger;
36  import org.bluecove.tester.me.LoggerCanvas;
37  import org.bluecove.tester.util.ThreadUtils;
38  import org.bluecove.tester.util.TimeUtils;
39  
40  public class BlueCoveTestCanvas extends LoggerCanvas implements CommandListener {
41  
42  	static final Command exitCommand = new Command("Exit", Command.EXIT, 0);
43  
44  	static final Command printStatsCommand = new Command("1-Print Stats", Command.ITEM, 1);
45  
46  	static final Command startDiscoveryCommand = new Command("*-Discovery", Command.ITEM, 2);
47  
48  	static final Command startServicesSearchCommand = new Command("7-Services Search", Command.ITEM, 2);
49  
50  	static final Command startClientCommand = new Command("2-Client Start", Command.ITEM, 2);
51  
52  	static final Command stopClientCommand = new Command("3-Client Stop", Command.ITEM, 3);
53  
54  	static final Command startServerCommand = new Command("5-Server Start", Command.ITEM, 4);
55  
56  	static final Command stopServerCommand = new Command("6-Server Stop", Command.ITEM, 5);
57  
58  	static final Command startSwitcherCommand = new Command("8-Switcher Start", Command.ITEM, 6);
59  
60  	static final Command stopSwitcherCommand = new Command("9-Switcher Stop", Command.ITEM, 7);
61  
62  	static final Command clearCommand = new Command("#-Clear", Command.ITEM, 8);
63  
64  	static final Command printFailureLogCommand = new Command("4-Print FailureLog", Command.ITEM, 8);
65  
66  	static final Command startClientStressCommand = new Command("Client Stress Start", Command.ITEM, 9);
67  
68  	static final Command startClientLastServiceCommand = new Command("Client Last service Start", Command.ITEM, 10);
69  
70  	static final Command startClientLastDeviceCommand = new Command("Client Last device Start", Command.ITEM, 11);
71  
72  	static final Command startTCKAgentCommand = new Command("TCK Agent", Command.ITEM, 12);
73  
74  	static final Command configurationCommand = new Command("Options...", Command.ITEM, 13);
75  
76  	static final Command obexPutCommand = new Command("ObexPut", Command.ITEM, 14);
77  
78  	private Switcher switcher;
79  
80  	private int errorCount = 0;
81  
82  	public BlueCoveTestCanvas() {
83  		super();
84  		super.setTitle("BlueCoveT");
85  
86  		addCommand(exitCommand);
87  		addCommand(startDiscoveryCommand);
88  		addCommand(startServicesSearchCommand);
89  		addCommand(startClientCommand);
90  		addCommand(stopClientCommand);
91  		addCommand(startServerCommand);
92  		addCommand(stopServerCommand);
93  		addCommand(printStatsCommand);
94  		addCommand(startSwitcherCommand);
95  		addCommand(stopSwitcherCommand);
96  		addCommand(printFailureLogCommand);
97  		addCommand(clearCommand);
98  		addCommand(startClientStressCommand);
99  		addCommand(startClientLastServiceCommand);
100 		addCommand(startClientLastDeviceCommand);
101 		if (Configuration.likedTCKAgent) {
102 			addCommand(startTCKAgentCommand);
103 		}
104 		addCommand(configurationCommand);
105 		if (TestOBEXCilent.obexEnabled) {
106 			addCommand(obexPutCommand);
107 		}
108 		setCommandListener(this);
109 
110 		Configuration.storage = new StorageRMS();
111 	}
112 
113 	protected String getCanvasTitleText() {
114 		return "BlueCove Tester";
115 	}
116 
117 	protected String getCanvasStatusText() {
118 		StringBuffer msg = new StringBuffer();
119 		msg.append("(");
120 		msg.append("srv:").append((Switcher.isRunningServer()) ? "ON" : "off").append(" ").append(
121 				Switcher.serverStartCount);
122 		msg.append(" cli:").append((Switcher.isRunningClient()) ? "ON" : "off").append(" ").append(
123 				Switcher.clientStartCount);
124 		msg.append(" X:").append((Switcher.isRunning()) ? "ON" : "off");
125 		msg.append(" dc:").append(TestResponderClient.discoveryCount);
126 		msg.append(" er:").append(errorCount);
127 		msg.append(")");
128 		return msg.toString();
129 	}
130 
131 	protected void keyPressed(int keyCode) {
132 		switch (keyCode) {
133 		case '1':
134 			printStats();
135 			break;
136 		case '4':
137 			printFailureLog();
138 			break;
139 		case '0':
140 			logScrollBottom();
141 			break;
142 		case '*':
143 			Switcher.startDiscovery();
144 			break;
145 		case '7':
146 			Switcher.startServicesSearch();
147 			break;
148 		case '2':
149 			Switcher.startClient();
150 			break;
151 		case '3':
152 			Switcher.clientShutdown();
153 			break;
154 		case '5':
155 			Switcher.startServer();
156 			break;
157 		case '6':
158 			Switcher.serverShutdown();
159 			break;
160 		case '8':
161 			startSwitcher();
162 			break;
163 		case '9':
164 			stopSwitcher();
165 			break;
166 		case '#':
167 			clear();
168 			break;
169 		default:
170 			logLinesMove(getGameAction(keyCode));
171 		}
172 		repaint();
173 	}
174 
175 	protected void keyRepeated(int keyCode) {
176 		logLinesMove(getGameAction(keyCode));
177 	}
178 
179 	private void stopSwitcher() {
180 		if (switcher != null) {
181 			switcher.shutdown();
182 			switcher = null;
183 		}
184 	}
185 
186 	private void printStats() {
187 		Logger.info("--- discovery stats ---");
188 		int deviceCnt = 0;
189 		int deviceActiveCnt = 0;
190 		long activeDeadline = System.currentTimeMillis() - 1000 * 60 * 4;
191 		for (Enumeration iter = RemoteDeviceInfo.devices.elements(); iter.hasMoreElements();) {
192 			RemoteDeviceInfo dev = (RemoteDeviceInfo) iter.nextElement();
193 			deviceCnt++;
194 			StringBuffer buf = new StringBuffer();
195 			buf.append(TestResponderClient.niceDeviceName(dev.remoteDevice.getBluetoothAddress()));
196 			buf.append(" dc:").append(dev.serviceDiscovered.count);
197 			buf.append(" first:"); TimeUtils.appendTime(buf, dev.serviceDiscoveredFirstTime, false);
198 			buf.append(" last:"); TimeUtils.appendTime(buf, dev.serviceDiscoveredLastTime, false);
199 			Logger.info(buf.toString());
200 			buf = new StringBuffer();
201 			buf.append(" avg ddf:").append(dev.avgDiscoveryFrequencySec());
202 			buf.append(" sdf:").append(dev.avgServiceDiscoveryFrequencySec());
203 			buf.append(" ss:").append(dev.avgServiceSearchDurationSec());
204 			buf.append(" sss:").append(dev.serviceSearchSuccessPrc()).append("%");
205 			if (dev.serviceDiscoveredLastTime > activeDeadline) {
206 				deviceActiveCnt++;
207 				buf.append(" Active");
208 			} else {
209 				buf.append(" Down");
210 			}
211 			if (dev.variableData == 0) {
212 				buf.append(" No VarAttr");
213 			} else {
214 				buf.append(" srv:").append(dev.variableData);
215 				if (dev.variableDataUpdated) {
216 					buf.append(" var.OK");
217 				}
218 			}
219 			Logger.info(buf.toString());
220 		}
221 		StringBuffer buf = new StringBuffer();
222 		buf.append("all avg");
223 		buf.append(" srv:").append(TestResponderServer.avgServerDurationSec());
224 		buf.append(" di:").append(RemoteDeviceInfo.allAvgDeviceInquiryDurationSec());
225 		buf.append(" ss:").append(RemoteDeviceInfo.allAvgServiceSearchDurationSec());
226 		Logger.info(buf.toString());
227 
228 		buf = new StringBuffer();
229 		buf.append("all max");
230 		buf.append(" srv:").append(TestResponderServer.allServerDuration.durationMaxSec());
231 		buf.append(" di:").append(RemoteDeviceInfo.deviceInquiryDuration.durationMaxSec());
232 		buf.append(" ss:").append(RemoteDeviceInfo.allServiceSearch.durationMaxSec());
233 		Logger.info(buf.toString());
234 
235 		buf = new StringBuffer();
236 		buf.append("devices:").append(deviceCnt).append(" active:").append(deviceActiveCnt);
237 		buf.append(" threads:").append(Thread.activeCount());
238 		Logger.info(buf.toString());
239 		Logger.info("-----------------------");
240 		Logger.info("*Client Success:" + TestResponderClient.countSuccess + " Failure:"
241 				+ TestResponderClient.failure.countFailure);
242 		Logger.info("*Server Success:" + TestResponderServer.countSuccess + " Failure:"
243 				+ TestResponderServer.failure.countFailure);
244 		logScrollBottom();
245 	}
246 
247 	private void printFailureLog() {
248 		Logger.info("*Client Success:" + TestResponderClient.countSuccess + " Failure:"
249 				+ TestResponderClient.failure.countFailure);
250 		Logger.debug("Client avg conn concurrent " + TestResponderClient.concurrentStatistic.avg());
251 		Logger.debug("Client max conn concurrent " + TestResponderClient.concurrentStatistic.max());
252 		Logger.debug("Client avg conn time " + TestResponderClient.connectionDuration.avg() + " msec");
253 		Logger.debug("Client avg conn retry " + TestResponderClient.connectionRetyStatistic.avgPrc());
254 
255 		TestResponderClient.failure.writeToLog();
256 		Logger.info("*Server Success:" + TestResponderServer.countSuccess + " Failure:"
257 				+ TestResponderServer.failure.countFailure);
258 		Logger.debug("Server avg conn concurrent " + TestResponderServer.concurrentStatistic.avg());
259 		Logger.debug("Server avg conn time " + TestResponderServer.connectionDuration.avg() + " msec");
260 
261 		TestResponderServer.failure.writeToLog();
262 		logScrollBottom();
263 	}
264 
265 	private void clear() {
266 		clearLog();
267 		TestResponderClient.clear();
268 		TestResponderServer.clear();
269 		Switcher.clear();
270 		RemoteDeviceInfo.clear();
271 		repaint();
272 		Logger.runGarbageCollector();
273 	}
274 
275 	private void startSwitcher() {
276 		if (switcher == null) {
277 			switcher = new Switcher();
278 		}
279 		if (!switcher.isRunning) {
280 			(switcher.thread = new Thread(switcher)).start();
281 		} else {
282 			BlueCoveTestMIDlet.message("Warn", "Switcher isRunning");
283 		}
284 	}
285 
286 	public void commandAction(final Command c, Displayable d) {
287 		Runnable r = new Runnable() {
288 			public void run() {
289 				if (c == exitCommand) {
290 					Switcher.clientShutdown();
291 					Switcher.serverShutdownOnExit();
292 					BlueCoveTestMIDlet.exit();
293 					return;
294 				} else if (c == printStatsCommand) {
295 					printStats();
296 				} else if (c == printFailureLogCommand) {
297 					printFailureLog();
298 				} else if (c == clearCommand) {
299 					clear();
300 				} else if (c == startDiscoveryCommand) {
301 					Switcher.startDiscovery();
302 				} else if (c == startServicesSearchCommand) {
303 					Switcher.startServicesSearch();
304 				} else if (c == startClientCommand) {
305 					Switcher.startClient();
306 				} else if (c == startClientStressCommand) {
307 					Switcher.startClientStress();
308 				} else if (c == startClientLastServiceCommand) {
309 					Switcher.startClientLastURl();
310 				} else if (c == startClientLastDeviceCommand) {
311 					Switcher.startClientLastDevice();
312 				} else if (c == stopClientCommand) {
313 					Switcher.clientShutdown();
314 				} else if (c == stopServerCommand) {
315 					Switcher.serverShutdown();
316 				} else if (c == startServerCommand) {
317 					Switcher.startServer();
318 				} else if (c == startSwitcherCommand) {
319 					startSwitcher();
320 				} else if (c == stopSwitcherCommand) {
321 					stopSwitcher();
322 				} else if (c == configurationCommand) {
323 					try {
324 						BlueCoveTestMIDlet.setCurrentDisplayable(new BlueCoveTestConfigurationForm());
325 					} catch (Throwable e) {
326 						Logger.error("Internal error", e);
327 					}
328 				} else if ((Configuration.likedTCKAgent) && (c == startTCKAgentCommand)) {
329 					Switcher.startTCKAgent();
330 				} else if ((TestOBEXCilent.obexEnabled) && (c == obexPutCommand)) {
331 					TestOBEXCilent.obexPut();
332 				} else {
333 					if (c != null) {
334 						Logger.info("Command " + c.getLabel() + " not found");
335 					}
336 				}
337 			}
338 		};
339 		ThreadUtils.invokeLater(r, c.getLabel());
340 	}
341 
342 }