Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c73a3a57 authored by San Mehat's avatar San Mehat
Browse files

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



    Also removes some debugging

Signed-off-by: default avatarSan Mehat <san@google.com>
parent 47c1d734
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -29,7 +29,7 @@ public:
    FrameworkCommand(const char *cmd);
    FrameworkCommand(const char *cmd);
    virtual ~FrameworkCommand() { }
    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; }
    const char *getCommand() { return mCommand; }
};
};
+3 −1
Original line number Original line Diff line number Diff line
@@ -22,6 +22,8 @@
class SocketClient;
class SocketClient;


class FrameworkListener : public SocketListener {
class FrameworkListener : public SocketListener {
public:
    static const int CMD_ARGS_MAX = 8;
private:
private:
    FrameworkCommandCollection *mCommands;
    FrameworkCommandCollection *mCommands;


@@ -34,6 +36,6 @@ protected:
    virtual bool onDataAvailable(SocketClient *c);
    virtual bool onDataAvailable(SocketClient *c);


private:
private:
    void dispatchCommand(SocketClient *c, char *cmd);
    void dispatchCommand(SocketClient *c, char *data);
};
};
#endif
#endif
+1 −1
Original line number Original line Diff line number Diff line
@@ -25,7 +25,7 @@ FrameworkCommand::FrameworkCommand(const char *cmd) {
    mCommand = 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());
    LOGW("Command %s has no run handler!", getCommand());
    errno = ENOSYS;
    errno = ENOSYS;
    return -1;
    return -1;
+17 −13
Original line number Original line Diff line number Diff line
@@ -36,17 +36,14 @@ bool FrameworkListener::onDataAvailable(SocketClient *c) {
    if ((len = read(c->getSocket(), buffer, sizeof(buffer) -1)) < 0) {
    if ((len = read(c->getSocket(), buffer, sizeof(buffer) -1)) < 0) {
        LOGE("read() failed (%s)", strerror(errno));
        LOGE("read() failed (%s)", strerror(errno));
        return errno;
        return errno;
    } else if (!len) {
    } else if (!len)
        LOGW("Lost connection to client");
        return false;
        return false;
    }


    int offset = 0;
    int offset = 0;
    int i;
    int i;


    for (i = 0; i < len; i++) {
    for (i = 0; i < len; i++) {
        if (buffer[i] == '\n') {
        if (buffer[i] == '\0') {
            buffer[i] = '\0';
            dispatchCommand(c, buffer + offset);
            dispatchCommand(c, buffer + offset);
            offset = i + 1;
            offset = i + 1;
        }
        }
@@ -58,13 +55,20 @@ void FrameworkListener::registerCmd(FrameworkCommand *cmd) {
    mCommands->push_back(cmd);
    mCommands->push_back(cmd);
}
}


void FrameworkListener::dispatchCommand(SocketClient *cli, char *cmd) {
void FrameworkListener::dispatchCommand(SocketClient *cli, char *data) {
    char *next = cmd;
    int argc;
    char *cm;
    char *argv[FrameworkListener::CMD_ARGS_MAX];
    char *arg;

    if (!index(data, '"')) {
        char *next = data;
        char *field;
        int i;


    if (!(cm = strsep(&next, ":"))) {
        for (i = 0; (i < FrameworkListener::CMD_ARGS_MAX) &&
        cli->sendMsg(500, "Malformatted message", false);
                    (argv[i] = strsep(&next, " ")); i++);
        argc = i+1;
    } else {
        LOGD("blehhh not supported");
        return;
        return;
    }
    }


@@ -73,8 +77,8 @@ void FrameworkListener::dispatchCommand(SocketClient *cli, char *cmd) {
    for (i = mCommands->begin(); i != mCommands->end(); ++i) {
    for (i = mCommands->begin(); i != mCommands->end(); ++i) {
        FrameworkCommand *c = *i;
        FrameworkCommand *c = *i;


        if (!strcmp(cm, c->getCommand())) {
        if (!strcmp(argv[0], c->getCommand())) {
            if (c->runCommand(cli, next)) {
            if (c->runCommand(cli, argc, argv)) {
                LOGW("Handler '%s' error (%s)", c->getCommand(), strerror(errno));
                LOGW("Handler '%s' error (%s)", c->getCommand(), strerror(errno));
            }
            }
            return;
            return;
+3 −12
Original line number Original line Diff line number Diff line
@@ -33,19 +33,10 @@ int SocketClient::sendMsg(const char *msg) {
        return -1;
        return -1;
    }
    }


    char *tmp;
    // Send the message including null character
    const char *bp = msg;

    if (msg[strlen(msg)] != '\n') {
        tmp = (char *) alloca(strlen(msg) + 1);
        strcpy(tmp, msg);
        strcat(tmp, "\n");
        bp = tmp;
    }

    int rc = 0;
    int rc = 0;
    const char *p = bp;
    const char *p = msg;
    int brtw = strlen(bp);
    int brtw = strlen(msg) + 1;


    pthread_mutex_lock(&mWriteMutex);
    pthread_mutex_lock(&mWriteMutex);
    while(brtw) {
    while(brtw) {
Loading