From 6f4c139c2f4082649bbfcc3947ee54995a6cbd17 Mon Sep 17 00:00:00 2001
From: Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
Date: Mon, 27 Mar 2017 12:54:24 +0200
Subject: [PATCH] move checks into *_check(), set default values in _init()

---
 lib/node.c |  6 ++++--
 lib/path.c | 54 +++++++++++++++++++++++++++++-------------------------
 2 files changed, 33 insertions(+), 27 deletions(-)

diff --git a/lib/node.c b/lib/node.c
index df37b6e9b..46396daaa 100644
--- a/lib/node.c
+++ b/lib/node.c
@@ -20,6 +20,9 @@ int node_init(struct node *n, struct node_type *vt)
 	n->_vt = vt;
 	n->_vd = alloc(vt->size);
 	
+	/* Default values */
+	n->vectorize = 1;
+	
 	list_push(&vt->instances, n);
 
 	n->state = STATE_INITIALIZED;
@@ -41,8 +44,7 @@ int node_parse(struct node *n, config_setting_t *cfg)
 	p = plugin_lookup(PLUGIN_TYPE_NODE, type);
 	assert(&p->node == n->_vt);
 	
-	if (!config_setting_lookup_int(cfg, "vectorize", &n->vectorize))
-		n->vectorize = 1;
+	config_setting_lookup_int(cfg, "vectorize", &n->vectorize);
 
 	n->name = name;
 	n->cfg = cfg;
diff --git a/lib/path.c b/lib/path.c
index 0a5408d0d..75ccc8f98 100644
--- a/lib/path.c
+++ b/lib/path.c
@@ -151,6 +151,13 @@ int path_init(struct path *p, struct super_node *sn)
 	list_init(&p->hooks);
 	list_init(&p->destinations);
 	
+	/* Default values */
+	p->reverse = 0;
+	p->enabled = 1;
+
+	p->samplelen = DEFAULT_VALUES;
+	p->queuelen = DEFAULT_QUEUELEN;
+	
 	p->super_node = sn;
 	
 	p->state = STATE_INITIALIZED;
@@ -160,9 +167,9 @@ int path_init(struct path *p, struct super_node *sn)
 
 int path_parse(struct path *p, config_setting_t *cfg, struct list *nodes)
 {
-	config_setting_t *cfg_out, *cfg_hook;
+	config_setting_t *cfg_out, *cfg_hooks;
 	const char *in;
-	int ret, samplelen, queuelen;
+	int ret;
 
 	struct node *source;
 	struct list destinations = { .state = STATE_DESTROYED };
@@ -189,37 +196,32 @@ int path_parse(struct path *p, config_setting_t *cfg, struct list *nodes)
 		cerror(cfg, "Missing output nodes for path");
 
 	ret = node_parse_list(&destinations, cfg_out, nodes);
-	if (ret <= 0)
+	if (ret || list_length(&destinations) == 0)
 		cerror(cfg_out, "Invalid output nodes");
 
 	/* Optional settings */
-	cfg_hook = config_setting_get_member(cfg, "hook");
-	if (cfg_hook)
-		hook_parse_list(&p->hooks, cfg_hook);
+	cfg_hooks = config_setting_get_member(cfg, "hooks");
+	if (cfg_hooks) {
+		ret = hook_parse_list(&p->hooks, cfg_hooks, p);
+		if (ret)
+			return ret;
+	}
 
-	if (!config_setting_lookup_bool(cfg, "reverse", &p->reverse))
-		p->reverse = 0;
-	if (!config_setting_lookup_bool(cfg, "enabled", &p->enabled))
-		p->enabled = 1;
-	if (!config_setting_lookup_int(cfg, "values", &samplelen))
-		samplelen = DEFAULT_VALUES;
-	if (!config_setting_lookup_int(cfg, "queuelen", &queuelen))
-		queuelen = DEFAULT_QUEUELEN;
+	config_setting_lookup_bool(cfg, "reverse", &p->reverse);
+	config_setting_lookup_bool(cfg, "enabled", &p->enabled);
+	config_setting_lookup_int(cfg, "values", &p->samplelen);
+	config_setting_lookup_int(cfg, "queuelen", &p->queuelen);
 
-	if (!IS_POW2(queuelen)) {
-		queuelen = LOG2_CEIL(queuelen);
-		warn("Queue length should always be a power of 2. Adjusting to %d", queuelen);
+	if (!IS_POW2(p->queuelen)) {
+		p->queuelen = LOG2_CEIL(p->queuelen);
+		warn("Queue length should always be a power of 2. Adjusting to %d", p->queuelen);
 	}
 
 	p->cfg = cfg;
-	
-	/* Check if nodes are suitable */
-	if (source->_vt->read == NULL)
-		cerror(cfg, "Input node '%s' is not supported as a source.", node_name(source));
 
 	p->source = alloc(sizeof(struct path_source));
 	p->source->node = source;
-	p->source->samplelen = samplelen;
+	p->source->samplelen = p->samplelen;
 
 	for (size_t i = 0; i < list_length(&destinations); i++) {
 		struct node *n = list_at(&destinations, i);
@@ -242,15 +244,17 @@ int path_check(struct path *p)
 	assert(p->state != STATE_DESTROYED);
 	
 	for (size_t i = 0; i < list_length(&p->destinations); i++) {
-		struct node *n = list_at(&p->destinations, i);
+		struct path_destination *pd = list_at(&p->destinations, i);
 		
-		if (!n->_vt->write)
-			error("Destiation node '%s' is not supported as a sink for path '%s'", node_name(n), path_name(p));
+		if (!pd->node->_vt->write)
+			error("Destiation node '%s' is not supported as a sink for path '%s'", node_name(pd->node), path_name(p));
 	}
 
 	if (!p->source->node->_vt->read)
 		error("Source node '%s' is not supported as source for path '%s'", node_name(p->source->node), path_name(p));
 	
+	p->state = STATE_CHECKED;
+	
 	return 0;
 }