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>
|
|
|
|
#include <poll.h>
|
2014-06-05 09:34:36 +00:00
|
|
|
#include <errno.h>
|
2014-06-05 09:34:29 +00:00
|
|
|
|
|
|
|
#include "utils.h"
|
|
|
|
#include "path.h"
|
|
|
|
|
2014-06-05 09:34:36 +00:00
|
|
|
struct path* path_create(struct node *in, struct node *out)
|
2014-06-05 09:34:29 +00:00
|
|
|
{
|
|
|
|
struct path *p = malloc(sizeof(struct path));
|
|
|
|
if (!p)
|
|
|
|
return NULL;
|
|
|
|
|
2014-06-05 09:34:36 +00:00
|
|
|
memset(p, 0, sizeof(struct path));
|
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
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void path_destroy(struct path *p)
|
|
|
|
{
|
|
|
|
if (!p)
|
|
|
|
return;
|
|
|
|
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void * path_run(void *arg)
|
|
|
|
{
|
|
|
|
struct path *p = (struct path *) arg;
|
|
|
|
struct pollfd pfd;
|
|
|
|
struct msg m;
|
|
|
|
|
|
|
|
pfd.fd = p->in->sd;
|
|
|
|
pfd.events = POLLIN;
|
|
|
|
|
2014-06-05 09:34:46 +00:00
|
|
|
debug(1, "Established path: %12s => %s => %-12s", p->in->name, NAME, p->out->name);
|
2014-06-05 09:34:29 +00:00
|
|
|
|
|
|
|
/* main thread loop */
|
|
|
|
while (p->state == RUNNING) {
|
|
|
|
/* wait for new incoming messages */
|
|
|
|
//poll(&pfd, 1, 1);
|
|
|
|
|
|
|
|
/* receive message */
|
|
|
|
node_recv(p->in, &m);
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
|
|
/* send messages */
|
2014-06-05 09:34:38 +00:00
|
|
|
node_send(p->out, &m);
|
2014-06-05 09:34:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int path_start(struct path *p)
|
|
|
|
{
|
2014-06-05 09:34:38 +00:00
|
|
|
if (!p)
|
|
|
|
return -EFAULT;
|
|
|
|
|
2014-06-05 09:34:29 +00:00
|
|
|
p->state = RUNNING;
|
|
|
|
pthread_create(&p->tid, NULL, &path_run, (void *) p);
|
|
|
|
}
|
|
|
|
|
|
|
|
int path_stop(struct path *p)
|
|
|
|
{
|
|
|
|
void * ret;
|
|
|
|
|
2014-06-05 09:34:38 +00:00
|
|
|
if (!p)
|
|
|
|
return -EFAULT;
|
|
|
|
|
2014-06-05 09:34:29 +00:00
|
|
|
p->state = STOPPED;
|
|
|
|
|
|
|
|
pthread_cancel(p->tid);
|
|
|
|
pthread_join(p->tid, &ret);
|
|
|
|
|
|
|
|
return 0; // TODO
|
|
|
|
}
|