From 65e7a20b5d440b0c224a665fccdb5c9d475fa509 Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Sat, 4 Mar 2017 01:38:56 +0000 Subject: execution table improvments --- tbl_qcmd.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++-------------- tbl_qcmd.h | 48 ++++++++++++++++--------------- 2 files changed, 99 insertions(+), 45 deletions(-) diff --git a/tbl_qcmd.c b/tbl_qcmd.c index 261259d..7045af9 100644 --- a/tbl_qcmd.c +++ b/tbl_qcmd.c @@ -13,15 +13,16 @@ tbl_exec *tbl_exec_list_c(int size) MVAR_ALLOC_STRC(ret,tbl_exec,{ return NULL;}); + ret->id = uniq_id(); ret->size = 0; ret->max_size = size; - MVAR_ALLOC_ARR(reg_cmd,tble_exec*,size,{ + MVAR_ALLOC_ARR(cmd,tble_exec*,size,{ free(ret); return NULL; }) - ret->reg_cmd = reg_cmd; + ret->cmd = cmd; return ret; } @@ -34,7 +35,7 @@ int tbl_exec_add(tbl_exec *tbl, tble_exec *cmd) return -1; } - tbl->reg_cmd[tbl->size] = cmd; + tbl->cmd[tbl->size] = cmd; if (tbl->size+1max_size) { tbl->size += 1; @@ -51,24 +52,34 @@ int tbl_exec_in_s(tbl_exec *tbl, char *cmd) return -1; } -tble_exec *tbl_exec_search_cmd(tbl_exec *tbl, char *cmd) +tble_exec *tbl_exec_search_cmd(tbl_exec *tbl, tble_qcmd *cmd) { tble_exec *ret = NULL; - tble_exec *cur_cmd = NULL; + tble_exec *cur_exec = NULL; int i; if ((tbl == NULL)||(cmd == NULL)) { + PERM(); + return NULL; + } + + if (tbl->id != cmd->tid) + { + PERM(); return NULL; } for (i=0;imax_size;i++) { - cur_cmd = tbl->reg_cmd[i]; - if ((strncmp(cur_cmd->cmd,cmd,strlen(cmd)) == 0) && - (strlen(cmd) == strlen(cur_cmd->cmd))) + cur_exec = tbl->cmd[i]; + //command match + if ((strncmp(cur_exec->cmd, cmd->cmd, strlen(cmd->cmd)) == 0) && + (strlen(cur_exec->cmd) == strlen(cmd->cmd))) + // command id and exec id match + if (cur_exec->id == cmd->tidx) { - ret = cur_cmd; + ret = cur_exec; break; } } @@ -121,7 +132,7 @@ int tbl_exec_print_tbl(tbl_exec *tbl, int flags) for (i=0; isize; i++) { - el = tbl->reg_cmd[i]; + el = tbl->cmd[i]; if (el != NULL) { printf(">%02d< ",i); @@ -144,6 +155,7 @@ tbl_qcmd* tbl_qcmd_c(int size) MVAR_ALLOC_STRC(ret,tbl_qcmd,{ return NULL;}); + ret->id = uniq_id(); ret->size = 0; ret->max_size = size; @@ -158,21 +170,43 @@ tbl_qcmd* tbl_qcmd_c(int size) return ret; } -int tbl_qcmd_set_exec(tble_qcmd *qcmd, tble_exec *exec) + +int tbl_qcmd_exec(tbl_qcmd *tcmd, tbl_exec *texec) { - int ret = -1; + int ret=-1; + int i,j; - if ((qcmd == NULL) || (exec == NULL)) + if (tcmd == NULL) + { + PERM(); return -1; + } - //set executed command - qcmd->cmd = alloc_new_str(exec->cmd); - qcmd->state = QCMD_PREPARE; - qcmd->idx_exec = exec->id; + if (texec == NULL) + { + PERM(); + return -1; + } - ret = 0; + //EFFICIENT CODE MATE + //search for commands that are in NONE state + for (i=0; isize; i++) + { + tble_qcmd *tc = tcmd->cmd[i]; + if (tc->state == QCMD_INIT) + for (j=0;jsize;j++) + { + tble_exec *e = texec->cmd[j]; + if (strncmp(e->cmd, tc->cmd, strlen(e->cmd)) == 0) + { + tc->tid = texec->id; //save table id + tc->tidx = e->id; //set executor id + tc->state = QCMD_READY; //change state, that we ready to be executed + } + } + } - return ret; + return 0; } @@ -181,6 +215,9 @@ int tbl_qcmd_add(tbl_qcmd *tbl, tble_qcmd *cmd) if (tbl == NULL || cmd == NULL) return -1; cmd->id = uniq_id(); + //all command that are just added will be in zero state + //should match to some executot then can change state + cmd->state = QCMD_INIT; tbl->cmd[tbl->size] = cmd; if (tbl->size+1max_size) { @@ -277,6 +314,17 @@ int tbl_qcmd_resp(tbl_qcmd *tbl, tble_cmd_resp *resp) tble_cmd_param* tbl_cmd_param(tble_qcmd *cmd) { + if (cmd == NULL) + { + return NULL; + } + + if (cmd->state != QCMD_READY) + { + PERM(); + return NULL; + } + MVAR_ALLOC_STRC(ret,tble_cmd_param,{ PERM(); @@ -361,7 +409,7 @@ int tbl_cmd_resp_print(tble_cmd_resp *resp) printf("QID:[%d] ", resp->qid); printf("TYPE:[%d] ", resp->type); printf("RET_CODE:[%d] ", resp->ret_code); - printf("RESP:[%d] ", resp->resp); + printf("RESP:[%s] ", resp->resp); printf("\n"); return 0; @@ -410,9 +458,13 @@ int tbl_qcmd_print_cmd(tble_qcmd *tbl, int flags) { printf("IN_Q:[%d] ", tbl->in_q); } - if (flags&TBL_PF_QCMD_IDX_EXEC) + if (flags&TBL_PF_QCMD_TID) + { + printf("TID:[%d] ", tbl->tid); + } + if (flags&TBL_PF_QCMD_TIDX) { - printf("IDX_EXEC:[%d] ", tbl->idx_exec); + printf("TIDX:[%d] ", tbl->tidx); } printf("\n"); diff --git a/tbl_qcmd.h b/tbl_qcmd.h index 5a0058f..b598ebb 100644 --- a/tbl_qcmd.h +++ b/tbl_qcmd.h @@ -21,6 +21,7 @@ typedef struct tble_exec char *name; /*module name, can be used just for note*/ char *cmd; /*command name*/ int type; + void *(*callback)(void *); //callback } tble_exec; #define TBL_PF_EXEC_ID 1<<1 @@ -35,18 +36,21 @@ Fields: */ typedef struct tbl_exec { + int id; int size; int max_size; - tble_exec **reg_cmd; + tble_exec **cmd; } tbl_exec; -#define QCMD_NONE 0 //nothing happening with this command -#define QCMD_TIMEOUT 1 //command running to long without result -#define QCMD_DONE 2 //comand finsihed execution -#define QCMD_RUNNING 3 //command still in execution state -#define QCMD_PREPARE 4 //something need to be done before make it to run -#define QCMD_REPLY 5 //this is replay for command -#define QCMD_NOTFOUDN 6 //command isnot found +#define QCMD_NONE 0 //nothing happening with this command +#define QCMD_TIMEOUT 1 //command running to long without result +#define QCMD_DONE 2 //comand finsihed execution +#define QCMD_RUNNING 3 //command still in execution state +#define QCMD_PREPARE 4 //something need to be done before make it to run +#define QCMD_REPLY 5 //there is replay for command +#define QCMD_NOTFOUND 6 //command cant find executor +#define QCMD_READY 7 //command ready to start execution +#define QCMD_INIT 8 //command is set, need to find executor /* Fields: */ @@ -61,7 +65,8 @@ typedef struct tble_qcmd char *param; //params ALLOC mqd_t out_q; //UNUSED mqd_t in_q; //UNUSED - int idx_exec; //table unique id of executor, check if still there ;] + int tid; //table id + int tidx; //table unique id of executor, check if still there ;] } tble_qcmd; //list of print fields @@ -74,7 +79,8 @@ typedef struct tble_qcmd #define TBL_PF_QCMD_PARAM 1<<7 #define TBL_PF_QCMD_OUT_Q 1<<8 #define TBL_PF_QCMD_IN_Q 1<<9 -#define TBL_PF_QCMD_IDX_EXEC 1<<10 +#define TBL_PF_QCMD_TID 1<<10 +#define TBL_PF_QCMD_TIDX 1<<11 #define TBL_PF_QCMD_ALL 0x0fffffff @@ -84,6 +90,7 @@ Fields: */ typedef struct tbl_qcmd { + int id; int size; int max_size; tble_qcmd **cmd; /*list of command executing and waiting for replay*/ @@ -166,10 +173,10 @@ Input: tbl - table of executed commands cmd - command name to search in the list Output: - >=0 - if everything whent ok - -1 - if there was some kind of mistake + !NULL - if everything whent ok, NOFREE + NULL - if there was some kind of mistake */ -tble_exec *tbl_exec_search_cmd(tbl_exec *tbl, char *cmd); +tble_exec *tbl_exec_search_cmd(tbl_exec *tbl, tble_qcmd *cmd); int tbl_exec_print(tble_exec *tble, int flags); @@ -203,15 +210,12 @@ Output: tbl_qcmd* tbl_qcmd_c(int size); /* -add info from executor to command +Match command with execution table. Set id if its match some command. +There could be multiple tables Input: - tbl - array of executed commands - cmd - command to be added to execution list Output: - 0 - if everything whent ok - -1 - if there was some kind of mistake */ -int tbl_qcmd_set_exec(tble_qcmd *qcmd, tble_exec *exec); +int tbl_qcmd_exec(tbl_qcmd *tcmd, tbl_exec *texec); /* add command to executed array @@ -260,7 +264,8 @@ int tbl_qcmd_resp(tbl_qcmd *tbl, tble_cmd_resp *resp); /* -create cmd param. take command and create parametrs to pass +create cmd param. take command and create parametrs to pass to executor. +command should be in right state to do so. It should be READY Input: Output: !NULL - if everything whent ok @@ -335,9 +340,6 @@ Output: int tbl_qcmd_destroy_table(tbl_qcmd *tbl); - - - /*mvar things*/ //shitty macro #define MVAR_ALLOC_STRC(VNAME,VTYPE,VRET)\ -- cgit v1.2.3