1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/lib/nodes/loopback.cpp

170 lines
4.3 KiB
C++
Raw Permalink Normal View History

/** Node-type for loopback connections.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2020-01-20 17:17:00 +01:00
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
* @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.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <cstring>
2018-10-21 10:36:17 +02:00
#include <villas/node.h>
#include <villas/plugin.h>
#include <villas/node/config.h>
#include <villas/nodes/loopback.hpp>
#include <villas/exceptions.hpp>
#include <villas/memory.h>
using namespace villas;
2019-06-04 16:55:38 +02:00
using namespace villas::utils;
static struct plugin p;
int loopback_init(struct vnode *n)
{
struct loopback *l = (struct loopback *) n->_vd;
l->mode = QueueSignalledMode::AUTO;
l->queuelen = DEFAULT_QUEUE_LENGTH;
return 0;
}
2020-08-25 21:00:52 +02:00
int loopback_parse(struct vnode *n, json_t *cfg)
{
2017-10-18 15:39:53 +02:00
struct loopback *l = (struct loopback *) n->_vd;
2019-04-22 23:45:38 +02:00
const char *mode_str = nullptr;
json_error_t err;
int ret;
2020-09-10 17:35:35 +02:00
ret = json_unpack_ex(cfg, &err, 0, "{ s?: i, s?: s }",
"queuelen", &l->queuelen,
"mode", &mode_str
);
if (ret)
jerror(&err, "Failed to parse configuration of node %s", node_name(n));
if (mode_str) {
2018-10-20 18:47:21 +02:00
if (!strcmp(mode_str, "auto"))
2019-06-23 16:13:23 +02:00
l->mode = QueueSignalledMode::AUTO;
2018-10-21 19:08:19 +01:00
#ifdef HAVE_EVENTFD
2018-10-20 18:47:21 +02:00
else if (!strcmp(mode_str, "eventfd"))
2019-06-23 16:13:23 +02:00
l->mode = QueueSignalledMode::EVENTFD;
2018-10-21 19:08:19 +01:00
#endif
else if (!strcmp(mode_str, "pthread"))
2019-06-23 16:13:23 +02:00
l->mode = QueueSignalledMode::PTHREAD;
else if (!strcmp(mode_str, "polling"))
2019-06-23 16:13:23 +02:00
l->mode = QueueSignalledMode::POLLING;
#ifdef __APPLE__
else if (!strcmp(mode_str, "pipe"))
2019-06-23 16:13:23 +02:00
l->mode = QueueSignalledMode::PIPE;
#endif /* __APPLE__ */
else
error("Unknown mode '%s' in node %s", mode_str, node_name(n));
}
return 0;
}
int loopback_prepare(struct vnode *n)
{
2017-10-18 15:39:53 +02:00
struct loopback *l = (struct loopback *) n->_vd;
return queue_signalled_init(&l->queue, l->queuelen, memory_default, l->mode);
}
int loopback_destroy(struct vnode *n)
{
2017-10-18 15:39:53 +02:00
struct loopback *l= (struct loopback *) n->_vd;
return queue_signalled_destroy(&l->queue);
}
2020-08-25 21:00:52 +02:00
int loopback_read(struct vnode *n, struct sample *smps[], unsigned cnt, unsigned *release)
{
int avail;
2017-10-18 15:39:53 +02:00
struct loopback *l = (struct loopback *) n->_vd;
struct sample *cpys[cnt];
avail = queue_signalled_pull_many(&l->queue, (void **) cpys, cnt);
for (int i = 0; i < avail; i++) {
sample_copy(smps[i], cpys[i]);
sample_decref(cpys[i]);
}
return avail;
}
2020-08-25 21:00:52 +02:00
int loopback_write(struct vnode *n, struct sample *smps[], unsigned cnt, unsigned *release)
{
2017-10-18 15:39:53 +02:00
struct loopback *l = (struct loopback *) n->_vd;
sample_incref_many(smps, cnt);
return queue_signalled_push_many(&l->queue, (void **) smps, cnt);
}
2020-08-25 21:00:52 +02:00
char * loopback_print(struct vnode *n)
{
2017-10-18 15:39:53 +02:00
struct loopback *l = (struct loopback *) n->_vd;
2019-04-22 23:45:38 +02:00
char *buf = nullptr;
strcatf(&buf, "queuelen=%d", l->queuelen);
return buf;
2017-10-16 23:08:46 +02:00
}
2020-08-25 21:00:52 +02:00
int loopback_poll_fds(struct vnode *n, int fds[])
{
2017-10-18 15:39:53 +02:00
struct loopback *l = (struct loopback *) n->_vd;
2017-10-16 23:08:46 +02:00
fds[0] = queue_signalled_fd(&l->queue);
return 1;
}
__attribute__((constructor(110)))
static void register_plugin() {
p.name = "loopback";
p.description = "Loopback to connect multiple paths";
2019-06-23 16:13:23 +02:00
p.type = PluginType::NODE;
p.node.instances.state = State::DESTROYED;
p.node.vectorize = 0;
p.node.flags = 0;
p.node.size = sizeof(struct loopback);
p.node.parse = loopback_parse;
p.node.print = loopback_print;
p.node.prepare = loopback_prepare;
p.node.init = loopback_init;
p.node.destroy = loopback_destroy;
p.node.read = loopback_read;
p.node.write = loopback_write;
p.node.poll_fds = loopback_poll_fds;
int ret = vlist_init(&p.node.instances);
if (!ret)
vlist_init_and_push(&plugins, &p);
}
__attribute__((destructor(110)))
static void deregister_plugin() {
2020-06-16 02:35:34 +02:00
vlist_remove_all(&plugins, &p);
2019-06-04 16:55:38 +02:00
}