2016-06-08 22:38:50 +02:00
|
|
|
/** Hook-releated functions.
|
2014-08-31 14:43:28 +00:00
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2017-03-09 13:30:19 -03:00
|
|
|
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
|
2017-04-27 12:56:43 +02: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.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* 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.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2015-06-02 21:53:04 +02:00
|
|
|
*********************************************************************************/
|
2018-05-13 13:52:02 +02:00
|
|
|
|
2014-08-31 14:43:28 +00:00
|
|
|
#include <string.h>
|
2015-09-28 19:18:39 +02:00
|
|
|
#include <math.h>
|
2014-08-31 14:43:28 +00:00
|
|
|
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/timing.h>
|
|
|
|
#include <villas/config.h>
|
|
|
|
#include <villas/hook.h>
|
|
|
|
#include <villas/path.h>
|
|
|
|
#include <villas/utils.h>
|
|
|
|
#include <villas/node.h>
|
|
|
|
#include <villas/plugin.h>
|
|
|
|
#include <villas/config_helper.h>
|
2015-06-10 15:10:51 +02:00
|
|
|
|
2017-09-02 14:27:58 +02:00
|
|
|
int hook_init(struct hook *h, struct hook_type *vt, struct path *p, struct node *n)
|
2016-10-22 20:42:05 -04:00
|
|
|
{
|
2017-03-17 01:08:48 -03:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
assert(h->state == STATE_DESTROYED);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-17 02:52:59 -03:00
|
|
|
h->priority = vt->priority;
|
2017-09-02 14:20:38 +02:00
|
|
|
|
2017-09-04 14:33:13 +02:00
|
|
|
/* Node hooks can only used with nodes,
|
|
|
|
Path hooks only with paths.. */
|
|
|
|
if ((!(vt->flags & HOOK_NODE) && n) ||
|
|
|
|
(!(vt->flags & HOOK_PATH) && p))
|
|
|
|
return -1;
|
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
h->path = p;
|
2017-09-02 14:27:58 +02:00
|
|
|
h->node = n;
|
2017-03-20 09:13:01 -03:00
|
|
|
|
|
|
|
h->_vt = vt;
|
|
|
|
h->_vd = alloc(vt->size);
|
2017-03-27 12:26:11 +02:00
|
|
|
|
|
|
|
ret = h->_vt->init ? h->_vt->init(h) : 0;
|
2017-03-17 01:08:48 -03:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-17 01:08:48 -03:00
|
|
|
h->state = STATE_INITIALIZED;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-17 01:08:48 -03:00
|
|
|
return 0;
|
2016-10-22 20:42:05 -04:00
|
|
|
}
|
2016-01-15 15:34:28 +01:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
int hook_parse(struct hook *h, json_t *cfg)
|
2016-01-15 15:34:28 +01:00
|
|
|
{
|
2017-03-17 01:08:48 -03:00
|
|
|
int ret;
|
2017-08-03 00:19:27 +02:00
|
|
|
json_error_t err;
|
2017-03-17 02:52:59 -03:00
|
|
|
|
2017-03-17 01:08:48 -03:00
|
|
|
assert(h->state != STATE_DESTROYED);
|
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
ret = json_unpack_ex(cfg, &err, 0, "{ s?: i }",
|
|
|
|
"priority", &h->priority
|
|
|
|
);
|
|
|
|
if (ret)
|
|
|
|
jerror(&err, "Failed to parse configuration of hook '%s'", plugin_name(h->_vt));
|
2017-03-27 12:26:11 +02:00
|
|
|
|
|
|
|
ret = h->_vt->parse ? h->_vt->parse(h, cfg) : 0;
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
h->cfg = cfg;
|
2017-03-17 01:08:48 -03:00
|
|
|
h->state = STATE_PARSED;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-17 01:08:48 -03:00
|
|
|
return 0;
|
2016-10-22 20:42:05 -04:00
|
|
|
}
|
|
|
|
|
2017-07-13 22:13:40 +02:00
|
|
|
int hook_parse_cli(struct hook *h, int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (h->_vt->parse_cli) {
|
|
|
|
ret = h->_vt->parse_cli(h, argc, argv);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
h->state = STATE_PARSED;
|
|
|
|
}
|
|
|
|
else {
|
2017-08-03 00:19:27 +02:00
|
|
|
h->cfg = json_load_cli(argc, argv);
|
|
|
|
if (!h->cfg)
|
|
|
|
return -1;
|
2017-07-13 22:13:40 +02:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
ret = hook_parse(h, h->cfg);
|
2017-07-13 22:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-17 01:08:48 -03:00
|
|
|
int hook_destroy(struct hook *h)
|
2016-10-22 20:42:05 -04:00
|
|
|
{
|
2017-03-17 01:08:48 -03:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
assert(h->state != STATE_DESTROYED);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
ret = h->_vt->destroy ? h->_vt->destroy(h) : 0;
|
2017-03-17 01:08:48 -03:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-20 09:13:01 -03:00
|
|
|
if (h->_vd)
|
|
|
|
free(h->_vd);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
h->state = STATE_DESTROYED;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-22 20:42:05 -04:00
|
|
|
return 0;
|
2016-01-15 15:34:28 +01:00
|
|
|
}
|
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
int hook_start(struct hook *h)
|
|
|
|
{
|
2017-09-04 14:32:48 +02:00
|
|
|
if (h->_vt->start) {
|
|
|
|
debug(LOG_HOOK | 10, "Running hook %s: type=start, priority=%d", plugin_name(h->_vt), h->priority);
|
2017-07-09 19:35:15 +02:00
|
|
|
|
2017-09-04 14:32:48 +02:00
|
|
|
return h->_vt->start(h);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
2017-03-27 12:26:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int hook_stop(struct hook *h)
|
|
|
|
{
|
2017-09-04 14:32:48 +02:00
|
|
|
if (h->_vt->stop) {
|
|
|
|
debug(LOG_HOOK | 10, "Running hook %s: type=stop, priority=%d", plugin_name(h->_vt), h->priority);
|
2017-07-09 19:35:15 +02:00
|
|
|
|
2017-09-04 14:32:48 +02:00
|
|
|
return h->_vt->stop(h);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
2017-03-27 12:26:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int hook_periodic(struct hook *h)
|
|
|
|
{
|
2017-09-04 14:32:48 +02:00
|
|
|
if (h->_vt->periodic) {
|
|
|
|
debug(LOG_HOOK | 10, "Running hook %s: type=periodic, priority=%d", plugin_name(h->_vt), h->priority);
|
2017-07-09 19:35:15 +02:00
|
|
|
|
2017-09-04 14:32:48 +02:00
|
|
|
return h->_vt->periodic(h);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
2017-03-27 12:26:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int hook_restart(struct hook *h)
|
|
|
|
{
|
2017-09-16 15:33:57 +02:00
|
|
|
if (h->_vt->restart) {
|
|
|
|
debug(LOG_HOOK | 10, "Running hook %s: type=restart, priority=%d", plugin_name(h->_vt), h->priority);
|
2017-07-13 22:13:40 +02:00
|
|
|
|
2017-09-16 15:33:57 +02:00
|
|
|
return h->_vt->restart(h);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
2017-03-27 12:26:11 +02:00
|
|
|
}
|
|
|
|
|
2017-08-05 22:18:47 +02:00
|
|
|
int hook_read(struct hook *h, struct sample *smps[], unsigned *cnt)
|
2017-03-27 12:26:11 +02:00
|
|
|
{
|
2017-09-04 14:32:48 +02:00
|
|
|
if (h->_vt->read) {
|
|
|
|
debug(LOG_HOOK | 10, "Running hook %s: type=read, priority=%d", plugin_name(h->_vt), h->priority);
|
2017-07-13 22:13:40 +02:00
|
|
|
|
2017-09-04 14:32:48 +02:00
|
|
|
return h->_vt->read(h, smps, cnt);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
2017-03-27 12:26:11 +02:00
|
|
|
}
|
|
|
|
|
2017-08-30 16:38:07 +02:00
|
|
|
int hook_process(struct hook *h, struct sample *smps[], unsigned *cnt)
|
|
|
|
{
|
2017-09-04 14:32:48 +02:00
|
|
|
if (h->_vt->process) {
|
|
|
|
debug(LOG_HOOK | 10, "Running hook %s: type=process, priority=%d", plugin_name(h->_vt), h->priority);
|
2017-08-30 16:38:07 +02:00
|
|
|
|
2017-09-04 14:32:48 +02:00
|
|
|
return h->_vt->process(h, smps, cnt);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
2017-08-30 16:38:07 +02:00
|
|
|
}
|
|
|
|
|
2017-08-05 22:18:47 +02:00
|
|
|
int hook_write(struct hook *h, struct sample *smps[], unsigned *cnt)
|
2017-07-09 19:35:15 +02:00
|
|
|
{
|
2017-09-04 14:32:48 +02:00
|
|
|
if (h->_vt->write) {
|
|
|
|
debug(LOG_HOOK | 10, "Running hook %s: type=write, priority=%d", plugin_name(h->_vt), h->priority);
|
2017-07-09 19:35:15 +02:00
|
|
|
|
2017-09-04 14:32:48 +02:00
|
|
|
return h->_vt->write(h, smps, cnt);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
2017-07-09 19:35:15 +02:00
|
|
|
}
|
|
|
|
|
2017-08-30 16:38:07 +02:00
|
|
|
static int hook_run_list(struct list *hs, struct sample *smps[], unsigned cnt, int (*func)(struct hook *, struct sample **, unsigned *))
|
2017-03-27 12:26:11 +02:00
|
|
|
{
|
2017-08-05 22:18:47 +02:00
|
|
|
unsigned ret;
|
2017-03-27 12:26:11 +02:00
|
|
|
|
|
|
|
for (size_t i = 0; i < list_length(hs); i++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct hook *h = (struct hook *) list_at(hs, i);
|
2017-03-27 12:26:11 +02:00
|
|
|
|
2017-08-30 16:38:07 +02:00
|
|
|
ret = func(h, smps, &cnt);
|
2017-03-27 12:26:11 +02:00
|
|
|
if (ret || !cnt)
|
|
|
|
/* Abort hook processing if earlier hooks removed all samples
|
|
|
|
* or they returned something non-zero */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2017-08-30 16:38:07 +02:00
|
|
|
int hook_read_list(struct list *hs, struct sample *smps[], unsigned cnt)
|
2017-03-27 12:26:11 +02:00
|
|
|
{
|
2017-08-30 16:38:07 +02:00
|
|
|
return hook_run_list(hs, smps, cnt, hook_read);
|
|
|
|
}
|
2017-03-27 12:26:11 +02:00
|
|
|
|
2017-08-30 16:38:07 +02:00
|
|
|
int hook_process_list(struct list *hs, struct sample *smps[], unsigned cnt)
|
|
|
|
{
|
|
|
|
return hook_run_list(hs, smps, cnt, hook_process);
|
|
|
|
}
|
2017-03-27 12:26:11 +02:00
|
|
|
|
2017-08-30 16:38:07 +02:00
|
|
|
int hook_write_list(struct list *hs, struct sample *smps[], unsigned cnt)
|
|
|
|
{
|
|
|
|
return hook_run_list(hs, smps, cnt, hook_write);
|
2017-03-27 12:26:11 +02:00
|
|
|
}
|
|
|
|
|
2017-03-17 01:08:48 -03:00
|
|
|
int hook_cmp_priority(const void *a, const void *b)
|
|
|
|
{
|
2016-01-14 22:59:57 +01:00
|
|
|
struct hook *ha = (struct hook *) a;
|
|
|
|
struct hook *hb = (struct hook *) b;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-17 02:52:59 -03:00
|
|
|
return ha->priority - hb->priority;
|
2016-01-14 22:59:57 +01:00
|
|
|
}
|
|
|
|
|
2017-09-02 14:27:58 +02:00
|
|
|
int hook_parse_list(struct list *list, json_t *cfg, struct path *o, struct node *n)
|
2017-03-05 10:06:32 -04:00
|
|
|
{
|
2017-08-03 00:19:27 +02:00
|
|
|
if (!json_is_array(cfg))
|
|
|
|
error("Hooks must be configured as a list of objects");
|
|
|
|
|
|
|
|
size_t index;
|
2017-10-16 08:08:35 +02:00
|
|
|
json_t *json_hook;
|
|
|
|
json_array_foreach(cfg, index, json_hook) {
|
2017-08-03 00:19:27 +02:00
|
|
|
int ret;
|
|
|
|
const char *type;
|
2018-05-13 13:52:02 +02:00
|
|
|
struct hook_type *ht;
|
2017-08-03 00:19:27 +02:00
|
|
|
json_error_t err;
|
|
|
|
|
2017-10-16 08:08:35 +02:00
|
|
|
ret = json_unpack_ex(json_hook, &err, 0, "{ s: s }", "type", &type);
|
2017-08-03 00:19:27 +02:00
|
|
|
if (ret)
|
|
|
|
jerror(&err, "Failed to parse hook");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-05-13 13:52:02 +02:00
|
|
|
ht = hook_type_lookup(type);
|
|
|
|
if (!ht)
|
2017-08-03 00:19:27 +02:00
|
|
|
jerror(&err, "Unkown hook type '%s'", type);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-10-18 15:39:53 +02:00
|
|
|
struct hook *h = (struct hook *) alloc(sizeof(struct hook));
|
2017-07-13 22:13:40 +02:00
|
|
|
|
2018-05-13 13:52:02 +02:00
|
|
|
ret = hook_init(h, ht, o, n);
|
2017-03-17 02:52:59 -03:00
|
|
|
if (ret)
|
2018-05-13 13:52:02 +02:00
|
|
|
error("Failed to initialize hook: %s", type);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-10-16 08:08:35 +02:00
|
|
|
ret = hook_parse(h, json_hook);
|
2017-03-17 02:52:59 -03:00
|
|
|
if (ret)
|
2017-08-03 00:19:27 +02:00
|
|
|
jerror(&err, "Failed to parse hook configuration");
|
2016-11-20 12:59:37 -05:00
|
|
|
|
2017-07-09 14:36:09 +02:00
|
|
|
list_push(list, h);
|
2016-11-20 12:59:37 -05:00
|
|
|
}
|
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
return 0;
|
2017-04-12 23:13:02 +02:00
|
|
|
}
|