FileDocCategorySizeDatePackage
NamsTestService.javaAPI DocphoneME MR2 API (J2ME)13331Wed May 02 18:00:04 BST 2007com.sun.midp.main

NamsTestService

public class NamsTestService extends Object implements Runnable, com.sun.midp.events.EventListener
A service for testing the Native AMS (nams). Listens on a socket and processes commands by calling native methods that invoke the native AMS API. Receives Native AMS callbacks and sends the information out through the socket.

Fields Summary
static final int
PORT
static final String
PFX
javax.microedition.io.ServerSocketConnection
serv
DataInputStream
in
PrintStream
out
boolean
reading
String[]
helpmsg
Help message strings.
Constructors Summary
NamsTestService(javax.microedition.io.ServerSocketConnection s, com.sun.midp.events.EventQueue eq)
Constructor for the test service. Starts the listener thread and registers itself as a listener for test events.

        serv = s;
        new Thread(this).start();

        eq.registerEventListener(EventTypes.TEST_EVENT, this);
    
Methods Summary
java.lang.Integercheck(java.lang.String[] argv, int rqd, int idx)
A simple argument checking function. Given an argument array argv, ensures that it is exactly rqd elements long, and then attempts to parse argument idx as an integer. If all of these are successful, the parsed integer value is returned in an Integer object. Otherwise, null is returned.

        if (argv.length != rqd) {
            reply("> ?");
            return null;
        }

        try {
            return Integer.valueOf(argv[idx]);
        } catch (NumberFormatException nfe) {
            reply("> ?");
            return null;
        }
    
private static native voidcleanup()
Cleans up the native portion of the NAMS Test Service.

static voidd(java.lang.String s)
Prints string s to stdout, prefixed by PFX.


                 
        
        System.out.print(PFX + s + "\n");
    
voidecho(java.lang.String[] argv, java.lang.String pfx, int argStart)

        String s = pfx + argv[argStart];
        for (int i = argStart+1; i < argv.length; i++) {
            s += " " + argv[i];
        }
        reply(s);
    
private java.lang.StringgetStateByValue(int state)
Converts a midlet state (including foreground/background states) value into readable string.

param
state state value to convert
return
a string representing the given state value

        /* IMPL_NOTE: see midpNativeAppManager.h for the definitions */
        final String[] stateStrings = {
            "MIDP_MIDLET_STATE_ACTIVE",
            "MIDP_MIDLET_STATE_PAUSED",
            "MIDP_MIDLET_STATE_DESTROYED",
            "MIDP_MIDLET_STATE_ERROR",
            "MIDP_DISPLAY_STATE_FOREGROUND",
            "MIDP_DISPLAY_STATE_BACKGROUND",
            "MIDP_DISPLAY_STATE_FOREGROUND_REQUEST",
            "MIDP_DISPLAY_STATE_BACKGROUND_REQUEST"
        };

        if (state >= 1 && state < stateStrings.length) {
            return stateStrings[state - 1];
        }

        return "unknown (" + state + ")";
    
public static voidinit(com.sun.midp.security.SecurityToken token, com.sun.midp.events.EventQueue eq)
Initializes the nams service. Sets up translation of native callbacks into events, opens up a socket, and creates the service.

        ServerSocketConnection serv;

        initialize(MIDletSuiteUtils.getIsolateId());

        try {
            Socket s = new Socket();
            s.open(PORT, token);
            serv = (ServerSocketConnection) s;
        } catch (Throwable t) {
            d("failed " + t.toString());
            t.printStackTrace();
            return;
        }

        new NamsTestService(serv, eq);
    
private static native voidinitialize(int isolateId)
Initializes the native portion of the NAMS Test Service.

param
isolateId the isolateId to which test events are sent

public booleanpreprocess(com.sun.midp.events.Event event, com.sun.midp.events.Event waitingEvent)
Preprocesses events. Does no preprocessing, so always returns true.


    // -------------------- interface EventListener --------------------

                  
          
        return true;
    
public voidprocess(com.sun.midp.events.Event event)
Processes test events. Decodes the event and sends the information out through the socket.

        NativeEvent e = (NativeEvent)event;
        int appId = e.intParam1;
        int callbackId = e.intParam2;
        String state = getStateByValue(e.intParam3);
        int reason = e.intParam4;
        String s;

        switch (callbackId) {
        case 0:         // background
            reply("> background, appId = " + appId + ", state = " + state +
                  ", reason = " + reason);
            break;
        case 1:         // foreground
            reply("> foreground, appId = " + appId + ", state = " + state +
                  ", reason = " + reason);
        case 2:         // state change
            reply("> state, appId = " + appId + ", state = " + state +
                  ", reason = " + reason);
            break;
        default:
            reply(
                "> callbackId=" + callbackId +
                " appId=" + appId +
                " state=" + state +
                " reason=" + reason);
            break;
        }
    
