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/hook_list.cpp

198 lines
4.1 KiB
C++
Raw Permalink Normal View History

/** Hook-releated functions.
*
* @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
*********************************************************************************/
2019-03-26 15:33:47 +01:00
#include <villas/plugin.hpp>
#include <villas/hook.hpp>
2019-04-23 13:12:04 +02:00
#include <villas/hook_list.hpp>
#include <villas/list.hpp>
#include <villas/utils.hpp>
#include <villas/sample.hpp>
2019-03-26 15:33:47 +01:00
using namespace villas;
using namespace villas::node;
void HookList::parse(json_t *json, int mask, Path *o, Node *n)
{
2021-02-16 14:15:14 +01:00
if (!json_is_array(json))
throw ConfigError(json, "node-config-hook", "Hooks must be configured as a list of hook objects");
size_t i;
json_t *json_hook;
2021-02-16 14:15:14 +01:00
json_array_foreach(json, i, json_hook) {
int ret;
const char *type;
Hook::Ptr h;
json_error_t err;
2021-07-06 18:32:04 +02:00
json_t *json_config;
2021-07-06 18:32:04 +02:00
switch (json_typeof(json_hook)) {
case JSON_STRING:
type = json_string_value(json_hook);
json_config = json_object();
break;
case JSON_OBJECT:
2021-07-06 18:33:33 +02:00
ret = json_unpack_ex(json_hook, &err, 0, "{ s: s }", "type", &type);
if (ret)
throw ConfigError(json_hook, err, "node-config-hook", "Failed to parse hook");
2021-07-06 18:32:04 +02:00
json_config = json_hook;
break;
default:
throw ConfigError(json_hook, "node-config-hook", "Hook must be configured by simple string or object");
}
2022-02-25 09:55:43 -05:00
auto hf = plugin::registry->lookup<HookFactory>(type);
2019-03-26 15:33:47 +01:00
if (!hf)
2020-08-17 17:10:07 +02:00
throw ConfigError(json_hook, "node-config-hook", "Unknown hook type '{}'", type);
2019-03-26 15:33:47 +01:00
if (!(hf->getFlags() & mask))
throw ConfigError(json_hook, "node-config-hook", "Hook '{}' not allowed here", type);
h = hf->make(o, n);
2021-07-06 18:32:04 +02:00
h->parse(json_config);
push_back(h);
}
}
void HookList::check()
2021-07-06 18:33:33 +02:00
{
for (auto h : *this)
2021-07-06 18:33:33 +02:00
h->check();
}
void HookList::prepare(SignalList::Ptr signals, int m, Path *p, Node *n)
{
2019-04-02 09:17:59 -04:00
if (!m)
goto skip_add;
2019-03-26 15:33:47 +01:00
/* Add internal hooks if they are not already in the list */
2022-02-25 09:55:43 -05:00
for (auto f : plugin::registry->lookup<HookFactory>()) {
2019-03-26 15:33:47 +01:00
if ((f->getFlags() & m) == m) {
auto h = f->make(p, n);
push_back(h);
2019-03-26 15:33:47 +01:00
}
}
2019-04-02 09:17:59 -04:00
skip_add:
/* Remove filters which are not enabled */
remove_if([](Hook::Ptr h) { return !h->isEnabled(); });
2019-03-26 15:33:47 +01:00
/* We sort the hooks according to their priority */
sort([](const value_type &a, const value_type b) { return a->getPriority() < b->getPriority(); });
unsigned i = 0;
auto sigs = signals;
for (auto h : *this) {
h->prepare(sigs);
2019-03-26 15:33:47 +01:00
sigs = h->getSignals();
auto logger = h->getLogger();
logger->debug("Signal list after hook #{}:", i++);
2022-02-24 07:31:54 -05:00
if (logger->level() <= spdlog::level::debug)
sigs->dump(logger);
}
}
int HookList::process(struct Sample * smps[], unsigned cnt)
{
2019-06-23 16:13:23 +02:00
unsigned current, processed = 0;
if (size() == 0)
2019-03-26 15:33:47 +01:00
return cnt;
for (current = 0; current < cnt; current++) {
struct Sample *smp = smps[current];
2019-03-26 15:33:47 +01:00
for (auto h : *this) {
2019-06-23 16:13:23 +02:00
auto ret = h->process(smp);
smp->signals = h->getSignals();
switch (ret) {
2019-06-23 16:13:23 +02:00
case Hook::Reason::ERROR:
return -1;
2019-06-23 16:13:23 +02:00
case Hook::Reason::OK:
2019-03-26 15:33:47 +01:00
continue;
2019-06-23 16:13:23 +02:00
case Hook::Reason::SKIP_SAMPLE:
goto skip;
2019-06-23 16:13:23 +02:00
case Hook::Reason::STOP_PROCESSING:
goto stop;
}
}
2019-03-26 15:33:47 +01:00
stop: SWAP(smps[processed], smps[current]);
processed++;
skip: {}
}
return processed;
}
void HookList::periodic()
{
for (auto h : *this)
h->periodic();
}
void HookList::start()
{
for (auto h : *this)
h->start();
}
void HookList::stop()
{
for (auto h : *this)
h->stop();
}
SignalList::Ptr HookList::getSignals() const
{
auto h = back();
2021-06-24 06:27:35 -04:00
if (!h)
return nullptr;
2019-03-26 15:33:47 +01:00
return h->getSignals();
}
2020-08-25 20:24:18 +02:00
unsigned HookList::getSignalsMaxCount() const
{
unsigned max_cnt = 0;
for (auto h : *this) {
unsigned sigs_cnt = h->getSignals()->size();
2021-07-06 18:33:33 +02:00
if (sigs_cnt > max_cnt)
max_cnt = sigs_cnt;
}
2021-07-06 18:33:33 +02:00
return max_cnt;
}
json_t * HookList::toJson() const
2020-08-25 20:24:18 +02:00
{
json_t *json_hooks = json_array();
for (auto h : *this)
2020-08-25 20:24:18 +02:00
json_array_append(json_hooks, h->getConfig());
return json_hooks;
2021-02-16 14:15:14 +01:00
}
void HookList::dump(Logger logger, std::string subject) const
{
logger->debug("Hooks of {}:", subject);
unsigned i = 0;
for (auto h : *this)
logger->debug(" {}: {}", i++, h->getFactory()->getName());
}