Merge origin/master
authorFulup Ar Foll <fulup@iot.bzh>
Wed, 17 Feb 2016 13:41:07 +0000 (14:41 +0100)
committerFulup Ar Foll <fulup@iot.bzh>
Wed, 17 Feb 2016 13:41:07 +0000 (14:41 +0100)
.gitignore
include/local-def.h
plugins/afm-main-plugin/afm-main-plugin.c
plugins/afm-main-plugin/utils-jbus.c
plugins/audio/audio-alsa.c
plugins/audio/audio-api.c
plugins/audio/audio-pulse.c
src/config.c
src/main.c
src/rest-api.c
src/session.c

index 6f88c4e..d15108f 100644 (file)
@@ -8,3 +8,6 @@ dist/*
 CMakeFiles/
 CMakeCache.txt
 nbproject/private/*
+Makefile
+cmake_install.cmake
+*.so
index 6d21940..01f1062 100644 (file)
@@ -1,5 +1,5 @@
 /*
-   alsajson-gw -- provide a REST/HTTP interface to ALSA-Mixer
+   local-def.h -- provide a REST/HTTP interface
 
    Copyright (C) 2015, Fulup Ar Foll
 
@@ -88,7 +88,7 @@ typedef enum  {AFB_PLUGIN_JSON=123456789, AFB_PLUGIN_JSCRIPT=987654321,  AFB_PLU
 typedef enum  { AFB_FALSE, AFB_TRUE, AFB_FATAL, AFB_FAIL, AFB_WARNING, AFB_EMPTY, AFB_SUCCESS, AFB_DONE, AFB_UNAUTH} AFB_error;
 
 extern char *ERROR_LABEL[];
-#define ERROR_LABEL_DEF {"false", "true","fatal", "fail", "warning", "empty", "success"}
+#define ERROR_LABEL_DEF {"false", "true", "fatal", "fail", "warning", "empty", "success"}
 
 #define BANNER "<html><head><title>Application Framework Binder</title></head><body>Application Framework </body></html>"
 #define JSON_CONTENT  "application/json"
@@ -108,6 +108,7 @@ typedef struct {
 } AFB_errorT;
 
 typedef enum  {AFB_POST_NONE=0, AFB_POST_JSON, AFB_POST_FORM, AFB_POST_EMPTY} AFB_PostType;
+typedef enum  {AFB_MODE_LOCAL=0, AFB_MODE_REMOTE, AFB_MODE_GLOBAL} AFB_Mode;
 
 // Post Upload File Handle
 typedef struct {
@@ -130,7 +131,7 @@ typedef struct {
   int    uid;               // post uid for debug
   AFB_PostType type;        // JSON or FORM
   AFB_apiCB  completeCB;    // callback when post is completed
-  char   *private;          // use internally to keep track or partial buffer
+  char   *privatebuf;       // use internally to keep track or partial buffer
   struct MHD_PostProcessor *pp; // iterator handle
 } AFB_PostHandle;
 
@@ -170,7 +171,6 @@ typedef struct {
   char *console;           // console device name (can be a file or a tty)
   int  localhostOnly;
   int   httpdPort;
-  char *smack;             // smack label
   char *ldpaths;           // list of plugins directories
   char *rootdir;           // base dir for httpd file download
   char *rootbase;          // Angular HTML5 base URL
@@ -184,6 +184,7 @@ typedef struct {
   int  apiTimeout;
   int  cntxTimeout;        // Client Session Context timeout
   int  pluginCount;        // loaded plugins count
+  AFB_Mode mode;           // mode of listening
   AFB_aliasdir *aliasdir;  // alias mapping for icons,apps,...
 } AFB_config;
 
@@ -207,7 +208,7 @@ typedef struct {
   AFB_sessionE session;
   AFB_apiCB callback;
   char *info;
-  AFB_privateApi *private;
+  AFB_privateApi *privateapi;
 } AFB_restapi;
 
 // Plugin definition
@@ -264,6 +265,7 @@ typedef struct {
   void *httpd;            // anonymous structure for httpd handler
   int  fakemod;           // respond to GET/POST request without interacting with sndboard
   int  forceexit;         // when autoconfig from script force exit before starting server
+  int  readyfd;           // a #fd to signal when ready to serve
   AFB_plugin **plugins;   // pointer to REST/API plugins 
   magic_t  magic;         // Mime type file magic lib
   sigjmp_buf restartCkpt; // context save for restart set/longjmp
index 5315da9..1fcc554 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#define _GNU_SOURCE         /* See feature_test_macros(7) */
+#include <stdio.h>
 
 #include "local-def.h"
 
 #include "utils-jbus.h"
 
