Improve plugin logic, pass plugins count to session
authorManuel Bachmann <manuel.bachmann@iot.bzh>
Tue, 22 Dec 2015 15:31:39 +0000 (16:31 +0100)
committerManuel Bachmann <manuel.bachmann@iot.bzh>
Tue, 22 Dec 2015 15:34:08 +0000 (16:34 +0100)
We now pre-reserve for 20 plugins (arbitrary for now,
we downsize if necessary).
Plugins count is now passed to the session in the
"pluginCount" variable.

Signed-off-by: Manuel Bachmann <manuel.bachmann@iot.bzh>
include/local-def.h
src/rest-api.c

index 68c4286..22a3608 100644 (file)
@@ -261,6 +261,7 @@ typedef struct {
   int  fakemod;           // respond to GET/POST request without interacting with sndboard
   int  forceexit;         // when autoconfig from script force exit before starting server
   AFB_plugin **plugins;   // pointer to REST/API plugins 
+  int pluginCount;        // loaded plugins count
   magic_t  magic;         // Mime type file magic lib
   sigjmp_buf restartCkpt; // context save for restart set/longjmp
 } AFB_session;
index 3d9ffeb..a1bf039 100644 (file)
@@ -571,9 +571,10 @@ void initPlugins(AFB_session *session) {
     struct dirent *pluginDir;
     DIR *dir;
     afbJsonType = json_object_new_string (AFB_MSG_JTYPE);
-    int i = 0;
+    int num = 0;
 
-    plugins = (AFB_plugin **) malloc (sizeof(AFB_plugin));
+    /* pre-allocate for 20 plugins, we will downsize if necessary */
+    plugins = (AFB_plugin **) malloc (20*sizeof(AFB_plugin));
 
     if ((dir = opendir(session->config->plugins)) == NULL) {
         fprintf(stderr, "Could not open plugin directory [%s], exiting...\n", session->config->plugins);
@@ -590,7 +591,7 @@ void initPlugins(AFB_session *session) {
         pluginRegisterFct = dlsym (plugin, "pluginRegister");
         free (pluginPath);
         if (!plugin) {
-            if (verbose) fprintf(stderr, "[%s] is not a binary plugin, continuing...\n", pluginDir->d_name);
+            if (verbose) fprintf(stderr, "[%s] is not loadable, continuing...\n", pluginDir->d_name);
             continue;
         } else if (!pluginRegisterFct) {
             if (verbose) fprintf(stderr, "[%s] is not an AFB plugin, continuing...\n", pluginDir->d_name);
@@ -598,12 +599,14 @@ void initPlugins(AFB_session *session) {
         }
 
         if (verbose) fprintf(stderr, "[%s] is a valid AFB plugin, loading it\n", pluginDir->d_name);
-        plugins = (AFB_plugin **) realloc (plugins, (i+1)*sizeof(AFB_plugin));
-        plugins[i] = (AFB_plugin *) malloc (sizeof(AFB_plugin));
-        plugins[i] = (**pluginRegisterFct)();
-        i++;
+        plugins[num] = (AFB_plugin *) malloc (sizeof(AFB_plugin));
+        plugins[num] = (**pluginRegisterFct)();
+        num++;
+        /* only 20 plugins are supported at that time */
+        if (num == 20) break;
     }
-    plugins[i] = NULL;
+    plugins = (AFB_plugin **) realloc (plugins, (num+1)*sizeof(AFB_plugin));
+    plugins[num] = NULL;
 
     closedir (dir);
 
@@ -614,4 +617,5 @@ void initPlugins(AFB_session *session) {
 
     // complete plugins and save them within current sessions    
     session->plugins = RegisterJsonPlugins(plugins);
+    session->pluginCount = num;
 }