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
/**
* 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.io.InputStream;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.bluetooth.DataElement;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.io.Connection;
import javax.microedition.io.Connector;
import javax.obex.HeaderSet;
import javax.obex.Operation;
import javax.obex.ResponseCodes;
import javax.obex.ServerRequestHandler;
import javax.obex.SessionNotifier;

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.util.BluetoothTypesInfo;

public class TestResponderServerOBEX implements Runnable {

private SessionNotifier serverConnection;

private boolean isStoped = false;

private boolean isRunning = false;

private Thread thread;

private Object threadLocalBluetoothStack;

public static final UUID OBEX_OBJECT_PUSH = new UUID(0x1105);

public static final String OBEX_OBJECT_PUSH_SERVER_NAME = "OBEX Object Push";

private TestResponderServerOBEX() {

}

public static TestResponderServerOBEX startServer() {
TestResponderServerOBEX srv = new TestResponderServerOBEX();
srv.threadLocalBluetoothStack = Configuration.threadLocalBluetoothStack;
srv.thread = RuntimeDetect.cldcStub.createNamedThread(srv, "ServerOBEX");
srv.thread.start();
return srv;
}

public boolean isRunning() {
return isRunning;
}

public void run() {
isStoped = false;
boolean deviceServiceClassesUpdated = false;
RuntimeDetect.cldcStub.setThreadLocalBluetoothStack(threadLocalBluetoothStack);
LocalDevice localDevice;
try {
localDevice = LocalDevice.getLocalDevice();
if (Configuration.testServerOBEX_TCP.booleanValue()) {
serverConnection = (SessionNotifier) Connector
.open(BluetoothTypesInfo.PROTOCOL_SCHEME_TCP_OBEX + "://");
} else {
StringBuffer url = new StringBuffer(BluetoothTypesInfo.PROTOCOL_SCHEME_BT_OBEX);
url.append("://localhost:");
if (Configuration.testServerOBEXObjectPush) {
url.append(OBEX_OBJECT_PUSH);
url.append(";name=");
url.append(OBEX_OBJECT_PUSH_SERVER_NAME);
} else {
url.append(Configuration.blueCoveOBEXUUID());
url.append(";name=");
url.append(Consts.RESPONDER_SERVERNAME);
url.append("_ox");
}

url.append(Configuration.serverURLParams());

serverConnection = (SessionNotifier) Connector.open(url.toString());
if (Configuration.testServerOBEXObjectPush) {
try {
ServiceRecord record = localDevice.getRecord(serverConnection);

final int OBJECT_TRANSFER_SERVICE = 0x100000;

try {
record.setDeviceServiceClasses(OBJECT_TRANSFER_SERVICE);
} catch (Throwable e) {
Logger.debug("setDeviceServiceClasses", e);
}

DataElement bluetoothProfileDescriptorList = new DataElement(DataElement.DATSEQ);
DataElement obbexPushProfileDescriptor = new DataElement(DataElement.DATSEQ);
obbexPushProfileDescriptor.addElement(new DataElement(DataElement.UUID, OBEX_OBJECT_PUSH));
obbexPushProfileDescriptor.addElement(new DataElement(DataElement.U_INT_2, 0x100));
bluetoothProfileDescriptorList.addElement(obbexPushProfileDescriptor);
record.setAttributeValue(0x0009, bluetoothProfileDescriptorList);

final short ATTR_SUPPORTED_FORMAT_LIST_LIST = 0x0303;
DataElement supportedFormatList = new DataElement(DataElement.DATSEQ);
// any type of object.
supportedFormatList.addElement(new DataElement(DataElement.U_INT_1, 0xFF));
record.setAttributeValue(ATTR_SUPPORTED_FORMAT_LIST_LIST, supportedFormatList);

final short UUID_PUBLICBROWSE_GROUP = 0x1002;
final short ATTR_BROWSE_GRP_LIST = 0x0005;
DataElement browseClassIDList = new DataElement(DataElement.DATSEQ);
UUID browseClassUUID = new UUID(UUID_PUBLICBROWSE_GROUP);
browseClassIDList.addElement(new DataElement(DataElement.UUID, browseClassUUID));
record.setAttributeValue(ATTR_BROWSE_GRP_LIST, browseClassIDList);

localDevice.updateRecord(record);
} catch (Throwable e) {
Logger.error("OBEX Service Updating SDP", e);
}
} else if (Configuration.testServiceAttributes.booleanValue()) {
ServiceRecord record = localDevice.getRecord(serverConnection);
if (record == null) {
Logger.warn("Bluetooth ServiceRecord is null");
} else {
TestResponderServer.buildServiceRecord(record);

try {
record.setDeviceServiceClasses(BluetoothTypesInfo.DeviceClassConsts.INFORMATION_SERVICE);
deviceServiceClassesUpdated = true;
} catch (Throwable e) {
if (e.getMessage().startsWith("Not Supported on")) {
Logger.error("setDeviceServiceClasses " + e.getMessage());
} else {
Logger.error("setDeviceServiceClasses", e);
}
}

try {
LocalDevice.getLocalDevice().updateRecord(record);
Logger.debug("OBEX ServiceRecord updated");
} catch (Throwable e) {
Logger.error("OBEX Service Record update error", e);
}
}
}
}
} catch (Throwable e) {
Logger.error("OBEX Server start error", e);
isStoped = true;
return;
}

if (deviceServiceClassesUpdated) {
Logger.info("DeviceClass:" + BluetoothTypesInfo.toString(localDevice.getDeviceClass()));
}

try {
int errorCount = 0;
int count = 0;
isRunning = true;
boolean showServiceRecordOnce = true;
while (!isStoped) {
RequestHandler handler = new RequestHandler();
try {
count++;
Logger.info("Accepting OBEX connections");
if (showServiceRecordOnce) {
try {
Logger.debug("OxUrl:"
+ localDevice.getRecord(serverConnection).getConnectionURL(
Configuration.getRequiredSecurity(), false));
} catch (IllegalArgumentException e) {
Logger.debug("Can't get local serviceRecord", e);
}
showServiceRecordOnce = false;
}
if (Configuration.authenticateOBEX.getValue() != 0) {
handler.auth = new OBEXTestAuthenticator("server" + count);
handler.connectionAccepted(serverConnection.acceptAndOpen(handler, handler.auth));
} else {
handler.connectionAccepted(serverConnection.acceptAndOpen(handler));
}
} catch (InterruptedIOException e) {
isStoped = true;
break;
} catch (Throwable e) {
if (errorCount > 3) {
isStoped = true;
}
if (isStoped) {
return;
}
errorCount++;
Logger.error("acceptAndOpen ", e);
continue;
}
errorCount = 0;
}
} finally {
close();
Logger.info("OBEX Server finished! " + TimeUtils.timeNowToString());
isRunning = false;
}
}

void close() {
try {
if (serverConnection != null) {
serverConnection.close();
}
Logger.debug("OBEX ServerConnection closed");
} catch (Throwable e) {
Logger.error("OBEX Server stop error", e);
}
}

void closeServer() {
isStoped = true;
close();
}

/*
* We testing on Java 1.1 and Timer is not important
*/
private class NoTimeWrapper {

Object timer;

NoTimeWrapper() {
try {
timer = new Timer();
} catch (Throwable e) {
Logger.warn("OBEX Server has no timer");
}
}

void schedule(final RequestHandler handler) {
if (timer != null) {
((Timer) timer).schedule(new TimerTask() {
public void run() {
handler.notConnectedClose();
}
}, 1000 * 30);
}
}

void cancel() {
if (timer != null) {
((Timer) timer).cancel();
}
}
}

