summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFreeArtMan <dos21h@gmail.com>2017-03-03 21:40:08 +0000
committerFreeArtMan <dos21h@gmail.com>2017-03-03 21:40:08 +0000
commite3c83ed9628dd3074b92ad64451d6aeb3937db4a (patch)
tree6e39f2fc5a35a1ec68fb1537706d39762309c4bb
parent927ab156f3ea4caac01e45e1d96e719fa3a53638 (diff)
downloadagni-e3c83ed9628dd3074b92ad64451d6aeb3937db4a.tar.gz
agni-e3c83ed9628dd3074b92ad64451d6aeb3937db4a.zip
improved resp/param/cmd/exec table operation
-rw-r--r--tbl_qcmd.c237
-rw-r--r--tbl_qcmd.h170
2 files changed, 342 insertions, 65 deletions
diff --git a/tbl_qcmd.c b/tbl_qcmd.c
index 16ce944..261259d 100644
--- a/tbl_qcmd.c
+++ b/tbl_qcmd.c
@@ -76,38 +76,62 @@ tble_exec *tbl_exec_search_cmd(tbl_exec *tbl, char *cmd)
return ret;
}
-int tbl_exec_print(tbl_exec *tbl)
+
+int tbl_exec_print(tble_exec *tble, int flags)
+{
+ if (tble == NULL)
+ {
+ return -1;
+ }
+
+ if (flags&TBL_PF_EXEC_ID)
+ {
+ printf("ID:[%d] ", tble->id);
+ }
+
+ if (flags&TBL_PF_EXEC_NAME)
+ {
+ printf("NAME:[%s] ", tble->name);
+ }
+
+ if (flags&TBL_PF_EXEC_CMD)
+ {
+ printf("CMD:[%s] ", tble->cmd);
+ }
+
+ if (flags&TBL_PF_EXEC_TYPE)
+ {
+ printf("TYPE:[%d] ", tble->type);
+ }
+
+ printf("\n");
+
+ return 0;
+}
+
+int tbl_exec_print_tbl(tbl_exec *tbl, int flags)
{
int i;
- tble_exec *cmd=NULL;
+ tble_exec *el=NULL;
if (tbl == NULL)
{
return -1;
}
- for (i=0;i<tbl->size;i++)
+ for (i=0; i<tbl->size; i++)
{
- cmd = tbl->reg_cmd[i];
- if (cmd != NULL)
+ el = tbl->reg_cmd[i];
+ if (el != NULL)
{
- printf("->%d<-\n",i);
- printf("ID: %d\n", cmd->id);
- printf("TYPE: %d\n", cmd->type);
- if (cmd->name)
- {
- printf("%s\n", cmd->name);
- }
- if (cmd->cmd)
- {
- printf("%s\n", cmd->cmd);
- }
+ printf(">%02d< ",i);
+ tbl_exec_print(el, flags);
}
}
return 0;
}
-tble_qcmd *tbl_qcmd_c()
+tble_qcmd *tbl_qcmd_cmd_c()
{
MVAR_ALLOC_STRC(ret,tble_qcmd,{
return NULL;});
@@ -115,7 +139,7 @@ tble_qcmd *tbl_qcmd_c()
}
-tbl_qcmd* tbl_qcmd_list_c(int size)
+tbl_qcmd* tbl_qcmd_c(int size)
{
MVAR_ALLOC_STRC(ret,tbl_qcmd,{
return NULL;});
@@ -156,6 +180,7 @@ int tbl_qcmd_add(tbl_qcmd *tbl, tble_qcmd *cmd)
{
if (tbl == NULL || cmd == NULL)
return -1;
+ cmd->id = uniq_id();
tbl->cmd[tbl->size] = cmd;
if (tbl->size+1<tbl->max_size)
{
@@ -193,7 +218,7 @@ int tbl_qcmd_chk(tbl_qcmd *tbl)
return ret;
}
-
+//delete but dont free from list?
int tbl_qcmd_del(tbl_qcmd *tbl, int idx)
{
int ret=-1;
@@ -250,26 +275,186 @@ int tbl_qcmd_resp(tbl_qcmd *tbl, tble_cmd_resp *resp)
return ret;
}
-tble_cmd_param* tbl_cmd_param_c()
+tble_cmd_param* tbl_cmd_param(tble_qcmd *cmd)
{
MVAR_ALLOC_STRC(ret,tble_cmd_param,{
+ PERM();
return NULL;});
+ ret->id = uniq_id();
+ ret->qid = cmd->id;
+ ret->cmd = alloc_new_str(cmd->cmd);
+ ret->param = alloc_new_str(cmd->param);
+ ret->out_q = cmd->out_q;
+
return ret;
}
-int tbl_cmd_param_set(tble_cmd_param *cmd_param, tble_qcmd *cmd)
+int tbl_cmd_param_free(tble_cmd_param *param)
+{
+ if (param == NULL)
+ {
+ return -1;
+ }
+
+ free(param->cmd);
+ free(param->param);
+ free(param);
+
+ return 0;
+}
+
+int tbl_cmd_param_print(tble_cmd_param *param)
+{
+ if (param == NULL)
+ return -1;
+
+ printf("ID:[%d] ", param->id);
+ printf("QID:[%d] ", param->qid);
+ printf("OUT_Q:[%d] ", param->out_q);
+ printf("CMD:[%s] ", param->cmd);
+ printf("PARAM:[%s] ", param->param);
+ printf("\n");
+
+ return 0;
+}
+
+tble_cmd_resp* tbl_cmd_resp_c(tble_cmd_param *param)
+{
+ if (param == NULL)
+ {
+ PERM();
+ return NULL;
+ }
+
+ MVAR_ALLOC_STRC(ret, tble_cmd_resp,
+ {
+ PERM();
+ return NULL;
+ });
+
+ ret->id = uniq_id();
+ ret->qid = param->qid;
+
+ return ret;
+}
+
+int tbl_cmd_resp_free(tble_cmd_resp *resp)
+{
+ if (resp == NULL)
+ {
+ return -1;
+ }
+
+
+
+ return 0;
+}
+
+int tbl_cmd_resp_print(tble_cmd_resp *resp)
{
+ if (resp == NULL)
+ return -1;
+
+ printf("ID:[%d] ", resp->id);
+ printf("QID:[%d] ", resp->qid);
+ printf("TYPE:[%d] ", resp->type);
+ printf("RET_CODE:[%d] ", resp->ret_code);
+ printf("RESP:[%d] ", resp->resp);
+ printf("\n");
+
+ return 0;
+}
- if ((cmd_param == NULL) || (cmd == NULL))
+int tbl_qcmd_print_cmd(tble_qcmd *tbl, int flags)
+{
+ if (tbl == NULL)
{
return -1;
}
- cmd_param->id = cmd->id;
- cmd_param->cmd = alloc_new_str(cmd->cmd);
- cmd_param->param = alloc_new_str(cmd->param);
- cmd_param->out_q = cmd->out_q;
+
+ if (flags&TBL_PF_QCMD_ID)
+ {
+ printf("ID:[%d] ",tbl->id);
+ }
+ if (flags&TBL_PF_QCMD_CID)
+ {
+ printf("CID:[%d] ",tbl->cid);
+ }
+ if (flags&TBL_PF_QCMD_TIMESTAMP)
+ {
+ printf("TIMESTAMP:[%d] ", tbl->timestamp);
+ }
+ if (flags&TBL_PF_QCMD_STATE)
+ {
+ printf("STATE:[%d] ", tbl->state);
+ }
+ if (flags&TBL_PF_QCMD_TIMEOUT)
+ {
+ printf("TIMEOUT:[%d] ", tbl->timeout);
+ }
+ if (flags&TBL_PF_QCMD_CMD)
+ {
+ printf("CMD:[%s] ", tbl->cmd);
+ }
+ if (flags&TBL_PF_QCMD_PARAM)
+ {
+ printf("PARAM:[%s] ", tbl->param);
+ }
+ if (flags&TBL_PF_QCMD_OUT_Q)
+ {
+ printf("OUT_Q:[%d] ", tbl->out_q);
+ }
+ if (flags&TBL_PF_QCMD_IN_Q)
+ {
+ printf("IN_Q:[%d] ", tbl->in_q);
+ }
+ if (flags&TBL_PF_QCMD_IDX_EXEC)
+ {
+ printf("IDX_EXEC:[%d] ", tbl->idx_exec);
+ }
+ printf("\n");
return 0;
}
+
+int tbl_qcmd_print_tbl(tbl_qcmd *tbl, int flags)
+{
+ int i = -1;
+ tble_qcmd *el = NULL;
+
+ if (tbl == NULL)
+ {
+ PERM();
+ return -1;
+ }
+
+ if (tbl->cmd == NULL)
+ {
+ PERM();
+ return -1;
+ }
+
+ for (i=0; i<tbl->size; i++)
+ {
+ el = tbl->cmd[i];
+ printf(">%02d< ", i);
+ tbl_qcmd_print_cmd(el, flags);
+ }
+
+ return 0;
+}
+
+
+int tbl_qcmd_destroy_cmd(tble_qcmd *tbl)
+{
+ return 0;
+}
+
+
+int tbl_qcmd_destroy_table(tbl_qcmd *tbl)
+{
+ return 0;
+}
+
diff --git a/tbl_qcmd.h b/tbl_qcmd.h
index 3485117..5a0058f 100644
--- a/tbl_qcmd.h
+++ b/tbl_qcmd.h
@@ -6,6 +6,9 @@
#include <mqueue.h>
#include <string.h>
+#include "debug.h"
+#include "util.h"
+
#define EXEC_CURRENT 0 //executes in current thread
#define EXEC_SEPERATE 1 //executes in seperate thread
@@ -14,12 +17,18 @@ Fields:
*/
typedef struct tble_exec
{
- int id;
+ int id;
char *name; /*module name, can be used just for note*/
char *cmd; /*command name*/
- int type;
+ int type;
} tble_exec;
+#define TBL_PF_EXEC_ID 1<<1
+#define TBL_PF_EXEC_NAME 1<<2
+#define TBL_PF_EXEC_CMD 1<<3
+#define TBL_PF_EXEC_TYPE 1<<4
+#define TBL_PF_EXEC_ALL 0x0fffffff
+
/*
list of registed commands
Fields:
@@ -36,41 +45,60 @@ typedef struct tbl_exec
#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
/*
Fields:
*/
-typedef struct tble_qcmd
+typedef struct tble_qcmd
{
- int id;
- int timestamp; //when command started to execute
- int state; //command execution state
- int timeout; //timeout time for command
- char *cmd; //cmd name
- char *param; //params
- mqd_t out_q;
- mqd_t in_q;
- int idx_exec; //table unique id of executor, check if still there ;]
+ int id; //command index in table
+ int cid; //caller id, who called this command
+ int timestamp; //when command started to execute
+ int state; //command execution state
+ int timeout; //timeout time for command
+ char *cmd; //cmd name ALLOC
+ 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 ;]
} tble_qcmd;
+//list of print fields
+#define TBL_PF_QCMD_ID 1<<1
+#define TBL_PF_QCMD_CID 1<<2
+#define TBL_PF_QCMD_TIMESTAMP 1<<3
+#define TBL_PF_QCMD_STATE 1<<4
+#define TBL_PF_QCMD_TIMEOUT 1<<5
+#define TBL_PF_QCMD_CMD 1<<6
+#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_ALL 0x0fffffff
+
+
/*
table of commands executing
Fields:
*/
-typedef struct tbl_qcmd
+typedef struct tbl_qcmd
{
- int size;
- int max_size;
+ int size;
+ int max_size;
tble_qcmd **cmd; /*list of command executing and waiting for replay*/
} tbl_qcmd;
/*
-use this structure to give params to command execution thread
+use this structure to give params to command execution thread. used more or less
+like proxy type that prepare all needed info to pass to executor. Could have
+some info for execution or from executor.
Fields:
*/
typedef struct tble_cmd_param
{
- int id;
+ int id; //uniq id for cmd_param
+ int qid; //command id that created this param
mqd_t out_q;
char *cmd;
char *param;
@@ -81,14 +109,15 @@ Fields:
*/
typedef struct tble_cmd_resp
{
- int id;
- int type;
- int ret_code;
+ int id; //response id
+ int qid; //parametr id, matches with pid id
+ int type;
+ int ret_code;
char *resp;
} tble_cmd_resp;
/*
-create exec command
+create exec command, used to add to table
Input:
Output:
!NULL - if everything whent ok
@@ -142,6 +171,8 @@ Output:
*/
tble_exec *tbl_exec_search_cmd(tbl_exec *tbl, char *cmd);
+int tbl_exec_print(tble_exec *tble, int flags);
+
/*
print all entries in table
Input:
@@ -150,26 +181,26 @@ Output:
>=0 - if everything whent ok
-1 - if there was some kind of mistake
*/
-int tbl_exec_print(tbl_exec *tbl);
+int tbl_exec_print_tbl(tbl_exec *tbl, int flags);
/*
create qcmd commands
Input:
Output:
- !NULL - if everything whent ok
+ !NULL - if everything whent ok ALLOC, NOFREE
NULL - if there was some kind of mistake
*/
-tble_qcmd *tbl_qcmd_c();
+tble_qcmd *tbl_qcmd_cmd_c();
/*
-create array for executed commands
+create array of commands in processing
Input:
Output:
!NULL - if everything whent ok
NULL - if there was some kind of mistake
*/
-tbl_qcmd* tbl_qcmd_list_c(int size);
+tbl_qcmd* tbl_qcmd_c(int size);
/*
add info from executor to command
@@ -186,7 +217,7 @@ int tbl_qcmd_set_exec(tble_qcmd *qcmd, tble_exec *exec);
add command to executed array
Input:
tbl - array of executed commands
- cmd - command to be added to execution list
+ cmd - command to be added to execution list ALLOC,NOFREE
Output:
0 - if everything whent ok
-1 - if there was some kind of mistake
@@ -200,7 +231,7 @@ Output:
>=0 - if everything whent ok, returns index in the table (ret-1)
-1 - if there was some kind of mistake, or nothing happened
*/
-int tbl_qcmd_chk(tbl_qcmd *tbl);
+int tbl_qcmd_chk(tbl_qcmd *tbl);
/*
delete command from the list
@@ -211,40 +242,101 @@ Output:
0 - if everything whent ok
-1 - if there was some kind of mistake
*/
-int tbl_qcmd_del(tbl_qcmd *tbl, int idx);
+int tbl_qcmd_del(tbl_qcmd *tbl, int idx);
+
/*
-if there is response then try to match response and return code to
+if there is response then try to match response and return code to
executed command requester
Input:
tbl - array of executed cmds
resp - response recieved, match it to table values
Output:
- 0 - if everything whent ok
+ >0 -
-1 - if there was some kind of mistake
*/
-int tbl_qcmd_resp(tbl_qcmd *tbl, tble_cmd_resp *resp);
+int tbl_qcmd_resp(tbl_qcmd *tbl, tble_cmd_resp *resp);
+
+
+
+/*
+create cmd param. take command and create parametrs to pass
+Input:
+Output:
+ !NULL - if everything whent ok
+ NULL - if there was some kind of mistake
+*/
+tble_cmd_param* tbl_cmd_param(tble_qcmd *cmd);
+
+/*
+free param structure
+Input:
+Output:
+ 0 - if everything whent ok
+ -1 - if there was some kind of mistake
+*/
+int tbl_cmd_param_free(tble_cmd_param *param);
+
+int tbl_cmd_param_print(tble_cmd_param *param);
+
+/*
+should be used in executor. takes param and creates response structure.
+after response structure should be filled with response result
+Input:
+Output:
+ !NULL - if everything whent ok
+ NULL - if there was some kind of mistake
+*/
+tble_cmd_resp* tbl_cmd_resp_c(tble_cmd_param *param);
/*
-create cmd param
+free response structure
Input:
Output:
!NULL - if everything whent ok
NULL - if there was some kind of mistake
*/
-tble_cmd_param* tbl_cmd_param_c();
+int tbl_cmd_resp_free(tble_cmd_resp *resp);
+
+int tbl_cmd_resp_print(tble_cmd_resp *resp);
+
+/*
+print table of commands in execution
+Input:
+Output:
+ 0 - if everything whent ok
+ -1 - if there was some kind of mistake
+*/
+int tbl_qcmd_print_cmd(tble_qcmd *tbl, int flags);
/*
-create cmd param
+print table of commands in execution
Input:
Output:
0 - if everything whent ok
-1 - if there was some kind of mistake
*/
-int tbl_cmd_param_set(tble_cmd_param *cmd_param, tble_qcmd *cmd);
+int tbl_qcmd_print_tbl(tbl_qcmd *tbl, int flags);
+
+/*
+Input:
+Output:
+ 0 - if everything whent ok
+ -1 - if there was some kind of mistake
+*/
+int tbl_qcmd_destroy_cmd(tble_qcmd *tbl);
+
+/*
+Input:
+Output:
+ 0 - if everything whent ok
+ -1 - if there was some kind of mistake
+*/
+int tbl_qcmd_destroy_table(tbl_qcmd *tbl);
+
+
+
-char *alloc_new_str_s(char *str, size_t size);
-char *alloc_new_str(char *str);
/*mvar things*/
//shitty macro