Webserver for peach-board

RPCCommand.cpp

Committer:
thudt90
Date:
2015-03-12
Revision:
1:f1f734dd23ee
Parent:
0:6ec14f880a00

File content as of revision 1:f1f734dd23ee:

#include "RPCCommand.h"
#include "mbed.h"
#include "RPCType.h"


RPCCommand::RPCCommand():
    cmd(),
    obj_name(NULL),
    func_name(NULL)
{

}

bool RPCCommand::decode(char *buffer)
{
    // buffer = /DigitalOut/new?arg=ytgf&name=thu
    if(buffer == NULL)
        return false;
    if(buffer[0] != '/')
        return false;

    ++buffer;
    // buffer = DigitalOut/new?arg=ytgf&name=thu
    char *tmp = strchr(buffer ,'/');
    // tmp = /new?arg=ytgf&name=thu

    if(tmp == NULL)
        return false;
    if(tmp == buffer)
        return false;

    tmp[0] = '\0';
    // tmp = new?arg=ytgf&name=thu
    obj_name = buffer;
    // obj_name = DigitalOut/new?arg=ytgf&name=thu

    buffer = tmp+1;
    //buffer = new?arg=ytgf&name=thu

    if(buffer[0] == '\0' || buffer[0] == '?')
        return false;

    func_name = buffer;
    // func_name = new?arg=ytgf&name=thu

    tmp = strchr(buffer, '?');
    //tmp = ?arg=ytgf&name=thu
    if(tmp != NULL) {
        if(tmp[1] == '\0')
            return false;
        tmp[0] = '\0';
    }

    cmd[0] = '\0';
    //cmd[0] = null
    strcat(cmd, "/");
    strcat(cmd, obj_name);
    strcat(cmd, "/");
    strcat(cmd, func_name);
    //cmd = '\0'/DigitalOut/new?arg=ytgf&name=thu/new?arg=ytgf&name=thu

    if(tmp == NULL)
        return true;

    buffer = tmp+1;
    //buffer = arg=ytgf&name=thu
    do {
        tmp = strchr(buffer, '&');
        //tmp = &name=thu
        // lan 2: tmp = null

        if(tmp != NULL) {
            if(tmp[1] == '\0' || buffer == tmp)
                return false;
            tmp[0] = '\0';
            // tmp = '\0'name=thu
        }

        char *sep = strchr(buffer, '=');
        //sep = =ytgf&name=thu
        // lan 2: sep = =thu
        if(sep == NULL)
            return false;
        if(sep == buffer)
            return false;
        if(sep[1] == '\0' || sep[1] == '&')
            return false;

        strcat(cmd, " ");

        strcat(cmd, sep+1);
        //cmd = '\0'/DigitalOut/new?arg=ytgf&name=thu/new?arg=ytgf&name=thu ytgf&name=thu
        // lan 2: cmd = '\0'/DigitalOut/new?arg=ytgf&name=thu/new?arg=ytgf&name=thu ytgf&name=thu thu

        if(tmp != NULL)
            buffer = tmp+1;
        // buffer = name=thu
        else
            buffer = NULL;
    } while(buffer);

    return true;
}



char* RPCCommand::get_cmd() const
{
    return (char*)cmd;
}

RPC_COMMAND_TYPE RPCCommand::get_type() const
{
    if(!strcmp(func_name, "new") && RPCType::instance().is_supported_type(obj_name))
        return CREATE;

    RPC* r = RPC::lookup(obj_name);
    if(r == NULL)
        return INVALID;

    if(!strcmp(func_name, "delete"))
        return DELETE;

    const struct rpc_method *methods = r->get_rpc_methods();
    int i = 0;
    while(methods[i].name != NULL) {
        if(!strcmp(func_name, methods[i].name)) {
            return FUNCTION_CALL;
        }
        ++i;
    }

    return INVALID;
}

char* RPCCommand::get_obj_name() const
{
    return obj_name;
}

char* RPCCommand::get_func_name() const
{
    return func_name;
}