2014-06-05 09:34:29 +00:00
|
|
|
/**
|
|
|
|
* Message paths
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
|
|
|
* @copyright 2014, Institute for Automation of Complex Power Systems, EONERC
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2014-06-05 09:34:36 +00:00
|
|
|
#include <errno.h>
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-06-05 09:34:58 +00:00
|
|
|
#include "cfg.h"
|
2014-06-05 09:34:29 +00:00
|
|
|
#include "utils.h"
|
|
|
|
#include "path.h"
|
|
|
|
|
2014-06-05 09:35:08 +00:00
|
|
|
/**
|
|
|
|
* @brief This is the main thread function per path
|
|
|
|
*/
|
2014-06-05 09:34:29 +00:00
|
|
|
static void * path_run(void *arg)
|
|
|
|
{
|
|
|
|
struct path *p = (struct path *) arg;
|
|
|
|
struct msg m;
|
|
|
|
|
|
|
|
/* main thread loop */
|
2014-06-05 09:35:04 +00:00
|
|
|
while (1) {
|
2014-06-05 09:35:08 +00:00
|
|
|
msg_recv(&m, p->in); /* Receive message */
|
2014-06-05 09:34:58 +00:00
|
|
|
|
|
|
|
/* Check message sequence number */
|
|
|
|
if (m.sequence < p->sequence) {
|
|
|
|
p->delayed++;
|
2014-06-05 09:35:08 +00:00
|
|
|
|
|
|
|
/* Delayed messages will be skipped */
|
2014-06-05 09:34:58 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (m.sequence == p->sequence) {
|
|
|
|
p->duplicated++;
|
|
|
|
}
|
|
|
|
|
2014-06-05 09:35:06 +00:00
|
|
|
p->sequence = m.sequence;
|
2014-06-05 09:34:58 +00:00
|
|
|
p->received++;
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-06-05 09:35:08 +00:00
|
|
|
/* 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;
|
2014-06-05 09:34:38 +00:00
|
|
|
}
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-06-05 09:35:08 +00:00
|
|
|
msg_send(&m, p->out); /* Send message */
|
2014-06-05 09:34:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int path_start(struct path *p)
|
|
|
|
{
|
|
|
|
pthread_create(&p->tid, NULL, &path_run, (void *) p);
|
|
|
|
}
|
|
|
|
|
|
|
|
int path_stop(struct path *p)
|
|
|
|
{
|
2014-06-05 09:35:04 +00:00
|
|
|
void *ret;
|
2014-06-05 09:34:29 +00:00
|
|
|
|
|
|
|
pthread_cancel(p->tid);
|
|
|
|
pthread_join(p->tid, &ret);
|
|
|
|
|
2014-06-05 09:35:04 +00:00
|
|
|
return 0;
|
2014-06-05 09:34:29 +00:00
|
|
|
}
|