ClientHandlerpublic class ClientHandler extends Object implements RunnableHandles communication with a client. |
Fields Summary |
---|
int | handleHandle that identifies corresponding data on server. | private static int | nextHandleCommon handlers counter. | byte[] | bluetoothAddressBluetooth address of device or device emulation client belongs to. | byte[] | ipAddressIf not null , IP address of computer client works at. | DeviceState | deviceStateKeeps device state if this handler handles device,
null otherwise. | Hashtable | deviceServicesif this handler handles device, then keeps services available at it,
else null . | private Messenger | messengerKeeps messenger that provides utilities for communicating with client. | private javax.microedition.io.SocketConnection | connectionKeeps socket connection to the server. | private InputStream | fromClientKeeps input stream from server. | private OutputStream | toClientKeeps output stream to server. |
Constructors Summary |
---|
ClientHandler(javax.microedition.io.SocketConnection connection)Constructs client handle to communicate with a client thru
given connection.
this.connection = connection;
fromClient = connection.openDataInputStream();
toClient = connection.openDataOutputStream();
handle = getNextHandle();
Thread t = new Thread(this);
t.start();
|
Methods Summary |
---|
private void | disconnect()Closes current socket connection.
try {
connection.close();
toClient.close();
fromClient.close();
} catch (IOException e) {
// ignoring
}
connection = null;
fromClient = null;
toClient = null;
| private static synchronized int | getNextHandle()Retrieves handle value for a new handler.
return ++nextHandle;
| public void | run()Implements Runnable , processes client's rquests.
Exception exception = null;
try {
loop:
while (true) {
messenger.receive(fromClient);
switch (messenger.getCode()) {
case Messenger.REGISTER_DEVICE:
ipAddress = messenger.getBytes();
deviceServices = new Hashtable();
// IMPL_NOTE: how it works against Push TCK tests?
// default device state: not discoverable
deviceState = new DeviceState();
bluetoothAddress = EmulationServer.getInstance().
registerDevice(deviceState);
messenger.sendBytes(toClient, Messenger.REGISTERED,
bluetoothAddress);
break;
case Messenger.UPDATE_DEVICE_STATE:
deviceState.update(messenger.getInt());
break;
case Messenger.REGISTER_SERVICE:
ServiceConnectionData serv = new ServiceConnectionData(
messenger.getBytes(),
ServiceConnectionData.SERVER_DATA);
serv.address = ipAddress;
ServiceKey key = EmulationServer.getInstance().
registerService(serv, bluetoothAddress);
if (!deviceServices.containsKey(key)) {
deviceServices.put(
new Integer(serv.socketPort), key);
}
break;
case Messenger.UNREGISTER_SERVICE:
key = (ServiceKey)deviceServices.
remove(new Integer(messenger.getInt()));
if (key != null) {
EmulationServer.getInstance().
unregisterService(key);
key = null;
}
break;
case Messenger.CONNECT_TO_SERVICE:
serv = EmulationServer.getInstance().connectToService(
new ServiceConnectionData(
messenger.getBytes(),
ServiceConnectionData.CONN_REQUEST_DATA));
messenger.sendBytes(toClient, Messenger.SERVICE_AT,
serv.toByteArray(
ServiceConnectionData.CONNECTION_DATA));
break;
case Messenger.START_INQUIRY:
InquiryResults results = EmulationServer.getInstance().
runInquiry(messenger.getInt(), bluetoothAddress);
messenger.sendBytes(toClient,
Messenger.INQUIRY_COMPLETED, results.toByteArray());
break;
case Messenger.DONE:
break loop;
default: throw new EmulationException("Unknown client request");
}
} // end of loop:
} catch (EmulationException ee) {
exception = ee;
} catch (IOException e) {
exception = e;
} finally {
if (exception != null) {
try {
messenger.send(toClient, Messenger.ERROR, exception.getMessage());
} catch (IOException e) {
// ignoring - stop handling anyway
}
}
if (deviceState != null) {
EmulationServer.getInstance().
unregisterDevice(bluetoothAddress);
Enumeration records = deviceServices.elements();
while (records.hasMoreElements()) {
EmulationServer.getInstance().unregisterService(
(ServiceKey)records.nextElement());
}
}
disconnect();
}
|
|