From abfd02fc7233734ffcc3c396efcbedddfba49727 Mon Sep 17 00:00:00 2001 From: ZoRo Date: Wed, 8 Feb 2017 22:39:58 +0000 Subject: Updated command parser. Event loop now support test commands --- agni.c | 29 +++++++++++++++-- mq_cmd.c | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- mq_cmd.h | 21 ++++++++++-- 3 files changed, 141 insertions(+), 19 deletions(-) diff --git a/agni.c b/agni.c index 32b3e12..ba35505 100644 --- a/agni.c +++ b/agni.c @@ -664,7 +664,7 @@ int th_event_manager(void *data) int err; int mq_event; //read any command - //mq_cmd *cmd = NULL; + mq_cmd *recv_cmd = NULL; // for recieved cmd from mq struct mq_attr out_attr, *ptr_out_attr=&out_attr; printf("Start event thread\n"); @@ -714,12 +714,35 @@ int th_event_manager(void *data) //if (run == 10) // break; - //applay to recieved command executor //if QUIT then quit the thread + recv_cmd = mq_cmd_creates(out_buf, out_attr.mq_msgsize, -1); + if (recv_cmd != NULL) + { + PNL(); + if (mq_cmd_o_cmp_cmd(recv_cmd,"QUIT") == 0) + { + printf("QUIT recieved lets quit main loop\n"); + break; + } + + if (mq_cmd_o_cmp_cmd(recv_cmd,"CMD1") == 0) + { + printf("Hey dude it works\n"); + } + + if (mq_cmd_o_cmp_cmd(recv_cmd,"CMD2") == 0) + { + printf("Hey dude it works second time\n"); + } + + } + + //applay to recieved command executor + //other command pass to cmd/execution matching table //pass to exec handler //get response if command is immidieate otherwise wait for response in next execution - + } free(out_buf); diff --git a/mq_cmd.c b/mq_cmd.c index 8956bfc..e573721 100644 --- a/mq_cmd.c +++ b/mq_cmd.c @@ -23,11 +23,12 @@ mq_cmd* mq_cmd_create(int id, char *cmd, size_t cmd_sz, char *param, size_t para ENL(); return NULL; } + memset(ret,0, sizeof(mq_cmd)); ret->id = id; ret->sz = MQ_CMD_MSG_SIZE; - ret->buf = malloc(ret->sz); - if (!ret->buf) + ret->buf = malloc(MQ_CMD_MSG_SIZE); + if (ret->buf == NULL) { ENL(); free(ret); @@ -60,7 +61,7 @@ mq_cmd* mq_cmd_creates(char *str, size_t sz, int id) return ret; } -int mq_cmd_id(mq_cmd *cmd, int *id) +int mq_cmd_id(mq_cmd *cmd) { int i,j,strt,sz; int ret=-1; @@ -73,11 +74,6 @@ int mq_cmd_id(mq_cmd *cmd, int *id) PERM(); return -1; } - if (id == NULL) - { - PERM(); - return -1; - } i = 0; while ((cmd->buf[i] != 0) && (isz)) @@ -100,8 +96,7 @@ int mq_cmd_id(mq_cmd *cmd, int *id) } buf[j] = 0x0; id_parse = atoi(buf); - *id = id_parse; - return 0; + return id_parse; } i++; } @@ -112,7 +107,7 @@ int mq_cmd_id(mq_cmd *cmd, int *id) //sz contains size of buf //%s formatting should work badly, writting to buf modifies cmd //be carefull -char* mq_cmd_cmd(mq_cmd *cmd, char **buf, size_t *sz) +int mq_cmd_cmd(mq_cmd *cmd, char **buf, size_t *sz) { size_t sz_parse; int i,j,strt,cnt,l_c; @@ -121,17 +116,17 @@ char* mq_cmd_cmd(mq_cmd *cmd, char **buf, size_t *sz) if (cmd == NULL) { PERM(); - return NULL; + return -1; } if (buf == NULL) { PERM(); - return NULL; + return -1; } if (sz == NULL) { PERM(); - return NULL; + return -1; } p = cmd->buf; @@ -236,3 +231,90 @@ void mq_cmd_free(mq_cmd *mq) } + +int mq_cmd_o_cmp_param(mq_cmd *cmd, char *str) +{ + int str_sz; //get strsize from strlen() + size_t buf_sz; + char *buf = NULL; + + if (cmd == NULL) + { + return -1; + } + + if (str == NULL) + { + return -1; + } + + if (-1 == mq_cmd_param(cmd, &buf, &buf_sz)) + { + return -1; + } + + str_sz = strlen(str); + if (strncmp(buf, str, str_sz) != 0) + { + return -1; + } + + return 0; +} + +int mq_cmd_o_cmp_cmd( mq_cmd *cmd, char *str) +{ + int str_sz; //get strsize from strlen() + size_t buf_sz; + char *buf = NULL; + + if (cmd == NULL) + { + PERM(); + return -1; + } + + if (str == NULL) + { + PERM(); + return -1; + } + + if (-1 == mq_cmd_cmd(cmd, &buf, &buf_sz)) + { + return -1; + } + + str_sz = strlen(str); + + if (strncmp(buf, str, str_sz) != 0) + { + return -1; + } + + return 0; +} + +int mq_cmd_o_cmp_id( mq_cmd *cmd, int id) +{ + int fret; + if (cmd == NULL) + { + PERM(); + return -1; + } + + if (id < 0) + { + PERM(); + return -1; + } + + if ((fret = mq_cmd_id(cmd)) != id) + { + ENL(); + return -1; + } + + return 0; +} diff --git a/mq_cmd.h b/mq_cmd.h index 120c427..4a6f6ea 100644 --- a/mq_cmd.h +++ b/mq_cmd.h @@ -14,15 +14,32 @@ typedef struct mq_cmd size_t sz; } mq_cmd; +/* +BASIC oprations over structure, create,destroy,get data +*/ mq_cmd* mq_cmd_create(int id, char *cmd, size_t cmd_sz, char *param, size_t param_sz); +//create new command from string, can set id, parsed id could be different mq_cmd* mq_cmd_creates(char *str, size_t sz, int id); -int mq_cmd_id(mq_cmd *cmd, int *id); -char* mq_cmd_cmd(mq_cmd *cmd, char **buf, size_t *sz); +//parse param from buffer +int mq_cmd_id(mq_cmd *cmd); +//parse command from buffer +int mq_cmd_cmd(mq_cmd *cmd, char **buf, size_t *sz); +//parse command param from buffer int mq_cmd_param(mq_cmd *cmd, char **param, size_t *sz); +//return buffer size size_t mq_cmd_size(mq_cmd *cmd); char *mq_cmd_buf(mq_cmd *cmd); void mq_cmd_free(mq_cmd *mq); +/* +ADVANCED opeartions over data +*/ +//compare with some values +//0 - same, -1 not same values +int mq_cmd_o_cmp_param(mq_cmd *cmd, char *str); +int mq_cmd_o_cmp_cmd( mq_cmd *cmd, char *str); +int mq_cmd_o_cmp_id( mq_cmd *cmd, int id); + #define CMD_CREATE(ID,CMD,PARAM) mq_cmd_create(ID,CMD,strlen(CMD),PARAM,strlen(PARAM)); -- cgit v1.2.3