2019-03-16 10:04:23 +01:00
|
|
|
/** Hook-releated functions.
|
|
|
|
*
|
2022-12-14 17:41:58 +01:00
|
|
|
* @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-16 10:04:23 +01:00
|
|
|
*********************************************************************************/
|
|
|
|
|
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>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/list.hpp>
|
2021-06-23 20:06:33 +02:00
|
|
|
#include <villas/utils.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/sample.hpp>
|
2019-03-16 10:04:23 +01:00
|
|
|
|
2019-03-26 15:33:47 +01:00
|
|
|
using namespace villas;
|
|
|
|
using namespace villas::node;
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
void HookList::parse(json_t *json, int mask, Path *o, Node *n)
|
2019-03-16 10:04:23 +01:00
|
|
|
{
|
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");
|
2019-03-16 10:04:23 +01:00
|
|
|
|
|
|
|
size_t i;
|
|
|
|
json_t *json_hook;
|
2021-02-16 14:15:14 +01:00
|
|
|
json_array_foreach(json, i, json_hook) {
|
2019-03-16 10:04:23 +01:00
|
|
|
int ret;
|
|
|
|
const char *type;
|
2021-08-10 10:12:48 -04:00
|
|
|
Hook::Ptr h;
|
2019-03-16 10:04:23 +01:00
|
|
|
json_error_t err;
|
2021-07-06 18:32:04 +02:00
|
|
|
json_t *json_config;
|
2019-03-16 10:04:23 +01:00
|
|
|
|
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");
|
|
|
|
}
|
2019-03-16 10:04:23 +01:00
|
|
|
|
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-16 10:04:23 +01:00
|
|
|
|
2019-03-26 15:33:47 +01:00
|
|
|
if (!(hf->getFlags() & mask))
|
|
|
|
throw ConfigError(json_hook, "node-config-hook", "Hook '{}' not allowed here", type);
|
2019-03-16 10:04:23 +01:00
|
|
|
|
2019-06-11 18:32:58 +02:00
|
|
|
h = hf->make(o, n);
|
2021-07-06 18:32:04 +02:00
|
|
|
h->parse(json_config);
|
2019-03-16 10:04:23 +01:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
push_back(h);
|
2019-03-16 10:04:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
void HookList::check()
|
2021-07-06 18:33:33 +02:00
|
|
|
{
|
2021-08-10 10:12:48 -04:00
|
|
|
for (auto h : *this)
|
2021-07-06 18:33:33 +02:00
|
|
|
h->check();
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
void HookList::prepare(SignalList::Ptr signals, int m, Path *p, Node *n)
|
2019-03-16 10:04:23 +01:00
|
|
|
{
|
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);
|
2021-08-10 10:12:48 -04:00
|
|
|
push_back(h);
|
2019-03-26 15:33:47 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-16 10:04:23 +01:00
|
|
|
|
2019-04-02 09:17:59 -04:00
|
|
|
skip_add:
|
2019-04-23 00:36:06 +02:00
|
|
|
/* Remove filters which are not enabled */
|
2021-08-10 10:12:48 -04:00
|
|
|
remove_if([](Hook::Ptr h) { return !h->isEnabled(); });
|
2019-04-23 00:36:06 +02:00
|
|
|
|
2019-03-26 15:33:47 +01:00
|
|
|
/* We sort the hooks according to their priority */
|
2021-08-10 10:12:48 -04:00
|
|
|
sort([](const value_type &a, const value_type b) { return a->getPriority() < b->getPriority(); });
|
2019-03-16 10:04:23 +01:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
unsigned i = 0;
|
|
|
|
auto sigs = signals;
|
|
|
|
for (auto h : *this) {
|
2019-06-11 18:32:58 +02:00
|
|
|
h->prepare(sigs);
|
2019-03-26 15:33:47 +01:00
|
|
|
|
|
|
|
sigs = h->getSignals();
|
2020-10-21 20:57:57 +02:00
|
|
|
|
2021-07-06 18:32:34 +02:00
|
|
|
auto logger = h->getLogger();
|
2021-08-10 10:12:48 -04:00
|
|
|
logger->debug("Signal list after hook #{}:", i++);
|
2022-02-24 07:31:54 -05:00
|
|
|
if (logger->level() <= spdlog::level::debug)
|
|
|
|
sigs->dump(logger);
|
2019-03-16 10:04:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int HookList::process(struct Sample * smps[], unsigned cnt)
|
2019-03-16 10:04:23 +01:00
|
|
|
{
|
2019-06-23 16:13:23 +02:00
|
|
|
unsigned current, processed = 0;
|
2019-03-16 10:04:23 +01:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
if (size() == 0)
|
2019-03-26 15:33:47 +01:00
|
|
|
return cnt;
|
|
|
|
|
2019-04-23 00:36:06 +02:00
|
|
|
for (current = 0; current < cnt; current++) {
|
2021-08-10 10:12:48 -04:00
|
|
|
struct Sample *smp = smps[current];
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
for (auto h : *this) {
|
2019-06-23 16:13:23 +02:00
|
|
|
auto ret = h->process(smp);
|
2020-10-21 20:57:57 +02:00
|
|
|
smp->signals = h->getSignals();
|
2019-03-16 10:04:23 +01:00
|
|
|
switch (ret) {
|
2019-06-23 16:13:23 +02:00
|
|
|
case Hook::Reason::ERROR:
|
2019-03-16 10:04:23 +01:00
|
|
|
return -1;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case Hook::Reason::OK:
|
2019-03-26 15:33:47 +01:00
|
|
|
continue;
|
2019-03-16 10:04:23 +01:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case Hook::Reason::SKIP_SAMPLE:
|
2019-03-16 10:04:23 +01:00
|
|
|
goto skip;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
case Hook::Reason::STOP_PROCESSING:
|
2019-03-16 10:04:23 +01:00
|
|
|
goto stop;
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2021-06-23 20:06:33 +02:00
|
|
|
stop: SWAP(smps[processed], smps[current]);
|
|
|
|
processed++;
|
2019-03-16 10:04:23 +01:00
|
|
|
skip: {}
|
|
|
|
}
|
|
|
|
|
2021-06-23 20:06:33 +02:00
|
|
|
return processed;
|
2019-03-16 10:04:23 +01:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
void HookList::periodic()
|
2019-03-16 10:04:23 +01:00
|
|
|
{
|
2021-08-10 10:12:48 -04:00
|
|
|
for (auto h : *this)
|
2019-06-11 18:32:58 +02:00
|
|
|
h->periodic();
|
2019-03-16 10:04:23 +01:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
void HookList::start()
|
2019-03-16 10:04:23 +01:00
|
|
|
{
|
2021-08-10 10:12:48 -04:00
|
|
|
for (auto h : *this)
|
2019-06-11 18:32:58 +02:00
|
|
|
h->start();
|
2019-03-16 10:04:23 +01:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
void HookList::stop()
|
2019-03-16 10:04:23 +01:00
|
|
|
{
|
2021-08-10 10:12:48 -04:00
|
|
|
for (auto h : *this)
|
2019-06-11 18:32:58 +02:00
|
|
|
h->stop();
|
2019-03-16 10:04:23 +01:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
SignalList::Ptr HookList::getSignals() const
|
2019-03-16 10:04:23 +01:00
|
|
|
{
|
2021-08-10 10:12:48 -04:00
|
|
|
auto h = back();
|
2021-06-24 06:27:35 -04:00
|
|
|
if (!h)
|
|
|
|
return nullptr;
|
2019-03-16 10:04:23 +01:00
|
|
|
|
2019-03-26 15:33:47 +01:00
|
|
|
return h->getSignals();
|
2019-03-16 10:04:23 +01:00
|
|
|
}
|
2020-08-25 20:24:18 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
unsigned HookList::getSignalsMaxCount() const
|
2021-07-01 22:26:44 +02:00
|
|
|
{
|
|
|
|
unsigned max_cnt = 0;
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
for (auto h : *this) {
|
|
|
|
unsigned sigs_cnt = h->getSignals()->size();
|
2021-07-06 18:33:33 +02:00
|
|
|
|
2021-07-01 22:26:44 +02:00
|
|
|
if (sigs_cnt > max_cnt)
|
|
|
|
max_cnt = sigs_cnt;
|
|
|
|
}
|
2021-07-06 18:33:33 +02:00
|
|
|
|
2021-07-01 22:26:44 +02:00
|
|
|
return max_cnt;
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
json_t * HookList::toJson() const
|
2020-08-25 20:24:18 +02:00
|
|
|
{
|
|
|
|
json_t *json_hooks = json_array();
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
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
|
|
|
}
|
2021-08-10 10:12:48 -04: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());
|
|
|
|
}
|