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:34:58 +00:00
|
|
|
extern struct config config;
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-06-05 09:34:58 +00:00
|
|
|
int path_create(struct path *p, struct node *in, struct node *out)
|
|
|
|
{
|
2014-06-05 09:35:06 +00:00
|
|
|
/* Reset counters */
|
|
|
|
p->received = 0;
|
|
|
|
p->delayed = 0;
|
|
|
|
p->duplicated = 0;
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-06-05 09:34:36 +00:00
|
|
|
p->in = in;
|
|
|
|
p->out = out;
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-06-05 09:34:58 +00:00
|
|
|
return 0;
|
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:34:58 +00:00
|
|
|
/* Receive message */
|
|
|
|
msg_recv(&m, p->in);
|
|
|
|
|
|
|
|
/* Check message sequence number */
|
|
|
|
if (m.sequence < p->sequence) {
|
|
|
|
p->delayed++;
|
|
|
|
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:34:58 +00:00
|
|
|
/* Call hooks */
|
2014-06-05 09:34:38 +00:00
|
|
|
for (int i = 0; i < MAX_HOOKS && p->hooks[i]; i++) {
|
|
|
|
p->hooks[i](&m);
|
|
|
|
}
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-06-05 09:34:58 +00:00
|
|
|
/* Send message */
|
|
|
|
msg_send(&m, p->out);
|
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
|
|
|
}
|