From 1dcf41797bda73d9c9c286d1eff8309ee2148e13 Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Sat, 23 Sep 2017 23:02:09 +0100 Subject: Moved RPC protocol file to main source --- cmd/cmd_version.c | 2 +- extlibs/nbrpc.c | 232 ------------------------------------------------------ extlibs/nbrpc.h | 31 -------- nbrpc.c | 232 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ nbrpc.h | 31 ++++++++ 5 files changed, 264 insertions(+), 264 deletions(-) delete mode 100644 extlibs/nbrpc.c delete mode 100644 extlibs/nbrpc.h create mode 100644 nbrpc.c create mode 100644 nbrpc.h diff --git a/cmd/cmd_version.c b/cmd/cmd_version.c index f436eb1..e1c7de5 100644 --- a/cmd/cmd_version.c +++ b/cmd/cmd_version.c @@ -9,7 +9,7 @@ void *cmd_version(void *data) printf("VERSION\n"); - snprintf(buf, buf_size, "VERSION:0.0.6" " DATE:" VERSION_DATE " COMMIT:" VERSION_COMMIT ""); + snprintf(buf, buf_size, "VERSION:0.0.7" " DATE:" VERSION_DATE " COMMIT:" VERSION_COMMIT ""); ret = alloc_new_str(buf); diff --git a/extlibs/nbrpc.c b/extlibs/nbrpc.c deleted file mode 100644 index 9adde5d..0000000 --- a/extlibs/nbrpc.c +++ /dev/null @@ -1,232 +0,0 @@ -#include "nbrpc.h" - -extern char* alloc_new_str(char *); -extern char *alloc_new_str_s(char *, size_t); - -rpc_request* rpc_req_new(char *method, char *params, int id) -{ - rpc_request *ret = NULL; - - ret = malloc(sizeof(rpc_request)); - if (!ret) - { - return NULL; - } - ret->id = id; - ret->method = alloc_new_str(method); - ret->params = alloc_new_str(params); - return ret; -} - - -rpc_response* rpc_resp_new(char *result, char *error, int id) -{ - rpc_response *ret = NULL; - - ret = malloc(sizeof(rpc_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); - - return ret; -} - - -int rpc_req_free(rpc_request *req) -{ - if (req) - { - free(req->method); - free(req->params); - free(req); - req = NULL; - } - -} - - -int rpc_resp_free(rpc_response *resp) -{ - if (resp) - { - free(resp->result); - free(resp->error); - free(resp); - resp = NULL; - } -} - - -int rpc_req_marsh( rpc_request *req, netbyte_store **nb_req) -{ - netbyte_store *nb = NULL; - nb_u32 nb_id; - nb_u8arr nb_method, nb_params; - - 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_print(nb); - //nb_print(nb); - - *nb_req = nb; - return 0; -} - - -int rpc_resp_marsh( rpc_response *resp, netbyte_store **nb_resp) -{ - int eret; - netbyte_store *nb = NULL; - nb_u32 nb_id; - nb_u8arr nb_result, nb_error; - - 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); - if (!eret) - { - ENL(); - } - - //nb_print(nb); - - *nb_resp = nb; - return 0; -} - - -int rpc_req_unmarsh( netbyte_store *nb_req, rpc_request **req) -{ - __nb_type *type=NULL; - rpc_request *__req=NULL; - - if (!nb_req) - { - return -1; - } - - if (!req) - { - return -1; - } - - __req = calloc(1,sizeof(rpc_request)); - - 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(); - } - - 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(); - } - - 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(); - } - - *req = __req; - - return 0; -} - - -int rpc_resp_unmarsh(netbyte_store *nb_resp, rpc_response **resp) -{ - __nb_type *type=NULL; - rpc_response *__resp=NULL; - - if (!nb_resp) - { - return -1; - } - - if (!resp) - { - return -1; - } - - __resp = calloc(1,sizeof(rpc_response)); - - 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(); - } - - 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(); - } - - 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(); - } - - *resp = __resp; - - return 0; -} \ No newline at end of file diff --git a/extlibs/nbrpc.h b/extlibs/nbrpc.h deleted file mode 100644 index 6583b51..0000000 --- a/extlibs/nbrpc.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef __NBRPC_H -#define __NBRPC_H - -#include - -#include "mq_ntf.h" - -typedef struct rpc_request -{ - char *method; - char *params; - int id; -} rpc_request; - -typedef struct rpc_response -{ - char *result; - char *error; - int id; -} rpc_response; - -rpc_request* rpc_req_new(char *method, char *params, int id); -rpc_response* rpc_resp_new(char *result, char *error, int id); -int rpc_req_free(rpc_request *req); -int rpc_resp_free(rpc_response *resp); -int rpc_req_marsh( rpc_request *req, netbyte_store **nb_req); -int rpc_resp_marsh( rpc_response *resp, netbyte_store **nb_resp); -int rpc_req_unmarsh( netbyte_store *nb_req, rpc_request **req); -int rpc_resp_unmarsh(netbyte_store *nb_resp, rpc_response **resp); - -#endif \ No newline at end of file diff --git a/nbrpc.c b/nbrpc.c new file mode 100644 index 0000000..9adde5d --- /dev/null +++ b/nbrpc.c @@ -0,0 +1,232 @@ +#include "nbrpc.h" + +extern char* alloc_new_str(char *); +extern char *alloc_new_str_s(char *, size_t); + +rpc_request* rpc_req_new(char *method, char *params, int id) +{ + rpc_request *ret = NULL; + + ret = malloc(sizeof(rpc_request)); + if (!ret) + { + return NULL; + } + ret->id = id; + ret->method = alloc_new_str(method); + ret->params = alloc_new_str(params); + return ret; +} + + +rpc_response* rpc_resp_new(char *result, char *error, int id) +{ + rpc_response *ret = NULL; + + ret = malloc(sizeof(rpc_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); + + return ret; +} + + +int rpc_req_free(rpc_request *req) +{ + if (req) + { + free(req->method); + free(req->params); + free(req); + req = NULL; + } + +} + + +int rpc_resp_free(rpc_response *resp) +{ + if (resp) + { + free(resp->result); + free(resp->error); + free(resp); + resp = NULL; + } +} + + +int rpc_req_marsh( rpc_request *req, netbyte_store **nb_req) +{ + netbyte_store *nb = NULL; + nb_u32 nb_id; + nb_u8arr nb_method, nb_params; + + 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_print(nb); + //nb_print(nb); + + *nb_req = nb; + return 0; +} + + +int rpc_resp_marsh( rpc_response *resp, netbyte_store **nb_resp) +{ + int eret; + netbyte_store *nb = NULL; + nb_u32 nb_id; + nb_u8arr nb_result, nb_error; + + 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); + if (!eret) + { + ENL(); + } + + //nb_print(nb); + + *nb_resp = nb; + return 0; +} + + +int rpc_req_unmarsh( netbyte_store *nb_req, rpc_request **req) +{ + __nb_type *type=NULL; + rpc_request *__req=NULL; + + if (!nb_req) + { + return -1; + } + + if (!req) + { + return -1; + } + + __req = calloc(1,sizeof(rpc_request)); + + 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(); + } + + 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(); + } + + 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(); + } + + *req = __req; + + return 0; +} + + +int rpc_resp_unmarsh(netbyte_store *nb_resp, rpc_response **resp) +{ + __nb_type *type=NULL; + rpc_response *__resp=NULL; + + if (!nb_resp) + { + return -1; + } + + if (!resp) + { + return -1; + } + + __resp = calloc(1,sizeof(rpc_response)); + + 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(); + } + + 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(); + } + + 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(); + } + + *resp = __resp; + + return 0; +} \ No newline at end of file diff --git a/nbrpc.h b/nbrpc.h new file mode 100644 index 0000000..2c7b479 --- /dev/null +++ b/nbrpc.h @@ -0,0 +1,31 @@ +#ifndef __AGNI_NBRPC_H +#define __AGNI_NBRPC_H + +#include + +#include "mq_ntf.h" + +typedef struct rpc_request +{ + char *method; + char *params; + int id; +} rpc_request; + +typedef struct rpc_response +{ + char *result; + char *error; + int id; +} rpc_response; + +rpc_request* rpc_req_new(char *method, char *params, int id); +rpc_response* rpc_resp_new(char *result, char *error, int id); +int rpc_req_free(rpc_request *req); +int rpc_resp_free(rpc_response *resp); +int rpc_req_marsh( rpc_request *req, netbyte_store **nb_req); +int rpc_resp_marsh( rpc_response *resp, netbyte_store **nb_resp); +int rpc_req_unmarsh( netbyte_store *nb_req, rpc_request **req); +int rpc_resp_unmarsh(netbyte_store *nb_resp, rpc_response **resp); + +#endif \ No newline at end of file -- cgit v1.2.3