aboutsummaryrefslogtreecommitdiffstats
path: root/tbl_qcmd.c
diff options
context:
space:
mode:
authorFreeArtMan <dos21h@gmail.com>2017-05-02 17:27:02 +0100
committerFreeArtMan <dos21h@gmail.com>2017-05-02 17:27:02 +0100
commit8933fc1b3890fe892737c1f3e85cc77585f67993 (patch)
tree328b2c0e10c48dae1d9ac0c607d0b8ca419ea047 /tbl_qcmd.c
parentfc91eba3845a9e9c741da41ba467c042cbb4ca85 (diff)
downloadagni-8933fc1b3890fe892737c1f3e85cc77585f67993.tar.gz
agni-8933fc1b3890fe892737c1f3e85cc77585f67993.zip
Commands are now removed from command queue if they are there too long. Commands removed if response for command is recieved
Diffstat (limited to 'tbl_qcmd.c')
-rw-r--r--tbl_qcmd.c111
1 files changed, 107 insertions, 4 deletions
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