-static const char _id_[]        = "id";
-static const char _runid_[]     = "runid";
-static char _runnables_[] = "runnables";
-static char _detail_[]    = "detail";
-static char _start_[]     = "start";
-static char _terminate_[] = "terminate";
-static char _stop_[]      = "stop";
-static char _continue_[]  = "continue";
-static char _runners_[]   = "runners";
-static char _state_[]     = "state";
-static char _install_[]   = "install";
-static char _uninstall_[] = "uninstall";
+static const char _id_[]    = "id";
+static const char _runid_[] = "runid";
+static char _runnables_[]   = "runnables";
+static char _detail_[]      = "detail";
+static char _start_[]       = "start";
+static char _terminate_[]   = "terminate";
+static char _stop_[]        = "stop";
+static char _continue_[]    = "continue";
+static char _runners_[]     = "runners";
+static char _state_[]       = "state";
+static char _install_[]     = "install";
+static char _uninstall_[]   = "uninstall";
+static const char _mode_[]  = "mode";
+static const char _local_[] = "local";
+static const char _remote_[]= "remote";
+static const char _auto_[]  = "auto";
+static const char _uri_[]   = "uri";
 
 static struct jbus *jbus;
 
@@ -70,6 +77,8 @@ static struct json_object *call(AFB_request *request, AFB_PostItem *item, const
 static struct json_object *call_void(AFB_request *request, AFB_PostItem *item)
 {
        struct json_object *obj = jbus_call_sj_sync(jbus, request->api, "true");
+       if (verbose)
+               fprintf(stderr, "(afm-main-plugin) call_void: true -> %s\n", obj ? json_object_to_json_string(obj) : "NULL");
        request->errcode = obj ? MHD_HTTP_OK : MHD_HTTP_FAILED_DEPENDENCY;
        return obj;
 }
@@ -88,6 +97,8 @@ static struct json_object *call_appid(AFB_request *request, AFB_PostItem *item)
                return NULL;
        }
        obj = jbus_call_sj_sync(jbus, request->api, sid);
+       if (verbose)
+               fprintf(stderr, "(afm-main-plugin) call_appid: %s -> %s\n", sid, obj ? json_object_to_json_string(obj) : "NULL");
        free(sid);
        request->errcode = obj ? MHD_HTTP_OK : MHD_HTTP_FAILED_DEPENDENCY;
        return obj;
@@ -102,6 +113,8 @@ static struct json_object *call_runid(AFB_request *request, AFB_PostItem *item)
                return NULL;
        }
        obj = jbus_call_sj_sync(jbus, request->api, id);
+       if (verbose)
+               fprintf(stderr, "(afm-main-plugin) call_runid: %s -> %s\n", id, obj ? json_object_to_json_string(obj) : "NULL");
        request->errcode = obj ? MHD_HTTP_OK : MHD_HTTP_FAILED_DEPENDENCY;
        return obj;
 }
@@ -111,9 +124,43 @@ static struct json_object *call_void__runnables(AFB_request *request, AFB_PostIt
        return embed(request, _runnables_, call_void(request, item));
 }
 
