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/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
#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>
|
2019-04-23 13:09:50 +02:00
|
|
|
#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) :
|
2020-09-11 14:57:05 +02:00
|
|
|
logger(logging.get("hook")),
|
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),
|
2020-09-11 14:57:05 +02:00
|
|
|
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()
|
|
|
|
{
|
2020-09-10 11:11:42 +02:00
|
|
|
int ret __attribute__((unused));
|
2020-09-11 14:57:05 +02:00
|
|
|
|
2020-09-10 11:11:42 +02:00
|
|
|
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
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
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,
|
|
|
|
"enabled", &enabled
|
|
|
|
);
|
|
|
|
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-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
|
|
|
}
|