Home | History | Annotate | Download | only in wds
      1 /*
      2  * Copyright 2008, The Android Open Source Project
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  *  * Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  *  * Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #define LOG_TAG "wds"
     27 #include "config.h"
     28 
     29 #include "Command.h"
     30 #include "Connection.h"
     31 #include "DebugServer.h"
     32 #include "wtf/MainThread.h"
     33 #include "wtf/Threading.h"
     34 #include <arpa/inet.h>
     35 #include <cutils/properties.h>
     36 #include <errno.h>
     37 #include <string.h>
     38 #include <sys/socket.h>
     39 #include <sys/types.h>
     40 #include <utils/Log.h>
     41 
     42 #if ENABLE(WDS)
     43 
     44 #define DEFAULT_PORT 9999
     45 #define log_errno(x) ALOGE("%s: %d", x, strerror(errno))
     46 
     47 namespace android {
     48 
     49 namespace WDS {
     50 
     51 static DebugServer* s_server = NULL;
     52 
     53 // Main thread function for createThread
     54 static void* mainThread(void* v) {
     55     DebugServer* server = static_cast<DebugServer*>(v);
     56     server->start();
     57     delete server;
     58     s_server = NULL;
     59     return NULL;
     60 }
     61 
     62 DebugServer* server() {
     63     if (s_server == NULL)
     64         s_server = new DebugServer();
     65     return s_server;
     66 }
     67 
     68 DebugServer::DebugServer() {
     69     // Read webcore.wds.enable to determine if the debug server should run
     70     char buf[PROPERTY_VALUE_MAX];
     71     int ret = property_get("webcore.wds.enable", buf, NULL);
     72     if (ret != -1 && strcmp(buf, "1") == 0) {
     73         ALOGD("WDS Enabled");
     74         m_threadId = createThread(mainThread, this, "WDS");
     75     }
     76     // Initialize the available commands.
     77     Command::Init();
     78 }
     79 
     80 void DebugServer::start() {
     81     ALOGD("DebugServer thread started");
     82 
     83     ConnectionServer cs;
     84     if (!cs.connect(DEFAULT_PORT)) {
     85         ALOGE("Failed to start the server socket connection");
     86         return;
     87     }
     88 
     89     while (true ) {
     90         ALOGD("Waiting for incoming connections...");
     91         Connection* conn = cs.accept();
     92         if (!conn) {
     93             log_errno("Failed to accept new connections");
     94             return;
     95         }
     96         ALOGD("...Connection established");
     97 
     98         Command* c = Command::Find(conn);
     99         if (!c) {
    100             ALOGE("Could not find matching command");
    101             delete conn;
    102         } else {
    103             // Dispatch the command, it will handle cleaning up the connection
    104             // when finished.
    105             c->dispatch();
    106         }
    107     }
    108 
    109     ALOGD("DebugServer thread finished");
    110 }
    111 
    112 } // end namespace WDS
    113 
    114 } // end namespace android
    115 
    116 #endif
    117