-static struct json_object *call_appid__runid(AFB_request *request, AFB_PostItem *item)
+static struct json_object *call_start(AFB_request *request, AFB_PostItem *item)
 {
-       return embed(request, _runid_, call_appid(request, item));
+       struct json_object *resp;
+       const char *id, *mode;
+       char *query;
+       int rc;
+
+       /* get the id */
+       id = getQueryValue(request, _id_);
+       if (id == NULL) {
+               request->errcode = MHD_HTTP_BAD_REQUEST;
+               return NULL;
+       }
+       /* get the mode */
+       mode = getQueryValue(request, _mode_);
+       if (mode == NULL || !strcmp(mode, _auto_)) {
+               mode = request->config->mode == AFB_MODE_REMOTE ? _remote_ : _local_;
+       }
+
+       /* create the query */
+       rc = asprintf(&query, "{\"id\":\"%s\",\"mode\":\"%s\"}", id, mode);
+       if (rc < 0) {
+               request->errcode = MHD_HTTP_INTERNAL_SERVER_ERROR;
+               return NULL;
+       }
+
+       /* calls the service */
+       resp = jbus_call_sj_sync(jbus, _start_, query);
+       if (verbose)
+               fprintf(stderr, "(afm-main-plugin) call_start: %s -> %s\n", query, resp ? json_object_to_json_string(resp) : "NULL");
+       free(query);
+
+       /* embed if needed */
+       if (json_object_get_type(resp) == json_type_int)
+               resp = embed(request, _runid_, resp);
+       request->errcode = resp ? MHD_HTTP_OK : MHD_HTTP_FAILED_DEPENDENCY;
+       return resp;
 }
 
 static struct json_object *call_void__runners(AFB_request *request, AFB_PostItem *item)
