diff options
author | FreeArtMan <dos21h@gmail.com> | 2017-04-20 16:17:32 +0100 |
---|---|---|
committer | FreeArtMan <dos21h@gmail.com> | 2017-04-20 16:17:32 +0100 |
commit | fc91eba3845a9e9c741da41ba467c042cbb4ca85 (patch) | |
tree | 8f2e0fb9ac5aa873e1e53a6dbde7338bc1866be9 | |
parent | 5007608202739ed1835ffc10a54dba02bc0f361f (diff) | |
download | agni-fc91eba3845a9e9c741da41ba467c042cbb4ca85.tar.gz agni-fc91eba3845a9e9c741da41ba467c042cbb4ca85.zip |
callback execution works
-rw-r--r-- | agni.c | 100 | ||||
-rw-r--r-- | tbl_qcmd.c | 36 | ||||
-rw-r--r-- | tbl_qcmd.h | 11 |
3 files changed, 70 insertions, 77 deletions
@@ -44,11 +44,6 @@ void *cmd_pong(void *data) char *param = (char *)data; char *ret = NULL; - if (param == NULL) - { - return NULL; - } - printf("PONG\n"); ret = alloc_new_str("PONG"); @@ -61,11 +56,6 @@ void *cmd_uptime(void *data) char *param = (char *)data; char *ret = NULL; - if (param == NULL) - { - return NULL; - } - printf("UPTIME\n"); ret = alloc_new_str("UpTime is infinite\n"); @@ -78,11 +68,6 @@ void *cmd_date(void *data) char *param = (char *)data; char *ret = NULL; - if (param == NULL) - { - return NULL; - } - printf("DATE\n"); ret = alloc_new_str("I dont whant to date with you\n"); @@ -858,73 +843,34 @@ int th_event_manager(void *data) mq_cmd_free(privcmd); } else if (mq_cmd_o_cmp_cmd(recv_cmd,"PRIVMSG") == 0) { - /* - printf("Some private message\n"); - if (mq_cmd_o_cmp_param(recv_cmd, ":DATE") == 0) - { - char msg[] = "No date"; - mq_cmd *privcmd = mq_cmd_create(mq_cmd_id(recv_cmd),"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg)); - - char *cmdBuf = mq_cmd_buf(privcmd); - mq_ntf_write(mq, MQ_IN, cmdBuf, strlen(cmdBuf)); - cmdBuf = NULL; - - mq_cmd_free(privcmd); - privcmd = NULL; - } else if (mq_cmd_o_cmp_param(recv_cmd, ":UPTIME") == 0) - { - char msg[] = "Forever"; - mq_cmd *privcmd = mq_cmd_create(mq_cmd_id(recv_cmd),"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg)); - - PRINT("%s\n", privcmd->buf); - - int id = mq_cmd_id(recv_cmd); - PRINT("%d\n",id); - - char *cmdBuf = mq_cmd_buf(privcmd); - mq_ntf_write(mq, MQ_IN, cmdBuf, strlen(cmdBuf)); - cmdBuf = NULL; - - mq_cmd_free(privcmd); - privcmd = NULL; - } else if (mq_cmd_o_cmp_param(recv_cmd, ":HELLO") == 0) - { - char msg[] = "PONG TO HELLO"; - mq_cmd *privcmd = mq_cmd_create(mq_cmd_id(recv_cmd),"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg)); - - PRINT("%s\n", privcmd->buf); - - int id = mq_cmd_id(recv_cmd); - PRINT("%d\n",id); - - char *cmdBuf = mq_cmd_buf(privcmd); - mq_ntf_write(mq, MQ_IN, cmdBuf, strlen(cmdBuf)); - cmdBuf = NULL; - - mq_cmd_free(privcmd); - privcmd = NULL; - } else - { - - char msg[] = "PONG EVENT THREAD"; - - mq_cmd *privcmd = mq_cmd_create(mq_cmd_id(recv_cmd),"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg)); - - char *cmdBuf = mq_cmd_buf(privcmd); - mq_ntf_write(mq, MQ_IN, cmdBuf, strlen(cmdBuf)); - - mq_cmd_free(privcmd); - - } - */ - - //check if command callback excists if (recv_cmd_param!=NULL) { if (tbl_exec_in_s(etbl, recv_cmd_param+1)>=0) //there is ':' in front of command, check if ptr not null otherwise die { - + void *ret_msg = NULL; + + if (tbl_exec_run(etbl, recv_cmd_param+1, &ret_msg)>=0) + { + if (ret_msg!=NULL) + { + mq_cmd *privcmd = mq_cmd_create( + mq_cmd_id(recv_cmd), + "PRIVMSG", + strlen("PRIVMSG"), + ret_msg, + strlen(ret_msg) + ); + + char *cmdBuf = mq_cmd_buf(privcmd); + mq_ntf_write(mq, MQ_IN, cmdBuf, strlen(cmdBuf)); + + mq_cmd_free(privcmd); + } + } else + { + PRINT("Command execution error\n"); + } } else { PRINT("Command not found\n"); @@ -70,6 +70,42 @@ int tbl_exec_in_s(tbl_exec *tbl, char *cmd) return -1; } +int tbl_exec_run(tbl_exec *tbl, char *cmd, void **resp) +{ + int ret=-1; + + int i; + tble_exec *el=NULL; + + if ((tbl == NULL) || (cmd == NULL) || (resp==NULL) || (*resp != NULL)) + { + PERM(); + return -1; + } + + for (i=0;i<tbl->size;i++) + { + el = tbl->cmd[i]; + if (strncmp(el->cmd, cmd, strlen(el->cmd))==0) + { + void *ret_resp=NULL; + + //execute callback + PRINT("EXEC CLB ID %d\n",i); + ret_resp = el->callback(NULL); //no params for now + + //return + if (ret_resp != NULL) + { + *resp = ret_resp; + ret = i; + } + } + } + + return ret; +} + tble_exec *tbl_exec_search_cmd(tbl_exec *tbl, tble_qcmd *cmd) { tble_exec *ret = NULL; @@ -173,6 +173,17 @@ Output: */ int tbl_exec_in_s(tbl_exec *tbl, char *cmd); +/* +Input: + tbl - table of executed commands + cmd - command to execute + resp - response from command, just string +Output: + >=0 - index in list of commands + -1 - somethign whent wrong +*/ +int tbl_exec_run(tbl_exec *tbl, char *cmd, void **resp); + /* if command is found then return pointer to it |