FileDocCategorySizeDatePackage
EmulationClient.javaAPI DocphoneME MR2 API (J2ME)5764Wed May 02 18:00:32 BST 2007com.sun.midp.jsr82emul

EmulationClient

public class EmulationClient extends Object
Represents a client for JSR 82 emulation environment. It is not a part of JSR 82 implementation and is only used within JSR 82 emulation mode. The emulation mode allows running tests without real native Bluetooth libraries or hardware. In the emulation mode the client runs under J2ME VM control and connects thru a socket to a server which runs somewhere on the Internet under J2SE VM control. A client represents a Bluetooth local or remote device, connection or anything that requires Bluetooth ether communication. A server in turn represents Bluetooth ether.

Fields Summary
protected static com.sun.midp.security.SecurityToken
internalSecurityToken
Internal security token that grants access to restricted API.
private static final int
EMUL_PORT
Keeps default port number for server connections.
private static String
serverIP
IP address of EmulationServer.
protected Messenger
messenger
Keeps messenger that proveds utilities for communicating with server.
protected javax.microedition.io.SocketConnection
connection
Keeps socket connection to the server.
protected InputStream
fromServer
Keeps input stream from server.
protected OutputStream
toServer
Keeps output stream to server.
Constructors Summary
protected EmulationClient()
Creates an instance.

  
            
      
Methods Summary
protected voidconnect()
Opens socket connection to current host at current port.

exception
IOException if there is an open connection or en error ocurred while trying to connect.

        if (connection != null) {
            throw new IOException("connection is already open");
        }
        
        connection = (SocketConnection) 
            new com.sun.midp.io.j2me.socket.Protocol().openPrim(
                internalSecurityToken,
                "//" + getServer() + ":" + EMUL_PORT);
                
        fromServer = connection.openDataInputStream();
        toServer = connection.openDataOutputStream();
    
protected voiddisconnect()
Closes current socket connection.

exception
IOException if an error occured or ocurred.

        if (connection != null) {
            messenger.send(toServer, Messenger.DONE, "");
            
            toServer.close();
            fromServer.close();
            connection.close();
            
            toServer = null;
            fromServer = null;
            connection = null;
        }
    
static native java.lang.StringgetLocalIP()
Retrieves local IP address.

return
String object containing local IP address, null if retrieving failed.

private static synchronized java.lang.StringgetServer()
Retrieves emulation server IP address, launches server if needed.

return
emulation server IP address.

        if (serverIP != null) {
            return serverIP;
        }
        
        serverIP = getServerIP();
        
        // Local IP of emulation server means that it is possibly
        // expected that current application will launch the server.
        if (serverIP == null || serverIP.length() == 0 
            || serverIP.equals("127.0.0.1")) {
            EmulationServer.launch();
            serverIP = "localhost";
        } else if (serverIP.equals(getLocalIP())) {
            EmulationServer.launch();
        }
        
        return serverIP;
    
private static native java.lang.StringgetServerIP()
Retrieves port of emulation server.

return
EmulationServer IP address from JSR82_EMUL_IP environment variable, null if the variable is not defined.