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/lib/path.c

754 lines
18 KiB
C
Raw Normal View History

/** Message paths.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2019-01-13 00:42:39 +01:00
* @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC
2017-04-27 12:56:43 +02:00
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
2017-04-27 12:56:43 +02:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
2017-04-27 12:56:43 +02:00
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2015-06-02 21:53:04 +02:00
*********************************************************************************/
2016-06-08 22:38:21 +02:00
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
2016-06-08 22:38:21 +02:00
#include <string.h>
#include <inttypes.h>
2017-08-30 23:53:35 +02:00
#include <poll.h>
#include <villas/node/config.h>
2017-12-09 02:19:28 +08:00
#include <villas/utils.h>
#include <villas/timing.h>
#include <villas/pool.h>
#include <villas/queue.h>
#include <villas/hook_list.h>
2017-12-09 02:19:28 +08:00
#include <villas/plugin.h>
#include <villas/memory.h>
#include <villas/stats.h>
#include <villas/node.h>
2018-08-20 18:31:27 +02:00
#include <villas/signal.h>
#include <villas/path.h>
#include <villas/path_source.h>
#include <villas/path_destination.h>
static void * path_run_single(void *arg)
{
2019-02-11 16:39:30 +01:00
int ret;
struct path *p = arg;
2019-01-07 10:28:55 +01:00
struct path_source *ps = (struct path_source *) vlist_at(&p->sources, 0);
2019-02-11 16:39:30 +01:00
while (p->state == STATE_STARTED) {
pthread_testcancel();
ret = path_source_read(ps, p, 0);
if (ret <= 0)
continue;
2019-01-07 10:28:55 +01:00
for (size_t i = 0; i < vlist_length(&p->destinations); i++) {
struct path_destination *pd = (struct path_destination *) vlist_at(&p->destinations, i);
path_destination_write(pd, p);
}
}
return NULL;
}
/** Main thread function per path: read samples -> write samples */
static void * path_run_poll(void *arg)
2016-11-07 22:17:45 -05:00
{
int ret;
2016-11-07 22:17:45 -05:00
struct path *p = arg;
2019-02-11 16:39:30 +01:00
while (p->state == STATE_STARTED) {
2017-10-16 23:07:42 +02:00
ret = poll(p->reader.pfds, p->reader.nfds, -1);
if (ret < 0)
serror("Failed to poll");
debug(10, "Path %s returned from poll(2)", path_name(p));
2017-10-16 23:07:42 +02:00
for (int i = 0; i < p->reader.nfds; i++) {
2019-01-07 10:28:55 +01:00
struct path_source *ps = (struct path_source *) vlist_at(&p->sources, i);
2017-10-16 23:07:42 +02:00
if (p->reader.pfds[i].revents & POLLIN) {
/* Timeout: re-enqueue the last sample */
if (p->reader.pfds[i].fd == task_fd(&p->timeout)) {
task_wait(&p->timeout);
p->last_sample->sequence = p->last_sequence++;
path_destination_enqueue(p, &p->last_sample, 1);
}
/* A source is ready to receive samples */
2019-02-11 16:39:30 +01:00
else
path_source_read(ps, p, i);
2017-10-16 23:07:42 +02:00
}
}
2019-01-07 10:28:55 +01:00
for (size_t i = 0; i < vlist_length(&p->destinations); i++) {
struct path_destination *pd = (struct path_destination *) vlist_at(&p->destinations, i);
2017-10-16 23:07:42 +02:00
path_destination_write(pd, p);
2017-10-16 23:07:42 +02:00
}
2016-11-07 22:17:45 -05:00
}
return NULL;
}
int path_init(struct path *p)
2015-12-11 17:56:14 +01:00
{
2018-08-20 18:31:27 +02:00
int ret;
assert(p->state == STATE_DESTROYED);
2014-12-05 12:39:52 +01:00
2019-01-07 10:28:55 +01:00
ret = vlist_init(&p->destinations);
2018-08-20 18:31:27 +02:00
if (ret)
return ret;
2019-01-07 10:28:55 +01:00
ret = vlist_init(&p->sources);
2018-08-20 18:31:27 +02:00
if (ret)
return ret;
ret = signal_list_init(&p->signals);
2018-08-20 18:31:27 +02:00
if (ret)
return ret;
ret = hook_list_init(&p->hooks);
2018-08-20 18:31:27 +02:00
if (ret)
return ret;
2017-09-02 14:20:38 +02:00
2017-08-27 18:01:12 +02:00
p->_name = NULL;
/* Default values */
2017-10-16 23:07:42 +02:00
p->mode = PATH_MODE_ANY;
p->rate = 0; /* Disabled */
p->builtin = 1;
p->reverse = 0;
p->enabled = 1;
p->poll = -1;
p->queuelen = DEFAULT_QUEUE_LENGTH;
p->original_sequence_no = 0;
p->state = STATE_INITIALIZED;
2016-06-08 22:38:21 +02:00
return 0;
}
static int path_prepare_poll(struct path *p)
{
int fds[16], ret, n = 0, m;
p->reader.pfds = NULL;
p->reader.nfds = 0;
2019-01-07 10:28:55 +01:00
for (int i = 0; i < vlist_length(&p->sources); i++) {
struct path_source *ps = (struct path_source *) vlist_at(&p->sources, i);
m = node_poll_fds(ps->node, fds);
if (m < 0)
continue;
p->reader.nfds += m;
p->reader.pfds = realloc(p->reader.pfds, p->reader.nfds * sizeof(struct pollfd));
for (int i = 0; i < m; i++) {
if (fds[i] < 0)
error("Failed to get file descriptor for node %s", node_name(ps->node));
/* This slot is only used if it is not masked */
p->reader.pfds[n].events = POLLIN;
p->reader.pfds[n++].fd = fds[i];
}
}
/* We use the last slot for the timeout timer. */
if (p->rate > 0) {
ret = task_init(&p->timeout, p->rate, CLOCK_MONOTONIC);
if (ret)
return ret;
p->reader.nfds++;
p->reader.pfds = realloc(p->reader.pfds, p->reader.nfds * sizeof(struct pollfd));
p->reader.pfds[p->reader.nfds-1].events = POLLIN;
p->reader.pfds[p->reader.nfds-1].fd = task_fd(&p->timeout);
if (p->reader.pfds[p->reader.nfds-1].fd < 0)
2018-05-23 09:05:49 +02:00
error("Failed to get file descriptor for timer of path %s", path_name(p));
}
return 0;
}
2019-02-24 09:06:48 +01:00
int path_prepare(struct path *p)
2017-08-30 23:53:35 +02:00
{
int ret;
2017-08-30 23:53:35 +02:00
assert(p->state == STATE_CHECKED);
#ifdef WITH_HOOKS
int m = p->builtin ? HOOK_PATH | HOOK_BUILTIN : 0;
2018-08-17 12:40:03 +02:00
/* Add internal hooks if they are not already in the list */
ret = hook_list_prepare(&p->hooks, &p->signals, m, p, NULL);
2018-08-17 12:40:03 +02:00
if (ret)
return ret;
#endif /* WITH_HOOKS */
2017-08-30 23:53:35 +02:00
/* Initialize destinations */
2018-08-07 09:16:17 +02:00
struct memory_type *pool_mt = &memory_hugepage;
2019-01-07 10:28:55 +01:00
int pool_size = MAX(1, vlist_length(&p->destinations)) * p->queuelen;
2019-01-07 10:28:55 +01:00
for (size_t i = 0; i < vlist_length(&p->destinations); i++) {
struct path_destination *pd = (struct path_destination *) vlist_at(&p->destinations, i);
2017-08-30 23:53:35 +02:00
2019-02-24 11:08:56 +01:00
if (node_type(pd->node)->pool_size > pool_size)
pool_size = node_type(pd->node)->pool_size;
2018-08-07 09:16:17 +02:00
2019-02-24 11:08:56 +01:00
if (node_type(pd->node)->memory_type)
2018-08-07 09:16:17 +02:00
pool_mt = node_memory_type(pd->node, &memory_hugepage);
ret = path_destination_init(pd, p->queuelen);
2017-08-30 23:53:35 +02:00
if (ret)
return ret;
}
2019-01-07 10:28:55 +01:00
bitset_init(&p->received, vlist_length(&p->sources));
bitset_init(&p->mask, vlist_length(&p->sources));
2018-08-20 18:31:27 +02:00
2017-08-30 23:53:35 +02:00
/* Initialize sources */
2019-01-07 10:28:55 +01:00
for (size_t i = 0; i < vlist_length(&p->sources); i++) {
struct path_source *ps = (struct path_source *) vlist_at(&p->sources, i);
2017-08-30 23:53:35 +02:00
ret = path_source_init(ps);
2017-08-30 23:53:35 +02:00
if (ret)
return ret;
2017-10-18 09:31:31 +02:00
if (ps->masked)
bitset_set(&p->mask, i);
ret = mapping_list_prepare(&ps->mappings);
if (ret)
return ret;
2019-01-07 10:28:55 +01:00
for (size_t i = 0; i < vlist_length(&ps->mappings); i++) {
struct mapping_entry *me = (struct mapping_entry *) vlist_at(&ps->mappings, i);
struct vlist *sigs = node_get_signals(me->node, NODE_DIR_IN);
for (int j = 0; j < me->length; j++) {
2018-08-20 18:31:27 +02:00
struct signal *sig;
/* For data mappings we simple refer to the existing
* signal descriptors of the source node. */
if (me->type == MAPPING_TYPE_DATA) {
sig = (struct signal *) vlist_at_safe(sigs, me->data.offset + j);
2018-08-20 18:31:27 +02:00
if (!sig) {
warning("Failed to create signal description for path %s", path_name(p));
2018-08-20 18:31:27 +02:00
continue;
}
signal_incref(sig);
}
/* For other mappings we create new signal descriptors */
else {
sig = alloc(sizeof(struct signal));
ret = signal_init_from_mapping(sig, me, j);
if (ret)
return -1;
}
vlist_extend(&p->signals, me->offset + j + 1, NULL);
vlist_set(&p->signals, me->offset + j, sig);
2018-08-20 18:31:27 +02:00
}
}
2017-08-30 23:53:35 +02:00
}
2019-01-07 10:28:55 +01:00
ret = pool_init(&p->pool, pool_size, SAMPLE_LENGTH(vlist_length(&p->signals)), pool_mt);
2017-08-30 23:53:35 +02:00
if (ret)
return ret;
2017-09-02 14:20:38 +02:00
2017-08-30 23:53:35 +02:00
/* Prepare poll() */
if (p->poll) {
ret = path_prepare_poll(p);
2017-10-16 23:07:42 +02:00
if (ret)
return ret;
2017-08-30 23:53:35 +02:00
}
2019-02-24 11:13:28 +01:00
p->state = STATE_PREPARED;
2017-08-30 23:53:35 +02:00
return 0;
}
2019-01-07 10:28:55 +01:00
int path_parse(struct path *p, json_t *cfg, struct vlist *nodes)
2015-12-11 17:56:14 +01:00
{
int ret;
json_error_t err;
2017-08-28 14:38:30 +02:00
json_t *json_in;
json_t *json_out = NULL;
json_t *json_hooks = NULL;
2017-10-16 23:07:42 +02:00
json_t *json_mask = NULL;
const char *mode = NULL;
2015-08-07 01:11:43 +02:00
2019-01-07 10:28:55 +01:00
struct vlist sources = { .state = STATE_DESTROYED };
struct vlist destinations = { .state = STATE_DESTROYED };
2019-01-07 10:28:55 +01:00
vlist_init(&sources);
vlist_init(&destinations);
ret = json_unpack_ex(cfg, &err, 0, "{ s: o, s?: o, s?: o, s?: b, s?: b, s?: b, s?: i, s?: s, s?: b, s?: F, s?: o, s?: b}",
2017-08-28 14:38:30 +02:00
"in", &json_in,
"out", &json_out,
"hooks", &json_hooks,
"reverse", &p->reverse,
"enabled", &p->enabled,
"builtin", &p->builtin,
2017-10-16 23:07:42 +02:00
"queuelen", &p->queuelen,
"mode", &mode,
"poll", &p->poll,
2017-10-16 23:07:42 +02:00
"rate", &p->rate,
"mask", &json_mask,
"original_sequence_no", &p->original_sequence_no
);
if (ret)
jerror(&err, "Failed to parse path configuration");
2017-08-28 14:38:30 +02:00
/* Input node(s) */
ret = mapping_list_parse(&sources, json_in, nodes);
2017-08-28 14:38:30 +02:00
if (ret)
error("Failed to parse input mapping of path %s", path_name(p));
/* Optional settings */
2017-10-16 23:07:42 +02:00
if (mode) {
if (!strcmp(mode, "any"))
p->mode = PATH_MODE_ANY;
else if (!strcmp(mode, "all"))
p->mode = PATH_MODE_ALL;
else
error("Invalid path mode '%s'", mode);
}
/* Output node(s) */
2017-08-28 14:38:30 +02:00
if (json_out) {
ret = node_list_parse(&destinations, json_out, nodes);
if (ret)
jerror(&err, "Failed to parse output nodes");
}
2019-01-07 10:28:55 +01:00
for (size_t i = 0; i < vlist_length(&sources); i++) {
struct mapping_entry *me = (struct mapping_entry *) vlist_at(&sources, i);
2017-08-30 23:53:35 +02:00
struct path_source *ps = NULL;
2017-09-02 14:20:38 +02:00
2017-08-30 23:53:35 +02:00
/* Check if there is already a path_source for this source */
2019-01-07 10:28:55 +01:00
for (size_t j = 0; j < vlist_length(&p->sources); j++) {
struct path_source *pt = (struct path_source *) vlist_at(&p->sources, j);
2017-09-02 14:20:38 +02:00
2017-08-30 23:53:35 +02:00
if (pt->node == me->node) {
ps = pt;
break;
}
}
2017-09-02 14:20:38 +02:00
2018-08-02 10:50:23 +02:00
/* Create new path_source of not existing */
2017-08-30 23:53:35 +02:00
if (!ps) {
ps = alloc(sizeof(struct path_source));
2017-09-02 14:20:38 +02:00
2017-08-30 23:53:35 +02:00
ps->node = me->node;
2017-10-18 09:31:31 +02:00
ps->masked = false;
ps->mappings.state = STATE_DESTROYED;
2019-01-07 10:28:55 +01:00
vlist_init(&ps->mappings);
2019-01-07 10:28:55 +01:00
vlist_push(&p->sources, ps);
2017-08-30 23:53:35 +02:00
}
2019-02-24 11:08:15 +01:00
if (!node_is_enabled(ps->node))
error("Source %s of path %s is not enabled", node_name(ps->node), path_name(p));
2019-01-07 10:28:55 +01:00
vlist_push(&ps->mappings, me);
}
2019-01-07 10:28:55 +01:00
for (size_t i = 0; i < vlist_length(&destinations); i++) {
struct node *n = (struct node *) vlist_at(&destinations, i);
2017-10-18 15:39:53 +02:00
struct path_destination *pd = (struct path_destination *) alloc(sizeof(struct path_destination));
2017-07-06 23:48:19 +02:00
pd->node = n;
2019-02-24 11:08:15 +01:00
if (!node_is_enabled(pd->node))
error("Destination %s of path %s is not enabled", node_name(pd->node), path_name(p));
2019-01-07 10:28:55 +01:00
vlist_push(&p->destinations, pd);
}
2017-10-16 23:07:42 +02:00
if (json_mask) {
json_t *json_entry;
size_t i;
if (!json_is_array(json_mask))
error("The 'mask' setting must be a list of node names");
json_array_foreach(json_mask, i, json_entry) {
const char *name;
struct node *node;
2017-10-18 09:31:31 +02:00
struct path_source *ps = NULL;
2017-10-16 23:07:42 +02:00
name = json_string_value(json_entry);
if (!name)
error("The 'mask' setting must be a list of node names");
2019-01-07 10:28:55 +01:00
node = vlist_lookup(nodes, name);
2017-10-16 23:07:42 +02:00
if (!node)
error("The 'mask' entry '%s' is not a valid node name", name);
/* Search correspondending path_source to node */
2019-01-07 10:28:55 +01:00
for (size_t i = 0; i < vlist_length(&p->sources); i++) {
struct path_source *pt = (struct path_source *) vlist_at(&p->sources, i);
2017-10-16 23:07:42 +02:00
2017-10-18 09:31:31 +02:00
if (pt->node == node) {
ps = pt;
2017-10-16 23:07:42 +02:00
break;
}
}
2017-10-18 09:31:31 +02:00
if (!ps)
2017-10-16 23:07:42 +02:00
error("Node %s is not a source of the path %s", node_name(node), path_name(p));
2017-10-18 09:31:31 +02:00
ps->masked = true;
2017-10-16 23:07:42 +02:00
}
}
2018-08-06 10:24:45 +02:00
/* Enable all by default */
else {
2019-01-07 10:28:55 +01:00
for (size_t i = 0; i < vlist_length(&p->sources); i++) {
struct path_source *ps = (struct path_source *) vlist_at(&p->sources, i);
2017-10-18 09:31:31 +02:00
ps->masked = true;
}
2017-10-16 23:07:42 +02:00
}
2018-06-21 09:37:01 +02:00
#ifdef WITH_HOOKS
2017-08-30 23:53:35 +02:00
if (json_hooks) {
ret = hook_list_parse(&p->hooks, json_hooks, HOOK_PATH, p, NULL);
2017-08-30 23:53:35 +02:00
if (ret)
return ret;
}
#endif /* WITH_HOOKS */
2017-08-30 23:53:35 +02:00
/* Autodetect whether to use poll() for this path or not */
if (p->poll == -1) {
2019-01-30 01:57:20 +01:00
if (p->rate > 0)
p->poll = 1;
else if (vlist_length(&p->sources) > 1)
p->poll = 1;
else
p->poll = 0;
}
2019-01-07 10:28:55 +01:00
ret = vlist_destroy(&sources, NULL, false);
if (ret)
return ret;
2019-01-07 10:28:55 +01:00
ret = vlist_destroy(&destinations, NULL, false);
if (ret)
return ret;
p->cfg = cfg;
p->state = STATE_PARSED;
return 0;
}
int path_check(struct path *p)
{
2017-03-13 23:51:38 -03:00
assert(p->state != STATE_DESTROYED);
2017-10-16 23:07:42 +02:00
if (p->rate < 0)
error("Setting 'rate' of path %s must be a positive number.", path_name(p));
if (p->poll) {
if (p->rate <= 0) {
/* Check that all path sources provide a file descriptor for polling */
for (size_t i = 0; i < vlist_length(&p->sources); i++) {
struct path_source *ps = (struct path_source *) vlist_at(&p->sources, i);
2019-02-24 11:08:56 +01:00
if (!node_type(ps->node)->poll_fds)
error("Node %s can not be used in polling mode with path %s", node_name(ps->node), path_name(p));
}
}
}
else {
/* Check that we do not need to multiplex between multiple sources when polling is disabled */
if (vlist_length(&p->sources) > 1)
error("Setting 'poll' must be active if the path has more than one source");
/* Check that we do not use the fixed rate feature when polling is disabled */
if (p->rate > 0)
error("Setting 'poll' must be activated when used together with setting 'rate'");
}
2019-01-07 10:28:55 +01:00
for (size_t i = 0; i < vlist_length(&p->sources); i++) {
struct path_source *ps = (struct path_source *) vlist_at(&p->sources, i);
2017-08-28 14:38:30 +02:00
2019-02-24 11:08:56 +01:00
if (!node_type(ps->node)->read)
error("Node %s is not supported as a source for path %s", node_name(ps->node), path_name(p));
2017-08-28 14:38:30 +02:00
}
2019-01-07 10:28:55 +01:00
for (size_t i = 0; i < vlist_length(&p->destinations); i++) {
struct path_destination *pd = (struct path_destination *) vlist_at(&p->destinations, i);
2019-02-24 11:08:56 +01:00
if (!node_type(pd->node)->write)
error("Destiation node %s is not supported as a sink for path %s", node_name(pd->node), path_name(p));
}
2017-08-28 14:38:30 +02:00
if (!IS_POW2(p->queuelen)) {
p->queuelen = LOG2_CEIL(p->queuelen);
warning("Queue length should always be a power of 2. Adjusting to %d", p->queuelen);
2017-08-28 14:38:30 +02:00
}
p->state = STATE_CHECKED;
return 0;
}
int path_start(struct path *p)
2016-11-07 22:17:45 -05:00
{
int ret;
2017-10-18 09:31:31 +02:00
char *mode, *mask;
2019-02-24 11:13:28 +01:00
assert(p->state == STATE_PREPARED);
2017-10-16 23:07:42 +02:00
switch (p->mode) {
2017-10-18 09:31:31 +02:00
case PATH_MODE_ANY: mode = "any"; break;
case PATH_MODE_ALL: mode = "all"; break;
default: mode = "unknown"; break;
2017-10-16 23:07:42 +02:00
}
2017-10-18 09:31:31 +02:00
mask = bitset_dump(&p->mask);
info("Starting path %s: #signals=%zu, mode=%s, poll=%s, mask=%s, rate=%.2f, enabled=%s, reversed=%s, queuelen=%d, #hooks=%zu, #sources=%zu, #destinations=%zu, original_sequence_no=%s",
2017-08-31 11:31:43 +02:00
path_name(p),
2019-01-07 10:28:55 +01:00
vlist_length(&p->signals),
2017-10-16 23:07:42 +02:00
mode,
p->poll ? "yes" : "no",
2017-10-18 09:31:31 +02:00
mask,
2017-10-16 23:07:42 +02:00
p->rate,
path_is_enabled(p) ? "yes" : "no",
path_is_reversed(p) ? "yes" : "no",
2018-08-20 18:31:27 +02:00
p->queuelen,
2019-01-07 10:28:55 +01:00
vlist_length(&p->hooks),
vlist_length(&p->sources),
vlist_length(&p->destinations),
p->original_sequence_no ? "yes" : "no"
2017-08-31 11:31:43 +02:00
);
2017-10-18 09:31:31 +02:00
free(mask);
#ifdef WITH_HOOKS
ret = hook_list_start(&p->hooks);
if (ret)
return ret;
#endif /* WITH_HOOKS */
2017-09-02 14:20:38 +02:00
2017-10-16 23:07:42 +02:00
p->last_sequence = 0;
2017-10-18 09:31:31 +02:00
bitset_clear_all(&p->received);
2017-03-13 23:51:38 -03:00
2018-08-20 18:31:27 +02:00
/* We initialize the intial sample */
p->last_sample = sample_alloc(&p->pool);
if (!p->last_sample)
return -1;
2019-01-07 10:28:55 +01:00
p->last_sample->length = vlist_length(&p->signals);
2018-08-20 18:31:27 +02:00
p->last_sample->signals = &p->signals;
p->last_sample->sequence = 0;
p->last_sample->flags = p->last_sample->length > 0 ? SAMPLE_HAS_DATA : 0;
2018-08-20 18:31:27 +02:00
for (size_t i = 0; i < p->last_sample->length; i++) {
2019-01-07 10:28:55 +01:00
struct signal *sig = (struct signal *) vlist_at(p->last_sample->signals, i);
2018-08-20 18:31:27 +02:00
p->last_sample->data[i] = sig->init;
}
/* Start one thread per path for sending to destinations
*
* Special case: If the path only has a single source and this source
* does not offer a file descriptor for polling, we will use a special
* thread function.
*/
ret = pthread_create(&p->tid, NULL, p->poll ? path_run_poll : path_run_single, p);
if (ret)
return ret;
p->state = STATE_STARTED;
return 0;
2016-11-07 22:17:45 -05:00
}
int path_stop(struct path *p)
2016-11-07 22:17:45 -05:00
{
int ret;
2019-02-11 16:39:30 +01:00
if (p->state != STATE_STARTED && p->state != STATE_STOPPING)
return 0;
info("Stopping path: %s", path_name(p));
2019-02-11 16:39:30 +01:00
if (p->state != STATE_STOPPING)
p->state = STATE_STOPPING;
2017-09-03 10:52:46 +02:00
2019-02-12 18:17:09 +01:00
/* Cancel the thread in case is currently in a blocking syscall */
ret = pthread_cancel(p->tid);
if (ret)
return ret;
2017-09-03 10:52:46 +02:00
ret = pthread_join(p->tid, NULL);
if (ret)
return ret;
#ifdef WITH_HOOKS
ret = hook_list_stop(&p->hooks);
if (ret)
return ret;
#endif /* WITH_HOOKS */
2018-08-20 18:31:27 +02:00
sample_decref(p->last_sample);
p->state = STATE_STOPPED;
return 0;
2016-11-07 22:17:45 -05:00
}
int path_destroy(struct path *p)
2016-06-08 22:38:21 +02:00
{
int ret;
if (p->state == STATE_DESTROYED)
return 0;
#ifdef WITH_HOOKS
ret = hook_list_destroy(&p->hooks);
if (ret)
return ret;
#endif
ret = signal_list_destroy(&p->signals);
if (ret)
return ret;
ret = vlist_destroy(&p->sources, (dtor_cb_t) path_source_destroy, true);
if (ret)
return ret;
ret = vlist_destroy(&p->destinations, (dtor_cb_t) path_destination_destroy, true);
if (ret)
return ret;
2017-10-16 23:07:42 +02:00
if (p->reader.pfds)
free(p->reader.pfds);
if (p->_name)
free(p->_name);
2017-09-02 14:20:38 +02:00
2017-10-16 23:07:42 +02:00
if (p->rate > 0)
task_destroy(&p->timeout);
2017-08-30 23:53:35 +02:00
pool_destroy(&p->pool);
p->state = STATE_DESTROYED;
return 0;
}
const char * path_name(struct path *p)
{
if (!p->_name) {
2017-08-28 14:38:30 +02:00
strcatf(&p->_name, "[");
2019-01-07 10:28:55 +01:00
for (size_t i = 0; i < vlist_length(&p->sources); i++) {
struct path_source *ps = (struct path_source *) vlist_at(&p->sources, i);
2017-08-28 14:38:30 +02:00
strcatf(&p->_name, " %s", node_name_short(ps->node));
}
2017-09-02 14:20:38 +02:00
2017-08-28 14:38:30 +02:00
strcatf(&p->_name, " ] " CLR_MAG("=>") " [");
2019-01-07 10:28:55 +01:00
for (size_t i = 0; i < vlist_length(&p->destinations); i++) {
struct path_destination *pd = (struct path_destination *) vlist_at(&p->destinations, i);
2017-08-28 14:38:30 +02:00
strcatf(&p->_name, " %s", node_name_short(pd->node));
}
2017-08-28 14:38:30 +02:00
strcatf(&p->_name, " ]");
}
return p->_name;
}
2017-08-28 14:38:30 +02:00
int path_uses_node(struct path *p, struct node *n)
{
2019-01-07 10:28:55 +01:00
for (size_t i = 0; i < vlist_length(&p->destinations); i++) {
struct path_destination *pd = (struct path_destination *) vlist_at(&p->destinations, i);
2016-11-07 22:17:45 -05:00
if (pd->node == n)
return 0;
}
2017-09-02 14:20:38 +02:00
2019-01-07 10:28:55 +01:00
for (size_t i = 0; i < vlist_length(&p->sources); i++) {
struct path_source *ps = (struct path_source *) vlist_at(&p->sources, i);
2017-08-28 14:38:30 +02:00
if (ps->node == n)
return 0;
}
2016-11-07 22:17:45 -05:00
2017-08-28 14:38:30 +02:00
return -1;
}
2016-11-07 22:17:45 -05:00
bool path_is_simple(const struct path *p)
2016-11-07 22:17:45 -05:00
{
int ret;
const char *in = NULL, *out = NULL;
json_error_t err;
ret = json_unpack_ex(p->cfg, &err, 0, "{ s: s, s: s }", "in", &in, "out", &out);
if (ret)
return false;
ret = node_is_valid_name(in);
if (!ret)
return false;
ret = node_is_valid_name(out);
if (!ret)
return false;
2019-02-11 16:39:30 +01:00
return true;
}
bool path_is_enabled(const struct path *p)
{
return p->enabled;
}
bool path_is_reversed(const struct path *p)
{
return p->reverse;
2017-04-05 12:40:21 +02:00
}
2019-03-08 15:21:01 +01:00
struct vlist * path_get_signals(struct path *p)
{
return &p->signals;
}