@@ -124,24 +171,25 @@ static struct json_object *call_void__runners(AFB_request *request, AFB_PostItem
 static struct json_object *call_file__appid(AFB_request *request, AFB_PostItem *item)
 {
        if (item == NULL) {
-               struct json_object *obj;
-               char *query;
                const char *filename = getPostPath(request);
-                
-                if (filename != NULL) {
-                    request->jresp = NULL;
-                    if (0 >= asprintf(&query, "\"%s\"", filename))
-                            request->errcode = MHD_HTTP_INTERNAL_SERVER_ERROR;
-                    else {
-                            obj = jbus_call_sj_sync(jbus, request->api, query);
-                            free(query);
-                            if (obj)
-                                    request->jresp = embed(request, _id_, obj);
-                            else
-                                    request->errcode = MHD_HTTP_FAILED_DEPENDENCY;
-                    }
-                    unlink(filename);
-                }
+               if (filename != NULL) {
+                       struct json_object *obj;
+                       char *query;
+                       request->jresp = NULL;
+                       if (0 >= asprintf(&query, "\"%s\"", filename))
+                               request->errcode = MHD_HTTP_INTERNAL_SERVER_ERROR;
+                       else {
+                               obj = jbus_call_sj_sync(jbus, request->api, query);
+                               if (verbose)
+                                       fprintf(stderr, "(afm-main-plugin) call_file_appid: %s -> %s\n", query, obj ? json_object_to_json_string(obj) : "NULL");
+                               free(query);
+                               if (obj)
+                                       request->jresp = embed(request, _id_, obj);
+                               else
+                                       request->errcode = MHD_HTTP_FAILED_DEPENDENCY;
+                       }
+                       unlink(filename);
+               }
        }
        return getPostFile (request, item, "/tmp/upload");
 }
@@ -150,7 +198,7 @@ static AFB_restapi plug_apis[] =
 {
        {_runnables_, AFB_SESSION_CHECK, (AFB_apiCB)call_void__runnables,  "Get list of runnable applications"},
        {_detail_   , AFB_SESSION_CHECK, (AFB_apiCB)call_appid, "Get the details for one application"},
-       {_start_    , AFB_SESSION_CHECK, (AFB_apiCB)call_appid__runid, "Start an application"},
+       {_start_    , AFB_SESSION_CHECK, (AFB_apiCB)call_start, "Start an application"},
        {_terminate_, AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Terminate a running application"},
        {_stop_     , AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Stop (pause) a running application"},
        {_continue_ , AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Continue (resume) a stopped application"},
@@ -171,6 +219,9 @@ static AFB_plugin plug_desc = {
 AFB_plugin *pluginRegister()
 {
        jbus = create_jbus(1, "/org/AGL/afm/user");
-       return jbus ? &plug_desc : NULL;
+        if (jbus)
+               return &plug_desc;
+       fprintf(stderr, "ERROR: %s:%d: can't connect to DBUS session\n", __FILE__, __LINE__);
+       return NULL;
 }
 
index 1f85a4c..67e3ce4 100644 (file)
@@ -669,6 +669,7 @@ int jbus_dispatch_multiple(struct jbus **jbuses, int njbuses, int maxcount)
 
        for (i = r = 0 ; i < njbuses && r < maxcount ; i++) {
                dbus_connection_read_write(jbuses[i]->connection, 0);
+               sts = dbus_connection_get_dispatch_status(jbuses[i]->connection);
                while(sts == DBUS_DISPATCH_DATA_REMAINS &&  r < maxcount) {
                        sts = dbus_connection_dispatch(jbuses[i]->connection);
                        r++;
@@ -690,10 +691,17 @@ int jbus_read_write_dispatch_multiple(struct jbus **jbuses, int njbuses, int tom
        assert(fds != NULL);
 
        r = jbus_dispatch_multiple(jbuses, njbuses, maxcount);
+       if (r)
+               return r;
        n = jbus_fill_pollfds(jbuses, njbuses, fds);
-       s = poll(fds, n, toms);
-       if (s < 0)
-               return r ? r : s;
+       for(;;) {
+               s = poll(fds, n, toms);
+               if (s >= 0)
+                       break;
+               if (errno != EINTR)
+                       return r ? r : s;
+               toms = 0;
+       }
        n = jbus_dispatch_pollfds(jbuses, njbuses, fds, maxcount - r);
        return n >= 0 ? r + n : r ? r : n;
 }
index 7310ea8..bc777fc 100644 (file)
@@ -130,6 +130,8 @@ PUBLIC unsigned char _alsa_init (const char *name, audioCtxHandleT *ctx) {
     ctx->mute = _alsa_get_mute (num);
     ctx->idx = num;
 
+    if (verbose) fprintf (stderr, "Successfully initialized ALSA backend.\n");
+
     return 0;
 }
 
index 4a8d0da..637f9d0 100644 (file)
 
 void _backend_init (const char *name, audioCtxHandleT *ctx) {
 
+    char *backend_env = getenv ("AFB_AUDIO_OUTPUT");
+    unsigned char res = -1;
+
 # ifdef HAVE_PULSE
-    if (_pulse_init (name, ctx) < 0)
-# endif
-    _alsa_init (name, ctx);
+    if (!backend_env || (strcasecmp (backend_env, "Alsa") != 0))
+        res = _pulse_init (name, ctx);
+    if (res < 0)
+#endif
+    res = _alsa_init (name, ctx);
+
+    if (res < 0 && verbose)
+        fprintf (stderr, "Could not initialize Audio backend\n");
 }
 
 void _backend_free (audioCtxHandleT *ctx) {
index 4caf3db..ceffcf7 100644 (file)
@@ -94,6 +94,8 @@ PUBLIC unsigned char _pulse_init (const char *name, audioCtxHandleT *ctx) {
 
     client_count++;
 
+    if (verbose) fprintf (stderr, "Successfully initialized PulseAudio backend.\n");
+
     return 0;
 }
 
index f818f7a..a64e131 100644 (file)
@@ -61,7 +61,8 @@ PUBLIC AFB_error configLoadFile (AFB_session * session, AFB_config *cliconfig) {
    
    // TBD integrate alias-dir array with config-file
    session->config->aliasdir = cliconfig->aliasdir;
-   
+   session->config->mode = cliconfig->mode;
+
    // default HTTP port
    if (cliconfig->httpdPort == 0) session->config->httpdPort=1234;
    else session->config->httpdPort=cliconfig->httpdPort;
@@ -108,12 +109,6 @@ PUBLIC AFB_error configLoadFile (AFB_session * session, AFB_config *cliconfig) {
        session->config->rootapi= cliconfig->rootapi;
    }
 
-   if  (cliconfig->smack == NULL) {
-       session->config->smack = "demo";
-   } else {
-       session->config->smack= cliconfig->smack;
-   }
-
    if  (cliconfig->ldpaths == NULL) {
        session->config->ldpaths = PLUGIN_INSTALL_DIR;
    } else {
@@ -186,10 +181,6 @@ PUBLIC AFB_error configLoadFile (AFB_session * session, AFB_config *cliconfig) {
       session->config->rootapi =  strdup (json_object_get_string (value));
    }
    
-   if (!cliconfig->smack && json_object_object_get_ex (AFBConfig, "smack", &value)) {
-      session->config->smack =  strdup (json_object_get_string (value));
-   }
-   
    if (!cliconfig->ldpaths && json_object_object_get_ex (AFBConfig, "plugins", &value)) {
       session->config->ldpaths =  strdup (json_object_get_string (value));
    }
@@ -255,7 +246,6 @@ PUBLIC void configStoreFile (AFB_session * session) {
    json_object_object_add (AFBConfig, "rootdir"       , json_object_new_string (session->config->rootdir));
    json_object_object_add (AFBConfig, "rootapi"       , json_object_new_string (session->config->rootapi));
    json_object_object_add (AFBConfig, "rootbase"      , json_object_new_string (session->config->rootbase));
-   json_object_object_add (AFBConfig, "smack"         , json_object_new_string (session->config->smack));
    json_object_object_add (AFBConfig, "plugins"       , json_object_new_string (session->config->ldpaths));
    json_object_object_add (AFBConfig, "sessiondir"    , json_object_new_string (session->config->sessiondir));
    json_object_object_add (AFBConfig, "pidfile"       , json_object_new_string (session->config->pidfile));
index b17435e..78d7f79 100644 (file)
@@ -50,34 +50,37 @@ static sigjmp_buf exitPoint; // context save for set/longjmp
 
 
 // Define command line option
- #define SET_VERBOSE        101
- #define SET_BACKGROUND     105
- #define SET_FORGROUND      106
- #define KILL_PREV_EXIT     107
- #define KILL_PREV_REST     108
- #define SET_FAKE_MOD       109
-
- #define SET_TCP_PORT       120
- #define SET_ROOT_DIR       121
- #define SET_ROOT_BASE      122
- #define SET_ROOT_API       123
- #define SET_ROOT_ALIAS     124
-
- #define SET_CACHE_TO       130
- #define SET_USERID         131
- #define SET_PID_FILE       132
- #define SET_SESSION_DIR    133
- #define SET_CONFIG_FILE    134
- #define SET_CONFIG_SAVE    135
- #define SET_CONFIG_EXIT    138
-
- #define SET_AUTH_TOKEN     141
- #define SET_LDPATH        142
- #define SET_APITIMEOUT     143
- #define SET_CNTXTIMEOUT    144
-
- #define DISPLAY_VERSION    150
- #define DISPLAY_HELP       151
+#define SET_VERBOSE        101
+#define SET_BACKGROUND     105
+#define SET_FORGROUND      106
+#define KILL_PREV_EXIT     107
+#define KILL_PREV_REST     108
+#define SET_FAKE_MOD       109
+
+#define SET_TCP_PORT       120
+#define SET_ROOT_DIR       121
+#define SET_ROOT_BASE      122
+#define SET_ROOT_API       123
+#define SET_ROOT_ALIAS     124
+
+#define SET_CACHE_TO       130
+#define SET_USERID         131
+#define SET_PID_FILE       132
+#define SET_SESSION_DIR    133
+#define SET_CONFIG_FILE    134
+#define SET_CONFIG_SAVE    135
+#define SET_CONFIG_EXIT    138
+
+#define SET_AUTH_TOKEN     141
+#define SET_LDPATH         142
+#define SET_APITIMEOUT     143
+#define SET_CNTXTIMEOUT    144
+
+#define DISPLAY_VERSION    150
+#define DISPLAY_HELP       151
+
+#define SET_MODE           160
+#define SET_READYFD        161
 
 
 // Supported option
@@ -111,6 +114,9 @@ static  AFB_options cliOptions [] = {
   
   {DISPLAY_VERSION  ,0,"version"         , "Display version and copyright"},
   {DISPLAY_HELP     ,0,"help"            , "Display this help"},
+
+  {SET_MODE         ,1,"mode"            , "set the mode: either local, remote or global"},
+  {SET_READYFD      ,1,"readyfd"         , "set the #fd to signal when ready"},
   {0, 0, 0}
  };
 
@@ -238,6 +244,12 @@ static void listenLoop (AFB_session *session) {
         err = httpdStart (session);
         if (err != AFB_SUCCESS) return;
 
+       if (session->readyfd != 0) {
+               static const char readystr[] = "READY=1";
+               write(session->readyfd, readystr, sizeof(readystr) - 1);
+               close(session->readyfd);
+       }
+
         // infinite loop
         httpdLoop(session);
 
@@ -413,6 +425,19 @@ int main(int argc, char *argv[])  {
        session->killPrevious  = 2;
        break;
 
+    case SET_MODE:
+       if (optarg == 0) goto needValueForOption;
+       if (!strcmp(optarg, "local")) cliconfig.mode = AFB_MODE_LOCAL;
+       else if (!strcmp(optarg, "remote")) cliconfig.mode = AFB_MODE_REMOTE;
+       else if (!strcmp(optarg, "global")) cliconfig.mode = AFB_MODE_GLOBAL;
+       else goto badMode;
+       break;
+
+    case SET_READYFD:
+       if (optarg == 0) goto needValueForOption;
+       if (!sscanf (optarg, "%u", &session->readyfd)) goto notAnInteger;
+       break;
+
     case DISPLAY_VERSION:
        if (optarg != 0) goto noValueForOption;
        printVersion();
@@ -440,7 +465,7 @@ int main(int argc, char *argv[])  {
   if  ((session->background == 0) && (session->foreground == 0)) session->foreground=1;
 
   // open syslog if ever needed
-  openlog("AGB-log", 0, LOG_DAEMON);
+  openlog("AFB-log", 0, LOG_DAEMON);
 
   // -------------- Try to kill any previous process if asked ---------------------
   if (session->killPrevious) {
@@ -615,6 +640,11 @@ notAnInteger:
           ,gnuOptions[optionIndex].name, gnuOptions[optionIndex].name);
   exit (-1);
 
+badMode:
+  fprintf (stderr,"\nERR:AFB-daemon option [--%s] only accepts local, global or remote.\n\n"
+          ,gnuOptions[optionIndex].name);
+  exit (-1);
+
 exitOnSignal:
   fprintf (stderr,"\n%s INF:AFB-daemon pid=%d received exit signal (Hopefully crtl-C or --kill-previous !!!)\n\n"
                  ,configTime(), getpid());
index da937c4..0751ede 100644 (file)
@@ -45,7 +45,7 @@ PUBLIC void endPostRequest(AFB_PostHandle *postHandle) {
     if (postHandle->type == AFB_POST_FORM) {
          if (verbose) fprintf(stderr, "End PostForm Request UID=%d\n", postHandle->uid);
     }
-    if (postHandle->private) free(postHandle->private);
+    if (postHandle->privatebuf) free(postHandle->privatebuf);
     free(postHandle);
 }
 
@@ -281,10 +281,11 @@ STATIC int doPostIterate (void *cls, enum MHD_ValueKind kind, const char *key,
     
   // retrieve API request from Post iterator handle  
   AFB_PostHandle *postHandle  = (AFB_PostHandle*)cls;
-  AFB_request *request = (AFB_request*)postHandle->private;
+  AFB_request *request = (AFB_request*)postHandle->privatebuf;
   AFB_PostRequest postRequest;
   
-  fprintf (stderr, "postHandle key=%s filename=%s len=%d mime=%s\n", key, filename, size, mimetype);
+  if (verbose)
+    fprintf (stderr, "postHandle key=%s filename=%s len=%d mime=%s\n", key, filename, size, mimetype);
    
   // Create and Item value for Plugin API
   item.kind     = kind;
@@ -405,7 +406,7 @@ PUBLIC int doRestApi(struct MHD_Connection *connection, AFB_session *session, co
                 request = createRequest (connection, session, url);
                 if (request->jresp != NULL) goto ProcessApiCall;
                 postHandle->type   = AFB_POST_FORM;
-                postHandle->private= (void*)request;
+                postHandle->privatebuf = (void*)request;
                 postHandle->pp     = MHD_create_post_processor (connection, MAX_POST_SIZE, &doPostIterate, postHandle);
                 
                 if (NULL == postHandle->pp) {
@@ -430,7 +431,7 @@ PUBLIC int doRestApi(struct MHD_Connection *connection, AFB_session *session, co
 
                 // Size is OK, let's allocate a buffer to hold post data
                 postHandle->type = AFB_POST_JSON;
-                postHandle->private = malloc(contentlen + 1); // allocate memory for full POST data + 1 for '\0' enf of string
+                postHandle->privatebuf = malloc(contentlen + 1); // allocate memory for full POST data + 1 for '\0' enf of string
 
                 // if (verbose) fprintf(stderr, "Create PostJson[uid=%d] Size=%d\n", postHandle->uid, contentlen);
                 return MHD_YES;
@@ -455,7 +456,7 @@ PUBLIC int doRestApi(struct MHD_Connection *connection, AFB_session *session, co
             // Process JsonPost request when buffer is completed let's call API    
             if (postHandle->type == AFB_POST_JSON) {
                 // if (verbose) fprintf(stderr, "Updating PostJson[uid=%d]\n", postHandle->uid);
-                memcpy(&postHandle->private[postHandle->len], upload_data, *upload_data_size);
+                memcpy(&postHandle->privatebuf[postHandle->len], upload_data, *upload_data_size);
                 postHandle->len = postHandle->len + *upload_data_size;
             }
             
@@ -491,8 +492,8 @@ PUBLIC int doRestApi(struct MHD_Connection *connection, AFB_session *session, co
                 }
 
                 // Before processing data, make sure buffer string is properly ended
-                postHandle->private[postHandle->len] = '\0';
-                postRequest.data = postHandle->private;
+                postHandle->privatebuf[postHandle->len] = '\0';
+                postRequest.data = postHandle->privatebuf;
                 request->post = &postRequest;
 
                 // if (verbose) fprintf(stderr, "Close Post[%d] Buffer=%s\n", postHandle->uid, request->post->data);
@@ -563,15 +564,15 @@ STATIC AFB_plugin ** RegisterJsonPlugins(AFB_plugin **plugins) {
               
             // Prebuild each API jtype to boost API json response
             for (jdx = 0; plugins[idx]->apis[jdx].name != NULL; jdx++) {
-                AFB_privateApi *private = malloc (sizeof (AFB_privateApi));
-                if (plugins[idx]->apis[jdx].private != NULL) {
+                AFB_privateApi *privateapi = malloc (sizeof (AFB_privateApi));
+                if (plugins[idx]->apis[jdx].privateapi != NULL) {
                     fprintf (stderr, "WARNING: plugin=%s api=%s private handle should be NULL=0x%x\n"
-                            ,plugins[idx]->prefix,plugins[idx]->apis[jdx].name, plugins[idx]->apis[jdx].private);
+                            ,plugins[idx]->prefix,plugins[idx]->apis[jdx].name, plugins[idx]->apis[jdx].privateapi);
                 }
-                private->len = strlen (plugins[idx]->apis[jdx].name);
-                private->jtype=json_object_new_string(plugins[idx]->apis[jdx].name);
-                json_object_get(private->jtype); // increase reference count to make it permanent
-                plugins[idx]->apis[jdx].private = private;
+                privateapi->len = strlen (plugins[idx]->apis[jdx].name);
+                privateapi->jtype=json_object_new_string(plugins[idx]->apis[jdx].name);
+                json_object_get(privateapi->jtype); // increase reference count to make it permanent
+                plugins[idx]->apis[jdx].privateapi = privateapi;
             }
         }
     }
@@ -637,17 +638,18 @@ STATIC void scanDirectory(char *dirpath, int dirfd, AFB_plugin **plugins, int *c
             }
 
             if (verbose) fprintf(stderr, "[%s] is a valid AFB plugin, loading pos[%d]\n", pluginDir.d_name, *count);
-            plugins[*count] = (AFB_plugin *) malloc (sizeof(AFB_plugin));
-            plugins[*count] = (**pluginRegisterFct)();
-            *count = *count +1;
-
+            plugins[*count] = pluginRegisterFct();
+            if (!plugins[*count]) {
+                if (verbose) fprintf(stderr, "ERROR: plugin [%s] register function failed. continuing...\n", pluginDir.d_name);
+            } else
+                *count = *count +1;
         }
     }
     closedir (dir);
 }
 
 void initPlugins(AFB_session *session) {
-    static AFB_plugin **plugins;
+    AFB_plugin **plugins;
     
     afbJsonType = json_object_new_string (AFB_MSG_JTYPE);
     int count = 0;
@@ -655,7 +657,7 @@ void initPlugins(AFB_session *session) {
     int dirfd;
 
     /* pre-allocate for AFB_MAX_PLUGINS plugins, we will downsize later */
-    plugins = (AFB_plugin **) malloc (AFB_MAX_PLUGINS *sizeof(AFB_plugin));
+    plugins = (AFB_plugin **) malloc (AFB_MAX_PLUGINS *sizeof(AFB_plugin*));
     
     // Loop on every directory passed in --plugins=xxx
     while (dirpath = strsep(&session->config->ldpaths, ":")) {
@@ -674,7 +676,7 @@ void initPlugins(AFB_session *session) {
     }
     
     // downsize structure to effective number of loaded plugins
-    plugins = (AFB_plugin **)realloc (plugins, (count+1)*sizeof(AFB_plugin));
+    plugins = (AFB_plugin **)realloc (plugins, (count+1)*sizeof(AFB_plugin*));
     plugins[count] = NULL;
 
     // complete plugins and save them within current sessions    
index 167c4e6..9e25c27 100644 (file)
@@ -583,6 +583,10 @@ PUBLIC AFB_error ctxTokenRefresh (AFB_clientCtx *clientCtx, AFB_request *request
     // Old token was valid let's regenerate a new one    
     uuid_generate(newuuid);         // create a new UUID
     uuid_unparse_lower(newuuid, clientCtx->token);
+    
+    // keep track of time for session timeout and further clean up
+    clientCtx->timeStamp=time(NULL);
+    
     return (AFB_SUCCESS);    
     
 }