FileDocCategorySizeDatePackage
DdmServer.javaAPI DocAndroid 1.5 API5656Wed May 06 22:41:04 BST 2009org.apache.harmony.dalvik.ddmc

DdmServer

public class DdmServer extends Object
This represents our connection to the DDM Server.

Fields Summary
public static final int
CLIENT_PROTOCOL_VERSION
private static HashMap
mHandlerMap
private static final int
CONNECTED
private static final int
DISCONNECTED
private static volatile boolean
mRegistrationComplete
private static boolean
mRegistrationTimedOut
Constructors Summary
private DdmServer()
Don't instantiate; all members and methods are static.



                 
      
Methods Summary
private static voidbroadcast(int event)

        synchronized (mHandlerMap) {
            Collection values = mHandlerMap.values();
            Iterator iter = values.iterator();

            while (iter.hasNext()) {
                ChunkHandler handler = (ChunkHandler) iter.next();
                switch (event) {
                    case CONNECTED:
                        handler.connected();
                        break;
                    case DISCONNECTED:
                        handler.disconnected();
                        break;
                    default:
                        throw new UnsupportedOperationException();
                }
            }
        }
    
private static Chunkdispatch(int type, byte[] data, int offset, int length)

        ChunkHandler handler;

        synchronized (mHandlerMap) {
            /*
             * If registration hasn't completed, and we haven't timed out
             * waiting for it, wait a bit.
             */
            while (!mRegistrationComplete && !mRegistrationTimedOut) {
                //System.out.println("dispatch() waiting for reg");
                try {
                    mHandlerMap.wait(1000);     // 1.0 sec
                } catch (InterruptedException ie) {
                    continue;
                }

                if (!mRegistrationComplete) {
                    /* timed out, don't wait again */
                    System.out.println("DDM dispatch reg wait timeout");
                    mRegistrationTimedOut = true;
                }
            }

            handler = mHandlerMap.get(type);
        }
        //System.out.println(" dispatch cont");

        if (handler == null) {
            System.err.println("Can't dispatch DDM chunk "
                + Integer.toHexString(type) + ": no handler defined");
            return null;
        }

        Chunk chunk = new Chunk(type, data, offset, length);
        return handler.handleChunk(chunk);
    
private static native voidnativeSendChunk(int type, byte[] data, int offset, int length)

public static voidregisterHandler(int type, ChunkHandler handler)
Register an instance of the ChunkHandler class to handle a specific chunk type. Throws an exception if the type already has a handler registered.

        if (handler == null)
            throw new NullPointerException();

        synchronized (mHandlerMap) {
            if (mHandlerMap.get(type) != null)
                throw new RuntimeException("type " + Integer.toHexString(type)
                    + " already registered");

            mHandlerMap.put(type, handler);
        }
    
public static voidregistrationComplete()
The application must call here after it finishes registering handlers.

        // sync on mHandlerMap because it's convenient and makes a kind of
        // sense
        synchronized (mHandlerMap) {
            mRegistrationComplete = true;
            mHandlerMap.notifyAll();
        }
    
public static voidsendChunk(Chunk chunk)
Send a chunk of data to the DDM server. This takes the form of a JDWP "event", which does not elicit a response from the server. Use this for "unsolicited" chunks.

        nativeSendChunk(chunk.type, chunk.data, chunk.offset, chunk.length);
    
public static ChunkHandlerunregisterHandler(int type)
Unregister the existing handler for the specified type. Returns the old handler.

        synchronized (mHandlerMap) {
            return mHandlerMap.remove(type);
        }