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
/**
* BlueCove - Java library for Bluetooth
* Copyright (C) 2006-2007 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.awt;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.obex.ClientSession;
import javax.obex.HeaderSet;
import javax.obex.Operation;

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

import net.sf.bluecove.Configuration;
import net.sf.bluecove.OBEXTestAuthenticator;
import net.sf.bluecove.util.BluetoothTypesInfo;

public class ObexClientConnectionThread extends Thread {

private Object threadLocalBluetoothStack;

private String serverURL;

private String name;

private String text;

boolean isPut;

boolean isRunning = false;

boolean timeouts;

String status;

private boolean stoped = false;

private ClientSession clientSession;

private static int count = 0;

public ObexClientConnectionThread(String serverURL, String name, String text, boolean isPut) {
this.serverURL = serverURL;
this.name = name;
this.text = text;
this.isPut = isPut;
threadLocalBluetoothStack = Configuration.threadLocalBluetoothStack;
count++;
}

public void run() {
final boolean isUserIdRequired = true;
final boolean isFullAccess = true;

isRunning = true;
try {
RuntimeDetect.cldcStub.setThreadLocalBluetoothStack(threadLocalBluetoothStack);

status = "Connecting...";
clientSession = (ClientSession) Connector.open(serverURL, Connector.READ_WRITE, timeouts);
if (stoped) {
return;
}
if (Configuration.authenticateOBEX.getValue() != 0) {
clientSession.setAuthenticator(new OBEXTestAuthenticator("client" + count));
}
status = "Connected";
HeaderSet hsConnect = clientSession.createHeaderSet();
if (Configuration.authenticateOBEX.getValue() == 1) {
hsConnect.createAuthenticationChallenge("OBEX-Con-Auth-Test", isUserIdRequired, isFullAccess);
}
HeaderSet hsConnectReply = clientSession.connect(hsConnect);
Logger.debug("connect responseCode "
+ BluetoothTypesInfo.toStringObexResponseCodes(hsConnectReply.getResponseCode()));

HeaderSet hsOperation = clientSession.createHeaderSet();
hsOperation.setHeader(HeaderSet.NAME, name);
hsOperation.setHeader(HeaderSet.TYPE, "text");

if (Configuration.authenticateOBEX.getValue() == 2) {
hsOperation.createAuthenticationChallenge("OBEX-OP-Auth-Test", isUserIdRequired, isFullAccess);
}
if (stoped) {
return;
}
if (isPut) {
byte data[] = text.getBytes("iso-8859-1");
hsOperation.setHeader(HeaderSet.LENGTH, new Long(data.length));
status = "Putting";
Operation po = clientSession.put(hsOperation);

OutputStream os = po.openOutputStream();
os.write(data);
os.close();

Logger.debug("put responseCode " + BluetoothTypesInfo.toStringObexResponseCodes(po.getResponseCode()));

HeaderSet receivedHeaders = po.getReceivedHeaders();
String description = (String) receivedHeaders.getHeader(HeaderSet.DESCRIPTION);
if (description != null) {
Logger.debug("Description " + description);
}

po.close();
} else {
status = "Getting";
Operation po = clientSession.get(hsOperation);

InputStream is = po.openInputStream();
StringBuffer buf = new StringBuffer();
while (!stoped) {
int i = is.read();
if (i == -1) {
break;
}
buf.append((char) i);
}
if (buf.length() > 0) {
Logger.debug("got:" + buf);
}
is.close();

Logger.debug("get responseCode " + BluetoothTypesInfo.toStringObexResponseCodes(po.getResponseCode()));

HeaderSet receivedHeaders = po.getReceivedHeaders();
String description = (String) receivedHeaders.getHeader(HeaderSet.DESCRIPTION);
if (description != null) {
Logger.debug("Description " + description);
}

po.close();
}

HeaderSet hsd = clientSession.disconnect(null);
Logger.debug("disconnect responseCode "
+ BluetoothTypesInfo.toStringObexResponseCodes(hsd.getResponseCode()));

status = "Finished";

} catch (IOException e) {
status = "Communication error " + e.toString();
Logger.error("Communication error", e);
} catch (Throwable e) {
status = "Error " + e.toString();
Logger.error("Error", e);
} finally {
isRunning = false;
IOUtils.closeQuietly(clientSession);
clientSession = null;
if (stoped) {
status = "Terminated";
}
}
}

public void shutdown() {
stoped = true;
if (clientSession != null) {
IOUtils.closeQuietly(clientSession);
clientSession = null;
}
}
}

Change log

r2607 by skarzhevskyy on Dec 17, 2008   Diff
OBEX tests in J2ME
Go to: 
Project members, sign in to write a code review

Older revisions

r2471 by skarzhevskyy on Nov 30, 2008   Diff
Change license to Apache License,
Version 2.0, Update headers
r2450 by skarzhevskyy on Nov 14, 2008   Diff
Implementation of
DiscoveryAgent.retrieveDevices on MS
stack
New interface functions for other
stacks.
r2408 by skarzhevskyy on Oct 9, 2008   Diff
organize product to modules
All revisions of this file

File info

Size: 5500 bytes, 189 lines

File properties

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