From c73a3a5771a2d29d1bae666bfde12f751d66fc96 Mon Sep 17 00:00:00 2001 From: San Mehat Date: Mon, 15 Jun 2009 14:06:03 -0700 Subject: [PATCH] libsysutils: Move to a null terminated string protocol using space as a field separator. Also removes some debugging Signed-off-by: San Mehat --- include/sysutils/FrameworkCommand.h | 2 +- include/sysutils/FrameworkListener.h | 4 +++- libsysutils/src/FrameworkCommand.cpp | 2 +- libsysutils/src/FrameworkListener.cpp | 30 +++++++++++++++------------ libsysutils/src/SocketClient.cpp | 15 +++----------- libsysutils/src/SocketListener.cpp | 1 - 6 files changed, 25 insertions(+), 29 deletions(-) diff --git a/include/sysutils/FrameworkCommand.h b/include/sysutils/FrameworkCommand.h index 5b50247b5..6c1fca620 100644 --- a/include/sysutils/FrameworkCommand.h +++ b/include/sysutils/FrameworkCommand.h @@ -29,7 +29,7 @@ public: FrameworkCommand(const char *cmd); virtual ~FrameworkCommand() { } - virtual int runCommand(SocketClient *c, char *data) = 0; + virtual int runCommand(SocketClient *c, int argc, char **argv) = 0; const char *getCommand() { return mCommand; } }; diff --git a/include/sysutils/FrameworkListener.h b/include/sysutils/FrameworkListener.h index 8a83c3346..4e3d39648 100644 --- a/include/sysutils/FrameworkListener.h +++ b/include/sysutils/FrameworkListener.h @@ -22,6 +22,8 @@ class SocketClient; class FrameworkListener : public SocketListener { +public: + static const int CMD_ARGS_MAX = 8; private: FrameworkCommandCollection *mCommands; @@ -34,6 +36,6 @@ protected: virtual bool onDataAvailable(SocketClient *c); private: - void dispatchCommand(SocketClient *c, char *cmd); + void dispatchCommand(SocketClient *c, char *data); }; #endif diff --git a/libsysutils/src/FrameworkCommand.cpp b/libsysutils/src/FrameworkCommand.cpp index 94e7426ca..c52eac76f 100644 --- a/libsysutils/src/FrameworkCommand.cpp +++ b/libsysutils/src/FrameworkCommand.cpp @@ -25,7 +25,7 @@ FrameworkCommand::FrameworkCommand(const char *cmd) { mCommand = cmd; } -int FrameworkCommand::runCommand(SocketClient *c, char *data) { +int FrameworkCommand::runCommand(SocketClient *c, int argc, char **argv) { LOGW("Command %s has no run handler!", getCommand()); errno = ENOSYS; return -1; diff --git a/libsysutils/src/FrameworkListener.cpp b/libsysutils/src/FrameworkListener.cpp index e8ae8478e..e9ca8977f 100644 --- a/libsysutils/src/FrameworkListener.cpp +++ b/libsysutils/src/FrameworkListener.cpp @@ -36,17 +36,14 @@ bool FrameworkListener::onDataAvailable(SocketClient *c) { if ((len = read(c->getSocket(), buffer, sizeof(buffer) -1)) < 0) { LOGE("read() failed (%s)", strerror(errno)); return errno; - } else if (!len) { - LOGW("Lost connection to client"); + } else if (!len) return false; - } int offset = 0; int i; for (i = 0; i < len; i++) { - if (buffer[i] == '\n') { - buffer[i] = '\0'; + if (buffer[i] == '\0') { dispatchCommand(c, buffer + offset); offset = i + 1; } @@ -58,13 +55,20 @@ void FrameworkListener::registerCmd(FrameworkCommand *cmd) { mCommands->push_back(cmd); } -void FrameworkListener::dispatchCommand(SocketClient *cli, char *cmd) { - char *next = cmd; - char *cm; - char *arg; +void FrameworkListener::dispatchCommand(SocketClient *cli, char *data) { + int argc; + char *argv[FrameworkListener::CMD_ARGS_MAX]; - if (!(cm = strsep(&next, ":"))) { - cli->sendMsg(500, "Malformatted message", false); + if (!index(data, '"')) { + char *next = data; + char *field; + int i; + + for (i = 0; (i < FrameworkListener::CMD_ARGS_MAX) && + (argv[i] = strsep(&next, " ")); i++); + argc = i+1; + } else { + LOGD("blehhh not supported"); return; } @@ -73,8 +77,8 @@ void FrameworkListener::dispatchCommand(SocketClient *cli, char *cmd) { for (i = mCommands->begin(); i != mCommands->end(); ++i) { FrameworkCommand *c = *i; - if (!strcmp(cm, c->getCommand())) { - if (c->runCommand(cli, next)) { + if (!strcmp(argv[0], c->getCommand())) { + if (c->runCommand(cli, argc, argv)) { LOGW("Handler '%s' error (%s)", c->getCommand(), strerror(errno)); } return; diff --git a/libsysutils/src/SocketClient.cpp b/libsysutils/src/SocketClient.cpp index f0e846f4f..857ed4d10 100644 --- a/libsysutils/src/SocketClient.cpp +++ b/libsysutils/src/SocketClient.cpp @@ -33,19 +33,10 @@ int SocketClient::sendMsg(const char *msg) { return -1; } - char *tmp; - const char *bp = msg; - - if (msg[strlen(msg)] != '\n') { - tmp = (char *) alloca(strlen(msg) + 1); - strcpy(tmp, msg); - strcat(tmp, "\n"); - bp = tmp; - } - + // Send the message including null character int rc = 0; - const char *p = bp; - int brtw = strlen(bp); + const char *p = msg; + int brtw = strlen(msg) + 1; pthread_mutex_lock(&mWriteMutex); while(brtw) { diff --git a/libsysutils/src/SocketListener.cpp b/libsysutils/src/SocketListener.cpp index 1f80121a5..1a937c296 100644 --- a/libsysutils/src/SocketListener.cpp +++ b/libsysutils/src/SocketListener.cpp @@ -157,7 +157,6 @@ void SocketListener::runListener() { if (FD_ISSET(fd, &read_fds)) { pthread_mutex_unlock(&mClientsLock); if (!onDataAvailable(*it)) { - LOGD("SocketListener closing client socket"); close(fd); pthread_mutex_lock(&mClientsLock); delete *it;