diff options
-rw-r--r-- | agni.c | 2 | ||||
-rw-r--r-- | cmd/cmd_todo.c | 68 | ||||
-rw-r--r-- | cmd/cmd_todo.h | 1 | ||||
-rw-r--r-- | log.c | 0 | ||||
-rw-r--r-- | nbrpc_call.c | 301 | ||||
-rw-r--r-- | nbrpc_call.h | 11 | ||||
-rw-r--r-- | nbrpc_event.h | 2 | ||||
-rw-r--r-- | tbl_qcmd.c | 45 | ||||
-rw-r--r-- | tbl_qcmd.h | 3 |
9 files changed, 424 insertions, 9 deletions
@@ -850,7 +850,7 @@ int th_event_manager(void *data) FREE(stat_str); } - if (tbl_exec_run(etbl, exec_params, &ret_msg)>=0) + if (tbl_exec_run(etbl, exec_params, "", "", "", &ret_msg)>=0) { if (ret_msg != NULL) { diff --git a/cmd/cmd_todo.c b/cmd/cmd_todo.c index 0f3ae58..e525d38 100644 --- a/cmd/cmd_todo.c +++ b/cmd/cmd_todo.c @@ -3,8 +3,74 @@ void *cmd_todo(void *data) { char *ret = NULL; + char *req_data = NULL; + int fret; - + rpc_call_request *req = NULL; + rpc_call_response *resp = NULL; + netbyte_store *nb_req = NULL, *nb_resp=NULL; + char *nb_buf = NULL; + + PRINT("TODO\n"); + + req_data = (char *)data; + if (!req_data) + { + PERM(); + return NULL; + } + + //---------------------------------------------------------------------------- + //prepare request + nb_req = malloc(sizeof(netbyte_store)); + nb_init(nb_req); + nb_load(nb_req, req_data); + + if (nb_req == NULL) + { + ERROR("implement response\n"); + return NULL; + } + + PNL(); + fret = rpc_call_req_unmarsh(nb_req, &req); + PNL(); + if (fret != 0) + { + ERROR("Invalid request format\n"); + return NULL; + } + PNL(); + + //---------------------------------------------------------------------------- + //main code + + + //---------------------------------------------------------------------------- + //prepare response + nb_resp = malloc(sizeof(netbyte_store)); + nb_init(nb_resp); + + PNL(); + resp = rpc_call_resp_new("Success","None",1); + resp->user = alloc_new_str(""); + resp->server = alloc_new_str(""); + resp->mask = alloc_new_str(""); + PNL(); + rpc_call_resp_marsh(resp, &nb_resp); + + PNL(); + nb_buf = nb_create(nb_resp); + if (nb_buf) + { + ret = nb_buf; + } + + PNL(); + nb_free(nb_resp); + PNL(); + rpc_call_resp_free(resp); + PNL(); return ret; } diff --git a/cmd/cmd_todo.h b/cmd/cmd_todo.h index b5227e7..1e436ba 100644 --- a/cmd/cmd_todo.h +++ b/cmd/cmd_todo.h @@ -11,6 +11,7 @@ #include "util.h" #include "debug.h" +#include "nbrpc_call.h" void *cmd_todo(void *data); diff --git a/nbrpc_call.c b/nbrpc_call.c index 3fddde7..1fc60ba 100644 --- a/nbrpc_call.c +++ b/nbrpc_call.c @@ -1,49 +1,346 @@ #include "nbrpc_call.h" +extern char* alloc_new_str(char *); +extern char *alloc_new_str_s(char *, size_t); + + rpc_call_request* rpc_call_req_new(char *method, char *params, int id) { + rpc_call_request *ret = NULL; + + if (!method) + { + return NULL; + } + if (!params) + { + return NULL; + } + + ret = malloc(sizeof(rpc_call_request)); + if (!ret) + { + return NULL; + } + ret->id = id; + ret->method = alloc_new_str(method); + ret->params = alloc_new_str(params); + ret->user = NULL; + ret->mask = NULL; + ret->server == NULL; + return ret; } rpc_call_response* rpc_call_resp_new(char *result, char *error, int id) { + rpc_call_response *ret = NULL; + + ret = malloc(sizeof(rpc_call_response)); + if (!ret) + { + return NULL; + } + PRINT("resp->id %d\n", id); + ret->id = id; + ret->result = alloc_new_str(result); + ret->error = alloc_new_str(error); + ret->user = NULL; + ret->mask = NULL; + ret->server = NULL; + return ret; } int rpc_call_req_free(rpc_call_request *req) { - + if (req) + { + FREE(req->user); + FREE(req->mask); + FREE(req->server); + FREE(req->method); + FREE(req->params); + FREE(req); + } } int rpc_call_resp_free(rpc_call_response *resp) { - + if (resp) + { + FREE(resp->result); + FREE(resp->error); + FREE(resp->user); + FREE(resp->mask); + FREE(resp->server); + FREE(resp); + } } int rpc_call_req_marsh( rpc_call_request *req, netbyte_store **nb_req) { + netbyte_store *nb = NULL; + nb_u32 nb_id; + nb_u8arr nb_method, nb_params, nb_user, nb_mask, nb_server; + + nb = calloc(1,sizeof(netbyte_store)); + nb_init(nb); + + nb_u8arr_create(&nb_method, strlen(req->method), req->method); + nb_add_u8arr(nb, &nb_method); + + nb_u8arr_create(&nb_params, strlen(req->params), req->params); + nb_add_u8arr(nb, &nb_params); + + nb_u32_create(&nb_id, req->id); + //PRINT("%d\n", req->id); + //PRINT("%d\n", nb_id.val); + nb_add_u32(nb, &nb_id); + nb_u8arr_create(&nb_user, strlen(req->user), req->user); + nb_add_u8arr(nb, &nb_user); + + nb_u8arr_create(&nb_mask, strlen(req->mask), req->mask); + nb_add_u8arr(nb, &nb_params); + + nb_u8arr_create(&nb_server, strlen(req->server), req->server); + nb_add_u8arr(nb, &nb_server); + + //nb_print(nb); + //nb_print(nb); + + *nb_req = nb; + return 0; } int rpc_call_resp_marsh( rpc_call_response *resp, netbyte_store **nb_resp) { + int eret; + netbyte_store *nb = NULL; + nb_u32 nb_id; + nb_u8arr nb_result, nb_error, nb_user, nb_mask, nb_server; + + nb = calloc(1,sizeof(netbyte_store)); + nb_init(nb); + + eret = nb_u8arr_create(&nb_result, strlen(resp->result), resp->result); + eret |= nb_add_u8arr(nb, &nb_result); + + eret = nb_u8arr_create(&nb_error, strlen(resp->error), resp->error); + eret |= nb_add_u8arr(nb, &nb_error); + + eret = nb_u32_create(&nb_id, resp->id); + //PRINT("%d\n", resp->id); + //PRINT("%d\n", nb_id.val); + eret |= nb_add_u32(nb, &nb_id); + + eret = nb_u8arr_create(&nb_user, strlen(resp->user), resp->user); + eret |= nb_add_u8arr(nb, &nb_user); + + eret = nb_u8arr_create(&nb_mask, strlen(resp->mask), resp->mask); + eret |= nb_add_u8arr(nb, &nb_mask); + + eret = nb_u8arr_create(&nb_server, strlen(resp->server), resp->server); + eret |= nb_add_u8arr(nb, &nb_server); + if (!eret) + { + ENL(); + } + + //nb_print(nb); + + *nb_resp = nb; + return 0; } int rpc_call_req_unmarsh( netbyte_store *nb_req, rpc_call_request **req) { + __nb_type *type=NULL; + rpc_call_request *__req=NULL; + + if (!nb_req) + { + return -1; + } + + if (!req) + { + return -1; + } + + __req = calloc(1,sizeof(rpc_call_request)); + + //method + if (0 == nb_val(nb_req, 0, &type)) + if (type->type == NBT_U8ARRAY) + { + nb_u8arr *u8arr = (nb_u8arr *)type->nb_val; + __req->method = alloc_new_str_s(u8arr->val, u8arr->len); + } else + { + ENL(); + } + + //params + if (0 == nb_val(nb_req, 1, &type)) + if (type->type == NBT_U8ARRAY) + { + nb_u8arr *u8arr = (nb_u8arr *)type->nb_val; + __req->params = alloc_new_str_s(u8arr->val, u8arr->len); + } else + { + ENL(); + } + + //id + if (0 == nb_val(nb_req, 2, &type)) + if (type->type == NBT_U32) + { + //PNL(); + nb_u32 *u32 = (nb_u32 *)type->nb_val; + __req->id = u32->val; + } else + { + ENL(); + } + + //user + if (0 == nb_val(nb_req, 3, &type)) + if (type->type == NBT_U8ARRAY) + { + //PNL(); + nb_u8arr *u8arr = (nb_u8arr *)type->nb_val; + __req->user = alloc_new_str_s(u8arr->val, u8arr->len); + } else + { + ENL(); + } + + //mask + if (0 == nb_val(nb_req, 4, &type)) + if (type->type == NBT_U8ARRAY) + { + //PNL(); + nb_u8arr *u8arr = (nb_u8arr *)type->nb_val; + __req->mask = alloc_new_str_s(u8arr->val, u8arr->len); + } else + { + ENL(); + } + + //server + if (0 == nb_val(nb_req, 5, &type)) + if (type->type == NBT_U8ARRAY) + { + //PNL(); + nb_u8arr *u8arr = (nb_u8arr *)type->nb_val; + __req->server = alloc_new_str_s(u8arr->val, u8arr->len); + } else + { + ENL(); + } + *req = __req; + + return 0; } int rpc_call_resp_unmarsh(netbyte_store *nb_resp, rpc_call_response **resp) { + __nb_type *type=NULL; + rpc_call_response *__resp=NULL; + + if (!nb_resp) + { + return -1; + } + + if (!resp) + { + return -1; + } + + __resp = calloc(1,sizeof(rpc_call_response)); + + //result + if (0 == nb_val(nb_resp, 0, &type)) + if (type->type == NBT_U8ARRAY) + { + nb_u8arr *u8arr = (nb_u8arr *)type->nb_val; + __resp->result = alloc_new_str_s(u8arr->val, u8arr->len); + } else + { + ENL(); + } + + //error + if (0 == nb_val(nb_resp, 1, &type)) + if (type->type == NBT_U8ARRAY) + { + nb_u8arr *u8arr = (nb_u8arr *)type->nb_val; + __resp->error = alloc_new_str_s(u8arr->val, u8arr->len); + } else + { + ENL(); + } + + //id + if (0 == nb_val(nb_resp, 2, &type)) + if (type->type == NBT_U32) + { + PNL(); + nb_u32 *u32 = (nb_u32 *)type->nb_val; + __resp->id = u32->val; + //PRINT("%d\n",u32->val) + } else + { + ENL(); + } + + //user + if (0 == nb_val(nb_resp, 3, &type)) + if (type->type == NBT_U8ARRAY) + { + nb_u8arr *u8arr = (nb_u8arr *)type->nb_val; + __resp->user = alloc_new_str_s(u8arr->val, u8arr->len); + } else + { + ENL(); + } + + //mask + if (0 == nb_val(nb_resp, 1, &type)) + if (type->type == NBT_U8ARRAY) + { + nb_u8arr *u8arr = (nb_u8arr *)type->nb_val; + __resp->mask = alloc_new_str_s(u8arr->val, u8arr->len); + } else + { + ENL(); + } + + //server + if (0 == nb_val(nb_resp, 1, &type)) + if (type->type == NBT_U8ARRAY) + { + nb_u8arr *u8arr = (nb_u8arr *)type->nb_val; + __resp->server = alloc_new_str_s(u8arr->val, u8arr->len); + } else + { + ENL(); + } + + *resp = __resp; + return 0; } diff --git a/nbrpc_call.h b/nbrpc_call.h index 183f911..3d7ea9a 100644 --- a/nbrpc_call.h +++ b/nbrpc_call.h @@ -5,24 +5,31 @@ #include <mq_ntf.h> +#include "mmm.h" + typedef struct rpc_call_request { + //rpc char *method; char *params; + int id; + //extra params char *user; char *mask; char *server; - int id; + } rpc_call_request; typedef struct rpc_call_response { + //rpc char *result; char *error; + int id; + //extra params char *user; char *mask; char *server; - int id; } rpc_call_response; rpc_call_request* rpc_call_req_new(char *method, char *params, int id); diff --git a/nbrpc_event.h b/nbrpc_event.h index 020ecba..5b0bad8 100644 --- a/nbrpc_event.h +++ b/nbrpc_event.h @@ -5,6 +5,8 @@ #include <mq_ntf.h> +#include "mmm.h" + typedef struct rpc_request { char *method; @@ -81,7 +81,7 @@ int tbl_exec_in_s(tbl_exec *tbl, char *cmd) return -1; } -int tbl_exec_run(tbl_exec *tbl, char *cmd, void **resp) +int tbl_exec_run(tbl_exec *tbl, char *cmd, char *user, char *mask, char *server, void **resp) { int ret=-1; void *clbk_data=NULL; @@ -90,6 +90,7 @@ int tbl_exec_run(tbl_exec *tbl, char *cmd, void **resp) int i; tble_exec *el=NULL; + //user,mask,server could be NULL if ((tbl == NULL) || (cmd == NULL) || (resp==NULL) || (*resp != NULL)) { PERM(); @@ -140,7 +141,47 @@ int tbl_exec_run(tbl_exec *tbl, char *cmd, void **resp) ret_resp = el->callback(clbk_data); //no params for now } else if (el->type == TBL_T_RPC) { - ERROR("Not implemented\n"); + rpc_call_request *req = NULL; + rpc_call_response *resp = NULL; + netbyte_store *nb_req=NULL, *nb_resp=NULL; + char *buf_nb = NULL, *resp_buf=NULL; + + nb_resp = malloc(sizeof(netbyte_store)); + nb_req = malloc(sizeof(netbyte_store)); + nb_init(nb_resp); + nb_init(nb_req); + + //create request + req = rpc_call_req_new(el->name, "asd", 1); + if (!req) PERM(); + if (user != NULL) + { + req->user = alloc_new_str(user); + } + if (mask != NULL) + { + req->mask = alloc_new_str(mask); + } + if (server != NULL) + { + req->server = alloc_new_str(server); + } + rpc_call_req_marsh(req, &nb_req); + buf_nb = nb_create(nb_req); + resp_buf = el->callback(buf_nb); + if (resp_buf!=NULL) + { + nb_load(nb_resp, resp_buf); + rpc_call_resp_unmarsh(nb_resp, &resp); + ret_resp = alloc_new_str(resp->result); + rpc_call_resp_free(resp); + } else + { + ERROR("No response from RPC command\n"); + } + FREE(buf_nb); + rpc_call_req_free(req); + } else if (el->type == TBL_T_LUA) { ERROR("Not implemented\n"); @@ -11,6 +11,7 @@ #include "util.h" #include "irc_parse.h" +#include "nbrpc_call.h" #define EXEC_CURRENT 0 //executes in current thread #define EXEC_SEPERATE 1 //executes in seperate thread @@ -192,7 +193,7 @@ Output: >=0 - index in list of commands -1 - somethign whent wrong */ -int tbl_exec_run(tbl_exec *tbl, char *cmd, void **resp); +int tbl_exec_run(tbl_exec *tbl, char *cmd, char *user, char *mask, char *server, void **resp);//in future to change cmd to some dict structure /* |