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

NotifierEmul

public class NotifierEmul extends Object implements EmulUnit
Emulates JSR 82 notifier.

Fields Summary
private int
handle
Integer handle that identifies notifier at native level.
private static com.sun.midp.security.SecurityToken
internalSecurityToken
Internal security token that grants access to restricted API.
DeviceEmul
device
Device this notifier works at.
com.sun.midp.io.j2me.serversocket.Socket
serverSocket
Server socet to accept connections.
static int
nextSocketPort
next free socket port.
private ServiceConnectionData
serviceData
Keeps options of service connection string used for this notifier creation.
int
port
PSM or channel id.
private ConnectionEmul
clientConnection
An emulation of client connection.
Acceptor
acceptor
Current connection acceptor, null if there is no one.
private static final byte
NOTIF_ACCEPT
Accept request code.
private static final byte
NOTIF_CLOSE
Close request code.
private static final byte
NOTIF_INIT
Initialize request code.
Constructors Summary
public NotifierEmul(int handle)
Creates new instance and registers it in emulation environment with given handle. It is initialized by the next request to emulation.

param
handle integer handle reserved for the new instance in native layer.

        this.handle = handle;
        // IMPL_NOTE redundant?
        device = DeviceEmul.getLocalDeviceEmul();
        
        // Registering this notifier as a processor for requests to emulation
        ConnRegistry.getInstance().register(handle, this);
    
Methods Summary
public voidclose()
Closes the notifier (emulation).

exception
IOException if emulation server does not respond properly

        if (acceptor != null) {
            acceptor.interrupt();
            acceptor = null;
        }
            
        if (serverSocket != null) {
            try {
                serverSocket.close();
            } catch (IOException e) {
                error();
            }
            
            serverSocket = null;
        }
        
        clientConnection = null;
        
        ConnRegistry.getInstance().unregister(handle);
        if (-1 != serviceData.socketPort) {
            device.unregisterService(serviceData.socketPort);
        }
    
private voiderror()
Rgisters error occued to pass it to code above the porting layer.

/*need revisit*/
public voidinitialize(ServiceConnectionData serviceData)
Initializes this notifier emulation unit.

param
serviceData packed info on service details

        this.serviceData = serviceData;        
        
        serverSocket = new Socket();
    
        success:
        {
            final int maxTrials = 32;
            for (int i = 0; i < maxTrials; i++, nextSocketPort++) {
                try {
                    serverSocket.open(nextSocketPort, internalSecurityToken);
                } catch (IOException e) {
                    // consider port is busy and just continue trials
                    continue;
                }
                
                serviceData.socketPort = nextSocketPort++;
                break success;
            }
            
            error();
        }
    
private native voidnotifyAccepted(int thisHandle, int connHandle, byte[] peerAddr, int receiveMTU, int transmitMTU)
Notifies porting layer on completing acception.

param
thisHandle
param
connHandle handle connection created as a result of acception, -1 if acception failed.
param
peerAddr bluetooth address of device connected.
param
receiveMTU established ReceiveMTU of accepted connection.
param
transmitMTU established TransmitMTU of accepted connection. IMPL_NOTE peerAddr may become useless when moving emul below porting layer completed; thisHandle may be retrieved thru KNI

public voidprocess(BytePack request)
Processes request from porting layer to emulation.

param
request packed request to process

    
                      
        
        switch(request.extract()) {
        case NOTIF_ACCEPT:
            Log.log("processing NOTIF_ACCEPT");
            startAccept();
            Log.log("processing NOTIF_ACCEPT done");
            break;
        case NOTIF_CLOSE:
            Log.log("processing NOTIF_CLOSE");
            close();
            Log.log("processing NOTIF_CLOSE done");
            break;
        case NOTIF_INIT:
            Log.log("processing NOTIF_INIT");
            initialize(new ServiceConnectionData(
                request.extractBytes(ServiceConnectionData.SERVER_DATA_SIZE),
                ServiceConnectionData.SERVER_DATA));
            Log.log("processing NOTIF_INIT done");
            break;
        default:
            throw new EmulationException("Unknown Notifier request");
        }
    
public voidstartAccept()
Starts accepting incoming connection. IMPL_NOTE it should completely substitute accept() when moving emul below porting layer completed

        acceptor = new Acceptor();
        acceptor.start();
        
        try {
            device.registerService(serviceData);
        
        } catch (IOException e) {
            error();
            // IMPL_NOTE 
            // add e details;
        }