mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
loopback_internal: add new node-type for internal loopback
This commit is contained in:
parent
e8fad23baf
commit
88ef85844d
4 changed files with 230 additions and 2 deletions
|
@ -43,7 +43,8 @@ class SuperNode;
|
|||
}
|
||||
|
||||
enum class NodeFlags {
|
||||
PROVIDES_SIGNALS = (1 << 0)
|
||||
PROVIDES_SIGNALS = (1 << 0),
|
||||
INTERNAL = (1 << 1)
|
||||
};
|
||||
|
||||
/** C++ like vtable construct for node_types */
|
||||
|
|
67
include/villas/nodes/loopback_internal.hpp
Normal file
67
include/villas/nodes/loopback_internal.hpp
Normal file
|
@ -0,0 +1,67 @@
|
|||
/** Node-type for internal loopback connections.
|
||||
*
|
||||
* @file
|
||||
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
||||
* @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/>.
|
||||
*********************************************************************************/
|
||||
|
||||
/**
|
||||
* @ingroup node
|
||||
* @addtogroup loopback Loopback connections
|
||||
* @{
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <villas/queue_signalled.h>
|
||||
#include <villas/pool.h>
|
||||
|
||||
/* Forward declarations */
|
||||
struct vnode;
|
||||
struct sample;
|
||||
|
||||
/** Node-type for signal generation.
|
||||
* @see node_type
|
||||
*/
|
||||
struct loopback_internal {
|
||||
int queuelen;
|
||||
enum QueueSignalledMode mode;
|
||||
struct queue_signalled queue;
|
||||
|
||||
struct vnode *source;
|
||||
};
|
||||
|
||||
/** @see node_type::print */
|
||||
char * loopback_internal_print(struct vnode *n);
|
||||
|
||||
/** @see node_type::start */
|
||||
int loopback_internal_start(struct vnode *n);
|
||||
|
||||
/** @see node_type::stop */
|
||||
int loopback_internal_stop(struct vnode *n);
|
||||
|
||||
/** @see node_type::read */
|
||||
int loopback_internal_read(struct vnode *n, struct sample *smps[], unsigned cnt, unsigned *release);
|
||||
|
||||
/** @see node_type::write */
|
||||
int loopback_internal_write(struct vnode *n, struct sample *smps[], unsigned cnt, unsigned *release);
|
||||
|
||||
struct vnode * loopback_internal_create(struct vnode *orig);
|
||||
|
||||
/** @} */
|
|
@ -20,7 +20,9 @@
|
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
###################################################################################
|
||||
|
||||
set(NODE_SRC)
|
||||
set(NODE_SRC
|
||||
loopback_internal.cpp
|
||||
)
|
||||
|
||||
if(LIBNL3_ROUTE_FOUND)
|
||||
list(APPEND LIBRARIES PkgConfig::LIBNL3_ROUTE)
|
||||
|
|
158
lib/nodes/loopback_internal.cpp
Normal file
158
lib/nodes/loopback_internal.cpp
Normal file
|
@ -0,0 +1,158 @@
|
|||
/** Node-type for internal loopback_internal connections.
|
||||
*
|
||||
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
||||
* @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>
|
||||
|
||||
#include <villas/node.h>
|
||||
#include <villas/plugin.h>
|
||||
#include <villas/node/config.h>
|
||||
#include <villas/nodes/loopback_internal.hpp>
|
||||
#include <villas/exceptions.hpp>
|
||||
#include <villas/memory.h>
|
||||
|
||||
using namespace villas;
|
||||
using namespace villas::utils;
|
||||
|
||||
static struct plugin p;
|
||||
|
||||
int loopback_internal_init(struct vnode *n)
|
||||
{
|
||||
struct loopback_internal *l = (struct loopback_internal *) n->_vd;
|
||||
|
||||
l->mode = QueueSignalledMode::EVENTFD;
|
||||
l->queuelen = DEFAULT_QUEUE_LENGTH;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int loopback_internal_prepare(struct vnode *n)
|
||||
{
|
||||
struct loopback_internal *l = (struct loopback_internal *) n->_vd;
|
||||
|
||||
return queue_signalled_init(&l->queue, l->queuelen, memory_default, QueueSignalledMode::EVENTFD);
|
||||
}
|
||||
|
||||
int loopback_internal_destroy(struct vnode *n)
|
||||
{
|
||||
struct loopback_internal *l= (struct loopback_internal *) n->_vd;
|
||||
|
||||
return queue_signalled_destroy(&l->queue);
|
||||
}
|
||||
|
||||
int loopback_internal_read(struct vnode *n, struct sample *smps[], unsigned cnt, unsigned *release)
|
||||
{
|
||||
int avail;
|
||||
|
||||
struct loopback_internal *l = (struct loopback_internal *) 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;
|
||||
}
|
||||
|
||||
int loopback_internal_write(struct vnode *n, struct sample *smps[], unsigned cnt, unsigned *release)
|
||||
{
|
||||
struct loopback_internal *l = (struct loopback_internal *) n->_vd;
|
||||
|
||||
sample_incref_many(smps, cnt);
|
||||
|
||||
return queue_signalled_push_many(&l->queue, (void **) smps, cnt);
|
||||
}
|
||||
|
||||
char * loopback_internal_print(struct vnode *n)
|
||||
{
|
||||
struct loopback_internal *l = (struct loopback_internal *) n->_vd;
|
||||
char *buf = nullptr;
|
||||
|
||||
strcatf(&buf, "queuelen=%d", l->queuelen);
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
int loopback_internal_poll_fds(struct vnode *n, int fds[])
|
||||
{
|
||||
struct loopback_internal *l = (struct loopback_internal *) n->_vd;
|
||||
|
||||
fds[0] = queue_signalled_fd(&l->queue);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct vnode * loopback_internal_create(struct vnode *orig)
|
||||
{
|
||||
int ret;
|
||||
struct vnode *n;
|
||||
struct loopback_internal *l;
|
||||
|
||||
n = new struct vnode;
|
||||
if (!n)
|
||||
throw MemoryAllocationError();
|
||||
|
||||
l = (struct loopback_internal *) n->_vd;
|
||||
|
||||
ret = node_init(n, &p.node);
|
||||
if (ret)
|
||||
return nullptr;
|
||||
|
||||
l->source = orig;
|
||||
|
||||
asprintf(&n->name, "%s_lo%zu", node_name_short(orig), vlist_length(&orig->sources));
|
||||
|
||||
ret = signal_list_copy(&n->in.signals, &orig->in.signals);
|
||||
if (ret)
|
||||
return nullptr;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
__attribute__((constructor(110)))
|
||||
static void register_plugin() {
|
||||
p.name = "loopback_internal";
|
||||
p.description = "internal loopback to connect multiple paths";
|
||||
p.type = PluginType::NODE;
|
||||
p.node.instances.state = State::DESTROYED;
|
||||
p.node.vectorize = 0;
|
||||
p.node.flags = (int) NodeFlags::PROVIDES_SIGNALS | (int) NodeFlags::INTERNAL;
|
||||
p.node.size = sizeof(struct loopback_internal);
|
||||
p.node.print = loopback_internal_print;
|
||||
p.node.prepare = loopback_internal_prepare;
|
||||
p.node.init = loopback_internal_init;
|
||||
p.node.destroy = loopback_internal_destroy;
|
||||
p.node.read = loopback_internal_read;
|
||||
p.node.write = loopback_internal_write;
|
||||
p.node.poll_fds = loopback_internal_poll_fds;
|
||||
|
||||
int ret = vlist_init(&p.node.instances);
|
||||
if (!ret)
|
||||
vlist_init_and_push(&plugins, &p);
|
||||
}
|
||||
|
||||
__attribute__((destructor(110)))
|
||||
static void deregister_plugin() {
|
||||
vlist_remove_all(&plugins, &p);
|
||||
}
|
Loading…
Add table
Reference in a new issue