private class RequestHandler extends ServerRequestHandler {

OBEXTestAuthenticator auth;

NoTimeWrapper notConnectedTimer = new NoTimeWrapper();

boolean isConnected = false;

Connection cconn;

RemoteDevice remoteDevice;

void connectionAccepted(Connection cconn) {
Logger.info("Received OBEX connection");
this.cconn = cconn;
if (!Configuration.testServerOBEX_TCP.booleanValue()) {
try {
remoteDevice = RemoteDevice.getRemoteDevice(cconn);
Logger.debug("connected toBTAddress " + remoteDevice.getBluetoothAddress());
} catch (IOException e) {
Logger.error("OBEX Server error", e);
}
}
if (!isConnected) {
notConnectedTimer.schedule(this);
}
}

void notConnectedClose() {
if (!isConnected) {
Logger.debug("OBEX connection timeout");
IOUtils.closeQuietly(cconn);
}
}

private void debugHeaderSet(HeaderSet headers) {
if (headers == null) {
return;
}
try {
int[] headerIDArray = headers.getHeaderList();
if (headerIDArray == null) {
return;
}
Logger.debug("Headers.length:" + headerIDArray.length);

for (int i = 0; i < headerIDArray.length; i++) {
int hi = headerIDArray[i];
Object value = headers.getHeader(hi);
Logger.debug("h[" + hi + "]=" + value);
}
} catch (IOException e) {
Logger.error("headers", e);
}
}

public int onConnect(HeaderSet request, HeaderSet reply) {
isConnected = true;
notConnectedTimer.cancel();
Logger.debug("OBEX onConnect");
debugHeaderSet(request);
if (Configuration.authenticate.booleanValue()) {
if (!remoteDevice.isAuthenticated()) {
return ResponseCodes.OBEX_HTTP_FORBIDDEN;
}
Logger.debug("OBEX connection Authenticated");
}
return ResponseCodes.OBEX_HTTP_OK;
}

public void onDisconnect(HeaderSet request, HeaderSet reply) {
Logger.debug("OBEX onDisconnect");
debugHeaderSet(request);
}

public int onSetPath(HeaderSet request, HeaderSet reply, boolean backup, boolean create) {
Logger.debug("OBEX onSetPath");
debugHeaderSet(request);
return super.onSetPath(request, reply, backup, create);
}

public int onDelete(HeaderSet request, HeaderSet reply) {
Logger.debug("OBEX onDelete");
debugHeaderSet(request);
return super.onDelete(request, reply);
}

public int onPut(Operation op) {
Logger.debug("OBEX onPut");
try {
HeaderSet hs = op.getReceivedHeaders();
debugHeaderSet(hs);
String name = (String) hs.getHeader(HeaderSet.NAME);
if (name != null) {
Logger.debug("name:" + name);

HeaderSet sendHeaders = createHeaderSet();
sendHeaders.setHeader(HeaderSet.DESCRIPTION, name);
op.sendHeaders(sendHeaders);
}

InputStream is = op.openInputStream();

StringBuffer buf = new StringBuffer();
while (!isStoped) {
int data = is.read();
if (data == -1) {
Logger.debug("EOS recived");
break;
}
char c = (char) data;
buf.append(c);
if ((c == '\n') || (buf.length() > 30)) {
Logger.debug("cc:" + StringUtils.toBinaryText(buf));
buf = new StringBuffer();
}
}
if (buf.length() > 0) {
Logger.debug("cc:" + StringUtils.toBinaryText(buf));
}
op.close();
return ResponseCodes.OBEX_HTTP_OK;
} catch (IOException e) {
Logger.error("OBEX Server onPut error", e);
return ResponseCodes.OBEX_HTTP_UNAVAILABLE;
} finally {
Logger.debug("OBEX onPut ends");
}
}

public int onGet(Operation op) {
Logger.debug("OBEX onGet");
String message = "Hello client! now " + new Date().toString();
try {
HeaderSet hs = op.getReceivedHeaders();
debugHeaderSet(hs);
String name = (String) hs.getHeader(HeaderSet.NAME);

if (name != null) {
message += "\nYou ask for [" + name + "]";
}
byte[] messageBytes = message.getBytes();
if (name != null) {
HeaderSet sendHeaders = createHeaderSet();
sendHeaders.setHeader(HeaderSet.DESCRIPTION, name);
sendHeaders.setHeader(HeaderSet.LENGTH, new Long(messageBytes.length));
op.sendHeaders(sendHeaders);
}

OutputStream os = op.openOutputStream();
os.write(messageBytes);
os.flush();
os.close();
op.close();
return ResponseCodes.OBEX_HTTP_OK;
} catch (IOException e) {
Logger.error("OBEX Server onGet error", e);
return ResponseCodes.OBEX_HTTP_UNAVAILABLE;
} finally {
Logger.debug("OBEX onGet ends");
}
}

public void onAuthenticationFailure(byte[] userName) {
Logger.debug("OBEX AuthFailure " + new String(userName));
}

}
}

Change log

r2916 by skarzhevskyy on Mar 13, 2009   Diff
Updated javadocs and Copyright
Go to: 
Project members, sign in to write a code review

Older revisions

r2607 by skarzhevskyy on Dec 17, 2008   Diff
OBEX tests in J2ME
r2471 by skarzhevskyy on Nov 30, 2008   Diff
Change license to Apache License,
Version 2.0, Update headers
r2408 by skarzhevskyy on Oct 9, 2008   Diff
organize product to modules
All revisions of this file

File info

Size: 13327 bytes, 453 lines

File properties

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