My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/**
* BlueCove - Java library for Bluetooth
* Copyright (C) 2006-2009 Vlad Skarzhevskyy
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @author vlads
* @version $Id$
*/
package net.sf.bluecove;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.UUID;
import javax.microedition.io.Connection;
import javax.microedition.io.Connector;

import org.bluecove.tester.log.Logger;
import org.bluecove.tester.util.IOUtils;
import org.bluecove.tester.util.RuntimeDetect;
import org.bluecove.tester.util.StringUtils;
import org.bluecove.tester.util.TimeUtils;

import net.sf.bluecove.se.JavaSECommon;
import net.sf.bluecove.util.BluetoothTypesInfo;
import net.sf.bluecove.util.CollectionUtils;
import net.sf.bluecove.util.CountStatistic;
import net.sf.bluecove.util.IntVar;
import net.sf.bluecove.util.TimeStatistic;

/**
*
*/
public class TestResponderClient extends TestResponderCommon implements Runnable, CanShutdown {

public static int countSuccess = 0;

public static FailureLog failure = new FailureLog("Client failure");

public static int discoveryCount = 0;

public static int connectionCount = 0;

public static int discoveryDryCount = 0;

public static int discoverySuccessCount = 0;

public static long lastSuccessfulDiscovery;

public static int countConnectionThreads = 0;

private int connectedConnectionsExpect = 1;

private int connectionLogPrefixLength = 0;

private int connectedConnectionsInfo = 1;

private Vector concurrentConnections = new Vector();

public static CountStatistic concurrentStatistic = new CountStatistic();

public static TimeStatistic connectionDuration = new TimeStatistic();

public static CountStatistic connectionRetyStatistic = new CountStatistic();

public Thread thread;

private Object threadLocalBluetoothStack;

boolean stoped = false;

TestClientConfig config;

boolean isRunning = false;

boolean runStressTest = false;

TestClientBluetoothInquirer bluetoothInquirer;

public static Hashtable recentDeviceNames = new Hashtable/* <BTAddress,Name> */();

boolean configured = false;

static int sdAttrRetrievableMax = 255;

public static synchronized void clear() {
countSuccess = 0;
failure.clear();
discoveryCount = 0;
concurrentStatistic.clear();
connectionDuration.clear();
}

public TestResponderClient() throws BluetoothStateException {
this(true);
}

public TestResponderClient(boolean logLocalDevice) throws BluetoothStateException {

config = new TestClientConfig();

if (logLocalDevice) {
TestResponderCommon.startLocalDevice();
}
threadLocalBluetoothStack = Configuration.threadLocalBluetoothStack;

String v = LocalDevice.getProperty("bluetooth.sd.attr.retrievable.max");
if (v != null) {
sdAttrRetrievableMax = Integer.valueOf(v).intValue();
if ((sdAttrRetrievableMax > 7) && (RuntimeDetect.isJ2ME)) {
sdAttrRetrievableMax = 7;
}
}

}

static boolean isMultiProtocol() {
return ((Configuration.supportL2CAP) && Configuration.testL2CAP.booleanValue() && Configuration.testRFCOMM
.booleanValue());
}

public void connectAndTest(String serverURL, String urlArgs, IntVar firstCase, IntVar lastCase,
TestResponderClientConnection connectionHandler) {
String deviceAddress = BluetoothTypesInfo.extractBluetoothAddress(serverURL);
String deviceName = niceDeviceName(deviceAddress);
long start = System.currentTimeMillis();
Logger.debug("connect:" + deviceName + " " + serverURL);
String logPrefix = "";
if (isMultiProtocol()) {
deviceName = connectionHandler.protocolID() + " " + deviceName;
}
if (connectedConnectionsExpect > 1) {
logPrefix = "[" + StringUtils.padRight(deviceName, connectionLogPrefixLength, ' ') + "] ";
}
for (int testType = firstCase.getValue(); (!stoped) && (runStressTest || testType <= lastCase.getValue()); testType++) {
Connection conn = null;
ConnectionHolder c = null;
TestStatus testStatus = new TestStatus();
testStatus.pairBTAddress = deviceAddress;
TestTimeOutMonitor monitor = null;
long connectionStartTime = 0;
try {
if (!runStressTest) {
Logger.debug(logPrefix + "test #" + testType + " connects");
} else {
testType = Configuration.STERSS_TEST_CASE.getValue();
}
int connectionOpenTry = 0;
while ((conn == null) && (!stoped)) {
try {
conn = Connector.open(serverURL + urlArgs, Connector.READ_WRITE, true);
} catch (IOException e) {
connectionOpenTry++;
if ((stoped) || (connectionOpenTry > CommunicationTester.clientConnectionOpenRetry)) {
throw e;
}
Logger.debug(logPrefix + "Connector error", e);
Thread.sleep(Configuration.clientSleepOnConnectionRetry);
Logger.debug(logPrefix + "connect retry:" + connectionOpenTry);
String cCount = LocalDevice.getProperty("bluecove.connections");
if ((cCount != null) && (!"0".equals(cCount))) {
Logger.debug(logPrefix + "has connections:" + cCount);
}
}
}
if (stoped) {
return;
}
c = connectionHandler.connected(conn);

connectionStartTime = System.currentTimeMillis();
connectionRetyStatistic.add(connectionOpenTry);
connectionCount++;

c.registerConcurrent(concurrentConnections);
c.concurrentNotify();

if (connectedConnectionsInfo < c.concurrentCount) {
connectedConnectionsInfo = c.concurrentCount;
Logger.info(logPrefix + "now connected:" + connectedConnectionsInfo);
synchronized (TestResponderClient.this) {
TestResponderClient.this.notifyAll();
}
}
c.active();
monitor = TestTimeOutMonitor.create(logPrefix + "test #" + testType, c,
Configuration.clientTestTimeOutSec);
if (!runStressTest) {
Logger.debug(logPrefix + "run test #" + testType);
} else {
Logger.debug(logPrefix + "connected:" + connectionCount);
if (connectionCount % 5 == 0) {
Logger.debug("Test time " + TimeUtils.secSince(start));
}
}
if (testType > Consts.TEST_SERVER_TERMINATE) {
Configuration.setLastServerURL(serverURL);
}

connectionHandler.executeTest(testType, testStatus);

if (monitor.isShutdownCalled()) {
failure.addFailure(deviceName + " test #" + testType + " " + testStatus.getName()
+ " terminated by by TimeOut");
} else if (testStatus.isError) {
failure.addFailure(deviceName + " test #" + testType + " " + testStatus.getName());
} else if (testStatus.isSuccess) {
countSuccess++;
Logger.debug(logPrefix + "test #" + testType + " " + testStatus.getName() + ": OK");
} else if (testStatus.streamClosed) {
Logger.debug(logPrefix + "see server log");
} else {
connectionHandler.replySuccess(logPrefix, testType, testStatus);
countSuccess++;
Logger.debug(logPrefix + "test #" + testType + " " + testStatus.getName() + ": OK");
}
if (connectionCount % 5 == 0) {
Logger.info("*Success:" + countSuccess + " Failure:" + failure.countFailure);
}
Configuration.setLastServerURL(serverURL);

// Delay to see if many connections are made.
if ((connectedConnectionsExpect > 1) && (connectedConnectionsInfo < connectedConnectionsExpect)) {
synchronized (TestResponderClient.this) {
try {
TestResponderClient.this.wait(3 * 1000);
} catch (InterruptedException e) {
break;
}
}
Logger.debug(logPrefix + "concurrentCount " + c.concurrentCount);
}
} catch (Throwable e) {
if (!stoped) {
if ((monitor != null) && (monitor.isShutdownCalled())) {
failure.addFailure(deviceName + " test #" + testType + " " + testStatus.getName()
+ " terminated by by TimeOut");
} else if (testType < Consts.TEST_SERVER_TERMINATE) {
failure.addFailure(deviceName + " test #" + testType + " " + testStatus.getName(), e);
}
}
Logger.error(deviceName + " test #" + testType + " " + testStatus.getName(), e);
} finally {
if (connectionStartTime != 0) {
connectionDuration.add(TimeUtils.since(connectionStartTime));
}
if (monitor != null) {
monitor.finish();
}
if (c != null) {
c.disconnected();
if (c.concurrentCount != 0) {
concurrentStatistic.add(c.concurrentCount);
}
c.shutdown();
} else {
IOUtils.closeQuietly(conn);
}
}
if ((Configuration.clientTestStopOnErrorCount != 0)
&& (failure.countFailure >= Configuration.clientTestStopOnErrorCount)) {
shutdown();
}
// Let the server restart
if (!stoped) {
try {
Thread.sleep(Configuration.clientSleepBetweenConnections);
} catch (InterruptedException e) {
break;
}
}
}
if (!Configuration.clientContinuous.booleanValue()) {
connectionHandler.sendStopServerCmd(serverURL);
}
}

private boolean connectAndTest(String serverURL) {
if (BluetoothTypesInfo.isRFCOMM(serverURL)) {
if (Configuration.testRFCOMM.booleanValue()) {
TestResponderClientRFCOMM.connectAndTest(this, serverURL);
return true;
}
} else if (BluetoothTypesInfo.isL2CAP(serverURL)) {
if (Configuration.supportL2CAP) {
if (Configuration.testL2CAP.booleanValue()) {
TestResponderClientL2CAP.connectAndTest(this, serverURL);
return true;
}
} else {
Logger.warn("Can't test L2CAP on this stack");
}
} else {
Logger.warn("No tests for connection type " + serverURL);
}
return false;
}

private class ClientConnectionTread extends Thread {

String url;

boolean started;

ClientConnectionTread(String url) {
// CLDC_1_0 super("ClientConnectionTread" +
// (++countConnectionThreads));
++countConnectionThreads;

this.url = url;
}

public void run() {
RuntimeDetect.cldcStub.setThreadLocalBluetoothStack(threadLocalBluetoothStack);
started = connectAndTest(url);
}
}

private void connectAndTest(Vector urls) {
int numberOfURLs = urls.size();
if ((!Configuration.clientTestConnectionsMultipleThreads) || (numberOfURLs == 1)) {
connectedConnectionsExpect = 1;
connectedConnectionsInfo = 1;
for (Enumeration en = urls.elements(); en.hasMoreElements();) {
if (stoped) {
break;
}
String url = (String) en.nextElement();
connectAndTest(url);
}
} else {
connectedConnectionsExpect = numberOfURLs;
connectedConnectionsInfo = 1;

connectionLogPrefixLength = 0;
for (Enumeration en = urls.elements(); en.hasMoreElements();) {
String deviceAddress = BluetoothTypesInfo.extractBluetoothAddress((String) en.nextElement());
String deviceName = niceDeviceName(deviceAddress);
if (deviceName.length() > connectionLogPrefixLength) {
connectionLogPrefixLength = deviceName.length();
}
}

if (isMultiProtocol()) {
connectionLogPrefixLength += 3;
}

Logger.debug("start " + numberOfURLs + " threads");
Vector threads = new Vector();
for (Enumeration en = urls.elements(); en.hasMoreElements();) {
ClientConnectionTread t = new ClientConnectionTread((String) en.nextElement());
t.start();
threads.addElement(t);
}
int connectedConnectionsStartedExpect = 0;
for (Enumeration en = threads.elements(); en.hasMoreElements();) {
ClientConnectionTread t = (ClientConnectionTread) en.nextElement();
if (t.started) {
connectedConnectionsStartedExpect++;
}
try {
t.join();
} catch (InterruptedException e) {
break;
}
}
if (connectedConnectionsInfo < connectedConnectionsStartedExpect) {
if (!stoped) {
failure.addFailure("Fails to establish " + connectedConnectionsStartedExpect
+ " connections same time");
Logger.error("Fails to establish " + connectedConnectionsStartedExpect + " connections same time");
}
} else {
if (connectedConnectionsInfo > 1) {
Logger.info("Established " + connectedConnectionsInfo + " connections same time");
}
}
}
}

public void configured() {
synchronized (this) {
configured = true;
this.notifyAll();
}
}

public long lastActivityTime() {
return lastSuccessfulDiscovery;
}

public void run() {
synchronized (this) {
while (!configured) {
try {
this.wait();
} catch (InterruptedException e) {
return;
}
}
}
RuntimeDetect.cldcStub.setThreadLocalBluetoothStack(threadLocalBluetoothStack);

Logger.debug(config.logID + "Client started..." + TimeUtils.timeNowToString());
isRunning = true;
try {
bluetoothInquirer = new TestClientBluetoothInquirer(config);
Switcher.clientStarted(this);

int startTry = 0;
if (config.connectURL != null) {
if (!config.connectURL.equals("")) {
bluetoothInquirer.serverURLs.addElement(config.connectURL);
}
} else if (config.connectDevice != null) {
Configuration.clientContinuousDiscoveryDevices = false;
bluetoothInquirer.devices.addElement(new RemoteDeviceIheritance(config.connectDevice));
}

while (!stoped) {
if ((config.connectURL != null) && (config.connectURL.equals(""))) {
try {
bluetoothInquirer.serverURLs.removeAllElements();
DiscoveryAgent discoveryAgent = LocalDevice.getLocalDevice().getDiscoveryAgent();
UUID uuid = (config.searchOnlyBluecoveUuid) ? Configuration.blueCoveUUID()
: Configuration.discoveryUUID;
String url = discoveryAgent.selectService(uuid, Configuration.getRequiredSecurity(), false);
if (url != null) {
Logger.debug(config.logID + "selectService service found " + url);
bluetoothInquirer.serverURLs.addElement(url);
} else {
Logger.debug(config.logID + "selectService service not found");
}
} catch (BluetoothStateException e) {
Logger.error(config.logID + "Cannot selectService", e);
}
} else if ((!bluetoothInquirer.hasServers())
|| (Configuration.clientContinuousDiscovery.booleanValue() && (config.connectURL == null))
|| (!Configuration.clientTestConnections)) {
if (!bluetoothInquirer.runDeviceInquiry()) {
if (stoped) {
break;
}
startTry++;
try {
Thread.sleep(Configuration.clientSleepOnDeviceInquiryError);
} catch (Exception e) {
break;
}
if (startTry < 3) {
continue;
}
Switcher.yield(this);
} else {
startTry = 0;
}
while (bluetoothInquirer.inquiring) {
try {
Thread.sleep(1000);
} catch (Exception e) {
break;
}
}
}
if ((Configuration.clientTestConnections) && (bluetoothInquirer.hasServers())) {
discoveryDryCount = 0;
discoverySuccessCount++;
lastSuccessfulDiscovery = System.currentTimeMillis();
if (!config.discoveryOnce) {
connectAndTest(bluetoothInquirer.serverURLs);
}
} else {
discoveryDryCount++;
if ((discoveryDryCount % 5 == 0) && (lastSuccessfulDiscovery != 0)) {
Logger.debug(config.logID + "No services " + discoveryDryCount + " times for "
+ TimeUtils.secSince(lastSuccessfulDiscovery) + " " + discoverySuccessCount);
}
}
Logger.info(config.logID + "*Success:" + countSuccess + " Failure:" + failure.countFailure);
if ((countSuccess + failure.countFailure > 0) && (!Configuration.clientContinuous.booleanValue())) {
break;
}
if (stoped || config.discoveryOnce || config.connectOnce) {
break;
}
Switcher.yield(this);
}
} catch (Throwable e) {
if (!stoped) {
Logger.error(config.logID + "client error ", e);
}
} finally {
Switcher.clientEnds(this);
config.connectURL = null;
isRunning = false;
Logger.info(config.logID + "Client finished! " + TimeUtils.timeNowToString());
Switcher.yield(this);
}
}

public boolean isAnyServiceFound() {
if (bluetoothInquirer != null) {
return bluetoothInquirer.anyServicesFound;
} else {
return false;
}
}

public void shutdown() {
Logger.info(config.logID + "shutdownClient");
stoped = true;
if (bluetoothInquirer != null) {
bluetoothInquirer.shutdown();
}
if (RuntimeDetect.cldcStub != null) {
RuntimeDetect.cldcStub.interruptThread(thread);
}
Vector concurrentConnectionsCopy = CollectionUtils.copy(concurrentConnections);
for (Enumeration iter = concurrentConnectionsCopy.elements(); iter.hasMoreElements();) {
ConnectionHolder t = (ConnectionHolder) iter.nextElement();
t.shutdown();
}
}

}

Change log

r2937 by skarzhevskyy on Mar 20, 2009   Diff
tests on Windows Mobile
Go to: 
Project members, sign in to write a code review

Older revisions

r2916 by skarzhevskyy on Mar 13, 2009   Diff
Updated javadocs and Copyright
r2607 by skarzhevskyy on Dec 17, 2008   Diff
OBEX tests in J2ME
r2595 by skarzhevskyy on Dec 16, 2008   Diff
refactor client
All revisions of this file

File info

Size: 17265 bytes, 546 lines

File properties

svn:mime-type
text/plain
svn:eol-style
native
svn:keywords
Date Author Id Revision
Powered by Google Project Hosting