DdmHandleHello.java revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.ddm;
18
19import org.apache.harmony.dalvik.ddmc.Chunk;
20import org.apache.harmony.dalvik.ddmc.ChunkHandler;
21import org.apache.harmony.dalvik.ddmc.DdmServer;
22import android.util.Config;
23import android.util.Log;
24import android.os.Debug;
25
26import java.nio.ByteBuffer;
27
28/**
29 * Handle a HELO chunk.
30 */
31public class DdmHandleHello extends ChunkHandler {
32
33    public static final int CHUNK_HELO = type("HELO");
34    public static final int CHUNK_WAIT = type("WAIT");
35
36    private static DdmHandleHello mInstance = new DdmHandleHello();
37
38
39    /* singleton, do not instantiate */
40    private DdmHandleHello() {}
41
42    /**
43     * Register for the messages we're interested in.
44     */
45    public static void register() {
46        DdmServer.registerHandler(CHUNK_HELO, mInstance);
47    }
48
49    /**
50     * Called when the DDM server connects.  The handler is allowed to
51     * send messages to the server.
52     */
53    public void connected() {
54        if (Config.LOGV)
55            Log.v("ddm-hello", "Connected!");
56
57        if (true) {
58            /* test spontaneous transmission */
59            byte[] data = new byte[] { 0, 1, 2, 3, 4, -4, -3, -2, -1, 127 };
60            Chunk testChunk =
61                new Chunk(ChunkHandler.type("TEST"), data, 1, data.length-2);
62            DdmServer.sendChunk(testChunk);
63        }
64    }
65
66    /**
67     * Called when the DDM server disconnects.  Can be used to disable
68     * periodic transmissions or clean up saved state.
69     */
70    public void disconnected() {
71        if (Config.LOGV)
72            Log.v("ddm-hello", "Disconnected!");
73    }
74
75    /**
76     * Handle a chunk of data.  We're only registered for "HELO".
77     */
78    public Chunk handleChunk(Chunk request) {
79        if (Config.LOGV)
80            Log.v("ddm-hello", "Handling " + name(request.type) + " chunk");
81
82        if (false)
83            return createFailChunk(123, "This is a test");
84
85        /*
86         * Process the request.
87         */
88        ByteBuffer in = wrapChunk(request);
89
90        int serverProtoVers = in.getInt();
91        if (Config.LOGV)
92            Log.v("ddm-hello", "Server version is " + serverProtoVers);
93
94        /*
95         * Create a response.
96         */
97        String vmName = System.getProperty("java.vm.name", "?");
98        String vmVersion = System.getProperty("java.vm.version", "?");
99        String vmIdent = vmName + " v" + vmVersion;
100
101        //String appName = android.app.ActivityThread.currentPackageName();
102        //if (appName == null)
103        //    appName = "unknown";
104        String appName = DdmHandleAppName.getAppName();
105
106        ByteBuffer out = ByteBuffer.allocate(16
107                            + vmIdent.length()*2 + appName.length()*2);
108        out.order(ChunkHandler.CHUNK_ORDER);
109        out.putInt(DdmServer.CLIENT_PROTOCOL_VERSION);
110        out.putInt(android.os.Process.myPid());
111        out.putInt(vmIdent.length());
112        out.putInt(appName.length());
113        putString(out, vmIdent);
114        putString(out, appName);
115
116        Chunk reply = new Chunk(CHUNK_HELO, out);
117
118        /*
119         * Take the opportunity to inform DDMS if we are waiting for a
120         * debugger to attach.
121         */
122        if (Debug.waitingForDebugger())
123            sendWAIT(0);
124
125        return reply;
126    }
127
128    /**
129     * Send up a WAIT chunk.  The only currently defined value for "reason"
130     * is zero, which means "waiting for a debugger".
131     */
132    public static void sendWAIT(int reason) {
133        byte[] data = new byte[] { (byte) reason };
134        Chunk waitChunk = new Chunk(CHUNK_WAIT, data, 0, 1);
135        DdmServer.sendChunk(waitChunk);
136    }
137}
138
139