summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSan Mehat <san@google.com>2009-05-12 11:16:59 -0700
committerSan Mehat <san@google.com>2009-05-12 14:02:32 -0700
commitd768066ef54270a0d3ccfccd50ae8238db5a2cdd (patch)
treeeea4b978f43fad98d561ef97a7fec0d9d502ac14
parentc73d9e43a0c98a87222ef0c8749d6abba06c7778 (diff)
downloadsystem_core-d768066ef54270a0d3ccfccd50ae8238db5a2cdd.zip
system_core-d768066ef54270a0d3ccfccd50ae8238db5a2cdd.tar.gz
system_core-d768066ef54270a0d3ccfccd50ae8238db5a2cdd.tar.bz2
libsysutils: Tweak SocketListener and friends
Signed-off-by: San Mehat <san@google.com>
-rw-r--r--include/sysutils/SocketClient.h2
-rw-r--r--include/sysutils/SocketListener.h2
-rw-r--r--libsysutils/src/FrameworkListener.cpp20
-rw-r--r--libsysutils/src/SocketClient.cpp55
-rw-r--r--libsysutils/src/SocketListener.cpp9
5 files changed, 56 insertions, 32 deletions
diff --git a/include/sysutils/SocketClient.h b/include/sysutils/SocketClient.h
index 39ec7ae..cde64a5 100644
--- a/include/sysutils/SocketClient.h
+++ b/include/sysutils/SocketClient.h
@@ -15,8 +15,8 @@ public:
int getSocket() { return mSocket; }
+ int sendMsg(int code, char *msg, bool addErrno);
int sendMsg(char *msg);
- int sendMsg(char *msg, char *data);
};
typedef android::List<SocketClient *> SocketClientCollection;
diff --git a/include/sysutils/SocketListener.h b/include/sysutils/SocketListener.h
index be97421..30d050d 100644
--- a/include/sysutils/SocketListener.h
+++ b/include/sysutils/SocketListener.h
@@ -37,8 +37,8 @@ public:
int startListener();
int stopListener();
+ void sendBroadcast(int code, char *msg, bool addErrno);
void sendBroadcast(char *msg);
- void sendBroadcast(char *msg, char *data);
protected:
virtual bool onDataAvailable(SocketClient *c) = 0;
diff --git a/libsysutils/src/FrameworkListener.cpp b/libsysutils/src/FrameworkListener.cpp
index 9210ca5..2a96e9a 100644
--- a/libsysutils/src/FrameworkListener.cpp
+++ b/libsysutils/src/FrameworkListener.cpp
@@ -30,7 +30,7 @@ FrameworkListener::FrameworkListener(const char *socketName) :
}
bool FrameworkListener::onDataAvailable(SocketClient *c) {
- char buffer[101];
+ char buffer[255];
int len;
if ((len = read(c->getSocket(), buffer, sizeof(buffer) -1)) < 0) {
@@ -41,15 +41,14 @@ bool FrameworkListener::onDataAvailable(SocketClient *c) {
return false;
}
- int start = 0;
+ int offset = 0;
int i;
- buffer[len] = '\0';
-
for (i = 0; i < len; i++) {
- if (buffer[i] == '\0') {
- dispatchCommand(c, buffer + start);
- start = i + 1;
+ if (buffer[i] == '\n') {
+ buffer[i] = '\0';
+ dispatchCommand(c, buffer + offset);
+ offset = i + 1;
}
}
return true;
@@ -60,11 +59,11 @@ void FrameworkListener::registerCmd(FrameworkCommand *cmd) {
}
void FrameworkListener::dispatchCommand(SocketClient *cli, char *cmd) {
-
+ LOGD("Dispatching '%s'", cmd);
char *cm, *last;
if (!(cm = strtok_r(cmd, ":", &last))) {
- cli->sendMsg("BAD_MSG");
+ cli->sendMsg(500, "Malformatted message", false);
return;
}
@@ -81,7 +80,6 @@ void FrameworkListener::dispatchCommand(SocketClient *cli, char *cmd) {
}
}
- LOGE("No cmd handlers defined for '%s'", cmd);
- cli->sendMsg("UNKNOWN_CMD");
+ cli->sendMsg(500, "Command not recognized", false);
return;
}
diff --git a/libsysutils/src/SocketClient.cpp b/libsysutils/src/SocketClient.cpp
index 6db62b3..930fa4c 100644
--- a/libsysutils/src/SocketClient.cpp
+++ b/libsysutils/src/SocketClient.cpp
@@ -2,6 +2,7 @@
#include <errno.h>
#include <sys/types.h>
#include <pthread.h>
+#include <string.h>
#define LOG_TAG "SocketClient"
#include <cutils/log.h>
@@ -13,29 +14,55 @@ SocketClient::SocketClient(int socket) {
pthread_mutex_init(&mWriteMutex, NULL);
}
+int SocketClient::sendMsg(int code, char *msg, bool addErrno) {
+ char *buf;
+
+ if (addErrno) {
+ buf = (char *) alloca(strlen(msg) + strlen(strerror(errno)) + 8);
+ sprintf(buf, "%.3d %s (%s)", code, msg, strerror(errno));
+ } else {
+ buf = (char *) alloca(strlen(msg) + strlen("XXX "));
+ sprintf(buf, "%.3d %s", code, msg);
+ }
+ return sendMsg(buf);
+}
+
int SocketClient::sendMsg(char *msg) {
LOGD("SocketClient::sendMsg(%s)", msg);
+
if (mSocket < 0) {
errno = EHOSTUNREACH;
return -1;
}
+ char *bp;
+
+ if (msg[strlen(msg)] != '\n') {
+ bp = (char *) alloca(strlen(msg) + 1);
+ strcpy(bp, msg);
+ strcat(bp, "\n");
+ } else
+ bp = msg;
+
+ int rc = 0;
+ char *p = bp;
+ int brtw = strlen(bp);
+
pthread_mutex_lock(&mWriteMutex);
- if (write(mSocket, msg, strlen(msg) +1) < 0) {
- LOGW("Unable to send msg '%s' (%s)", msg, strerror(errno));
+ while(brtw) {
+ if ((rc = write(mSocket,p, brtw)) < 0) {
+ LOGW("Unable to send msg '%s' (%s)", msg, strerror(errno));
+ pthread_mutex_unlock(&mWriteMutex);
+ return -1;
+ } else if (!rc) {
+ LOGW("0 length write :(");
+ errno = EIO;
+ pthread_mutex_unlock(&mWriteMutex);
+ return -1;
+ }
+ p += rc;
+ brtw -= rc;
}
pthread_mutex_unlock(&mWriteMutex);
return 0;
}
-
-int SocketClient::sendMsg(char *msg, char *data) {
- char *buffer = (char *) alloca(strlen(msg) + strlen(data) + 1);
- if (!buffer) {
- errno = -ENOMEM;
- return -1;
- }
- strcpy(buffer, msg);
- strcat(buffer, data);
- return sendMsg(buffer);
-}
-
diff --git a/libsysutils/src/SocketListener.cpp b/libsysutils/src/SocketListener.cpp
index cb69bfd..57f31f1 100644
--- a/libsysutils/src/SocketListener.cpp
+++ b/libsysutils/src/SocketListener.cpp
@@ -186,27 +186,26 @@ void SocketListener::runListener() {
}
}
-void SocketListener::sendBroadcast(char *msg) {
+void SocketListener::sendBroadcast(int code, char *msg, bool addErrno) {
pthread_mutex_lock(&mClientsLock);
SocketClientCollection::iterator i;
for (i = mClients->begin(); i != mClients->end(); ++i) {
- if ((*i)->sendMsg(msg)) {
+ if ((*i)->sendMsg(code, msg, addErrno)) {
LOGW("Error sending broadcast (%s)", strerror(errno));
}
}
pthread_mutex_unlock(&mClientsLock);
}
-void SocketListener::sendBroadcast(char *msg, char *data) {
+void SocketListener::sendBroadcast(char *msg) {
pthread_mutex_lock(&mClientsLock);
SocketClientCollection::iterator i;
for (i = mClients->begin(); i != mClients->end(); ++i) {
- if ((*i)->sendMsg(msg, data)) {
+ if ((*i)->sendMsg(msg)) {
LOGW("Error sending broadcast (%s)", strerror(errno));
}
}
pthread_mutex_unlock(&mClientsLock);
}
-