summaryrefslogtreecommitdiff
path: root/tbl_qcmd.c
diff options
context:
space:
mode:
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