diff --git a/ascon.c b/ascon.c index 799417dc..fefb8408 100644 --- a/ascon.c +++ b/ascon.c @@ -180,7 +180,7 @@ int AsconReadGarbage(int fd) if (l > 0) { /* swallow */ garbage[l] = '\0'; - printf("(((%s)))\n", garbage); + /* printf("(((%s)))\n", garbage); */ result += l; } else if (l == 0) { errno = ECONNRESET; diff --git a/devser.c b/devser.c index a2045fca..fc5d33f2 100644 --- a/devser.c +++ b/devser.c @@ -1,3 +1,4 @@ +#include #include #include "ascon.h" #include "devser.h" @@ -57,7 +58,7 @@ static void DevFreeActionList(DevAction * actions) } } -static double nextTime(double last, +static double nextTime(double due, double lastInterval, double now, double interval) { @@ -66,12 +67,12 @@ static double nextTime(double last, * following rules: * (1) result > now * (2) result is a multiple of interval - * (3) result > last + min(lastInterval,interval) * 0.99 + * (3) result >= due OR result >= due - lastInterval + interval */ if (interval > lastInterval) { - base = last + lastInterval * 0.99; + base = due - lastInterval * 0.01; } else { - base = last + interval * 0.99; + base = due - lastInterval + interval * 0.99; } if (now > base) { base = now; @@ -79,6 +80,7 @@ static double nextTime(double last, return (floor(base / interval) + 1) * interval; } + static DevAction *DevNextAction(DevSer * devser) { /* the action queue is primarily ordered by priority (high prioirty first), @@ -104,8 +106,7 @@ static DevAction *DevNextAction(DevSer * devser) action->timeDue = now; } else { /* increase timeDue according to interval */ - action->timeDue = nextTime(action->timeDue, action->interval, - now, action->interval); + action->timeDue = nextTime(0, 0, now, action->interval); } prio = action->prio; @@ -270,7 +271,7 @@ int DevSchedule(DevSer * devser, void *actionData, DevAction **ptr2prev; DevAction **ptr2insertPos = NULL; int ret; - + if (prio <= NullPRIO) { prio = NullPRIO + 1; } @@ -280,7 +281,7 @@ int DevSchedule(DevSer * devser, void *actionData, for (ptr2prev = &devser->actions, action = devser->actions; action != NULL; - ptr2prev = &action->next, action = action->next) { + action = action->next) { if (action->prio < prio && ptr2insertPos == NULL) { ptr2insertPos = ptr2prev; } @@ -292,21 +293,28 @@ int DevSchedule(DevSer * devser, void *actionData, && matchFunc(actionData, action->data)) { if (prio == action->prio && interval < 0) { /* do not move an action with equal prio */ - killFunc(actionData); - return 0; + if (killFunc) { + killFunc(actionData); + } + return 0; /* not queued */ } /* remove action from list */ *ptr2prev = action->next; foundAction = action; + } else { + ptr2prev = &action->next; } } - /* create if needed */ if (foundAction != NULL) { + /* a similar action was found */ action = foundAction; + if (killFunc) { + killFunc(actionData); + } ret = 0; - killFunc(actionData); } else { + /* create if needed */ action = calloc(1, sizeof(*action)); assert(action); action->data = actionData; @@ -335,7 +343,7 @@ int DevSchedule(DevSer * devser, void *actionData, } action->interval = interval; } - return ret; + return ret; /* when 0, actionData was killed */ } int DevQueue(DevSer * devser, void *actionData, DevPrio prio, diff --git a/devser.h b/devser.h index 39ffa54d..9de4d7dc 100644 --- a/devser.h +++ b/devser.h @@ -77,7 +77,7 @@ void DevDisconnect(DevSer * devser); * after the action has finished, i.e. when hdl returned NULL) * or NULL if no kill function is needed. * \return 1 when this was a new action, 0 when an action was overwritten - * in the second case the actionData's kill Function is called + * in the second case the actionData's kill Function is immediately called */ int DevQueue(DevSer * devser, void *actionData, DevPrio prio, DevActionHandler * hdl, DevActionMatch * matchFunc, @@ -95,7 +95,7 @@ int DevQueue(DevSer * devser, void *actionData, DevPrio prio, * \param matchFunc a match function with two arguments of the same type * \param killFunc the action data kill function or NULL if no kill function is needed. * \return 1 when this was a new action, 0 when an action was overwritten - * in the second case the actionData's kill Function is called + * in the second case the actionData's kill Function is immediately called */ int DevSchedule(DevSer * devser, void *actionData, DevPrio prio, double interval, diff --git a/scriptcontext.c b/scriptcontext.c index 05a6635e..64d503cc 100644 --- a/scriptcontext.c +++ b/scriptcontext.c @@ -1008,13 +1008,8 @@ void SctQueueNode(SctController * controller, Hdb * node, data->conCtx = NULL; data->answered = 1; - if (!DevQueue(data->controller->devser, data, prio, - SctWriteHandler, SctMatch, SctKillData)) { - if (data->name != NULL) { - free(data->name); - } - free(data); - } else { + if (DevQueue(data->controller->devser, data, prio, + SctWriteHandler, SctMatch, SctKillData)) { if (con != NULL) { data->conCtx = SCCopyConnection(con); }