From e87adc6cbcbe3ec74a4d2c07aca944c7af122910 Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Wed, 8 Mar 2017 23:31:29 +0000 Subject: Request/Response between threads now works --- Makefile | 5 +- agni.c | 257 ++++++++++++++++++++++++++++++++++++++++++------------- config_servers.h | 2 +- mmm_config.h | 4 +- mq_ntf.c | 12 +-- 5 files changed, 211 insertions(+), 69 deletions(-) diff --git a/Makefile b/Makefile index 6f4c881..2ce89d4 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ PROJECT=agni CC=gcc -CFLAGS= +CFLAGS=-g3 make: $(CC) $(CFLAGS) buf.c -c @@ -12,8 +12,9 @@ make: $(CC) $(CFLAGS) util.c -c $(CC) $(CFLAGS) sock_conn.c -c $(CC) $(CFLAGS) irc_parse.c -c + $(CC) $(CFLAGS) mmm.c -c $(CC) $(CFLAGS) tbl_qcmd.o mq_cmd.o mq_ntf.o buf.o mmm.o darray.o util.o sock_conn.o \ - irc_parse.o agni.c \ + irc_parse.o mmm.c agni.c \ -o $(PROJECT) -std=c11 -lrt leak: diff --git a/agni.c b/agni.c index 8dc5291..d405396 100644 --- a/agni.c +++ b/agni.c @@ -27,6 +27,7 @@ #include "util.h" #include "sock_conn.h" #include "irc_parse.h" +#include "mmm.h" /* no defence programming, no error checking, no argument checking just PoC @@ -55,6 +56,42 @@ void *cmd_pong(void *data) return ret; } +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"); + + return ret; +} + +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"); + + return ret; +} + + + /* Wrap call back, to manage how going be executed callback. Manage input and output params for callback. @@ -137,7 +174,7 @@ int uniq_id() - +/* int send_mq_cmd(mq_ntf_mdt *mq, int io, int cmd_id, @@ -231,7 +268,7 @@ int recv_mq_cmd(mq_ntf_mdt *mq, return 0; } - +*/ #define SEND_CMD_IN(MQ,ID,CMD,PARAM) \ send_mq_cmd(MQ,MQ_IN,ID,CMD,strlen(CMD),PARAM,strlen(PARAM)); #define SEND_CMD_OUT(MQ,ID,CMD,PARAM) \ @@ -392,7 +429,7 @@ int th_start_client(void *data) switch(mq_event) { case 0: - PRINT("EVENT 0\n"); + //PRINT("EVENT 0\n"); break; case 1: PRINT("MQ_EVENT IN\n"); @@ -402,48 +439,61 @@ int th_start_client(void *data) printf("Cant read input message \n"); } else { - PNL(); + //PNL(); in_buf[in_attr.mq_msgsize-1] = 0x0; - printf("Recieve %s\n", in_buf); + PRINT("Recieve %s\n", in_buf); } PNL(); break; default: printf("Unknown event type\n"); } - //PNL(); - //sleep(1); - - //PNL(); //fast code to exit if QUIT command is recieved + //for command in queue + //recieve commands from queue if (mq_event == 1) { //PNL(); recv_cmd = mq_cmd_creates(in_buf, in_attr.mq_msgsize, -1); if (recv_cmd != NULL) { + //char *bufPtr = mq_cmd_buf(recv_cmd); + //printf("Recieved response [%s]\n",bufPtr); + //char *cmdPtr; + //size_t cmdSz; + //mq_cmd_cmd(recv_cmd, &cmdPtr, &cmdSz); + //printf("CMD %s %d\n", cmdPtr, cmdSz); 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) + { + PRINT("Response\n") + char *paramPtr; + size_t paramSz; + mq_cmd_param(recv_cmd, ¶mPtr, ¶mSz); + printf("Recieved response [%s]%d\n",paramPtr,paramSz); + } else + { + PRINT("Unknown command\n"); } } + mq_cmd_free(recv_cmd); + recv_cmd = NULL; + } - //PNL(); - //recieve commands from queue + //recieve irc commands and pass to MQ //fix this by allowing drain just chunks, not whole buffer - //PNL(); + //memset(conn_buf, 0, TH_CONN_BUF_SZ); if((cret = read(conn,conn_buf, 256))>0) { - //printf("IN>>%s",conn_buf); irc_buf_puts(ib, conn_buf, cret); - //if (irc_buf_ready(ib) == 1) while (irc_buf_ready(ib) == 1) { - //PNL(); irc_line = irc_buf_line(ib); if (irc_line) { @@ -453,12 +503,9 @@ int th_start_client(void *data) write(conn,"PONG",strlen("PONG")); printf("OUT>>PONG\n"); } - - //PNL(); itok = irc_parse(irc_line,strlen(irc_line)); - //PNL(); - if (itok!=NULL) + if (itok != NULL) { int j; for (j=0;juser,uname); + //just send to test that thos commands works mate + fret2=snprintf(cmd_buf,128,":%s PRIVMSG %s PONGER\r\n",cfg->user,uname); printf("FORMATED [%s]",cmd_buf); fret = write(conn,cmd_buf,fret2); //create command and send to mq + PRINT("msg %s\n"); mq_cmd *privcmd = mq_cmd_create(1,"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg)); - char *str_mq = mq_cmd_buf(privcmd); - printf("str_mq %s\n",str_mq); - mq_ntf_write(mq, MQ_OUT, str_mq, strlen(str_mq)); - //free(str_mq); + char *bufPtr; + bufPtr = mq_cmd_buf(privcmd); + PRINT("BUF %s", bufPtr); + mq_ntf_write(mq, MQ_OUT, bufPtr, strlen(bufPtr)); mq_cmd_free(privcmd); @@ -510,13 +559,13 @@ int th_start_client(void *data) printf("OUT<id = uniq_id(); ecmd->name = alloc_new_str("local-executor"); @@ -601,18 +649,53 @@ int th_event_manager(void *data) if (-1 == tbl_exec_add(etbl, ecmd)) { PERM(); - return -1; + //return -1; + } + + ecmd = tbl_exec_c(); + if (ecmd == NULL) + { + PERM(); + //return -1; + } + ecmd->id = uniq_id(); + ecmd->name = alloc_new_str("local-executor"); + ecmd->cmd = alloc_new_str("DATE"); + ecmd->callback = cmd_date; + + if (-1 == tbl_exec_add(etbl, ecmd)) + { + PERM(); + //return -1; + } + + ecmd = tbl_exec_c(); + if (ecmd == NULL) + { + PERM(); + //return -1; + } + ecmd->id = uniq_id(); + ecmd->name = alloc_new_str("local-executor"); + ecmd->cmd = alloc_new_str("UPTIME"); + ecmd->callback = cmd_uptime; + + if (-1 == tbl_exec_add(etbl, ecmd)) + { + PERM(); + //return -1; } + tbl_exec_print_tbl(etbl, TBL_PF_EXEC_ALL); etbl = tbl_exec_list_c(10); if (etbl == NULL) { PERM(); - return -1; - } + //return -1; + }//PNL(); //create command table - qtbl = tbl_qcmd_c(10); + qtbl = tbl_qcmd_c(10);//well 10 should be ok right? //config mq printf("Start event thread\n"); @@ -638,7 +721,7 @@ int th_event_manager(void *data) switch(mq_event) { case 0: - PRINT("EVENT 0\n"); + //PRINT("EVENT 0\n"); break; case 1: PRINT("MQ_EVENT OUT\n"); @@ -662,47 +745,101 @@ int th_event_manager(void *data) //if QUIT then quit the thread if (mq_event == 1) { + PNL(); + + recv_cmd = mq_cmd_creates(out_buf, out_attr.mq_msgsize, 1); //PNL(); - recv_cmd = mq_cmd_creates(out_buf, out_attr.mq_msgsize, -1); if (recv_cmd != NULL) { - //PNL(); + PNL(); + PRINT("MQ:CMD %s\n", mq_cmd_buf(recv_cmd)); if (mq_cmd_o_cmp_cmd(recv_cmd,"QUIT") == 0) { printf("QUIT recieved lets quit main loop\n"); break; - } - - if (mq_cmd_o_cmp_cmd(recv_cmd,"PING") == 0) + } else if (mq_cmd_o_cmp_cmd(recv_cmd,"PING") == 0) { printf("Its fine im working\n"); //create command and send to mq char msg[] = "PONG"; mq_cmd *privcmd = mq_cmd_create(1,"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg)); - char *str_mq = mq_cmd_buf(privcmd); - mq_ntf_write(mq, MQ_IN, str_mq, strlen(str_mq)); - free(str_mq); + + //char *str_mq = mq_cmd_buf(privcmd); + //mq_ntf_write(mq, MQ_IN, str_mq, strlen(str_mq)); + mq_cmd_free(privcmd); - } - - if (mq_cmd_o_cmp_cmd(recv_cmd,"PRIVMSG") == 0) + } else if (mq_cmd_o_cmp_cmd(recv_cmd,"PRIVMSG") == 0) { printf("Some private message\n"); - char msg[] = "PONG"; + /*char msg[] = "PONG EVENT THREAD"; + mq_cmd *privcmd = mq_cmd_create(1,"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg)); - char *str_mq = mq_cmd_buf(privcmd); - mq_ntf_write(mq, MQ_IN, str_mq, strlen(str_mq)); - free(str_mq); + + //recieve params and match command to them + char *paramPtr=NULL; + size_t paramSz=0; + mq_cmd_param(recv_cmd, ¶mPtr, ¶mSz); + PRINT("param = %s\n",paramPtr); + mq_ntf_write(mq, MQ_IN, paramPtr, paramSz); + mq_cmd_free(privcmd); - } + */ + if (mq_cmd_o_cmp_param(recv_cmd, ":DATE") == 0) + { + char msg[] = "No date"; + mq_cmd *privcmd = mq_cmd_create(1,"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg)); - if (mq_cmd_o_cmp_cmd(recv_cmd,"CMD2") == 0) + 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(1,"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 + { + char msg[] = "PONG EVENT THREAD"; + + mq_cmd *privcmd = mq_cmd_create(1,"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg)); + + //recieve params and match command to them + //char *paramPtr=NULL; + //size_t paramSz=0; + //mq_cmd_param(recv_cmd, ¶mPtr, ¶mSz); + //PRINT("param = %s\n",paramPtr); + char *cmdBuf = mq_cmd_buf(privcmd); + mq_ntf_write(mq, MQ_IN, cmdBuf, strlen(cmdBuf)); + + mq_cmd_free(privcmd); + } + + } 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); } } + mq_cmd_free(recv_cmd); } //applay to recieved command executor @@ -804,7 +941,9 @@ int main(int argc, char **argv) evhnd_cfg->mq_listen = mq_array; clone(th_event_manager, evhnd_cfg->stack+EVENT_HND_STACK_SIZE, CLONE_VM|CLONE_FILES, (void *)evhnd_cfg); - PNL(); + //PNL(); + /* wait a sec while all threads will start running */ + sleep(1); /* run until all threads are up */ cnt_running = 1; while(cnt_running != 0) @@ -819,11 +958,13 @@ int main(int argc, char **argv) if (val != 0) cnt_running += 1; } - PRINT("cnt_running %d\n",cnt_running); + //PRINT("cnt_running %d\n",cnt_running); sleep(1); } free(cfg_list); + ERROR("Exit\n"); + return 0; } \ No newline at end of file diff --git a/config_servers.h b/config_servers.h index 5bd315e..8836db1 100644 --- a/config_servers.h +++ b/config_servers.h @@ -18,7 +18,7 @@ typedef struct irc_server_conf static irc_server_conf server_list[] = { { - .user = "cbot_git", + .user = "cbot_git2", .password = "asdsada", .server = "irc.freenode.net", .channels = {"#mainlv",NULL}, diff --git a/mmm_config.h b/mmm_config.h index cca4f6a..99294c8 100644 --- a/mmm_config.h +++ b/mmm_config.h @@ -5,10 +5,10 @@ //ONLY ONE OF OPTION CAN BE USED //noting is done with memory. only plain wrappers is usded. -#define MMM_PLAIN +//#define MMM_PLAIN //cleans free after free if free is not NULL -//#define MMM_PLAIN_SAFE +#define MMM_PLAIN_SAFE //show in terminal some data about used malocc and free //#define MMM_RECORD diff --git a/mq_ntf.c b/mq_ntf.c index d7c119b..dbeb9b8 100644 --- a/mq_ntf.c +++ b/mq_ntf.c @@ -100,8 +100,8 @@ int mq_ntf_select(mq_ntf_mdt *mq, int dir) //PNL(); FD_ZERO(&io_fds); FD_SET(fd, &io_fds); - tv.tv_sec = 3; - tv.tv_usec = 0; + tv.tv_sec = 0; + tv.tv_usec = 100000; //PNL(); //first argument magic numbder fdnum = select(fd+1, @@ -156,12 +156,12 @@ int mq_ntf_read(mq_ntf_mdt *mq, int dir, char *buf, size_t size) printf("queue %d out full\n", mq->id); return -1; } - PNL(); + //PNL(); /*bytes = mq_receive(mqd, buf, size > attr.mq_msgsize ? attr.mq_msgsize : size, &prio);*/ bytes = mq_receive(mqd, buf, size, &prio); - PNL(); + //PNL(); if (bytes == -1) { perror("mq_receive read out"); @@ -196,12 +196,12 @@ int mq_ntf_write(mq_ntf_mdt *mq, int dir, const char *buf, size_t size) printf("queue %d out full\n", mq->id); return -1; } - PNL(); + //PNL(); /*bytes = mq_send(mqd, buf, size > attr.mq_msgsize ? attr.mq_msgsize : size, prio);*/ bytes = mq_send(mqd, buf, size, prio); - PNL(); + //PNL(); if (bytes == -1) { perror("mq_send"); -- cgit v1.2.3