From db508491b6958c1847c8e962bb3112fb15d4e0af Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Thu, 5 Jun 2014 09:35:08 +0000 Subject: [PATCH] simplified hook mechanism git-svn-id: https://zerberus.eonerc.rwth-aachen.de:8443/svn/s2ss/trunk@37 8ec27952-4edc-4aab-86aa-e87bb2611832 --- include/path.h | 4 ++-- src/path.c | 23 +++++++++++++++-------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/include/path.h b/include/path.h index 3a6e84052..b05686b80 100644 --- a/include/path.h +++ b/include/path.h @@ -22,8 +22,8 @@ struct path /// Pointers to the incoming and outgoing node struct node *in, *out; - /// Hooks are called for every message which is passed - int (*hooks[MAX_HOOKS])(struct msg *m); + /// If non NULL this function is called for every received message + int (*hook)(struct msg *m); /// Counter for received messages unsigned int received; diff --git a/src/path.c b/src/path.c index ae857756a..06873c93c 100644 --- a/src/path.c +++ b/src/path.c @@ -22,12 +22,17 @@ int path_create(struct path *p, struct node *in, struct node *out) p->delayed = 0; p->duplicated = 0; + p->hook = NULL; + p->in = in; p->out = out; return 0; } +/** + * @brief This is the main thread function per path + */ static void * path_run(void *arg) { struct path *p = (struct path *) arg; @@ -35,12 +40,13 @@ static void * path_run(void *arg) /* main thread loop */ while (1) { - /* Receive message */ - msg_recv(&m, p->in); + msg_recv(&m, p->in); /* Receive message */ /* Check message sequence number */ if (m.sequence < p->sequence) { p->delayed++; + + /* Delayed messages will be skipped */ continue; } else if (m.sequence == p->sequence) { @@ -50,14 +56,15 @@ static void * path_run(void *arg) p->sequence = m.sequence; p->received++; - - /* Call hooks */ - for (int i = 0; i < MAX_HOOKS && p->hooks[i]; i++) { - p->hooks[i](&m); + /* Call hook */ + if (p->hook && p->hook(&m)) { + /* The hook can act as a simple filter + * Returning a non-zero value will skip + * the message from being forwarded */ + continue; } - /* Send message */ - msg_send(&m, p->out); + msg_send(&m, p->out); /* Send message */ } return NULL;