FileDocCategorySizeDatePackage
Protocol.javaAPI DocphoneME MR2 API (J2ME)7924Wed May 02 18:00:30 BST 2007com.sun.midp.io.j2me.tcpobex

Protocol

public class Protocol extends Object implements com.sun.cldc.io.ConnectionBaseInterface
Provides a wrapper for "tcpobex" protocol implementation to answer the GCF style.

Fields Summary
private static final boolean
DEBUG
Set to false in RR version - then the javac skip the code.
private static final String
cn
DEBUG: this class name for debug.
private boolean
clientPermitted
Shows whether slient permissions checked.
private boolean
serverPermitted
Shows whether server permissions checked.
private static com.sun.midp.security.SecurityToken
classSecurityToken
This class has a different security domain than the MIDlet suite.
protected String
origName
Connection url for netmon.
Constructors Summary
public Protocol()
Required for instantation via reflection.


              
      
Methods Summary
private voidcheckForPermission(int permission, java.lang.String name)
Makes sure caller has the com.sun.midp permission set to "allowed".

param
permission requested permission ID
param
name resource name to check permissions against
exception
IOInterruptedException if another thread interrupts the calling thread while this method is waiting to preempt the display.

        MIDletSuite midletSuite =
            MIDletStateHandler.getMidletStateHandler().getMIDletSuite();

        if (midletSuite != null) {
            try {
                midletSuite.checkForPermission(permission, name);
            } catch (InterruptedException ie) {
                throw new InterruptedIOException(
                    "Interrupted while trying to ask the user permission");
            }
        }
    
protected TCPOBEXConnectioncreateTransportConnection(java.lang.String host, int port)
Create tcp obex transport connection.

param
host Target host name (ip address).
param
port Target's port to connect to.
return
TCPOBEXConnection instance.


	    if (this.getClass() != Protocol.class) {
	        throw new SecurityException(
		    "Illegal Access to tcpobex implementation");
	    }

        return new TCPOBEXConnection(classSecurityToken, host, port);
    
protected TCPOBEXNotifiercreateTransportNotifier(int port)
Create tcp obex transport notifier.

param
port The server's port number to listen on.
return
TCPOBEXNotifier instance.


	    if (this.getClass() != Protocol.class) {
	        throw new SecurityException(
		    "Illegal Access to tcpobex implementation");
	    }

        return new TCPOBEXNotifier(classSecurityToken, port);
    
public javax.microedition.io.ConnectionopenPrim(java.lang.String name, int mode, boolean timeouts)
Creates the tcpsocket connection or notifier.

param
name The URL for the connection.
param
mode OBEX supports READ_WRITE mode only.
param
timeouts A flag to indicate that the caller (ignored).
return
ClientSession for client url or SessionNotifier for server url.


        // print what do we have here
        if (DEBUG) {
            System.out.println(cn + ":name = " + name + " mode = " + mode
                    + " timeouts = " + timeouts);
        }
        final String errorMsg = "Malformed URL: tcpobex:" + name;
        origName = "tcpobex:" + name;

        // this implementation supports READ_WRITE mode only
        if (mode != Connector.READ_WRITE) {
            throw new IllegalArgumentException("Unsupported mode: " + mode);
        }

        // do the URL parsing here
        if (!name.startsWith("//")) {
            throw new IllegalArgumentException(errorMsg);
        }

        /**
         * JSR82 Specification 1.0a contains two contradicting forms of
         * tcpobex server url. See 11.4.2 and 11.4.4. Supporting only
         * for described in example:pIndex
         *     tcpobex://:port
         *
         * The possible URL values are:
         *
         *     tcpobex://               - server on default port (650)
         *     tcpobex://:port          - server on specified port
         *     tcpobex://<name/ip>      - client on default port
         *     tcpobex://<name/ip>:port - client on specified port
         *
         */
        int pIndex = name.indexOf(':");
        boolean isServer = name.length() == 2 || pIndex == 2;
        int port = 650;

        // extract the port if specified
        if (pIndex != -1) {
            if (pIndex == name.length() - 1) {
                throw new IllegalArgumentException(errorMsg);
            }

            try {
                port = Integer.parseInt(name.substring(pIndex + 1));

                if (port <= 0) {
                    throw new NumberFormatException();
                }
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException(errorMsg);
            }
        }

        // server connection on specified port
        if (isServer) {

            // check for server permissions
            if (!serverPermitted) {
                checkForPermission(Permissions.TCP_OBEX_SERVER, name);
                serverPermitted = true;
            }
            return new SessionNotifierImpl(createTransportNotifier(port));
        }

        // check for client permissions
        if (!clientPermitted) {
            checkForPermission(Permissions.TCP_OBEX_CLIENT, name);
            clientPermitted = true;
        }

        // get the client target host name (ip address)
        pIndex = pIndex == -1 ? name.length() : pIndex;
        String host = name.substring(2, pIndex);
        return new ClientSessionImpl(createTransportConnection(host, port));