voidprocessLine(java.lang.String[] sa)
Processes a command and its arguments.

        if (sa.length == 0) {
            return;
        }

        echo(sa, "< ", 0);

        if ("quit".equals(sa[0])) {
            reading = false;
        } else if ("echo".equals(sa[0])) {
            // no need to do anything
            // if (sa.length > 1) {
            //    echo(sa, "> ", 1);
            // }
        } else if ("createstart".equals(sa[0])) {
            Integer app = check(sa, 4, 3);
            if (app != null) {
                int suiteId = MIDletSuite.UNUSED_SUITE_ID;
                try {
                    suiteId = Integer.parseInt(sa[1]);
                } catch (NumberFormatException nfe) {
                  // Intentionally ignored
                }
                NamsAPIWrapper.midletCreateStart(suiteId,
                    sa[2],
                    app.intValue());
            }
        } else if ("resume".equals(sa[0])) {
            Integer app = check(sa, 2, 1);
            if (app != null) {
                NamsAPIWrapper.midletResume(app.intValue());
            }
        } else if ("pause".equals(sa[0])) {
            Integer app = check(sa, 2, 1);
            if (app != null) {
                NamsAPIWrapper.midletPause(app.intValue());
            }
        } else if ("destroy".equals(sa[0])) {
            Integer app = check(sa, 3, 1);
            if (app != null) {
                int timeout = -1;
                try {
                    timeout = Integer.parseInt(sa[2]);
                } catch (NumberFormatException nfe) {
                  // Intentionally ignored
                }
                NamsAPIWrapper.midletDestroy(app.intValue(),
                    timeout);
            }
        } else if ("setfg".equals(sa[0])) {
            Integer app = check(sa, 2, 1);
            if (app != null) {
                NamsAPIWrapper.midletSetForeground(app.intValue());
            }
        } else if ("stop".equals(sa[0])) {
            NamsAPIWrapper.midpSystemStop();
        } else if ("help".equals(sa[0])) {
            for (int ii = 0; ii < helpmsg.length; ii++) {
                reply("> " + helpmsg[ii]);
            }
        } else {
            reply("> ?");
        }
    
voidreadLines()
Reads lines of input from the socket and processes them. Reads as long as the 'reading' boolean is true, then returns.

        StringBuffer sb = new StringBuffer(100);

        reading = true;
        while (reading) {
            int b;

            try {
                b = in.read();
            } catch (IOException ioe) {
                break;
            }

            if (b == -1 && sb.length() == 0) {
                break;
            }

            if (b == -1 || b == '\n") {
                int len = sb.length();
                if (len > 0 && sb.charAt(len-1) == '\r") {
                    sb.setLength(len-1);
                }
                processLine(tokenize(sb.toString()));
                sb.setLength(0);
            } else {
                sb.append((char)(b & 0xff));
            }
        }
    
voidreadSocket(javax.microedition.io.SocketConnection sock)
Opens input and output streams from a fresh connection, then processes input from the socket. Closes and cleans up after input processing completes.

        try {
            in = sock.openDataInputStream();
        } catch (IOException ioe) {
            d("input stream failed: " + ioe);
            try {
                sock.close();
            } catch (IOException ignored) { }
            return;
        }

        try {
            out = new PrintStream(sock.openDataOutputStream());
        } catch (IOException ioe) {
            d("output stream failed: " + ioe);

            try {
                in.close();
            } catch (IOException ignored) { }
            in = null;

            try {
                sock.close();
            } catch (IOException ignored) { }

            return;
        }

        out.println("> NAMS Test Service ready.");
        readLines();
        out.close();
        out = null;

        try {
            in.close();
        } catch (IOException ignored) { }
        in = null;

        try {
            sock.close();
        } catch (IOException ignored) { }
    
voidreply(java.lang.String s)

        d(s);

        // IMPL_NOTE not thread-safe
        // IMPL_NOTE if no connection, should buffer up events
        // and send them all out when a connection arrives

        if (out != null) {
            out.println(s);
        }
    
public voidrun()
The socket listener loop. Awaits a connection, processes requests from the connection, then goes back to waiting, forever.

        SocketConnection sock;

        while (true) {
            d("listening on port " + PORT);
            try {
                sock = (SocketConnection)serv.acceptAndOpen();
            } catch (IOException ioe) {
                d("accept failed: " + ioe);
                try {
                    serv.close();
                } catch (IOException ignored) { }
                return;
            }

            d("connected");
            readSocket(sock);
            d("disconnected");
        }
    
java.lang.String[]tokenize(java.lang.String st)
Tokenizes a string in a simple fashion. Given a line of input, returns an array of strings containing tokens. A token consists of a sequence of nonblank characters. Tokens are separated by one or more blanks.

        Vector vec = new Vector();
        int start = -1;
        int cur = 0;
        int len = st.length();

        while (true) {
            while (cur < len && st.charAt(cur) == ' ") {
                cur += 1;
            }

            start = cur;

            while (cur < len && st.charAt(cur) != ' ") {
                cur += 1;
            }

            if (start >= len) {
                break;
            }

            vec.addElement(st.substring(start, cur));
        }

        String[] arr = new String[vec.size()];
        vec.copyInto(arr);
        return arr;