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

103 lines
2.4 KiB
C++
Raw Permalink Normal View History

2019-03-26 15:33:47 +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-26 15:33:47 +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/>.
*********************************************************************************/
#include <cstring>
#include <cmath>
2019-03-26 15:33:47 +01:00
#include <villas/timing.h>
#include <villas/node/config.h>
#include <villas/hook.hpp>
#include <villas/node/exceptions.hpp>
#include <villas/path.h>
#include <villas/utils.hpp>
2019-03-26 15:33:47 +01:00
#include <villas/node.h>
const char *hook_reasons[] = {
"ok", "error", "skip-sample", "stop-processing"
};
using namespace villas;
using namespace villas::node;
2020-08-25 21:00:52 +02:00
Hook::Hook(struct vpath *p, struct vnode *n, int fl, int prio, bool en) :
2019-06-23 16:13:23 +02:00
state(State::INITIALIZED),
2019-03-26 15:33:47 +01:00
flags(fl),
priority(prio),
enabled(en),
path(p),
node(n),
2021-02-16 14:15:14 +01:00
config(nullptr)
2019-03-26 15:33:47 +01:00
{
int ret;
ret = signal_list_init(&signals);
if (ret)
throw RuntimeError("Failed to initialize signal list");
/* We dont need to parse builtin hooks. */
2019-06-23 16:13:23 +02:00
state = flags & (int) Hook::Flags::BUILTIN ? State::CHECKED : State::INITIALIZED;
2019-03-26 15:33:47 +01:00
}
Hook::~Hook()
{
int ret __attribute__((unused));
ret = signal_list_destroy(&signals);
2019-03-26 15:33:47 +01:00
}
2021-02-19 06:40:26 +01:00
void Hook::prepare(struct vlist *sigs)
2019-03-26 15:33:47 +01:00
{
2021-02-19 06:40:26 +01:00
int ret;
2019-06-23 16:13:23 +02:00
assert(state == State::CHECKED);
2019-03-26 15:33:47 +01:00
2021-02-19 06:40:26 +01:00
ret = signal_list_copy(&signals, sigs);
if (ret)
throw RuntimeError("Failed to copy signal list");
prepare();
2019-06-23 16:13:23 +02:00
state = State::PREPARED;
2019-03-26 15:33:47 +01:00
}
2021-02-16 14:15:14 +01:00
void Hook::parse(json_t *json)
2019-03-26 15:33:47 +01:00
{
2021-06-19 13:09:02 -04:00
int ret, en = -1;
2019-03-26 15:33:47 +01:00
json_error_t err;
2019-06-23 16:13:23 +02:00
assert(state != State::STARTED);
2019-03-26 15:33:47 +01:00
2021-02-16 14:15:14 +01:00
ret = json_unpack_ex(json, &err, 0, "{ s?: i, s?: b }",
2019-03-26 15:33:47 +01:00
"priority", &priority,
2021-06-19 13:09:02 -04:00
"enabled", &en
2019-03-26 15:33:47 +01:00
);
if (ret)
2021-02-16 14:15:14 +01:00
throw ConfigError(json, err, "node-config-hook");
2019-03-26 15:33:47 +01:00
2021-06-19 13:09:02 -04:00
if (en >= 0)
enabled = en != 0;
2021-02-16 14:15:14 +01:00
config = json;
2019-03-26 15:33:47 +01:00
2019-06-23 16:13:23 +02:00
state = State::PARSED;
2019-03-26 15:33:47 +01:00
}