diff --git a/include/hooks.h b/include/hooks.h index a2046e9ac..e33c8d616 100644 --- a/include/hooks.h +++ b/include/hooks.h @@ -91,6 +91,15 @@ void hook_init(struct list *nodes, struct list *paths, struct settings *set); /** Sort hook list according to the their priority. See hook::priority. */ int hooks_sort_priority(const void *a, const void *b); +/** Conditionally execute the hooks + * + * @param p A pointer to the path structure. + * @param t Which type of hooks should be executed? + * @retval 0 All registred hooks for the specified type have been executed successfully. + * @retval <0 On of the hook functions signalized, that the processing should be aborted; message should be skipped. + */ +int hook_run(struct path *p, enum hook_type t); + /** The following prototypes are example hooks * * @addtogroup hooks_examples Examples for hook functions diff --git a/include/path.h b/include/path.h index 1714f8eb9..d415001b2 100644 --- a/include/path.h +++ b/include/path.h @@ -126,15 +126,6 @@ void path_print_stats(struct path *p); */ const char * path_name(struct path *p); -/** Conditionally execute the hooks - * - * @param p A pointer to the path structure. - * @param t Which type of hooks should be executed? - * @retval 0 All registred hooks for the specified type have been executed successfully. - * @retval <0 On of the hook functions signalized, that the processing should be aborted; message should be skipped. - */ -int path_run_hook(struct path *p, enum hook_type t); - /** Check if node is used as source or destination of a path. */ int path_uses_node(struct path *p, struct node *n); diff --git a/lib/hooks.c b/lib/hooks.c index ee84dbaae..1870021d0 100644 --- a/lib/hooks.c +++ b/lib/hooks.c @@ -44,6 +44,23 @@ int hooks_sort_priority(const void *a, const void *b) { return ha->priority - hb->priority; } +int hook_run(struct path *p, enum hook_type t) +{ + int ret = 0; + + list_foreach(struct hook *h, &p->hooks) { + if (h->type & t) { + debug(22, "Running hook when=%u '%s' prio=%u", t, h->name, h->priority); + + ret = ((hook_cb_t) h->cb)(p, h, t); + if (ret) + return ret; + } + } + + return ret; +} + REGISTER_HOOK("print", 99, hook_print, HOOK_MSG) int hook_print(struct path *p, struct hook *h, int when) { @@ -324,7 +341,7 @@ int hook_restart(struct path *p, struct hook *h, int when) p->dropped = 0; p->received = 1; - if (path_run_hook(p, HOOK_PATH_RESTART)) + if (hook_run(p, HOOK_PATH_RESTART)) return -1; } diff --git a/lib/path.c b/lib/path.c index 88bab2090..8198f6d3f 100644 --- a/lib/path.c +++ b/lib/path.c @@ -31,23 +31,6 @@ static void path_write(struct path *p) } } -int path_run_hook(struct path *p, enum hook_type t) -{ - int ret = 0; - - list_foreach(struct hook *h, &p->hooks) { - if (h->type & t) { - debug(22, "Running hook when=%u '%s' prio=%u", t, h->name, h->priority); - - ret = ((hook_cb_t) h->cb)(p, h, t); - if (ret) - return ret; - } - } - - return ret; -} - /** Send messages asynchronously */ static void * path_run_async(void *arg) { @@ -67,7 +50,7 @@ static void * path_run_async(void *arg) if (p->received == 0) continue; - if (path_run_hook(p, HOOK_ASYNC)) + if (hook_run(p, HOOK_ASYNC)) continue; path_write(p); @@ -97,7 +80,7 @@ static void * path_run(void *arg) debug(15, "Received %u messages from node %s", recv, node_name(p->in)); /* Run preprocessing hooks */ - if (path_run_hook(p, HOOK_PRE)) { + if (hook_run(p, HOOK_PRE)) { p->skipped += recv; continue; } @@ -110,14 +93,14 @@ static void * path_run(void *arg) p->received++; /* Run hooks for filtering, stats collection and manipulation */ - if (path_run_hook(p, HOOK_MSG)) { + if (hook_run(p, HOOK_MSG)) { p->skipped++; continue; } } /* Run post processing hooks */ - if (path_run_hook(p, HOOK_POST)) { + if (hook_run(p, HOOK_POST)) { p->skipped += recv; continue; } @@ -138,7 +121,7 @@ int path_start(struct path *p) /* We sort the hooks according to their priority before starting the path */ list_sort(&p->hooks, hooks_sort_priority); - if (path_run_hook(p, HOOK_PATH_START)) + if (hook_run(p, HOOK_PATH_START)) return -1; /* At fixed rate mode, we start another thread for sending */ @@ -171,7 +154,7 @@ int path_stop(struct path *p) p->state = PATH_STOPPED; - if (path_run_hook(p, HOOK_PATH_STOP)) + if (hook_run(p, HOOK_PATH_STOP)) return -1; return 0; diff --git a/src/server.c b/src/server.c index 507f441b8..d6bbd7b7e 100644 --- a/src/server.c +++ b/src/server.c @@ -188,7 +188,7 @@ int main(int argc, char *argv[]) for (;;) { list_foreach(struct path *p, &paths) - path_run_hook(p, HOOK_PERIODIC); + hook_run(p, HOOK_PERIODIC); usleep(settings.stats * 1e6); } }