2017-07-14 13:12:32 +02:00
|
|
|
/** Node-type for loopback connections.
|
|
|
|
*
|
2022-03-15 09:18:01 -04:00
|
|
|
* @author Steffen Vogel <svogel2@eonerc.rwth-aachen.de>
|
2022-03-15 09:28:57 -04:00
|
|
|
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
|
2017-07-14 13:12:32 +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.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cstring>
|
2018-10-21 10:36:17 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/node/config.hpp>
|
|
|
|
#include <villas/node_compat.hpp>
|
2019-04-23 00:12:31 +02:00
|
|
|
#include <villas/nodes/loopback.hpp>
|
2020-09-10 11:16:23 +02:00
|
|
|
#include <villas/exceptions.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/node/memory.hpp>
|
2021-06-21 16:11:42 -04:00
|
|
|
#include <villas/utils.hpp>
|
2017-07-14 13:12:32 +02:00
|
|
|
|
2020-09-10 11:16:23 +02:00
|
|
|
using namespace villas;
|
2021-05-10 00:12:30 +02:00
|
|
|
using namespace villas::node;
|
2019-06-04 16:55:38 +02:00
|
|
|
using namespace villas::utils;
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
LoopbackNode::LoopbackNode(const std::string &name) :
|
|
|
|
Node(name),
|
|
|
|
queuelen(DEFAULT_QUEUE_LENGTH),
|
|
|
|
mode(QueueSignalledMode::AUTO)
|
2020-09-10 11:16:23 +02:00
|
|
|
{
|
2021-08-10 10:12:48 -04:00
|
|
|
queue.queue.state = State::DESTROYED;
|
|
|
|
}
|
2020-09-10 11:16:23 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
LoopbackNode::~LoopbackNode()
|
|
|
|
{
|
|
|
|
int ret __attribute__((unused));
|
2020-09-10 11:16:23 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
ret = queue_signalled_destroy(&queue);
|
2020-09-10 11:16:23 +02:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int LoopbackNode::prepare()
|
2017-07-14 13:12:32 +02:00
|
|
|
{
|
2021-08-10 10:12:48 -04:00
|
|
|
assert(state == State::CHECKED);
|
2017-07-14 13:12:32 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int ret = queue_signalled_init(&queue, queuelen, &memory::mmap, mode);
|
2017-08-03 00:19:27 +02:00
|
|
|
if (ret)
|
2021-08-10 10:12:48 -04:00
|
|
|
throw RuntimeError("Failed to initialize queue");
|
2017-07-14 16:29:05 +02:00
|
|
|
|
2022-03-06 21:12:38 -05:00
|
|
|
return Node::prepare();
|
2017-07-14 13:12:32 +02:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int LoopbackNode::stop()
|
2017-07-14 13:12:32 +02:00
|
|
|
{
|
2021-08-10 10:12:48 -04:00
|
|
|
int ret;
|
2017-07-14 13:12:32 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
ret = queue_signalled_close(&queue);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-07-14 13:12:32 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
return 0;
|
2017-07-14 13:12:32 +02:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int LoopbackNode::_read(struct Sample * smps[], unsigned cnt)
|
2017-07-14 13:12:32 +02:00
|
|
|
{
|
2017-07-14 16:29:05 +02:00
|
|
|
int avail;
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
struct Sample *cpys[cnt];
|
2017-07-14 16:29:05 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
avail = queue_signalled_pull_many(&queue, (void **) cpys, cnt);
|
2017-07-14 13:12:32 +02:00
|
|
|
|
2021-07-12 15:34:47 +02:00
|
|
|
sample_copy_many(smps, cpys, avail);
|
|
|
|
sample_decref_many(cpys, avail);
|
2017-07-14 16:29:05 +02:00
|
|
|
|
|
|
|
return avail;
|
2017-07-14 13:12:32 +02:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int LoopbackNode::_write(struct Sample * smps[], unsigned cnt)
|
2017-07-14 13:12:32 +02:00
|
|
|
{
|
2020-08-28 09:43:12 +02:00
|
|
|
sample_incref_many(smps, cnt);
|
2017-10-18 14:23:50 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
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;
|
2017-07-14 13:12:32 +02:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
std::vector<int> LoopbackNode::getPollFDs()
|
2017-07-14 13:12:32 +02:00
|
|
|
{
|
2021-08-10 10:12:48 -04:00
|
|
|
return { queue_signalled_fd(&queue) };
|
|
|
|
}
|
2017-07-14 13:12:32 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
const std::string & LoopbackNode::getDetails()
|
|
|
|
{
|
|
|
|
if (details.empty()) {
|
|
|
|
details = fmt::format("queuelen={}", queuelen);
|
|
|
|
}
|
2017-07-14 13:12:32 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
return details;
|
2017-10-16 23:08:46 +02:00
|
|
|
}
|
2017-07-14 13:12:32 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int LoopbackNode::parse(json_t *json)
|
2017-08-30 13:30:31 +02:00
|
|
|
{
|
2021-08-10 10:12:48 -04:00
|
|
|
const char *mode_str = nullptr;
|
2017-10-16 23:08:46 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
json_error_t err;
|
|
|
|
int ret;
|
2019-01-21 15:47:34 +01:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
ret = json_unpack_ex(json, &err, 0, "{ s?: i, s?: s }",
|
|
|
|
"queuelen", queuelen,
|
|
|
|
"mode", &mode_str
|
|
|
|
);
|
|
|
|
if (ret)
|
|
|
|
throw ConfigError(json, err, "node-config-node-loopback");
|
2017-08-30 13:30:31 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
if (mode_str) {
|
|
|
|
if (!strcmp(mode_str, "auto"))
|
|
|
|
mode = QueueSignalledMode::AUTO;
|
|
|
|
#ifdef HAVE_EVENTFD
|
|
|
|
else if (!strcmp(mode_str, "eventfd"))
|
|
|
|
mode = QueueSignalledMode::EVENTFD;
|
|
|
|
#endif
|
|
|
|
else if (!strcmp(mode_str, "pthread"))
|
|
|
|
mode = QueueSignalledMode::PTHREAD;
|
|
|
|
else if (!strcmp(mode_str, "polling"))
|
|
|
|
mode = QueueSignalledMode::POLLING;
|
|
|
|
else
|
|
|
|
throw ConfigError(json, "node-config-node-loopback-mode", "Unknown mode '{}'", mode_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2019-06-04 16:55:38 +02:00
|
|
|
}
|
2021-08-10 10:12:48 -04:00
|
|
|
|
|
|
|
static char n[] = "loopback";
|
|
|
|
static char d[] = "loopback node-type";
|
|
|
|
static NodePlugin<LoopbackNode, n, d, (int) NodeFactory::Flags::SUPPORTS_POLL |
|
|
|
|
(int) NodeFactory::Flags::SUPPORTS_READ |
|
|
|
|
(int) NodeFactory::Flags::SUPPORTS_WRITE> nf;
|