2019-03-16 10:04:23 +01:00
|
|
|
/** Hook-releated functions.
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2020-01-20 17:17:00 +01:00
|
|
|
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
|
2019-03-16 10:04:23 +01: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-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>
|
2019-03-16 10:04:23 +01:00
|
|
|
#include <villas/list.h>
|
2020-10-21 20:57:57 +02:00
|
|
|
#include <villas/sample.h>
|
2019-03-16 10:04:23 +01:00
|
|
|
|
2019-03-26 15:33:47 +01:00
|
|
|
using namespace villas;
|
|
|
|
using namespace villas::node;
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
int hook_list_init(struct vlist *hs)
|
2019-03-16 10:04:23 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = vlist_init(hs);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-26 15:33:47 +01:00
|
|
|
static int hook_destroy(Hook *h)
|
|
|
|
{
|
|
|
|
delete h;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
int hook_list_destroy(struct vlist *hs)
|
2019-03-16 10:04:23 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2019-03-26 15:33:47 +01:00
|
|
|
ret = vlist_destroy(hs, (dtor_cb_t) hook_destroy, false);
|
2019-03-16 10:04:23 +01:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
void hook_list_parse(struct vlist *hs, json_t *json, int mask, struct vpath *o, struct vnode *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;
|
2019-03-26 15:33:47 +01:00
|
|
|
Hook *h;
|
2019-03-16 10:04:23 +01:00
|
|
|
json_error_t err;
|
|
|
|
|
|
|
|
ret = json_unpack_ex(json_hook, &err, 0, "{ s: s }", "type", &type);
|
|
|
|
if (ret)
|
2019-03-26 15:33:47 +01:00
|
|
|
throw ConfigError(json_hook, err, "node-config-hook", "Failed to parse hook");
|
2019-03-16 10:04:23 +01:00
|
|
|
|
2019-03-26 15:33:47 +01:00
|
|
|
auto hf = plugin::Registry::lookup<HookFactory>(type);
|
|
|
|
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);
|
|
|
|
h->parse(json_hook);
|
|
|
|
h->check();
|
2019-03-16 10:04:23 +01:00
|
|
|
|
|
|
|
vlist_push(hs, h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 15:33:47 +01:00
|
|
|
static int hook_cmp_priority(const Hook *a, const Hook *b)
|
2019-03-16 10:04:23 +01:00
|
|
|
{
|
2019-03-26 15:33:47 +01:00
|
|
|
return a->getPriority() - b->getPriority();
|
2019-03-16 10:04:23 +01:00
|
|
|
}
|
|
|
|
|
2019-04-23 00:36:06 +02:00
|
|
|
static int hook_is_enabled(const Hook *h)
|
|
|
|
{
|
|
|
|
return h->isEnabled() ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
void hook_list_prepare(struct vlist *hs, vlist *sigs, int m, struct vpath *p, struct vnode *n)
|
2019-03-16 10:04:23 +01:00
|
|
|
{
|
2019-06-23 16:13:23 +02:00
|
|
|
assert(hs->state == State::INITIALIZED);
|
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 */
|
|
|
|
for (auto f : plugin::Registry::lookup<HookFactory>()) {
|
|
|
|
if ((f->getFlags() & m) == m) {
|
|
|
|
auto h = f->make(p, n);
|
2019-03-16 10:04:23 +01:00
|
|
|
|
2019-03-26 15:33:47 +01:00
|
|
|
vlist_push(hs, h);
|
|
|
|
}
|
|
|
|
}
|
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 */
|
|
|
|
vlist_filter(hs, (dtor_cb_t) hook_is_enabled);
|
|
|
|
|
2019-03-26 15:33:47 +01:00
|
|
|
/* We sort the hooks according to their priority */
|
|
|
|
vlist_sort(hs, (cmp_cb_t) hook_cmp_priority);
|
2019-03-16 10:04:23 +01:00
|
|
|
|
2019-03-26 15:33:47 +01:00
|
|
|
for (size_t i = 0; i < vlist_length(hs); i++) {
|
|
|
|
Hook *h = (Hook *) vlist_at(hs, i);
|
2019-03-16 10:04:23 +01:00
|
|
|
|
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-02-16 14:15:14 +01:00
|
|
|
auto logger = logging.get("hook");
|
|
|
|
signal_list_dump(logger, sigs);
|
2019-03-16 10:04:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
int hook_list_process(struct vlist *hs, 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
|
|
|
|
2019-03-26 15:33:47 +01:00
|
|
|
if (vlist_length(hs) == 0)
|
|
|
|
return cnt;
|
|
|
|
|
2019-04-23 00:36:06 +02:00
|
|
|
for (current = 0; current < cnt; current++) {
|
|
|
|
sample *smp = smps[current];
|
2019-03-16 10:04:23 +01:00
|
|
|
|
|
|
|
for (size_t i = 0; i < vlist_length(hs); i++) {
|
2019-03-26 15:33:47 +01:00
|
|
|
Hook *h = (Hook *) vlist_at(hs, i);
|
|
|
|
|
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
|
|
|
|
|
|
|
smps[processed++] = smp;
|
2019-03-16 10:04:23 +01:00
|
|
|
skip: {}
|
|
|
|
}
|
|
|
|
|
|
|
|
stop: return processed;
|
|
|
|
}
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
void hook_list_periodic(struct vlist *hs)
|
2019-03-16 10:04:23 +01:00
|
|
|
{
|
|
|
|
for (size_t j = 0; j < vlist_length(hs); j++) {
|
2019-03-26 15:33:47 +01:00
|
|
|
Hook *h = (Hook *) vlist_at(hs, j);
|
2019-03-16 10:04:23 +01:00
|
|
|
|
2019-06-11 18:32:58 +02:00
|
|
|
h->periodic();
|
2019-03-16 10:04:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
void hook_list_start(struct vlist *hs)
|
2019-03-16 10:04:23 +01:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < vlist_length(hs); i++) {
|
2019-03-26 15:33:47 +01:00
|
|
|
Hook *h = (Hook *) vlist_at(hs, i);
|
2019-03-16 10:04:23 +01:00
|
|
|
|
2019-06-11 18:32:58 +02:00
|
|
|
h->start();
|
2019-03-16 10:04:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
void hook_list_stop(struct vlist *hs)
|
2019-03-16 10:04:23 +01:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < vlist_length(hs); i++) {
|
2019-03-26 15:33:47 +01:00
|
|
|
Hook *h = (Hook *) vlist_at(hs, i);
|
2019-03-16 10:04:23 +01:00
|
|
|
|
2019-06-11 18:32:58 +02:00
|
|
|
h->stop();
|
2019-03-16 10:04:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
struct vlist * hook_list_get_signals(struct vlist *hs)
|
2019-03-16 10:04:23 +01:00
|
|
|
{
|
2019-03-26 15:33:47 +01:00
|
|
|
Hook *h = (Hook *) vlist_last(hs);
|
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-02-16 14:15:14 +01:00
|
|
|
json_t * hook_list_to_json(struct vlist *hs)
|
2020-08-25 20:24:18 +02:00
|
|
|
{
|
|
|
|
json_t *json_hooks = json_array();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < vlist_length(hs); i++) {
|
|
|
|
Hook *h = (Hook *) vlist_at_safe(hs, i);
|
|
|
|
|
|
|
|
json_array_append(json_hooks, h->getConfig());
|
|
|
|
}
|
|
|
|
|
|
|
|
return json_hooks;
|
2021-02-16 14:15:14 +01:00
|
|
|
}
|