FileDocCategorySizeDatePackage
ClientHandler.javaAPI DocphoneME MR2 API (J2ME)8344Wed May 02 18:00:30 BST 2007com.sun.midp.jsr82emul

ClientHandler

public class ClientHandler extends Object implements Runnable
Handles communication with a client.

Fields Summary
int
handle
Handle that identifies corresponding data on server.
private static int
nextHandle
Common handlers counter.
byte[]
bluetoothAddress
Bluetooth address of device or device emulation client belongs to.
byte[]
ipAddress
If not null, IP address of computer client works at.
DeviceState
deviceState
Keeps device state if this handler handles device, null otherwise.
Hashtable
deviceServices
if this handler handles device, then keeps services available at it, else null.
private Messenger
messenger
Keeps messenger that provides utilities for communicating with client.
private javax.microedition.io.SocketConnection
connection
Keeps socket connection to the server.
private InputStream
fromClient
Keeps input stream from server.
private OutputStream
toClient
Keeps output stream to server.
Constructors Summary
ClientHandler(javax.microedition.io.SocketConnection connection)
Constructs client handle to communicate with a client thru given connection.

param
connection an open connection with a client on the other side
exception
IOException if connection fails

        this.connection = connection;
        fromClient = connection.openDataInputStream();
        toClient = connection.openDataOutputStream();
        handle = getNextHandle();
        
        Thread t = new Thread(this);
        t.start();
    
Methods Summary
private voiddisconnect()
Closes current socket connection.

        try { 
            connection.close();
            toClient.close();
            fromClient.close();
        } catch (IOException e) {
            // ignoring
        }
        
        connection = null;
        fromClient = null;
        toClient = null;
    
private static synchronized intgetNextHandle()
Retrieves handle value for a new handler.

return
new free handle value

    
                       
         
        return ++nextHandle;
    
public voidrun()
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();
        }