system/corennnnn
Révision | 12a26e310be16dd88422fb139ee4b27f8c8aecf5 (tree) |
---|---|
l'heure | 2009-06-16 06:40:15 |
Auteur | Android (Google) Code Review <android-gerrit@goog...> |
Commiter | Android (Google) Code Review |
Merge change 4226 into donut
* changes:
@@ -29,7 +29,7 @@ public: | ||
29 | 29 | FrameworkCommand(const char *cmd); |
30 | 30 | virtual ~FrameworkCommand() { } |
31 | 31 | |
32 | - virtual int runCommand(SocketClient *c, char *data) = 0; | |
32 | + virtual int runCommand(SocketClient *c, int argc, char **argv) = 0; | |
33 | 33 | |
34 | 34 | const char *getCommand() { return mCommand; } |
35 | 35 | }; |
@@ -22,6 +22,8 @@ | ||
22 | 22 | class SocketClient; |
23 | 23 | |
24 | 24 | class FrameworkListener : public SocketListener { |
25 | +public: | |
26 | + static const int CMD_ARGS_MAX = 8; | |
25 | 27 | private: |
26 | 28 | FrameworkCommandCollection *mCommands; |
27 | 29 |
@@ -34,6 +36,6 @@ protected: | ||
34 | 36 | virtual bool onDataAvailable(SocketClient *c); |
35 | 37 | |
36 | 38 | private: |
37 | - void dispatchCommand(SocketClient *c, char *cmd); | |
39 | + void dispatchCommand(SocketClient *c, char *data); | |
38 | 40 | }; |
39 | 41 | #endif |
@@ -25,7 +25,7 @@ FrameworkCommand::FrameworkCommand(const char *cmd) { | ||
25 | 25 | mCommand = cmd; |
26 | 26 | } |
27 | 27 | |
28 | -int FrameworkCommand::runCommand(SocketClient *c, char *data) { | |
28 | +int FrameworkCommand::runCommand(SocketClient *c, int argc, char **argv) { | |
29 | 29 | LOGW("Command %s has no run handler!", getCommand()); |
30 | 30 | errno = ENOSYS; |
31 | 31 | return -1; |
@@ -36,17 +36,14 @@ bool FrameworkListener::onDataAvailable(SocketClient *c) { | ||
36 | 36 | if ((len = read(c->getSocket(), buffer, sizeof(buffer) -1)) < 0) { |
37 | 37 | LOGE("read() failed (%s)", strerror(errno)); |
38 | 38 | return errno; |
39 | - } else if (!len) { | |
40 | - LOGW("Lost connection to client"); | |
39 | + } else if (!len) | |
41 | 40 | return false; |
42 | - } | |
43 | 41 | |
44 | 42 | int offset = 0; |
45 | 43 | int i; |
46 | 44 | |
47 | 45 | for (i = 0; i < len; i++) { |
48 | - if (buffer[i] == '\n') { | |
49 | - buffer[i] = '\0'; | |
46 | + if (buffer[i] == '\0') { | |
50 | 47 | dispatchCommand(c, buffer + offset); |
51 | 48 | offset = i + 1; |
52 | 49 | } |
@@ -58,13 +55,20 @@ void FrameworkListener::registerCmd(FrameworkCommand *cmd) { | ||
58 | 55 | mCommands->push_back(cmd); |
59 | 56 | } |
60 | 57 | |
61 | -void FrameworkListener::dispatchCommand(SocketClient *cli, char *cmd) { | |
62 | - char *next = cmd; | |
63 | - char *cm; | |
64 | - char *arg; | |
58 | +void FrameworkListener::dispatchCommand(SocketClient *cli, char *data) { | |
59 | + int argc; | |
60 | + char *argv[FrameworkListener::CMD_ARGS_MAX]; | |
61 | + | |
62 | + if (!index(data, '"')) { | |
63 | + char *next = data; | |
64 | + char *field; | |
65 | + int i; | |
65 | 66 | |
66 | - if (!(cm = strsep(&next, ":"))) { | |
67 | - cli->sendMsg(500, "Malformatted message", false); | |
67 | + for (i = 0; (i < FrameworkListener::CMD_ARGS_MAX) && | |
68 | + (argv[i] = strsep(&next, " ")); i++); | |
69 | + argc = i+1; | |
70 | + } else { | |
71 | + LOGD("blehhh not supported"); | |
68 | 72 | return; |
69 | 73 | } |
70 | 74 |
@@ -73,8 +77,8 @@ void FrameworkListener::dispatchCommand(SocketClient *cli, char *cmd) { | ||
73 | 77 | for (i = mCommands->begin(); i != mCommands->end(); ++i) { |
74 | 78 | FrameworkCommand *c = *i; |
75 | 79 | |
76 | - if (!strcmp(cm, c->getCommand())) { | |
77 | - if (c->runCommand(cli, next)) { | |
80 | + if (!strcmp(argv[0], c->getCommand())) { | |
81 | + if (c->runCommand(cli, argc, argv)) { | |
78 | 82 | LOGW("Handler '%s' error (%s)", c->getCommand(), strerror(errno)); |
79 | 83 | } |
80 | 84 | return; |
@@ -33,19 +33,10 @@ int SocketClient::sendMsg(const char *msg) { | ||
33 | 33 | return -1; |
34 | 34 | } |
35 | 35 | |
36 | - char *tmp; | |
37 | - const char *bp = msg; | |
38 | - | |
39 | - if (msg[strlen(msg)] != '\n') { | |
40 | - tmp = (char *) alloca(strlen(msg) + 1); | |
41 | - strcpy(tmp, msg); | |
42 | - strcat(tmp, "\n"); | |
43 | - bp = tmp; | |
44 | - } | |
45 | - | |
36 | + // Send the message including null character | |
46 | 37 | int rc = 0; |
47 | - const char *p = bp; | |
48 | - int brtw = strlen(bp); | |
38 | + const char *p = msg; | |
39 | + int brtw = strlen(msg) + 1; | |
49 | 40 | |
50 | 41 | pthread_mutex_lock(&mWriteMutex); |
51 | 42 | while(brtw) { |
@@ -157,7 +157,6 @@ void SocketListener::runListener() { | ||
157 | 157 | if (FD_ISSET(fd, &read_fds)) { |
158 | 158 | pthread_mutex_unlock(&mClientsLock); |
159 | 159 | if (!onDataAvailable(*it)) { |
160 | - LOGD("SocketListener closing client socket"); | |
161 | 160 | close(fd); |
162 | 161 | pthread_mutex_lock(&mClientsLock); |
163 | 162 | delete *it; |