mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
generalize: *_type_lookup() as wrappers around plugin_lookup()
This commit is contained in:
parent
9ec69bcbdf
commit
a520e00a12
12 changed files with 92 additions and 37 deletions
|
@ -71,3 +71,6 @@ struct hook_type {
|
|||
int (*process)(struct hook *h, struct sample *smps[], unsigned *cnt); /**< Called whenever muxed samples are processed. */
|
||||
int (*write)(struct hook *h, struct sample *smps[], unsigned *cnt); /**< Called whenever samples are written to a node. */
|
||||
};
|
||||
|
||||
struct hook_type * hook_type_lookup(const char *name);
|
||||
|
||||
|
|
|
@ -166,4 +166,6 @@ int node_type_stop(struct node_type *vt);
|
|||
/** Return a printable representation of the node-type. */
|
||||
const char * node_type_name(struct node_type *vt);
|
||||
|
||||
struct node_type * node_type_lookup(const char *name);
|
||||
|
||||
/** @} */
|
||||
|
|
|
@ -50,7 +50,7 @@ ifeq ($(WITH_IO),1)
|
|||
endif
|
||||
|
||||
ifeq ($(WITH_HOOKS),1)
|
||||
LIB_SRCS += lib/hook.c
|
||||
LIB_SRCS += lib/hook.c lib/hook_type.c
|
||||
include lib/hooks/Makefile.inc
|
||||
endif
|
||||
|
||||
|
|
11
lib/hook.c
11
lib/hook.c
|
@ -19,6 +19,7 @@
|
|||
* 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 <string.h>
|
||||
#include <math.h>
|
||||
|
||||
|
@ -250,22 +251,22 @@ int hook_parse_list(struct list *list, json_t *cfg, struct path *o, struct node
|
|||
json_array_foreach(cfg, index, json_hook) {
|
||||
int ret;
|
||||
const char *type;
|
||||
struct plugin *p;
|
||||
struct hook_type *ht;
|
||||
json_error_t err;
|
||||
|
||||
ret = json_unpack_ex(json_hook, &err, 0, "{ s: s }", "type", &type);
|
||||
if (ret)
|
||||
jerror(&err, "Failed to parse hook");
|
||||
|
||||
p = plugin_lookup(PLUGIN_TYPE_HOOK, type);
|
||||
if (!p)
|
||||
ht = hook_type_lookup(type);
|
||||
if (!ht)
|
||||
jerror(&err, "Unkown hook type '%s'", type);
|
||||
|
||||
struct hook *h = (struct hook *) alloc(sizeof(struct hook));
|
||||
|
||||
ret = hook_init(h, &p->hook, o, n);
|
||||
ret = hook_init(h, ht, o, n);
|
||||
if (ret)
|
||||
error("Failed to initialize hook: %s", p->name);
|
||||
error("Failed to initialize hook: %s", type);
|
||||
|
||||
ret = hook_parse(h, json_hook);
|
||||
if (ret)
|
||||
|
|
35
lib/hook_type.c
Normal file
35
lib/hook_type.c
Normal file
|
@ -0,0 +1,35 @@
|
|||
/** Hook-releated functions.
|
||||
*
|
||||
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
||||
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
|
||||
* @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 <villas/hook_type.h>
|
||||
#include <villas/plugin.h>
|
||||
|
||||
struct hook_type * hook_type_lookup(const char *name)
|
||||
{
|
||||
struct plugin *p;
|
||||
|
||||
p = plugin_lookup(PLUGIN_TYPE_HOOK, name);
|
||||
if (!p)
|
||||
return NULL;
|
||||
|
||||
return &p->hook;
|
||||
}
|
|
@ -23,6 +23,7 @@
|
|||
#include <string.h>
|
||||
|
||||
#include <villas/config.h>
|
||||
#include <villas/hook.h>
|
||||
#include <villas/sample.h>
|
||||
#include <villas/node.h>
|
||||
#include <villas/utils.h>
|
||||
|
@ -101,7 +102,7 @@ int node_init2(struct node *n)
|
|||
|
||||
int node_parse(struct node *n, json_t *cfg, const char *name)
|
||||
{
|
||||
struct plugin *p;
|
||||
struct node_type *nt;
|
||||
int ret;
|
||||
|
||||
json_error_t err;
|
||||
|
@ -123,8 +124,8 @@ int node_parse(struct node *n, json_t *cfg, const char *name)
|
|||
if (ret)
|
||||
jerror(&err, "Failed to parse node '%s'", node_name(n));
|
||||
|
||||
p = plugin_lookup(PLUGIN_TYPE_NODE, type);
|
||||
assert(&p->node == n->_vt);
|
||||
nt = node_type_lookup(type);
|
||||
assert(nt == n->_vt);
|
||||
|
||||
#ifdef WITH_HOOKS
|
||||
if (json_hooks) {
|
||||
|
|
|
@ -69,3 +69,14 @@ const char * node_type_name(struct node_type *vt)
|
|||
{
|
||||
return plugin_name(vt);
|
||||
}
|
||||
|
||||
struct node_type * node_type_lookup(const char *name)
|
||||
{
|
||||
struct plugin *p;
|
||||
|
||||
p = plugin_lookup(PLUGIN_TYPE_NODE, name);
|
||||
if (!p)
|
||||
return NULL;
|
||||
|
||||
return &p->node;
|
||||
}
|
||||
|
|
|
@ -244,20 +244,20 @@ int super_node_parse_json(struct super_node *sn, json_t *cfg)
|
|||
const char *name;
|
||||
json_t *json_node;
|
||||
json_object_foreach(json_nodes, name, json_node) {
|
||||
struct plugin *p;
|
||||
struct node_type *nt;
|
||||
const char *type;
|
||||
|
||||
ret = json_unpack_ex(json_node, &err, 0, "{ s: s }", "type", &type);
|
||||
if (ret)
|
||||
jerror(&err, "Failed to parse node");
|
||||
|
||||
p = plugin_lookup(PLUGIN_TYPE_NODE, type);
|
||||
if (!p)
|
||||
nt = node_type_lookup(type);
|
||||
if (!nt)
|
||||
error("Invalid node type: %s", type);
|
||||
|
||||
struct node *n = (struct node *) alloc(sizeof(struct node));
|
||||
|
||||
ret = node_init(n, &p->node);
|
||||
ret = node_init(n, nt);
|
||||
if (ret)
|
||||
error("Failed to initialize node");
|
||||
|
||||
|
|
|
@ -80,10 +80,9 @@ check: if (optarg == endptr)
|
|||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
struct plugin *p;
|
||||
struct format_type *fmt;
|
||||
struct log log;
|
||||
struct io input;
|
||||
struct io output;
|
||||
struct io input, output;
|
||||
|
||||
ret = log_init(&log, level, LOG_ALL);
|
||||
if (ret)
|
||||
|
@ -102,11 +101,11 @@ check: if (optarg == endptr)
|
|||
};
|
||||
|
||||
for (int i = 0; i < ARRAY_LEN(dirs); i++) {
|
||||
p = plugin_lookup(PLUGIN_TYPE_FORMAT, dirs[i].name);
|
||||
if (!p)
|
||||
fmt = format_type_lookup(dirs[i].name);
|
||||
if (!fmt)
|
||||
error("Invalid format: %s", dirs[i].name);
|
||||
|
||||
ret = io_init(dirs[i].io, &p->io, NULL, SAMPLE_HAS_ALL);
|
||||
ret = io_init(dirs[i].io, fmt, NULL, SAMPLE_HAS_ALL);
|
||||
if (ret)
|
||||
error("Failed to initialize IO: %s", dirs[i].name);
|
||||
|
||||
|
|
16
src/hook.c
16
src/hook.c
|
@ -44,7 +44,6 @@
|
|||
int cnt;
|
||||
|
||||
struct sample **smps;
|
||||
struct plugin *p;
|
||||
|
||||
struct log l = { .state = STATE_DESTROYED };
|
||||
struct pool q = { .state = STATE_DESTROYED };
|
||||
|
@ -110,6 +109,9 @@ int main(int argc, char *argv[])
|
|||
int ret, recv, sent;
|
||||
char *format = "villas.human";
|
||||
|
||||
struct format_type *ft;
|
||||
struct hook_type *ht;
|
||||
|
||||
/* Default values */
|
||||
cnt = 1;
|
||||
|
||||
|
@ -181,11 +183,11 @@ check: if (optarg == endptr)
|
|||
error("Failed to initilize memory pool");
|
||||
|
||||
/* Initialize IO */
|
||||
p = plugin_lookup(PLUGIN_TYPE_FORMAT, format);
|
||||
if (!p)
|
||||
ft = format_type_lookup(format);
|
||||
if (!ft)
|
||||
error("Unknown IO format '%s'", format);
|
||||
|
||||
ret = io_init(&io, &p->io, NULL, SAMPLE_HAS_ALL);
|
||||
ret = io_init(&io, ft, NULL, SAMPLE_HAS_ALL);
|
||||
if (ret)
|
||||
error("Failed to initialize IO");
|
||||
|
||||
|
@ -194,11 +196,11 @@ check: if (optarg == endptr)
|
|||
error("Failed to open IO");
|
||||
|
||||
/* Initialize hook */
|
||||
p = plugin_lookup(PLUGIN_TYPE_HOOK, hook);
|
||||
if (!p)
|
||||
ht = hook_type_lookup(hook);
|
||||
if (!ht)
|
||||
error("Unknown hook function '%s'", hook);
|
||||
|
||||
ret = hook_init(&h, &p->hook, NULL, NULL);
|
||||
ret = hook_init(&h, ht, NULL, NULL);
|
||||
if (ret)
|
||||
error("Failed to initialize hook");
|
||||
|
||||
|
|
|
@ -295,7 +295,7 @@ check: if (optarg == endptr)
|
|||
|
||||
char *configfile = argv[optind];
|
||||
char *nodestr = argv[optind+1];
|
||||
struct plugin *p;
|
||||
struct format_type *fmt;
|
||||
|
||||
ret = log_init(&sn.log, level, LOG_ALL);
|
||||
if (ret)
|
||||
|
@ -325,11 +325,11 @@ check: if (optarg == endptr)
|
|||
if (ret)
|
||||
error("Failed to initalize real-time");
|
||||
|
||||
p = plugin_lookup(PLUGIN_TYPE_FORMAT, format);
|
||||
if (!p)
|
||||
fmt = format_type_lookup(format);
|
||||
if (!fmt)
|
||||
error("Invalid format: %s", format);
|
||||
|
||||
ret = io_init(&io, &p->io, NULL, SAMPLE_HAS_ALL);
|
||||
ret = io_init(&io, fmt, NULL, SAMPLE_HAS_ALL);
|
||||
if (ret)
|
||||
error("Failed to initialize IO");
|
||||
|
||||
|
|
15
src/signal.c
15
src/signal.c
|
@ -108,7 +108,8 @@ static void quit(int signal, siginfo_t *sinfo, void *ctx)
|
|||
int main(int argc, char *argv[])
|
||||
{
|
||||
int ret;
|
||||
struct plugin *p;
|
||||
struct node_type *nt;
|
||||
struct format_type *ft;
|
||||
|
||||
char *format = "villas.human"; /** @todo hardcoded for now */
|
||||
|
||||
|
@ -124,19 +125,19 @@ int main(int argc, char *argv[])
|
|||
if (ret)
|
||||
error("Failed to intialize signals");
|
||||
|
||||
p = plugin_lookup(PLUGIN_TYPE_NODE, "signal");
|
||||
if (!p)
|
||||
nt = node_type_lookup("signal");
|
||||
if (!nt)
|
||||
error("Signal generation is not supported.");
|
||||
|
||||
ret = node_init(&n, &p->node);
|
||||
ret = node_init(&n, nt);
|
||||
if (ret)
|
||||
error("Failed to initialize node");
|
||||
|
||||
p = plugin_lookup(PLUGIN_TYPE_FORMAT, format);
|
||||
if (!p)
|
||||
ft = format_type_lookup(format);
|
||||
if (!ft)
|
||||
error("Invalid output format '%s'", format);
|
||||
|
||||
ret = io_init(&io, &p->io, NULL, IO_FLUSH | (SAMPLE_HAS_ALL & ~SAMPLE_HAS_OFFSET));
|
||||
ret = io_init(&io, ft, NULL, IO_FLUSH | (SAMPLE_HAS_ALL & ~SAMPLE_HAS_OFFSET));
|
||||
if (ret)
|
||||
error("Failed to initialize output");
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue