From eb7d7ef09dce79ec0c95f8e8779abe963c4043f2 Mon Sep 17 00:00:00 2001
From: Samo Penic <samo.penic@gmail.com>
Date: Tue, 25 Dec 2018 19:49:00 +0000
Subject: [PATCH] First working version of plugins

---
 src/Makefile.am   |    8 +-
 src/main.c        |   10 ++
 src/plugins.c     |   82 +++++++++++++++++++++++++++
 src/plugins.h     |    6 ++
 src/demo_plugin.c |   18 ++++++
 src/general.h     |   32 ++++++++++
 6 files changed, 151 insertions(+), 5 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am
index cfd33d2..f075adc 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -13,8 +13,10 @@
 
 #------------- LIBS ----------
 
-lib_LTLIBRARIES= libtrisurf.la
-libtrisurf_la_SOURCES= general.c vertex.c bond.c triangle.c cell.c vesicle.c initial_distribution.c io.c dumpstate.c frame.c energy.c timestep.c vertexmove.c bondflip.c poly.c stats.c sh.c shcomplex.c constvol.c snapshot.c restore.c cluster.c
+lib_LTLIBRARIES= libtrisurf.la demoplugin.la
+libtrisurf_la_SOURCES= general.c vertex.c bond.c triangle.c cell.c vesicle.c initial_distribution.c io.c dumpstate.c frame.c energy.c timestep.c vertexmove.c bondflip.c poly.c stats.c sh.c shcomplex.c constvol.c snapshot.c restore.c cluster.c plugins.c
 #libtrisurf_la_CPPFLAGS = ${libxml2_CFLAGS}
 libtrisurf_la_LIBADD=${libxml2_LIBS}
-pkginclude_HEADERS=general.h vertex.h bond.h triangle.h cell.h vesicle.h initial_distribution.h io.h dumpstate.h frame.h energy.h timestep.h vertexmove.h bondflip.h poly.h stats.h sh.h shcomplex.h constvol.h snapshot.h restore.h cluster.h
+pkginclude_HEADERS=general.h vertex.h bond.h triangle.h cell.h vesicle.h initial_distribution.h io.h dumpstate.h frame.h energy.h timestep.h vertexmove.h bondflip.h poly.h stats.h sh.h shcomplex.h constvol.h snapshot.h restore.h cluster.h plugins.h
+demoplugin_la_SOURCES= demo_plugin.c
+demoplugin_la_LDFLAGS = -module -avoid-version -export-dynamic
diff --git a/src/demo_plugin.c b/src/demo_plugin.c
new file mode 100644
index 0000000..417c9c8
--- /dev/null
+++ b/src/demo_plugin.c
@@ -0,0 +1,18 @@
+#include "plugins.h"
+#include <stdlib.h>
+#include "general.h"
+char plugin_name[] = "First plugin";
+char plugin_description[]= "Plugin that shows how plugin should be made";
+char plugin_author[] = "SAMO PENIC";
+
+ts_plugin_details *init (){
+	ts_fprintf(stdout,"Hello. Plugin %s is initiating.\n\nThis will load the details section of the plugin\n", plugin_name);
+
+	ts_plugin_details *details=(ts_plugin_details *)calloc(1,sizeof(ts_plugin_details));
+	details->name = plugin_name;
+	return details;	
+}
+
+void cleanup(){
+	ts_fprintf(stdout,"Goodbye from plugin %s. This functions clears what would be created in init...\n",plugin_name);
+}
diff --git a/src/general.h b/src/general.h
index c530a68..50d5a84 100644
--- a/src/general.h
+++ b/src/general.h
@@ -300,6 +300,37 @@
 } ts_tape;
 
 
+/* plugins */
+typedef struct {
+	void (*at_start)(void);
+	void (*after_vesicle_init)(void *vesicle);
+	ts_bool (*vm_hard_constraint)(void *vesicle, ts_vertex *vtx, ts_vertex *odl_vtx);
+	ts_double (*vm_energy_before)(void *vesicle, ts_vertex *vtx);
+	ts_double (*vm_energy_after)(void *vesicle, ts_vertex *vtx);
+	ts_double (*vm_new_state_accepted)(void *vesicle, ts_vertex *vtx);
+	ts_double (*vm_new_state_rejected)(void *vesicle, ts_vertex *vtx);
+	void (*cleanup)(void);
+} ts_plugin_function;
+
+typedef struct {
+	ts_char *name;
+	ts_char *description;
+	ts_char *author;
+} ts_plugin_details;
+
+typedef struct {
+	ts_plugin_details *details;
+	ts_plugin_function *function;
+	ts_char *filename;
+	void *libhandle;
+} ts_plugin;
+
+typedef struct {
+	ts_uint n;
+	ts_plugin **plugin;
+} ts_plugin_list;
+
+/* end plugins */
 
 
 typedef struct {
@@ -330,6 +361,7 @@
 	ts_double nucleus_center[3];
 	ts_double area;
 	ts_confinement_plane confinement_plane;
+	ts_plugin_list plist;
 } ts_vesicle;
 
 
diff --git a/src/main.c b/src/main.c
index 3f91f43..a756275 100644
--- a/src/main.c
+++ b/src/main.c
@@ -17,15 +17,21 @@
 #include "shcomplex.h"
 #include "dumpstate.h"
 #include "restore.h"
-
+#include "plugins.h"
 #include <fcntl.h>
 /** Entrance function to the program
   * @param argv is a number of parameters used in program call (including the program name
   * @param argc is a pointer to strings (character arrays) which holds the arguments
   * @returns returns 0 on success, any other number on fail.
 */
