From f03fe00bff673e586c5fc02651805372ffbd9acd Mon Sep 17 00:00:00 2001
From: Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
Date: Sat, 18 Feb 2017 10:51:43 -0500
Subject: [PATCH] refactor more code to new plugin system

---
 include/villas/hooks.h  | 14 ++++++++++++--
 include/villas/plugin.h | 20 ++++++++++++++------
 lib/nodes/cbuilder.c    |  4 +---
 lib/nodes/websocket.c   | 23 ++++++++++++-----------
 lib/plugin.c            | 17 +++++++----------
 5 files changed, 46 insertions(+), 32 deletions(-)

diff --git a/include/villas/hooks.h b/include/villas/hooks.h
index 4dee690a8..dd9b70bfb 100644
--- a/include/villas/hooks.h
+++ b/include/villas/hooks.h
@@ -25,10 +25,20 @@
 
 #include "queue.h"
 #include "list.h"
-
-/* The configuration of hook parameters is done in "config.h" */
 #include "cfg.h"
 
+#define REGISTER_HOOK(nam, desc, prio, hist, fnc, typ)		\
+__attribute__((constructor)) void __register_ ## fnc () {	\
+	static struct hook h = {				\
+		.name = nam,					\
+		.description = desc,				\
+		.priority = prio,				\
+		.history = hist,				\
+		.type = typ,					\
+		.cb = fnc					\
+	};							\
+	list_push(&hooks, &h);					\
+}
 
 /* Forward declarations */
 struct path;
diff --git a/include/villas/plugin.h b/include/villas/plugin.h
index c67fe5b51..5085ad6bd 100644
--- a/include/villas/plugin.h
+++ b/include/villas/plugin.h
@@ -11,8 +11,11 @@
 
 #include "hooks.h"
 #include "api.h"
+
 #include "fpga/ip.h"
 
+#include "nodes/cbuilder.h"
+
 #define REGISTER_PLUGIN(p)					\
 __attribute__((constructor)) static void UNIQUE(__ctor)() {	\
 	list_push(&plugins, p);					\
@@ -51,18 +54,23 @@ struct plugin {
 	int (*unload)(struct plugin *p);
 	
 	union {
-		struct api_ressource api;
-		struct hook hook;
-		struct node_type node;
+		struct api_ressource	api;
+		struct node_type	node;
+		struct fpga_ip_type	ip;
+		struct hook		hook;
+		struct cbuilder_model	cb;
 	};
 };
 
-struct plugin * plugin_lookup(char *name, enum plugin_types type);
-
 int plugin_init(struct plugin *p, char *name, char *path);
 
+int plugin_destroy(struct plugin *p);
+
+int plugin_parse(struct plugin *p, config_setting_t *cfg);
+
 int plugin_load(struct plugin *p);
 
 int plugin_unload(struct plugin *p);
 
-int plugin_destroy(struct plugin *p);
+/** Find registered and loaded plugin with given name and type. */
+struct plugin * plugin_lookup(enum plugin_types type, const char *name);
\ No newline at end of file
diff --git a/lib/nodes/cbuilder.c b/lib/nodes/cbuilder.c
index 8296b697a..1d10af991 100644
--- a/lib/nodes/cbuilder.c
+++ b/lib/nodes/cbuilder.c
@@ -12,8 +12,6 @@
 
 #include "nodes/cbuilder.h"
 
-struct list cbmodels;	/**< Table of existing CBuilder models */
-
 int cbuilder_parse(struct node *n, config_setting_t *cfg)
 {
 	struct cbuilder *cb = n->_vd;
@@ -27,7 +25,7 @@ int cbuilder_parse(struct node *n, config_setting_t *cfg)
 	if (!config_setting_lookup_string(cfg, "model", &model))
 		cerror(cfg, "CBuilder model requires 'model' setting");
 	
-	cb->model = list_lookup(&cbmodels, model);
+	cb->model = (struct cbuilder_model *) plugin_lookup(PLUGIN_TYPE_MODEL_CBUILDER, model);
 	if (!cb->model)
 		cerror(cfg, "Unknown model '%s'", model);
 	
diff --git a/lib/nodes/websocket.c b/lib/nodes/websocket.c
index d7b51733c..4550d21d6 100644
--- a/lib/nodes/websocket.c
+++ b/lib/nodes/websocket.c
@@ -546,18 +546,19 @@ int websocket_write(struct node *n, struct sample *smps[], unsigned cnt)
 	return cnt;
 }
 
-static struct node_type vt = {
+static struct plugin p = {
 	.name		= "websocket",
 	.description	= "Send and receive samples of a WebSocket connection (libwebsockets)",
-	.vectorize	= 0, /* unlimited */
-	.size		= sizeof(struct websocket),
-	.open		= websocket_open,
-	.close		= websocket_close,
-	.destroy	= websocket_destroy,
-	.read		= websocket_read,
-	.write		= websocket_write,
-	.init		= websocket_init,
-	.deinit		= websocket_deinit
+	.type		= PLUGIN_TYPE_NODE,
+	.node		= {
+		.vectorize	= 0, /* unlimited */
+		.size		= sizeof(struct websocket),
+		.open		= websocket_open,
+		.close		= websocket_close,
+		.destroy	= websocket_destroy,
+		.read		= websocket_read,
+		.write		= websocket_write,
+	}
 };
 
-REGISTER_NODE_TYPE(&vt)
\ No newline at end of file
+REGISTER_PLUGIN(&p)
\ No newline at end of file
diff --git a/lib/plugin.c b/lib/plugin.c
index d32ae13ab..41bd9e12a 100644
--- a/lib/plugin.c
+++ b/lib/plugin.c
@@ -54,7 +54,7 @@ int plugin_destroy(struct plugin *p)
 	return 0;
 }
 
-int plugin_parse(struct plugin *p, config_setting_t *lcs)
+int plugin_parse(struct plugin *p, config_setting_t *cfg)
 {
 	const char *path;
 
@@ -62,23 +62,20 @@ int plugin_parse(struct plugin *p, config_setting_t *lcs)
 	if (!path)
 		cerror(cfg, "Setting 'plugin' must be a string.");
 		
-	p->
+	handle = dlopen(path, RTLD_NOW);
+	if (!handle)
+		error("Failed to load plugin %s", dlerror());
 		
-		handle = dlopen(path, RTLD_NOW);
-		if (!handle)
-			error("Failed to load plugin %s", dlerror());
-		
-		list_push_back(&cfg->plugins, handle);
-	}
+	list_push_back(&cfg->plugins, handle);
 
 	return 0;
 }
 
-struct plugin * plugin_lookup(char *name, enum plugin_types type)
+struct plugin * plugin_lookup(enum plugin_types type, const char *name)
 {
 	list_foreach(struct plugin *l, &plugins) {
 		if (l->type == type && strcmp(l->name, name) == 0)
-			return l
+			return l;
 	}
 	
 	return NULL;