summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--agni.c134
-rw-r--r--tbl_qcmd.c111
-rw-r--r--tbl_qcmd.h34
3 files changed, 155 insertions, 124 deletions
diff --git a/agni.c b/agni.c
index 4f8b220..1bf6799 100644
--- a/agni.c
+++ b/agni.c
@@ -143,119 +143,7 @@ tble_cmd_resp* cllbk_wrapper( void *(*call)(void *), tble_cmd_param *param)
return unique ID, with atomic counter should work in all cases
*/
_Atomic int _glbl_id=0;
-/*
-int uniq_id()
-{
- int ret=-1,id;
-
- //what possible could go wrong?
- // sry emilsp this is not important for now
- id = atomic_load(&_glbl_id);
- ret = id;
- id += 1;
- atomic_store(&_glbl_id, id);
- return ret;
-
-}
-*/
-
-
-/*
-int send_mq_cmd(mq_ntf_mdt *mq,
- int io,
- int cmd_id,
- char *cmd,
- size_t cmd_sz,
- char *param,
- size_t param_sz)
-{
- int err = 0;
- mq_cmd *cmd_send = NULL;
- cmd_send = mq_cmd_create(cmd_id,cmd,cmd_sz,param,param_sz);
- if (cmd_send == NULL)
- {
- printf("Cant create command\n");
- return -1;
- }
-
- err = -1;
- if (io == MQ_OUT)
- {
- err = mq_ntf_write(mq, MQ_OUT, cmd_send->buf, cmd_send->sz);
- } else if (io == MQ_IN)
- {
- err = mq_ntf_write(mq, MQ_IN, cmd_send->buf, cmd_send->sz);
- } else
- {
- printf("Unknown direction");
- return -1;
- }
-
- if (err == -1)
- {
- printf("Couldnt recieve command\n");
- mq_cmd_free(cmd_send);
- return -1;
- }
-
- mq_cmd_free(cmd_send);
-
- return 0;
-}
-
-int recv_mq_cmd(mq_ntf_mdt *mq,
- int io,
- mq_cmd **cmd) //return back recieved command
-{
- int err = 0;
- mq_cmd *cmd_recv = NULL;
- int recv_sz=-1;
- char *in_buf=NULL;
-
- in_buf = malloc(MQ_MSG_SIZE);
- if (in_buf == NULL)
- {
- ENL();
- return -1;
- }
-
- err = -1;
- if (io == MQ_OUT)
- {
- err = mq_ntf_read(mq, MQ_OUT, in_buf, MQ_MSG_SIZE);
- } else if (io == MQ_IN)
- {
- err = mq_ntf_read(mq, MQ_IN, in_buf, MQ_MSG_SIZE);
- } else
- {
- printf("Unknown direction");
- return -1;
- }
-
- if (err == -1)
- {
- printf("Couldnt recieve command\n");
- free(in_buf);
- return -1;
- }
- recv_sz = err;
-
- //in err should be still the size of recieved buffer
- cmd_recv = mq_cmd_creates(in_buf, recv_sz, 0);
- if (cmd_recv == NULL)
- {
- printf("Cannot create cmd\n");
- free(in_buf);
- return -1;
- }
-
- *cmd = cmd_recv;
- free(in_buf);
-
- 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) \
@@ -460,7 +348,7 @@ int th_start_client(void *data)
char *paramPtr;
size_t paramSz;
mq_cmd_param(recv_cmd, &paramPtr, &paramSz);
- printf("ID:%d Recieved response [%s]%d\n",mq_cmd_id(recv_cmd), paramPtr,paramSz);
+ printf("ID:%d Recieved response [%s]%zu\n",mq_cmd_id(recv_cmd), paramPtr, paramSz);
//wrong
tble_cmd_resp *match_resp = malloc(sizeof(tble_cmd_resp));
@@ -476,10 +364,11 @@ int th_start_client(void *data)
int cmd_id = tbl_qcmd_resp(qtbl, match_resp);
if (cmd_id > 0)
{
-
tble_qcmd *cmd = tbl_qcmd_match_id(qtbl, cmd_id);
if (cmd != NULL)
{
+
+ PRINT("Found ID in qcmd table %d\n", cmd_id);
char *resp_user = token_new_str(cmd->tok,0);
char *dest_user = token_new_str(cmd->tok,2);
char *sep = strchr(resp_user,'!');
@@ -505,8 +394,15 @@ int th_start_client(void *data)
write(conn, cmd_buf, fret);
}
- free(dest_user);
- free(resp_user);
+ free(dest_user); dest_user = NULL;
+ free(resp_user); resp_user = NULL;
+
+ //remove command by id
+ //for now just remove command without checkings its state
+ tbl_qcmd_del_by_id(qtbl, cmd_id); //no error check
+
+ //should be added proper command state check and so on
+ tbl_qcmd_mng_states(qtbl); // This should be runed regulary
}
}
@@ -527,7 +423,7 @@ int th_start_client(void *data)
}
- //recieve irc commands and pass to MQ
+ //recieve irc commands and pass to MQ, save command to table
//fix this by allowing drain just chunks, not whole buffer
//memset(conn_buf, 0, TH_CONN_BUF_SZ);
if((cret = read(conn,conn_buf, 256))>0)
@@ -605,7 +501,7 @@ 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
+ //create cmd table command and add to table
qcmd = tbl_qcmd_cmd_c();
if (qcmd != NULL)
{
@@ -613,7 +509,9 @@ int th_start_client(void *data)
qcmd->state = QCMD_NONE;
qcmd->cmd = alloc_new_str("PRIVMSG");
qcmd->param = alloc_new_str(msg);
+ qcmd->timestamp = time(NULL);
tbl_qcmd_add_tok(qcmd, itok);
+ //HOW? add commands that are in execution list, dont process all messages
tbl_qcmd_add(qtbl, qcmd);
tbl_qcmd_print_tbl(qtbl,TBL_PF_QCMD_ID
|TBL_PF_QCMD_CID
diff --git a/tbl_qcmd.c b/tbl_qcmd.c
index 3071999..bc609b6 100644
--- a/tbl_qcmd.c
+++ b/tbl_qcmd.c
@@ -355,11 +355,12 @@ int tbl_qcmd_chk(tbl_qcmd *tbl)
return ret;
}
-//delete but dont free from list?
-int tbl_qcmd_del(tbl_qcmd *tbl, int idx)
+//delete but dont free from list? well lets free
+int tbl_qcmd_del_by_idx(tbl_qcmd *tbl, int idx)
{
int ret=-1;
tble_qcmd *replace_cmd=NULL;
+ tble_qcmd *remove_cmd=NULL;
if (tbl == NULL || idx < 0)
{
@@ -378,8 +379,13 @@ int tbl_qcmd_del(tbl_qcmd *tbl, int idx)
}
//if last idx = size then will overwrite itself and decrease command counter
- replace_cmd = tbl->cmd[tbl->size];
+ replace_cmd = tbl->cmd[tbl->size]; //if tbl->size changed you get into troubles pal
tbl->size -= 1;
+
+ //free before replace
+ remove_cmd = tbl->cmd[idx];
+ tbl_qcmd_destroy_cmd(remove_cmd);
+
tbl->cmd[idx] = replace_cmd;
//looks like no errors
@@ -388,6 +394,63 @@ int tbl_qcmd_del(tbl_qcmd *tbl, int idx)
return ret;
}
+
+/*
+1 - command found and have been deleted
+0 - nothing have been deleted
+-1 - someking of error happened
+*/
+int tbl_qcmd_del_by_id(tbl_qcmd *tbl, int id)
+{
+ int i;
+ tble_qcmd *replace_cmd = NULL;
+ tble_qcmd *remove_cmd = NULL;
+
+
+ if (tbl == NULL)
+ {
+ PERM();
+ return -1;
+ }
+
+ if (id < 0)
+ {
+ PERM();
+ return -1;
+ }
+
+ for (i=0;i<tbl->size;i++)
+ {
+ tble_qcmd *iter = tbl->cmd[i];
+
+ if (iter->id == id)
+ {
+ // if its just last element reduce size and no other logic
+ if (i == tbl->size-1)
+ {
+ tbl_qcmd_destroy_cmd(tbl->cmd[i]);
+ tbl->size -= 1;
+ return 1;
+ } else
+ //if element not last one
+ {
+ replace_cmd = tbl->cmd[tbl->size-1];
+ tbl->size -= 1;
+
+ //free before replace
+ remove_cmd = iter;
+ tbl_qcmd_destroy_cmd(remove_cmd);
+
+ iter = replace_cmd;
+ return 1; //yea yea i know
+ }
+ }
+ }
+
+ return 0;
+}
+
+
int tbl_qcmd_add_tok(tble_qcmd *tbl, irc_token *tk)
{
if (tbl == NULL)
@@ -454,7 +517,7 @@ int tbl_qcmd_resp(tbl_qcmd *tbl, tble_cmd_resp *resp)
single_cmd = tbl->cmd[i];
//check this for particular checks
- if ((single_cmd->cid == resp->qid))
+ if (single_cmd->cid == resp->qid)
{
return single_cmd->id;
}
@@ -664,6 +727,7 @@ int tbl_qcmd_destroy_cmd(tble_qcmd *tble)
{
if (tble == NULL )
{
+ PERM();
return -1;
}
@@ -689,6 +753,7 @@ int tbl_qcmd_destroy_table(tbl_qcmd *tbl)
if (tbl == NULL)
{
+ PERM();
return -1;
}
@@ -707,3 +772,41 @@ int tbl_qcmd_destroy_table(tbl_qcmd *tbl)
return 0;
}
+int tbl_qcmd_mng_states(tbl_qcmd *tbl)
+{
+ int i;
+
+ if (tbl == NULL)
+ {
+ PERM();
+ return -1;
+ }
+
+
+ for (i=0;i<tbl->size;i++)
+ {
+ tble_qcmd *iter = tbl->cmd[i];
+ //now there is no states used, so just ignore state
+
+ if ((time(NULL) - iter->timestamp)>QCMD_DEFAULT_TIMEOUT)
+ {
+ iter->state = QCMD_TIMEOUT;
+ }
+ }
+
+ //remove all timed out commands
+ for (i=tbl->size-1; i>=0;i--)
+ {
+ tble_qcmd *iter = tbl->cmd[i];
+ if (iter->state == QCMD_TIMEOUT)
+ {
+ //can blow your balls
+ tbl_qcmd_del_by_id(tbl, iter->id); //now iter becomes invalid
+ iter = NULL;
+ }
+ }
+
+ //add more complicated state machine execution
+ //add more logging and more cases to log
+
+} \ No newline at end of file
diff --git a/tbl_qcmd.h b/tbl_qcmd.h
index 43357e5..bef4043 100644
--- a/tbl_qcmd.h
+++ b/tbl_qcmd.h
@@ -5,6 +5,7 @@
#include <stdlib.h>
#include <mqueue.h>
#include <string.h>
+#include <time.h>
#include "debug.h"
#include "util.h"
@@ -44,6 +45,8 @@ typedef struct tbl_exec
tble_exec **cmd;
} tbl_exec;
+#define QCMD_DEFAULT_TIMEOUT 10 //default amount of seconds to wait replay
+
#define QCMD_NONE 0 //nothing happening with this command
#define QCMD_TIMEOUT 1 //command running to long without result
#define QCMD_DONE 2 //comand finsihed execution
@@ -262,7 +265,7 @@ Output:
int tbl_qcmd_chk(tbl_qcmd *tbl);
/*
-delete command from the list
+delete command from the list, by command position in array
Input:
tbl - array of executed commands
idx - index in array that should be removed
@@ -270,7 +273,20 @@ Output:
0 - if everything whent ok
-1 - if there was some kind of mistake
*/
-int tbl_qcmd_del(tbl_qcmd *tbl, int idx);
+int tbl_qcmd_del_by_idx(tbl_qcmd *tbl, int idx);
+
+/*
+delete command from the list, search command id in the list and delete
+Input:
+ tbl - array of executed commands
+ idx - command id that should be deleted
+Output:
+ 1 - command found and removed
+ 0 - command havent been found or deleted
+ -1 - if there was some kind of error
+*/
+int tbl_qcmd_del_by_id(tbl_qcmd *tbl, int idx);
+
/*
@@ -370,6 +386,8 @@ Output:
*/
int tbl_qcmd_print_tbl(tbl_qcmd *tbl, int flags);
+
+
/*
Input:
Output:
@@ -386,6 +404,18 @@ Output:
*/
int tbl_qcmd_destroy_table(tbl_qcmd *tbl);
+/*
+Check table of commands, remove timedout commands if there was no response.
+Input:
+ tbl - name of command table to use
+Output:
+ 1 0 how many commands where affected, state changes, removed commands and so on
+ 0 - if everything whent ok
+ -1 - some kind of error
+*/
+int tbl_qcmd_mng_states(tbl_qcmd *tbl);
+
+
/*mvar things*/
//shitty macro