-
+#include <string.h>
 int main(int argv, char *argc[]){
+	ts_char *plugin0 = (ts_char *)calloc(255,sizeof(ts_char));
+	strcpy(plugin0,"/home/samo/programiranje/trisurf-ng/src/first.so");
+	ts_char **plugins=(ts_char **)calloc(1,sizeof(ts_char *));
+	*plugins=plugin0;
+	ts_plugin_list *plist=init_plugin_list(plugins,1);
+	ts_fprintf(stdout, "TRISURF in PRVI PLUGIN %s\n", plist->plugin[0]->details->name);
 	ts_vesicle *vesicle;
 	ts_tape *tape;
 	ts_uint start_iteration=0;
diff --git a/src/plugins.c b/src/plugins.c
new file mode 100644
index 0000000..7681984
--- /dev/null
+++ b/src/plugins.c
@@ -0,0 +1,82 @@
+/* This is a first attempt in implementation of plugins to Trisurf-ng. */
+
+/*
+1. Capabilities of Plugin
+-------------------------
+
+Every plugin should list it's capabilities -- that is what subrutines are available in it. Capabilities list for the moment are:
+
+* PLUGIN_PROGRAM_START (requires subrutine void at_start(void)): runs at startup
+* PLUGIN_VESICLE_INITIALIZED (requires subrutine void after_vesicle_init(vesicle *vesicle):
+	runs after vesicle gets initialized.
+
+* PLUGIN_VERTEX_MOVE (requires subrutines:
+
+	ts_bool vm_hard_constraint(vesicle *vesicle, vtx *vtx, vtx *old_vtx)
+	ts_double vm_energy_before(vesicle *vesicle, vtx *vtx)
+	ts_double vm_energy_after(vesicle *vesicle, vtx *vtx)
+	ts_double vm_new_state_accepted(vesicle *vesicle, vtx *vtx)
+	ts_double vm_new_state_rejected(vesicle *vesicle, vtx *vtx)
+
+	)
+
+	these subrutines are added into the vertex move into right places
+
+* PLUGIN_BOND_FLIP ( N/A yet)
+*/
+#include "plugins.h"
+#include <stdlib.h>
+#include <dlfcn.h>
+#include <string.h>
+#include "general.h"
+
+ts_plugin *init_plugin(ts_char *filename){
+	ts_plugin *plugin = (ts_plugin *)calloc(1, sizeof(ts_plugin));
+	plugin->filename=(ts_char *)calloc(strlen(filename),sizeof(ts_char));
+	strcpy(plugin->filename,filename);
+	plugin->libhandle = dlopen(plugin->filename, RTLD_NOW);
+	if(!plugin->libhandle){
+		ts_fprintf(stderr,"Error loading plugin: %s\n", filename);
+		fatal("Exiting",235);
+	}
+	ts_plugin_details* (*get_plugin_details)(void) = dlsym(plugin->libhandle, "init");
+	plugin->details = get_plugin_details();
+	plugin->function = (ts_plugin_function *)calloc(1,sizeof(ts_plugin_function));
+	
+	plugin->function->cleanup = dlsym(plugin->libhandle, "cleanup");
+	plugin->function->at_start = dlsym(plugin->libhandle, "at_start");
+	plugin->function->after_vesicle_init = dlsym(plugin->libhandle, "after_vesicle_init");
+	plugin->function->vm_hard_constraint = dlsym(plugin->libhandle, "vm_hard_constraint");
+	plugin->function->vm_energy_before = dlsym(plugin->libhandle, "vm_energy_before");
+	plugin->function->vm_energy_after = dlsym(plugin->libhandle, "vm_energy_after");
+	plugin->function->vm_new_state_rejected = dlsym(plugin->libhandle, "vm_new_state_rejected");
+	plugin->function->vm_new_state_accepted = dlsym(plugin->libhandle, "vm_new_state_accepted");
+
+	return plugin;
+}
+
+
+ts_plugin_list *init_plugin_list(ts_char **plugin_filenames, ts_uint number_of_plugins){
+	ts_plugin_list *plist=(ts_plugin_list *)calloc(1, sizeof(ts_plugin_list));
+	plist->plugin=(ts_plugin **)calloc(number_of_plugins, sizeof(ts_plugin *));
+	for(int i=0;i<number_of_plugins;i++){
+		plist->plugin[i]=init_plugin(plugin_filenames[i]);
+	}
+	return plist;
+}	
+
+void free_plugin(ts_plugin *plugin){
+	plugin->function->cleanup();
+	free(plugin->function);
+	free(plugin->details);
+	free(plugin->filename);
+	dlclose(plugin->libhandle);
+	free(plugin);
+}
+
+void free_plugin_list(ts_plugin_list *plist){
+	for(int i=0;i<plist->n;i++){
+		free_plugin(plist->plugin[i]);
+	}
+	free(plist);
+}
diff --git a/src/plugins.h b/src/plugins.h
new file mode 100644
index 0000000..161f215
--- /dev/null
+++ b/src/plugins.h
@@ -0,0 +1,6 @@
+#ifndef _H_PLUGINS
+#define _H_PLUGINS
+#include "general.h"
+ts_plugin *init_plugin(ts_char *filename);
+ts_plugin_list *init_plugin_list(ts_char **plugin_filenames, ts_uint number_of_filenames);
+#endif

--
Gitblit v1.9.3