summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile27
-rw-r--r--agni.c138
-rw-r--r--cmd_date.c0
-rw-r--r--cmd_date.h0
-rw-r--r--cmd_uptime.c0
-rw-r--r--cmd_uptime.h0
-rw-r--r--mq_cmd.c16
-rw-r--r--mq_cmd.h2
-rw-r--r--sock_conn.c40
-rw-r--r--tbl_qcmd.c10
10 files changed, 138 insertions, 95 deletions
diff --git a/Makefile b/Makefile
index 84e14c2..2e26925 100644
--- a/Makefile
+++ b/Makefile
@@ -1,15 +1,22 @@
+PROJECT=agni
+CC=gcc
+CFLAGS=-Wall
+
make:
- gcc buf.c -c
- gcc mmm.c -c
- gcc darray.c -c
- gcc mq_cmd.c -c
- gcc tbl_qcmd.c -c
- gcc util.c -c
- gcc sock_conn.c -c
- gcc irc_parse.c -c
- gcc tbl_qcmd.o mq_cmd.o buf.o mmm.o darray.o util.o sock_conn.o \
+ $(CC) $(CFLAGS) buf.c -c
+ $(CC) $(CFLAGS) mmm.c -c
+ $(CC) $(CFLAGS) darray.c -c
+ $(CC) $(CFLAGS) mq_cmd.c -c
+ $(CC) $(CFLAGS) tbl_qcmd.c -c
+ $(CC) $(CFLAGS) util.c -c
+ $(CC) $(CFLAGS) sock_conn.c -c
+ $(CC) $(CFLAGS) irc_parse.c -c
+ $(CC) $(CFLAGS) tbl_qcmd.o mq_cmd.o buf.o mmm.o darray.o util.o sock_conn.o \
irc_parse.o agni.c \
- -o agni -std=c11 -lrt
+ -o $(PROJECT) -std=c11 -lrt
+
+leak:
+ valgrind --leak-check=full --track-origins=yes --log-file=log.txt ./$(PROJECT)
clean:
rm -f agni *.o \ No newline at end of file
diff --git a/agni.c b/agni.c
index 431e746..e1cf0d5 100644
--- a/agni.c
+++ b/agni.c
@@ -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);
}
diff --git a/cmd_date.c b/cmd_date.c
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/cmd_date.c
diff --git a/cmd_date.h b/cmd_date.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/cmd_date.h
diff --git a/cmd_uptime.c b/cmd_uptime.c
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/cmd_uptime.c
diff --git a/cmd_uptime.h b/cmd_uptime.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/cmd_uptime.h
diff --git a/mq_cmd.c b/mq_cmd.c
index c5bd1b4..5570ec3 100644
--- a/mq_cmd.c
+++ b/mq_cmd.c
@@ -36,7 +36,7 @@ mq_cmd* mq_cmd_create(int id, char *cmd, size_t cmd_sz, char *param, size_t para
}
//if not null string that everything will blow up pal
//cmd[cmd_sz-1] = 0x0;
- //param[param_sz-1] = 0x0;
+ //param[param_sz-1] = 0x0;
snprintf(ret->buf,ret->sz-1,":%d:%s: %s", id, cmd, param);
return ret;
@@ -64,7 +64,6 @@ mq_cmd* mq_cmd_creates(char *str, size_t sz, int id)
int mq_cmd_id(mq_cmd *cmd)
{
int i,j,strt,sz;
- int ret=-1;
int id_parse;
char buf[16]; //fix this
/*:[ID]:*/
@@ -109,9 +108,7 @@ int mq_cmd_id(mq_cmd *cmd)
//be carefull
int mq_cmd_cmd(mq_cmd *cmd, char **buf, size_t *sz)
{
- size_t sz_parse;
- int i,j,strt,cnt,l_c;
- char *p;
+ int i,strt,cnt,l_c;
if (cmd == NULL)
{
@@ -129,8 +126,6 @@ int mq_cmd_cmd(mq_cmd *cmd, char **buf, size_t *sz)
return -1;
}
- p = cmd->buf;
-
i = 0;
l_c = 0;
while ((cmd->buf[i] != 0)&&(i<cmd->sz))
@@ -165,11 +160,7 @@ int mq_cmd_cmd(mq_cmd *cmd, char **buf, size_t *sz)
int mq_cmd_param(mq_cmd *cmd, char **param, size_t *sz)
{
- size_t sz_parse;
- int i,j,strt,cnt,l_c;
- char *p;
-
- p = cmd->buf;
+ int i,strt,l_c;
i = 0;
l_c = 0;
@@ -286,7 +277,6 @@ int mq_cmd_o_cmp_cmd( mq_cmd *cmd, char *str)
}
str_sz = strlen(str);
-
if (strncmp(buf, str, str_sz) != 0)
{
return -1;
diff --git a/mq_cmd.h b/mq_cmd.h
index e83cf3e..4a6f6ea 100644
--- a/mq_cmd.h
+++ b/mq_cmd.h
@@ -7,7 +7,7 @@
#include "debug.h"
-typedef struct mq_cmd
+typedef struct mq_cmd
{
int id;
char *buf;
diff --git a/sock_conn.c b/sock_conn.c
index b15ce47..12a00a4 100644
--- a/sock_conn.c
+++ b/sock_conn.c
@@ -54,7 +54,6 @@ irc_buf* irc_buf_create()
//cpy bytes from input to output
int __irc_buf_drain_io(irc_buf *ib)
{
- int ret=-1;
int cnt=0;
int i;
int j;
@@ -62,35 +61,43 @@ int __irc_buf_drain_io(irc_buf *ib)
//out buffer is allready full
if (ib->out_size-1 == ib->max_out_size)
{
- printf("Out buffer is full\n");
+ PRINT("Out buffer is full\n");
return -1;
}
//check if buffer is complete buffer
- if ((ib->ready == 1) //its marked as complete
- ||
+ if (ib->ready == 1) //its marked as complete
+ {
+ PRINT("Buffear allready ready\n");
+ return 0;
+ }
+
//in case if its not marked, -1 becouse its index in array
- ((ib->out_size >= 2)
+ if ((ib->out_size >= 2)
&&(ib->buf_out[ib->out_size-1]=='\n')
- &&(ib->buf_out[ib->out_size-2]=='\r')))
+ &&(ib->buf_out[ib->out_size-2]=='\r'))
{
- printf("Buffer is ready\n");
+ PRINT("Buffer is ready\n");
return 0;
}
//lets from in buffer put as much as possible to out buffer
i = ib->in_size-1;
j = ib->out_size;
- //PRINT("%d %d\n",i,j);
+ PRINT("in_size=%d out_size=%d\n",ib->in_size, ib->out_size);
cnt = 0;
while ((j<ib->max_out_size)&&(i>=0)) //buffers ar full or empty
{
- if (ib->out_size >= 2)
- if (((ib->buf_out[j-2]=='\r')&&(ib->buf_out[j-1]=='\n')))//bufferunderflow
+ //if (ib->out_size >= 2)
+ if (j >= 3) //be carefull with this line
{
- ib->ready = 1;
- //PNL();
- break;
+ if (((ib->buf_out[j-3]=='\r')&&(ib->buf_out[j-2]=='\n')))//bufferunderflow
+ {
+ PRINT("READY\n");
+ ib->ready = 1;
+ //PNL();
+ break;
+ }
}
ib->buf_out[j] = ib->buf_in[i];
i--;
@@ -101,14 +108,18 @@ int __irc_buf_drain_io(irc_buf *ib)
PRINT("Copied %d bytes\n", cnt);
ib->in_size = i+1; //i can end up as -1
ib->out_size = j; //
+ PRINT("in_size=%d out_size=%d\n",ib->in_size, ib->out_size);
//if out buffer full and its doesnt have proper ending
if (ib->out_size >= 2)
if ((ib->buf_out[ib->out_size-1]=='\n')&&(ib->buf_out[ib->out_size-2]=='\r'))
{
+ PRINT("READY\n");
ib->ready = 1;
return cnt;
}
+ PRINT("%d\n",cnt);
+
return cnt;
}
@@ -156,7 +167,7 @@ int irc_buf_puts(irc_buf *ib, char *in_buf, int sz)
}
//printf("%d %d\n",sz,cnt);
- ret = cnt;
+ ret = fret;
return ret;
}
@@ -262,6 +273,7 @@ int irc_connect( char *hostname, char *port )
if (0 != getaddrinfo(hostname, port, &serv, &res))
{
PERM();
+ perror("Couldnt get addr info");
return -1;
}
fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
diff --git a/tbl_qcmd.c b/tbl_qcmd.c
index dea78e0..16ce944 100644
--- a/tbl_qcmd.c
+++ b/tbl_qcmd.c
@@ -38,7 +38,7 @@ int tbl_exec_add(tbl_exec *tbl, tble_exec *cmd)
if (tbl->size+1<tbl->max_size)
{
tbl->size += 1;
- } else
+ } else
{
return -1;
}
@@ -65,7 +65,7 @@ tble_exec *tbl_exec_search_cmd(tbl_exec *tbl, char *cmd)
for (i=0;i<tbl->max_size;i++)
{
cur_cmd = tbl->reg_cmd[i];
- if ((strncmp(cur_cmd->cmd,cmd,strlen(cmd)) == 0) &&
+ if ((strncmp(cur_cmd->cmd,cmd,strlen(cmd)) == 0) &&
(strlen(cmd) == strlen(cur_cmd->cmd)))
{
ret = cur_cmd;
@@ -160,7 +160,7 @@ int tbl_qcmd_add(tbl_qcmd *tbl, tble_qcmd *cmd)
if (tbl->size+1<tbl->max_size)
{
tbl->size += 1;
- } else
+ } else
{
return -1;
}
@@ -181,7 +181,7 @@ int tbl_qcmd_chk(tbl_qcmd *tbl)
for (i=0;i<tbl->max_size;i++)
{
- //if command in some state like segfaulted or
+ //if command in some state like segfaulted or
// finished running then lets check
if ((tbl->cmd[i]->state == QCMD_TIMEOUT)||
(tbl->cmd[i]->state == QCMD_DONE))
@@ -209,7 +209,7 @@ int tbl_qcmd_del(tbl_qcmd *tbl, int idx)
return -1;
}
- if (tbl->size = 1)
+ if (tbl->size == 1)
{
tbl->size = 0;
return 0;