From 7b529a3875d2987b62e5fb5349fdfba36a59a7c3 Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Tue, 5 Sep 2017 19:56:33 +0100 Subject: server->event thread messaging, command execution and response. --- agni.c | 129 ++++++++++++++++++++++++++++------------------------- extlibs/netbytes.c | 4 +- tbl_qcmd.c | 20 ++++++--- tbl_qcmd.h | 15 ++++--- 4 files changed, 94 insertions(+), 74 deletions(-) diff --git a/agni.c b/agni.c index f835401..a879049 100644 --- a/agni.c +++ b/agni.c @@ -226,6 +226,13 @@ int th_start_client(void *data) PERM(); } + ////////////////////////////////////////////////////////////////////////////// + //nbrpc + char *buf_nb = NULL; + rpc_request *req=NULL; + rpc_response *resp=NULL; + netbyte_store *nb_req, *nb_resp; + atomic_fetch_add(&cfg->running,1); printf("Start client\n"); printf("Server %d\n",cfg->tid); @@ -480,6 +487,8 @@ int th_start_client(void *data) memset(cmd_buf,0,128); char *uname = token_new_str(itok,0); + char *ircident = token_new_str(itok,0); + char *irccmd = token_new_str(itok,1); char *dest_name = token_new_str(itok,2); char *msg = token_new_str(itok,3); //could send without first symbol pal @@ -526,7 +535,8 @@ int th_start_client(void *data) { qcmd->cid = uniq_id(); qcmd->state = QCMD_NONE; - qcmd->cmd = alloc_new_str("PRIVMSG"); + qcmd->ircident = alloc_new_str(ircident); + qcmd->cmd = alloc_new_str(irccmd); qcmd->param = alloc_new_str(msg); qcmd->timestamp = time(NULL); tbl_qcmd_add_tok(qcmd, itok); @@ -536,6 +546,7 @@ int th_start_client(void *data) // |TBL_PF_QCMD_CID // |TBL_PF_QCMD_STATE // |TBL_PF_QCMD_CMD + // |TBL_PF_QCMD_IRCIDENT // |TBL_PF_QCMD_PARAM // |TBL_PF_QCMD_TID // |TBL_PF_QCMD_TIDX); @@ -543,12 +554,22 @@ int th_start_client(void *data) //create command and send to mq //PRINT("MSG %s\n",msg); - mq_cmd *privcmd = mq_cmd_create(qcmd->cid,"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg)); - char *bufPtr; - bufPtr = mq_cmd_buf(privcmd); - //PRINT("%s\n",bufPtr); - PRINT("write %d bytes\n",mq_ntf_write(mq, MQ_OUT, bufPtr, strlen(bufPtr))); - mq_cmd_free(privcmd); + //mq_cmd *privcmd = mq_cmd_create(qcmd->cid,"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg)); + //char *bufPtr; + //bufPtr = mq_cmd_buf(privcmd); + ////PRINT("%s\n",bufPtr); + //PRINT("write %d bytes\n",mq_ntf_write(mq, MQ_OUT, bufPtr, strlen(bufPtr))); + //mq_cmd_free(privcmd); + + req = rpc_req_new(qcmd->cmd, qcmd->param, qcmd->cid); + rpc_req_marsh(req, &nb_req); + buf_nb = nb_create(nb_req); + memcpy(out_buf, buf_nb, nb_req->size > out_attr.mq_msgsize ? out_attr.mq_msgsize : nb_req->size); + mq_ntf_write(mq, MQ_OUT, out_buf, out_attr.mq_msgsize); + FREE(buf_nb); + PRINT("Write %d bytes\n", nb_req->size); + rpc_req_free(req); + nb_free(nb_req); //for safety make null, as no one should use it anymore qcmd = NULL; @@ -556,6 +577,8 @@ int th_start_client(void *data) FREE(msg); FREE(uname); + FREE(ircident); + FREE(irccmd); } token_destroy(itok); itok = NULL; @@ -657,6 +680,8 @@ int th_event_manager(void *data) int run; int mq_event; + + //read any command mq_cmd *recv_cmd = NULL; // for recieved cmd from mq struct mq_attr out_attr, *ptr_out_attr=&out_attr; @@ -713,6 +738,15 @@ int th_event_manager(void *data) printf("Cant get attribute\n"); } + ////////////////////////////////////////////////////////////////////////////// + //nbrpc prepare variables + rpc_request *req=NULL; + rpc_response *resp=NULL; + netbyte_store *nb_req=NULL, *nb_resp=NULL; + + nb_resp = malloc(sizeof(netbyte_store)); //not freed + nb_req = malloc(sizeof(netbyte_store)); //not freed + out_buf = malloc(out_attr.mq_msgsize); //maybe its not null @@ -773,76 +807,47 @@ int th_event_manager(void *data) //if QUIT then quit the thread if (mq_event == 1) { - recv_cmd = mq_cmd_creates(out_buf, out_attr.mq_msgsize, 1); - //PNL(); - if (recv_cmd != NULL) + nb_init(nb_req); + if (0 == nb_load(nb_req, out_buf)) { + rpc_req_unmarsh(nb_req, &req); + PRINT("EVENT-REQ: %d-%s(%s)\n", req->id, req->method, req->params); + nb_print(nb_req); - char *recv_cmd_param=NULL; - size_t recv_cmd_param_sz=-1; - - //get command fields - mq_cmd_param(recv_cmd, &recv_cmd_param, &recv_cmd_param_sz); - - if (mq_cmd_o_cmp_cmd(recv_cmd,"QUIT") == 0) - { - printf("QUIT recieved lets quit main loop\n"); - break; - } else if (mq_cmd_o_cmp_cmd(recv_cmd,"PRIVMSG") == 0) + if (strncmp(req->method,":PRIVMSG",8)) { - //check if command callback excists - PRINT("recv_cmd_param %s\n", recv_cmd_param); - if (recv_cmd_param!=NULL) + if (tbl_exec_in_s(etbl, req->params+1)>=0) { - 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, req->params+1, &ret_msg)>=0) { - void *ret_msg = NULL; - - PRINT("CMD [%s]\n",recv_cmd_param); - if (tbl_exec_run(etbl, recv_cmd_param+1, &ret_msg)>=0) + if (ret_msg != NULL) { - if (ret_msg!=NULL) - { - //prepare response message and send it it back - 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_cur, MQ_IN, cmdBuf, strlen(cmdBuf)); - - mq_cmd_free(privcmd); - } - } else - { - PRINT("Command execution error\n"); + printf("Ret message %s\n",ret_msg); + + //send response + + + FREE(ret_msg); } } else { - PRINT("Command not found\n"); - } + ERROR("Command execution error\n"); + } + } else + { + PRINT("Command not in a table\n"); } - } else if (mq_cmd_o_cmp_cmd(recv_cmd,"CMD2") == 0) - { - printf("Hey dude it works second time\n"); } else { - printf("Unknow command\n"); - char *cmdPtr; - size_t cmdSz; - mq_cmd_cmd(recv_cmd, &cmdPtr, &cmdSz); - char *cmdNew = alloc_new_str_s(cmdPtr,cmdSz); - printf("Unknown cmd [%s]\n", cmdNew); - FREE(cmdNew); + ERROR("Unsupporetd irc command\n"); } - + //PNL();nb_free(nb_req);PNL(); + } else + { + ERROR("wrong recieved rpc command\n"); } - mq_cmd_free(recv_cmd); } //applay to recieved command executor diff --git a/extlibs/netbytes.c b/extlibs/netbytes.c index 81ba4d0..68a04e3 100644 --- a/extlibs/netbytes.c +++ b/extlibs/netbytes.c @@ -651,7 +651,9 @@ int nb_load( netbyte_store *store, uint8_t *data ) break; } default: - printf("Unknown type"); + printf("Unknown type\n"); + //FIX non freed structures, if this happends in middle of transfer + return -1; } if ( c >= data + size ) { diff --git a/tbl_qcmd.c b/tbl_qcmd.c index c41f6ec..d3e0d7f 100644 --- a/tbl_qcmd.c +++ b/tbl_qcmd.c @@ -591,6 +591,7 @@ tble_cmd_param* tbl_cmd_param(tble_qcmd *cmd) ret->id = uniq_id(); ret->qid = cmd->id; + ret->ircident = alloc_new_str(cmd->ircident); ret->cmd = alloc_new_str(cmd->cmd); ret->param = alloc_new_str(cmd->param); ret->out_q = cmd->out_q; @@ -605,6 +606,8 @@ int tbl_cmd_param_free(tble_cmd_param *param) return -1; } + PNL(); + free(param->ircident); PNL(); free(param->cmd); PNL(); @@ -621,11 +624,12 @@ int tbl_cmd_param_print(tble_cmd_param *param) return -1; printf("PARAM "); - 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("ID:[%d] ", param->id); + printf("QID:[%d] ", param->qid); + printf("OUT_Q:[%d] ", param->out_q); + printf("IRCIDENT:[%s] ", param->ircident); + printf("CMD:[%s] ", param->cmd); + printf("PARAM:[%s] ", param->param); printf("\n"); return 0; @@ -711,6 +715,10 @@ int tbl_qcmd_print_cmd(tble_qcmd *tbl, int flags) { printf("TIMEOUT:[%d] ", tbl->timeout); } + if (flags&TBL_PF_QCMD_IRCIDENT) + { + printf("IRCIDENT:[%s] ", tbl->ircident); + } if (flags&TBL_PF_QCMD_CMD) { printf("CMD:[%s] ", tbl->cmd); @@ -778,6 +786,8 @@ int tbl_qcmd_destroy_cmd(tble_qcmd *tble) //? //Free token list TODO + free(tble->ircident); + tble->ircident = NULL; free(tble->cmd); tble->cmd = NULL; diff --git a/tbl_qcmd.h b/tbl_qcmd.h index bef4043..26f805e 100644 --- a/tbl_qcmd.h +++ b/tbl_qcmd.h @@ -66,6 +66,7 @@ typedef struct tble_qcmd int timestamp; //when command started to execute int state; //command execution state int timeout; //timeout time for command + char *ircident; //username,mask,host fam!~fam@localhost.localdomain char *cmd; //cmd name ALLOC char *param; //params ALLOC mqd_t out_q; //UNUSED @@ -81,12 +82,13 @@ typedef struct tble_qcmd #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_TID 1<<10 -#define TBL_PF_QCMD_TIDX 1<<11 +#define TBL_PF_QCMD_IRCIDENT 1<<6 +#define TBL_PF_QCMD_CMD 1<<7 +#define TBL_PF_QCMD_PARAM 1<<8 +#define TBL_PF_QCMD_OUT_Q 1<<9 +#define TBL_PF_QCMD_IN_Q 1<<10 +#define TBL_PF_QCMD_TID 1<<11 +#define TBL_PF_QCMD_TIDX 1<<12 #define TBL_PF_QCMD_ALL 0x0fffffff @@ -113,6 +115,7 @@ typedef struct tble_cmd_param int id; //uniq id for cmd_param int qid; //command id that created this param mqd_t out_q; + char *ircident; char *cmd; char *param; } tble_cmd_param; -- cgit v1.2.3