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/nodes/loopback_internal.cpp

106 lines
2.3 KiB
C++
Raw Normal View History

/** Node-type for internal loopback_internal connections.
*
* @author Steffen Vogel <post@steffenvogel.de>
2022-03-15 09:28:57 -04:00
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
2022-07-04 18:20:03 +02:00
* @license Apache 2.0
*********************************************************************************/
#include <cstring>
#include <villas/node/config.hpp>
#include <villas/nodes/loopback_internal.hpp>
#include <villas/exceptions.hpp>
#include <villas/node/memory.hpp>
#include <villas/uuid.hpp>
#include <villas/colors.hpp>
using namespace villas;
2021-05-10 00:12:30 +02:00
using namespace villas::node;
static InternalLoopbackNodeFactory nf;
InternalLoopbackNode::InternalLoopbackNode(Node *src, unsigned id, unsigned ql) :
queuelen(ql),
source(src)
{
auto name = fmt::format("{}.lo{}", src->getNameShort(), id);
uuid_t uuid;
int ret = uuid::generateFromString(uuid, fmt::format("lo{}", id), src->getUuid());
if (ret)
throw RuntimeError("Failed to initialize UUID");
Node(uuid, name);
factory = &nf;
name_long = fmt::format(CLR_RED("{}") "(" CLR_YEL("{}") ")", name_short, nf.getName());
auto logger_name = fmt::format("node:{}", getNameShort());
logger = logging.get(logger_name);
in.signals = source->getInputSignals(false);
ret = queue_signalled_init(&queue, queuelen);
2020-09-10 17:35:35 +02:00
if (ret)
throw RuntimeError("Failed to initialize queue");
state = State::PREPARED;
}
InternalLoopbackNode::~InternalLoopbackNode()
{
int ret __attribute__((unused));
ret = queue_signalled_destroy(&queue);
}
int InternalLoopbackNode::stop()
{
int ret;
ret = queue_signalled_close(&queue);
if (ret)
return ret;
return 0;
}
int InternalLoopbackNode::_read(struct Sample * smps[], unsigned cnt)
{
int avail;
struct Sample *cpys[cnt];
avail = queue_signalled_pull_many(&queue, (void **) cpys, cnt);
2021-06-23 20:05:11 +02:00
sample_copy_many(smps, cpys, avail);
sample_decref_many(cpys, avail);
return avail;
}
int InternalLoopbackNode::_write(struct Sample * smps[], unsigned cnt)
{
sample_incref_many(smps, cnt);
int pushed = queue_signalled_push_many(&queue, (void **) smps, cnt);
if (pushed < 0) {
sample_decref_many(smps, cnt);
return pushed;
}
/* Released unpushed samples */
if ((unsigned) pushed < cnt) {
sample_decref_many(smps + pushed, cnt - pushed);
logger->warn("Queue overrun");
}
return pushed;
}
std::vector<int> InternalLoopbackNode::getPollFDs()
{
return { queue_signalled_fd(&queue) };
}