summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFreeArtMan <dos21h@gmail.com>2017-03-14 23:20:43 +0000
committerFreeArtMan <dos21h@gmail.com>2017-03-14 23:20:43 +0000
commitad204ea75ef53fa9c183fb29845e2da33795089b (patch)
tree9933026ae73c1711367539441aac51cbdcf2aca4
parent85c7b6ed10cbeb3fbcb301dff6ab58e0398bde68 (diff)
downloadagni-ad204ea75ef53fa9c183fb29845e2da33795089b.tar.gz
agni-ad204ea75ef53fa9c183fb29845e2da33795089b.zip
Now request and response matching id
-rw-r--r--agni.c73
-rw-r--r--mq_cmd.c13
2 files changed, 75 insertions, 11 deletions
diff --git a/agni.c b/agni.c
index bb9dd40..e2ba337 100644
--- a/agni.c
+++ b/agni.c
@@ -345,6 +345,19 @@ int th_start_client(void *data)
irc_token *itok = NULL;
char *irc_line = NULL;
+ //table to match response/request
+ tble_qcmd *qcmd = NULL;
+ tbl_qcmd *qtbl = NULL;
+
+ //create response table
+ qtbl = tbl_qcmd_c(10);
+ if (qtbl == NULL)
+ {
+ PERM();
+ }
+
+
+
@@ -468,7 +481,33 @@ int th_start_client(void *data)
char *paramPtr;
size_t paramSz;
mq_cmd_param(recv_cmd, &paramPtr, &paramSz);
- printf("Recieved response [%s]%d\n",paramPtr,paramSz);
+ printf("ID:%d Recieved response [%s]%d\n",mq_cmd_id(recv_cmd), paramPtr,paramSz);
+
+ //wrong
+ tble_cmd_resp *match_resp = malloc(sizeof(tble_cmd_resp));
+ if (match_resp != NULL)
+ {
+ PNL();
+ match_resp->qid = mq_cmd_id(recv_cmd);
+ //for now any type is ok, make time consistant, for this usage
+ //match_cmd->type = QCMD_NONE
+ PNL();
+ match_resp->resp = alloc_new_str_s(paramPtr, paramSz);
+
+ PNL();
+ //check if command match
+ //int cmd_id = tbl_qcmd_resp(qtbl, match_resp);
+ //if (cmd_id > 0)
+ {
+ PNL();
+ // PRINT("%d\n",cmd_id);
+ }
+
+ tbl_cmd_param_free(match_resp);
+ }
+
+ //lets memleak here
+
} else
{
PRINT("Unknown command\n");
@@ -536,13 +575,35 @@ int th_start_client(void *data)
printf("FORMATED [%s]",cmd_buf);
fret = write(conn,cmd_buf,fret2);
+ //create cmd table command and set into table
+ qcmd = tbl_qcmd_cmd_c();
+ if (qcmd != NULL)
+ {
+ qcmd->cid = uniq_id();
+ qcmd->state = QCMD_NONE;
+ qcmd->cmd = alloc_new_str("PRIVMSG");
+ qcmd->param = alloc_new_str(msg);
+ tbl_qcmd_add(qtbl, qcmd);
+ tbl_qcmd_print_tbl(qtbl,TBL_PF_QCMD_ID
+ |TBL_PF_QCMD_CID
+ |TBL_PF_QCMD_STATE
+ |TBL_PF_QCMD_CMD
+ |TBL_PF_QCMD_PARAM
+ |TBL_PF_QCMD_TID
+ |TBL_PF_QCMD_TIDX);
+ }
+
+
//create command and send to mq
- mq_cmd *privcmd = mq_cmd_create(1,"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg));
+ mq_cmd *privcmd = mq_cmd_create(qcmd->cid,"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg));
char *bufPtr;
bufPtr = mq_cmd_buf(privcmd);
mq_ntf_write(mq, MQ_OUT, bufPtr, strlen(bufPtr));
mq_cmd_free(privcmd);
+ //for safety make null, as no one should use it anymore
+ qcmd = NULL;
+
free(msg);
free(uname);
@@ -740,7 +801,7 @@ int th_event_manager(void *data)
//create command and send to mq
char msg[] = "PONG";
- mq_cmd *privcmd = mq_cmd_create(1,"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg));
+ mq_cmd *privcmd = mq_cmd_create(mq_cmd_id(recv_cmd),"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg));
mq_cmd_free(privcmd);
} else if (mq_cmd_o_cmp_cmd(recv_cmd,"PRIVMSG") == 0)
@@ -749,7 +810,7 @@ int th_event_manager(void *data)
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));
+ mq_cmd *privcmd = mq_cmd_create(mq_cmd_id(recv_cmd),"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg));
char *cmdBuf = mq_cmd_buf(privcmd);
mq_ntf_write(mq, MQ_IN, cmdBuf, strlen(cmdBuf));
@@ -760,7 +821,7 @@ int th_event_manager(void *data)
} 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));
+ mq_cmd *privcmd = mq_cmd_create(mq_cmd_id(recv_cmd),"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg));
char *cmdBuf = mq_cmd_buf(privcmd);
mq_ntf_write(mq, MQ_IN, cmdBuf, strlen(cmdBuf));
@@ -772,7 +833,7 @@ int th_event_manager(void *data)
{
char msg[] = "PONG EVENT THREAD";
- mq_cmd *privcmd = mq_cmd_create(1,"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg));
+ mq_cmd *privcmd = mq_cmd_create(mq_cmd_id(recv_cmd),"PRIVMSG",strlen("PRIVMSG"),msg,strlen(msg));
char *cmdBuf = mq_cmd_buf(privcmd);
mq_ntf_write(mq, MQ_IN, cmdBuf, strlen(cmdBuf));
diff --git a/mq_cmd.c b/mq_cmd.c
index 5570ec3..93e7ea8 100644
--- a/mq_cmd.c
+++ b/mq_cmd.c
@@ -1,6 +1,6 @@
#include "mq_cmd.h"
-#define MQ_CMD_MSG_SIZE 8192
+#define MQ_CMD_MSG_SIZE 1024
//last string element should be string ;]
mq_cmd* mq_cmd_create(int id, char *cmd, size_t cmd_sz, char *param, size_t param_sz)
{
@@ -23,11 +23,12 @@ mq_cmd* mq_cmd_create(int id, char *cmd, size_t cmd_sz, char *param, size_t para
ENL();
return NULL;
}
- memset(ret,0, sizeof(mq_cmd));
+ //memset(ret, 0, sizeof(mq_cmd));
ret->id = id;
ret->sz = MQ_CMD_MSG_SIZE;
ret->buf = malloc(MQ_CMD_MSG_SIZE);
+ memset(ret->buf, 0, MQ_CMD_MSG_SIZE); //speed? who cares
if (ret->buf == NULL)
{
ENL();
@@ -47,13 +48,15 @@ mq_cmd* mq_cmd_creates(char *str, size_t sz, int id)
mq_cmd *ret = NULL;
//just naive belive everything is fine in string
- ret = malloc(sizeof(mq_cmd));
+ ret = malloc(sizeof(mq_cmd)); //can blow up
ret->buf = malloc(sz);
if (ret->buf == NULL)
{
PERM();
+ free(ret);
return NULL;
}
+ memset(ret->buf, 0, MQ_CMD_MSG_SIZE);
ret->sz = sz;
memcpy(ret->buf,str,sz);
ret->id = id;
@@ -185,7 +188,7 @@ int mq_cmd_param(mq_cmd *cmd, char **param, size_t *sz)
}
strt = i;
*param = cmd->buf+strt;
- *sz = cmd->sz - strt+1;
+ *sz = cmd->sz - strt+1;//BUG MUAHAHA
return 0;
}
@@ -218,8 +221,8 @@ void mq_cmd_free(mq_cmd *mq)
free(mq->buf);
mq->buf = NULL;
free(mq);
+ mq = NULL;
}
-
}