2014-07-14 11:49:44 +00:00
|
|
|
/* Nodes.
|
2014-06-05 09:34:29 +00:00
|
|
|
*
|
2022-03-15 09:18:01 -04:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
2022-03-15 09:28:57 -04:00
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
2022-07-04 18:20:03 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2015-06-02 21:53:04 +02:00
|
|
|
*/
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2019-10-30 02:38:56 +01:00
|
|
|
#include <openssl/md5.h>
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <regex>
|
2016-11-20 12:59:37 -05:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
#ifdef __linux__
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
2021-08-10 10:12:48 -04:00
|
|
|
#endif
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <villas/colors.hpp>
|
2019-06-23 16:13:23 +02:00
|
|
|
#include <villas/hook.hpp>
|
2019-04-23 13:12:04 +02:00
|
|
|
#include <villas/hook_list.hpp>
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <villas/mapping.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/node.hpp>
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <villas/node/config.hpp>
|
|
|
|
#include <villas/node/memory.hpp>
|
2021-06-21 16:12:47 -04:00
|
|
|
#include <villas/node_list.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/path.hpp>
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <villas/sample.hpp>
|
|
|
|
#include <villas/signal.hpp>
|
|
|
|
#include <villas/timing.hpp>
|
2019-04-23 13:09:50 +02:00
|
|
|
#include <villas/utils.hpp>
|
2021-09-19 19:26:03 +02:00
|
|
|
#include <villas/uuid.hpp>
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2019-01-21 22:14:41 +01:00
|
|
|
#ifdef WITH_NETEM
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <villas/kernel/if.hpp>
|
|
|
|
#include <villas/kernel/nl.hpp>
|
|
|
|
#include <villas/kernel/tc.hpp>
|
|
|
|
#include <villas/kernel/tc_netem.hpp>
|
2019-01-21 22:14:41 +01:00
|
|
|
#endif // WITH_NETEM
|
|
|
|
|
2019-06-23 13:35:42 +02:00
|
|
|
using namespace villas;
|
2021-06-21 16:12:47 -04:00
|
|
|
using namespace villas::node;
|
2019-06-04 16:55:38 +02:00
|
|
|
using namespace villas::utils;
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
Node::Node(const uuid_t &id, const std::string &name)
|
|
|
|
: logger(logging.get("node")), sequence_init(0), sequence(0),
|
|
|
|
in(NodeDirection::Direction::IN, this),
|
2024-02-26 11:15:51 +01:00
|
|
|
out(NodeDirection::Direction::OUT, this), configPath(),
|
2019-02-15 09:46:26 +01:00
|
|
|
#ifdef __linux__
|
2023-09-07 11:46:39 +02:00
|
|
|
fwmark(-1),
|
2019-02-15 11:18:32 +01:00
|
|
|
#endif // __linux__
|
2019-01-21 15:50:18 +01:00
|
|
|
#ifdef WITH_NETEM
|
2023-09-07 11:46:39 +02:00
|
|
|
tc_qdisc(nullptr), tc_classifier(nullptr),
|
2019-01-21 15:50:18 +01:00
|
|
|
#endif // WITH_NETEM
|
2023-09-07 11:46:39 +02:00
|
|
|
state(State::INITIALIZED), enabled(true), config(nullptr),
|
|
|
|
name_short(name), affinity(-1), // all cores
|
|
|
|
factory(nullptr) {
|
|
|
|
if (uuid_is_null(id)) {
|
|
|
|
uuid_generate_random(uuid);
|
|
|
|
} else {
|
|
|
|
uuid_copy(uuid, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!name_short.empty()) {
|
|
|
|
name_long = fmt::format(CLR_RED("{}"), name_short);
|
|
|
|
} else if (name_short.empty()) {
|
|
|
|
name_short = "<none>";
|
|
|
|
name_long = CLR_RED("<none>");
|
|
|
|
}
|
2021-08-10 10:12:48 -04:00
|
|
|
}
|
2018-05-24 09:04:41 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
Node::~Node() {
|
2021-08-10 10:12:48 -04:00
|
|
|
#ifdef WITH_NETEM
|
2023-09-07 11:46:39 +02:00
|
|
|
rtnl_qdisc_put(tc_qdisc);
|
|
|
|
rtnl_cls_put(tc_classifier);
|
2021-08-10 10:12:48 -04:00
|
|
|
#endif // WITH_NETEM
|
2018-05-24 09:04:41 +02:00
|
|
|
|
2024-03-07 16:13:02 +00:00
|
|
|
// Internal loopback nodes have no factory
|
|
|
|
// Only attempt removal for factories of other node-types.
|
|
|
|
if (factory != nullptr)
|
|
|
|
factory->instances.remove(this);
|
2018-05-24 09:04:41 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int Node::prepare() {
|
|
|
|
int ret;
|
2018-08-20 18:31:27 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ret = in.prepare();
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2019-03-15 17:19:28 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ret = out.prepare();
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2018-08-20 18:31:27 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
state = State::PREPARED;
|
2019-02-24 11:13:28 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return 0;
|
2018-08-20 18:31:27 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int Node::parse(json_t *json) {
|
|
|
|
assert(state == State::INITIALIZED || state == State::PARSED ||
|
|
|
|
state == State::CHECKED);
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int ret, en = enabled, init_seq = -1;
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
json_error_t err;
|
|
|
|
json_t *json_netem = nullptr;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ret = json_unpack_ex(json, &err, 0, "{ s?: b, s?: i }", "enabled", &en,
|
|
|
|
"initial_sequenceno", &init_seq);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2019-02-15 09:46:26 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
if (init_seq >= 0)
|
|
|
|
sequence_init = init_seq;
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2019-02-15 09:46:26 +01:00
|
|
|
#ifdef __linux__
|
2023-09-07 11:46:39 +02:00
|
|
|
ret = json_unpack_ex(json, &err, 0, "{ s?: { s?: o, s?: i } }", "out",
|
|
|
|
"netem", &json_netem, "fwmark", &fwmark);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2019-02-15 09:46:26 +01:00
|
|
|
#endif // __linux__
|
2016-06-08 22:38:21 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
enabled = en;
|
2020-10-16 11:08:40 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
if (json_netem) {
|
2019-01-21 15:50:18 +01:00
|
|
|
#ifdef WITH_NETEM
|
2023-09-07 11:46:39 +02:00
|
|
|
int enabled = 1;
|
2019-01-21 15:50:18 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ret = json_unpack_ex(json_netem, &err, 0, "{ s?: b }", "enabled", &enabled);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2019-01-21 15:50:18 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
if (enabled)
|
|
|
|
kernel::tc::netem_parse(&tc_qdisc, json_netem);
|
|
|
|
else
|
|
|
|
tc_qdisc = nullptr;
|
2019-01-21 15:50:18 +01:00
|
|
|
#endif // WITH_NETEM
|
2023-09-07 11:46:39 +02:00
|
|
|
}
|
2019-01-21 15:50:18 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
struct {
|
|
|
|
const char *str;
|
|
|
|
struct NodeDirection *dir;
|
|
|
|
} dirs[] = {{"in", &in}, {"out", &out}};
|
2018-05-26 01:10:33 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
const char *fields[] = {"signals", "builtin", "vectorize", "hooks"};
|
2018-06-12 20:45:03 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
for (unsigned j = 0; j < ARRAY_LEN(dirs); j++) {
|
|
|
|
json_t *json_dir = json_object_get(json, dirs[j].str);
|
2018-06-12 20:45:03 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
// Skip if direction is unused
|
|
|
|
if (!json_dir) {
|
|
|
|
json_dir = json_pack("{ s: b }", "enabled", 0);
|
|
|
|
}
|
2018-06-12 20:45:03 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
// Copy missing fields from main node config to direction config
|
|
|
|
for (unsigned i = 0; i < ARRAY_LEN(fields); i++) {
|
|
|
|
json_t *json_field_dir = json_object_get(json_dir, fields[i]);
|
|
|
|
json_t *json_field_node = json_object_get(json, fields[i]);
|
2018-06-12 20:45:03 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
if (json_field_node && !json_field_dir)
|
|
|
|
json_object_set(json_dir, fields[i], json_field_node);
|
|
|
|
}
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ret = dirs[j].dir->parse(json_dir);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2018-03-21 16:59:19 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
config = json;
|
2016-06-08 22:38:21 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return 0;
|
2015-03-31 13:54:04 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int Node::check() {
|
|
|
|
assert(state == State::CHECKED || state == State::PARSED ||
|
|
|
|
state == State::INITIALIZED);
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
in.check();
|
|
|
|
out.check();
|
2018-07-02 10:59:45 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
state = State::CHECKED;
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return 0;
|
2015-03-31 13:54:04 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int Node::start() {
|
|
|
|
int ret;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
assert(state == State::PREPARED);
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2024-02-28 11:00:35 +00:00
|
|
|
// logger->info("Starting node {}", getNameFull());
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ret = in.start();
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-03-13 23:51:38 -03:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ret = out.start();
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-01-21 15:50:18 +01:00
|
|
|
#ifdef __linux__
|
2023-09-07 11:46:39 +02:00
|
|
|
// Set fwmark for outgoing packets if netem is enabled for this node
|
|
|
|
if (fwmark >= 0) {
|
|
|
|
for (int fd : getNetemFDs()) {
|
|
|
|
ret = setsockopt(fd, SOL_SOCKET, SO_MARK, &fwmark, sizeof(fwmark));
|
|
|
|
if (ret)
|
|
|
|
throw RuntimeError("Failed to set FW mark for outgoing packets");
|
|
|
|
else
|
|
|
|
logger->debug("Set FW mark for socket (sd={}) to {}", fd, fwmark);
|
|
|
|
}
|
|
|
|
}
|
2019-01-21 15:50:18 +01:00
|
|
|
#endif // __linux__
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
state = State::STARTED;
|
|
|
|
sequence = sequence_init;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return 0;
|
2014-12-05 12:39:52 +01:00
|
|
|
}
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int Node::stop() {
|
|
|
|
int ret;
|
2015-11-16 10:51:00 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
if (state != State::STOPPING && state != State::STARTED &&
|
|
|
|
state != State::CONNECTED && state != State::PENDING_CONNECT)
|
|
|
|
return 0;
|
2015-11-29 22:45:46 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
logger->info("Stopping node");
|
|
|
|
setState(State::STOPPING);
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ret = in.stop();
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ret = out.stop();
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2018-11-22 09:56:40 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return 0;
|
2018-11-22 09:56:30 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int Node::restart() {
|
|
|
|
int ret;
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
assert(state == State::STARTED);
|
2018-05-24 09:04:41 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
logger->info("Restarting node");
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ret = stop();
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2020-09-10 11:20:35 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ret = start();
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2020-09-10 11:20:35 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return 0;
|
2017-03-11 23:50:30 -03:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int Node::read(struct Sample *smps[], unsigned cnt) {
|
|
|
|
int toread, readd, nread = 0;
|
|
|
|
unsigned vect;
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
if (state == State::PAUSED || state == State::PENDING_CONNECT)
|
|
|
|
return 0;
|
|
|
|
else if (state != State::STARTED && state != State::CONNECTED)
|
|
|
|
return -1;
|
2019-02-11 16:39:30 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
vect = factory->getVectorize();
|
|
|
|
if (!vect)
|
|
|
|
vect = cnt;
|
2017-09-02 14:20:38 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
while (cnt - nread > 0) {
|
|
|
|
toread = MIN(cnt - nread, vect);
|
|
|
|
readd = _read(&smps[nread], toread);
|
|
|
|
if (readd < 0)
|
|
|
|
return readd;
|
2020-08-28 09:43:34 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
nread += readd;
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-12-09 02:23:29 +08:00
|
|
|
#ifdef WITH_HOOKS
|
2023-09-07 11:46:39 +02:00
|
|
|
// Run read hooks
|
|
|
|
int rread = in.hooks.process(smps, nread);
|
|
|
|
if (rread < 0)
|
|
|
|
return rread;
|
|
|
|
|
|
|
|
int skipped = nread - rread;
|
|
|
|
if (skipped > 0) {
|
|
|
|
if (stats != nullptr)
|
|
|
|
stats->update(Stats::Metric::SMPS_SKIPPED, skipped);
|
|
|
|
|
|
|
|
logger->debug("Received {} samples of which {} have been skipped", nread,
|
|
|
|
skipped);
|
|
|
|
} else
|
|
|
|
logger->debug("Received {} samples", nread);
|
|
|
|
|
|
|
|
return rread;
|
2017-12-09 02:23:29 +08:00
|
|
|
#else
|
2023-09-07 11:46:39 +02:00
|
|
|
logger->debug("Received {} samples", nread);
|
2018-05-23 02:25:27 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return nread;
|
2017-12-09 02:23:29 +08:00
|
|
|
#endif // WITH_HOOKS
|
2017-03-11 23:50:30 -03:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int Node::write(struct Sample *smps[], unsigned cnt) {
|
|
|
|
int tosend, sent, nsent = 0;
|
|
|
|
unsigned vect;
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
if (state == State::PAUSED || state == State::PENDING_CONNECT)
|
|
|
|
return 0;
|
|
|
|
else if (state != State::STARTED && state != State::CONNECTED)
|
|
|
|
return -1;
|
2019-02-11 16:39:30 +01:00
|
|
|
|
2017-12-09 02:23:29 +08:00
|
|
|
#ifdef WITH_HOOKS
|
2023-09-07 11:46:39 +02:00
|
|
|
// Run write hooks
|
2023-10-31 15:53:42 +01:00
|
|
|
int hook_cnt = out.hooks.process(smps, cnt);
|
|
|
|
if (hook_cnt <= 0)
|
|
|
|
return hook_cnt;
|
|
|
|
cnt = hook_cnt;
|
2017-12-09 02:23:29 +08:00
|
|
|
#endif // WITH_HOOKS
|
2017-09-02 14:27:58 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
vect = getFactory()->getVectorize();
|
|
|
|
if (!vect)
|
|
|
|
vect = cnt;
|
2017-08-22 12:31:12 +02:00
|
|
|
|
2023-10-31 15:53:42 +01:00
|
|
|
while (cnt > static_cast<unsigned>(nsent)) {
|
2023-09-07 11:46:39 +02:00
|
|
|
tosend = MIN(cnt - nsent, vect);
|
|
|
|
sent = _write(&smps[nsent], tosend);
|
|
|
|
if (sent < 0)
|
|
|
|
return sent;
|
2017-08-22 12:31:12 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
nsent += sent;
|
|
|
|
logger->debug("Sent {} samples", sent);
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return nsent;
|
2017-03-11 23:50:30 -03:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
const std::string &Node::getNameFull() {
|
|
|
|
if (name_full.empty()) {
|
|
|
|
name_full = fmt::format("{}: uuid={}, #in.signals={}/{}, #in.hooks={}, "
|
|
|
|
"#out.hooks={}, in.vectorize={}, out.vectorize={}",
|
|
|
|
getName(), uuid::toString(uuid).c_str(),
|
|
|
|
getInputSignals(false)->size(),
|
|
|
|
getInputSignals(true)->size(), in.hooks.size(),
|
|
|
|
out.hooks.size(), in.vectorize, out.vectorize);
|
2018-05-24 09:04:41 +02:00
|
|
|
|
2019-01-30 00:42:35 +01:00
|
|
|
#ifdef WITH_NETEM
|
2023-09-07 11:46:39 +02:00
|
|
|
name_full += fmt::format(", out.netem={}", tc_qdisc ? "yes" : "no");
|
2019-01-30 00:42:35 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
if (tc_qdisc)
|
|
|
|
name_full += fmt::format(", fwmark={}", fwmark);
|
2019-01-30 00:42:35 +01:00
|
|
|
#endif // WITH_NETEM
|
2019-01-21 23:00:16 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
if (out.path) {
|
|
|
|
name_full += fmt::format(
|
|
|
|
", #out.signals={}/{}",
|
|
|
|
getOutputSignals(false) ? getOutputSignals(false)->size() : 0,
|
|
|
|
getOutputSignals() ? getOutputSignals()->size() : 0);
|
2020-07-04 17:15:54 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
name_full += fmt::format(", out.path={}", out.path->toString());
|
|
|
|
}
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
// Append node-type specific details
|
|
|
|
auto details = getDetails();
|
|
|
|
if (!details.empty())
|
|
|
|
name_full += fmt::format(", {}", details);
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return name_full;
|
2015-10-17 19:05:15 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
SignalList::Ptr Node::getInputSignals(bool after_hooks) const {
|
|
|
|
return in.getSignals(after_hooks);
|
2015-12-13 02:02:29 +01:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
SignalList::Ptr Node::getOutputSignals(bool after_hooks) const {
|
|
|
|
if (out.path)
|
|
|
|
return out.path->getOutputSignals();
|
2015-03-21 15:23:57 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return nullptr;
|
2019-01-21 15:47:34 +01:00
|
|
|
}
|
2019-01-21 15:50:18 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
unsigned Node::getInputSignalsMaxCount() const {
|
|
|
|
return in.getSignalsMaxCount();
|
2017-08-30 00:22:58 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
unsigned Node::getOutputSignalsMaxCount() const {
|
|
|
|
if (out.path)
|
|
|
|
return out.path->getOutputSignalsMaxCount();
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return 0;
|
2018-06-29 17:32:07 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
bool Node::isValidName(const std::string &name) {
|
|
|
|
std::regex re(RE_NODE_NAME);
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return std::regex_match(name, re);
|
2021-08-10 10:12:48 -04:00
|
|
|
}
|
2021-02-16 14:15:14 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
json_t *Node::toJson() const {
|
|
|
|
json_t *json_node;
|
|
|
|
json_t *json_signals_in = nullptr;
|
|
|
|
json_t *json_signals_out = nullptr;
|
|
|
|
|
|
|
|
json_signals_in = getInputSignals()->toJson();
|
|
|
|
|
|
|
|
auto output_signals = getOutputSignals();
|
|
|
|
if (output_signals)
|
|
|
|
json_signals_out = output_signals->toJson();
|
|
|
|
|
|
|
|
json_node = json_pack(
|
|
|
|
"{ s: s, s: s, s: s, s: i, s: { s: i, s: o? }, s: { s: i, s: o? } }",
|
|
|
|
"name", getNameShort().c_str(), "uuid", uuid::toString(uuid).c_str(),
|
|
|
|
"state", stateToString(state).c_str(), "affinity", affinity, "in",
|
|
|
|
"vectorize", in.vectorize, "signals", json_signals_in, "out", "vectorize",
|
|
|
|
out.vectorize, "signals", json_signals_out);
|
|
|
|
|
|
|
|
if (stats)
|
|
|
|
json_object_set_new(json_node, "stats", stats->toJson());
|
|
|
|
|
|
|
|
auto *status = _readStatus();
|
|
|
|
if (status)
|
|
|
|
json_object_set_new(json_node, "status", status);
|
|
|
|
|
|
|
|
/* Add all additional fields of node here.
|
2024-02-29 21:47:13 +01:00
|
|
|
* This can be used for metadata */
|
2023-09-07 11:46:39 +02:00
|
|
|
json_object_update(json_node, config);
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return json_node;
|
2021-08-10 10:12:48 -04:00
|
|
|
}
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
void Node::swapSignals() { SWAP(in.signals, out.signals); }
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
Node *NodeFactory::make(json_t *json, const uuid_t &id,
|
|
|
|
const std::string &name) {
|
|
|
|
int ret;
|
|
|
|
std::string type;
|
|
|
|
Node *n;
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
if (json_is_object(json))
|
|
|
|
throw ConfigError(json, "node-config-node",
|
|
|
|
"Node configuration must be an object");
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
json_t *json_type = json_object_get(json, "type");
|
2019-02-12 17:54:08 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
type = json_string_value(json_type);
|
2019-02-24 11:10:44 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
n = NodeFactory::make(type, id, name);
|
|
|
|
if (!n)
|
|
|
|
return nullptr;
|
2019-02-24 11:08:15 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ret = n->parse(json);
|
|
|
|
if (ret) {
|
|
|
|
delete n;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2023-06-30 10:51:01 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return n;
|
2020-10-21 20:56:51 +02:00
|
|
|
}
|
2019-03-08 15:21:01 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
Node *NodeFactory::make(const std::string &type, const uuid_t &id,
|
|
|
|
const std::string &name) {
|
|
|
|
NodeFactory *nf = plugin::registry->lookup<NodeFactory>(type);
|
|
|
|
if (!nf)
|
|
|
|
throw RuntimeError("Unknown node-type: {}", type);
|
2020-10-21 20:56:51 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return nf->make(id, name);
|
2019-02-12 17:54:08 +01:00
|
|
|
}
|
2020-08-25 20:24:18 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int NodeFactory::start(SuperNode *sn) {
|
|
|
|
getLogger()->info("Initialized node type which is used by {} nodes",
|
|
|
|
instances.size());
|
2021-07-01 22:26:44 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
state = State::STARTED;
|
2024-02-28 11:00:35 +00:00
|
|
|
logger->debug("NodeFactory::start()");
|
2021-07-01 22:26:44 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return 0;
|
2021-07-01 22:26:44 +02:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int NodeFactory::stop() {
|
|
|
|
getLogger()->info("De-initialized node type");
|
2020-08-25 20:24:18 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
state = State::STOPPED;
|
2020-08-25 20:24:18 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return 0;
|
2020-08-28 09:43:34 +02:00
|
|
|
}
|