diff options
Diffstat (limited to 'agni.c')
-rw-r--r-- | agni.c | 138 |
1 files changed, 86 insertions, 52 deletions
@@ -34,6 +34,8 @@ nothing else #define MQ_MSG_SIZE 8192 +//HACK +extern int __irc_buf_drain_io(irc_buf *ib); /* return unique ID, with atomic counter should work in all cases @@ -547,7 +549,7 @@ int recv_mq_cmd(mq_ntf_mdt *mq, |--------|--OUT-->|---------| */ -#define STACK_SIZE (1024*1024) +#define STACK_SIZE (1024*128) /* not supposed to be changed. */ typedef struct server_cfg { @@ -580,10 +582,9 @@ typedef struct server_cfg #define TH_CONN_BUF_SZ 1024 int th_start_client(void *data) { - int cmd_id = 1; + //int cmd_id = 1; int err; - //char cmd_buf[MQ_MSG_SIZE]; - //mq_cmd *cmd=NULL; + //mq_cmd *qcmd=NULL; //queue command int run; int mq_event; @@ -612,6 +613,31 @@ int th_start_client(void *data) printf("Server %d\n",cfg->tid); sleep(1); + PRINT("SERVER:%s PORT:%s USER:%s\n", cfg->server, cfg->port, cfg->user); + + //PNL(); + PRINT("SERVER:%s PORT:%s USER:%s\n", cfg->server, cfg->port, cfg->user); + //conn = irc_connect(cfg->server, cfg->port); + conn = irc_connect("irc.freenode.net", "6667"); + //PNL(); + if (conn < 0) + { + PNL(); + printf("cant connect to server\n"); + //well we dont whant to just exit from thread + //lets put inside main thread CONNection checker + //if no connection just send some commmand and create + //some logic how to deal with it + atomic_fetch_sub(&cfg->running,1); + //return 0; + } + //PNL(); + //send some commands to irc to register nick + snprintf(cmd_buf, TH_CONN_BUF_SZ, "USER %s 0 0 :%s\r\n", cfg->user, cfg->user); + write(conn, cmd_buf, strlen(cmd_buf)); + snprintf(cmd_buf, TH_CONN_BUF_SZ,"NICK %s \r\n", cfg->user); + write(conn, cmd_buf, strlen(cmd_buf)); + //prepare message queue mq = cfg->mq; err = mq_ntf_mplx(mq, 10000); @@ -624,49 +650,39 @@ int th_start_client(void *data) printf("Cant get attribute\n"); ENL(); } + PRINT("SERVER:%s PORT:%s USER:%s\n", cfg->server, cfg->port, cfg->user); out_buf = malloc(out_attr.mq_msgsize); + memset(out_buf, 0, out_attr.mq_msgsize); + if (out_buf == NULL) + { + ENL(); + } + PRINT("SERVER:%s PORT:%s USER:%s\n", cfg->server, cfg->port, cfg->user); if (mq_ntf_getattr(mq, MQ_IN, &ptr_in_attr) == -1) { printf("Cant get attribute\n"); ENL(); } in_buf = malloc(in_attr.mq_msgsize); + memset(in_buf, 0, in_attr.mq_msgsize); + if (in_buf == NULL) + { + ENL(); + } - PNL(); + PRINT("SERVER:%s PORT:%s USER:%s\n", cfg->server, cfg->port, cfg->user); //create irc parsing structures memset(conn_buf, 0, TH_CONN_BUF_SZ); memset(cmd_buf, 0, TH_CONN_BUF_SZ); - PNL(); ib = irc_buf_create(); - PNL(); if (ib == NULL) { PNL(); PERM(); + //return 0; } - PNL(); - PRINT("SERVER:%s PORT:%s USER:%s\n", cfg->server, cfg->port, cfg->user); - //conn = irc_connect(cfg->server, cfg->port); - conn = irc_connect("irc.freenode.net", cfg->port); - PNL(); - if (conn < 0) - { - PNL(); - printf("cant connect to server\n"); - //well we dont whant to just exit from thread - //lets put inside main thread CONNection checker - //if no connection just send some commmand and create - //some logic how to deal with it - atomic_fetch_sub(&cfg->running,1); - return 0; - } - PNL(); - //send some commands to irc to register nick - snprintf(cmd_buf, TH_CONN_BUF_SZ, "USER %s 0 0 :%s\r\n", cfg->user, cfg->user); - write(conn, cmd_buf, strlen(cmd_buf)); - snprintf(cmd_buf, TH_CONN_BUF_SZ,"NICK %s \r\n", cfg->user); - write(conn, cmd_buf, strlen(cmd_buf)); + //send command wait for response @@ -679,11 +695,8 @@ int th_start_client(void *data) mq_event = mq_ntf_select(mq, MQ_IN); switch(mq_event) { - case 1: - PRINT("EVENT 0\n"); - break; case 0: - PRINT("EVENT 1\n"); + PRINT("SERVER EVENT 0\n"); PNL(); if (mq_ntf_read(mq, MQ_IN, in_buf, in_attr.mq_msgsize) == -1) { @@ -697,13 +710,14 @@ int th_start_client(void *data) } PNL(); break; + case 1: default: printf("Unknown event type\n"); } - PNL(); + //PNL(); sleep(1); - PNL(); + //PNL(); //fast code to exit if QUIT command is recieved if (mq_event == 1) @@ -719,17 +733,18 @@ int th_start_client(void *data) } } } - PNL(); + //PNL(); //recieve commands from queue //recieve irc commands and pass to MQ //fix this by allowing drain just chunks, not whole buffer - PNL(); - if((cret = read(conn,conn_buf, 128))>0) + //PNL(); + 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) + //if (irc_buf_ready(ib) == 1) + while (irc_buf_ready(ib) == 1) { PNL(); irc_line = irc_buf_line(ib); @@ -743,9 +758,9 @@ int th_start_client(void *data) } - PNL(); + //PNL(); itok = irc_parse(irc_line,strlen(irc_line)); - PNL(); + //PNL(); if (itok!=NULL) { int j; @@ -756,9 +771,9 @@ int th_start_client(void *data) if (token_cmp(itok,1,"NOTICE")==1) { - int fret; - fret = write(conn,"NOTICE",strlen("NOTICE")); - printf("OUT<<Send %d\n",fret); + //int fret; + //fret = write(conn,"NOTICE",strlen("NOTICE")); + //printf("OUT<<Send %d\n",fret); } if (token_cmp(itok,1,"PING")==1) { @@ -769,28 +784,37 @@ int th_start_client(void *data) if (token_cmp(itok,1,"PRIVMSG") == 1) { int fret,fret2; - char send_back[1024]; + //char send_back[128]; - memset(send_back,0,1024); + memset(cmd_buf,0,128); char *uname = token_new_str(itok,0); char *msg = token_new_str(itok,3); char *sep = strchr(uname,'!'); uname[sep-uname] = 0x0; - fret2=snprintf(send_back,1024,":%s PRIVMSG %s %s\r\n",cfg->user,uname,msg); - printf("FORMATED [%s]",send_back); - fret = write(conn,send_back,fret2); + fret2=snprintf(cmd_buf,128,":%s PRIVMSG %s PONG\r\n",cfg->user,uname); + printf("FORMATED [%s]",cmd_buf); + fret = write(conn,cmd_buf,fret2); + + //create command and send to mq + 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_OUT, str_mq, strlen(str_mq)); + //free(str_mq); + mq_cmd_free(privcmd); + free(msg); free(uname); printf("OUT<<Send %d\n", fret); } - PNL(); + //PNL(); token_destroy(itok); } free(irc_line); irc_line = NULL; + __irc_buf_drain_io(ib); } } } @@ -901,9 +925,17 @@ int th_event_manager(void *data) break; } - if (mq_cmd_o_cmp_cmd(recv_cmd,"CMD1") == 0) + if (mq_cmd_o_cmp_cmd(recv_cmd,"PING") == 0) { - printf("Hey dude it works\n"); + 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); + mq_cmd_free(privcmd); } if (mq_cmd_o_cmp_cmd(recv_cmd,"CMD2") == 0) @@ -985,6 +1017,8 @@ int main(int argc, char **argv) //try to drain mq mq_ntf_drain(&mq_array[i]); + PRINT("SERVER:%s PORT:%s USER:%s\n", srvc->server, srvc->port, srvc->user); + /* clone new proc */ clone(th_start_client, srvc->stack+STACK_SIZE, CLONE_VM|CLONE_FILES, (void *)srvc); } |