diff options
Diffstat (limited to 'agni.c')
-rw-r--r-- | agni.c | 129 |
1 files changed, 67 insertions, 62 deletions
@@ -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 |