libsysutils: Move to a null terminated string protocol using space as a field separator.

Also removes some debugging

Signed-off-by: San Mehat <san@google.com>
This commit is contained in:
San Mehat 2009-06-15 14:06:03 -07:00
parent 47c1d7344a
commit c73a3a5771
6 changed files with 25 additions and 29 deletions

View File

@ -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; }
};

View File

@ -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

View File

@ -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;

View File

@ -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;

View File

@ -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) {

View File

@ -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;