1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-16 00:00:02 +01:00
VILLASnode/server/src/path.c

226 lines
5 KiB
C
Raw Normal View History

/** Message paths.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2015-06-02 21:53:04 +02:00
* @copyright 2014-2015, Institute for Automation of Complex Power Systems, EONERC
* This file is part of S2SS. All Rights Reserved. Proprietary and confidential.
2015-08-07 01:11:43 +02:00
* Unauthorized copying of this file, via any medium is strictly prohibited.
2015-06-02 21:53:04 +02:00
*********************************************************************************/
#include <stdlib.h>
#include <unistd.h>
#include "utils.h"
#include "path.h"
#include "timing.h"
2015-06-02 22:04:03 +02:00
#include "config.h"
2015-03-21 15:29:00 +01:00
#ifndef sigev_notify_thread_id
2015-09-19 12:24:11 +02:00
#define sigev_notify_thread_id _sigev_un._tid
2015-03-21 15:29:00 +01:00
#endif
2015-06-02 22:04:03 +02:00
extern struct settings settings;
2014-12-05 12:39:52 +01:00
static void path_write(struct path *p)
{
list_foreach(struct node *n, &p->destinations) {
int sent = node_write(
n, /* Destination node */
p->pool, /* Pool of received messages */
p->poolsize, /* Size of the pool */
p->received - n->combine,/* Index of the first message which should be sent */
n->combine /* Number of messages which should be sent */
);
debug(1, "Sent %u messages to node '%s'", sent, n->name);
p->sent += sent;
2015-08-07 01:11:43 +02:00
clock_gettime(CLOCK_REALTIME, &p->ts_sent);
}
}
2015-09-19 12:20:17 +02:00
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)
ret += ((hook_cb_t) h->cb)(p, h, t);
}
2015-09-19 12:20:17 +02:00
return ret;
}
/** Send messages asynchronously */
static void * path_run_async(void *arg)
{
2015-03-21 15:30:42 +01:00
struct path *p = arg;
/* Block until 1/p->rate seconds elapsed */
while (timerfd_wait(p->tfd)) {
if (path_run_hook(p, HOOK_ASYNC))
continue;
if (p->received > 0)
path_write(p);
}
return NULL;
}
/** Receive messages */
static void * path_run(void *arg)
{
2015-03-17 23:23:13 +01:00
struct path *p = arg;
2015-08-07 01:11:43 +02:00
/* Allocate memory for message pool */
p->pool = alloc(p->poolsize * sizeof(struct msg));
2015-05-07 13:01:18 +02:00
p->previous = p->current = p->pool;
/* Main thread loop */
for(;;) {
/* Receive message */
int recv = node_read(p->in, p->pool, p->poolsize, p->received, p->in->combine);
2015-10-09 15:49:26 +02:00
if (recv < 0)
error("Failed to receive message from node '%s'", p->in->name);
else if (recv == 0)
continue;
2015-08-07 01:11:43 +02:00
/** @todo Replace this timestamp by hardware timestamping */
clock_gettime(CLOCK_REALTIME, &p->ts_recv);
2015-08-07 01:11:43 +02:00
debug(10, "Received %u messages from node '%s'", recv, p->in->name);
2015-08-07 01:11:43 +02:00
/* Run preprocessing hooks */
2015-09-19 12:20:17 +02:00
if (path_run_hook(p, HOOK_PRE)) {
p->skipped += recv;
continue;
}
2015-08-07 01:11:43 +02:00
/* For each received message... */
for (int i = 0; i < recv; i++) {
2015-08-07 01:11:43 +02:00
p->previous = p->current;
2015-10-09 13:04:52 +02:00
p->current = &p->pool[p->received % p->poolsize];
2015-08-07 01:11:43 +02:00
p->received++;
2015-08-07 01:11:43 +02:00
/* Run hooks for filtering, stats collection and manipulation */
2015-09-19 12:20:17 +02:00
if (path_run_hook(p, HOOK_MSG)) {
p->skipped++;
continue;
}
}
2015-08-07 01:11:43 +02:00
/* Run post processing hooks */
2015-09-19 12:20:17 +02:00
if (path_run_hook(p, HOOK_POST)) {
p->skipped += recv;
continue;
}
2015-08-07 01:11:43 +02:00
/* At fixed rate mode, messages are send by another thread */
if (!p->rate)
path_write(p);
}
return NULL;
}
int path_start(struct path *p)
2014-12-05 12:39:52 +01:00
{ INDENT
char *buf = path_print(p);
info("Starting path: %s (poolsize = %u, msgsize = %u, #hooks = %zu)", buf, p->poolsize, p->msgsize, list_length(&p->hooks));
free(buf);
/* We sort the hooks according to their priority before starting the path */
int hook_cmp(const void *a, const void *b) {
return ((struct hook *) a)->priority - ((struct hook *) b)->priority;
}
list_sort(&p->hooks, hook_cmp);
2015-08-07 01:11:43 +02:00
2015-09-19 12:20:17 +02:00
if (path_run_hook(p, HOOK_PATH_START))
return -1;
2014-12-05 12:39:52 +01:00
/* At fixed rate mode, we start another thread for sending */
2015-06-10 15:13:02 +02:00
if (p->rate) {
struct itimerspec its = {
.it_interval = time_from_double(1 / p->rate),
.it_value = { 1, 0 }
};
p->tfd = timerfd_create(CLOCK_REALTIME, 0);
if (p->tfd < 0)
serror("Failed to create timer");
if (timerfd_settime(p->tfd, 0, &its, NULL))
serror("Failed to start timer");
pthread_create(&p->sent_tid, NULL, &path_run_async, p);
2015-06-10 15:13:02 +02:00
}
2015-03-21 15:31:42 +01:00
return pthread_create(&p->recv_tid, NULL, &path_run, p);
}
int path_stop(struct path *p)
2014-12-05 12:39:52 +01:00
{ INDENT
char *buf = path_print(p);
info("Stopping path: %s", buf);
free(buf);
2015-08-07 01:11:43 +02:00
pthread_cancel(p->recv_tid);
pthread_join(p->recv_tid, NULL);
if (p->rate) {
pthread_cancel(p->sent_tid);
pthread_join(p->sent_tid, NULL);
2015-03-21 15:30:42 +01:00
close(p->tfd);
}
2015-08-07 01:11:43 +02:00
2015-09-19 12:20:17 +02:00
if (path_run_hook(p, HOOK_PATH_STOP))
return -1;
return 0;
}
char * path_print(struct path *p)
{
char *buf = alloc(32);
strcatf(&buf, "%s " MAG("=>"), p->in->name);
2015-08-07 01:11:43 +02:00
if (list_length(&p->destinations) > 1) {
strcatf(&buf, " [");
list_foreach(struct node *n, &p->destinations)
strcatf(&buf, " %s", n->name);
strcatf(&buf, " ]");
}
else
strcatf(&buf, " %s", p->out->name);
2015-08-07 01:11:43 +02:00
return buf;
}
struct path * path_create()
{
struct path *p = alloc(sizeof(struct path));
list_init(&p->destinations, NULL);
list_init(&p->hooks, free);
2015-08-07 01:11:43 +02:00
list_foreach(struct hook *h, &hooks) {
if (h->type & HOOK_INTERNAL)
list_push(&p->hooks, memdup(h, sizeof(*h)));
}
return p;
}
void path_destroy(struct path *p)
{
path_run_hook(p, HOOK_DEINIT);
list_destroy(&p->destinations);
list_destroy(&p->hooks);
2015-08-07 01:11:43 +02:00
free(p->pool);
free(p);
}