work in progress
[src/app-framework-binder.git] / src / afb-apis.c
index 3dcad65..effef8a 100644 (file)
 #include <limits.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <signal.h>
+#include <time.h>
+#include <sys/syscall.h>
+#include <setjmp.h>
 
 #include "../include/local-def.h"
 
+#include "afb-req-itf.h"
 #include "afb-apis.h"
 
 struct api_desc {
@@ -278,3 +283,108 @@ int afb_apis_add_pathset(const char *pathset)
        };
 }
 
+// Check of apiurl is declare in this plugin and call it
+extern __thread sigjmp_buf *error_handler;
+static int callPluginApi(AFB_request * request)
+{
+       volatile int status, timerset;
+       timer_t timerid;
+       sigjmp_buf jmpbuf, *older;
+       struct sigevent sevp;
+       struct itimerspec its;
+
+       // save context before calling the API
+       timerset = 0;
+       older = error_handler;
+       status = setjmp(jmpbuf);
+       if (status != 0) {
+               status = 0;
+       }
+       else {
+               error_handler = &jmpbuf;
+               if (request->config->apiTimeout > 0) {
+                       timerset = 1; /* TODO: check statuses */
+                       sevp.sigev_notify = SIGEV_THREAD_ID;
+                       sevp.sigev_signo = SIGALRM;
+#if defined(sigev_notify_thread_id)
+                       sevp.sigev_notify_thread_id = syscall(SYS_gettid);
+#else
+                       sevp._sigev_un._tid = syscall(SYS_gettid);
+#endif
+                       timer_create(CLOCK_THREAD_CPUTIME_ID, &sevp, &timerid);
+                       its.it_interval.tv_sec = 0;
+                       its.it_interval.tv_nsec = 0;
+                       its.it_value.tv_sec = 15;
+                       its.it_value.tv_nsec = 0;
+                       timer_settime(timerid, 0, &its, NULL);
+               }
+
+               //doCallPluginApi(request, apiidx, verbidx, context);
+               status = 1;
+       }
+       if (timerset)
+               timer_delete(timerid);
+       error_handler = older;
+
+       return status;
+}
+
+static void handle(struct afb_req req, const struct api_desc *api, const struct AFB_restapi *verb)
+{
+       json_object *jresp, *jcall, *jreqt;
+
+       AFB_request request;
+
+       request.uuid = request.url = "fake";
+       request.prefix = api->prefix;
+       request.method = verb->name;
+       request.context = NULL;
+       request.restfull = 0;
+       request.errcode = 0;
+       request.config = NULL;
+       request.areq = &req;
+
+       switch(verb->session) {
+       case AFB_SESSION_CREATE:
+       case AFB_SESSION_RENEW:
+               /*if (check) new*/
+               break;
+       case AFB_SESSION_CLOSE:
+       case AFB_SESSION_CHECK:
+               /*check*/
+               break;
+       case AFB_SESSION_NONE:
+       default:
+               break;
+       }
+       verb->callback(&request, NULL);
+
+       if (verb->session == AFB_SESSION_CLOSE)
+               /*close*/;
+}
+
+int afb_apis_handle(struct afb_req req, const char *api, size_t lenapi, const char *verb, size_t lenverb)
+{
+       int i, j;
+       const struct api_desc *a;
+       const struct AFB_restapi *v;
+
+//fprintf(stderr,"afb_apis_handle prefix:%.*s verb:%.*s\n",(int)lenapi,api,(int)lenverb,verb);
+       a = apis_array;
+       for (i = 0 ; i < apis_count ; i++, a++) {
+               if (a->prefixlen == lenapi && !strncasecmp(a->prefix, api, lenapi)) {
+//fprintf(stderr,"afb_apis_handle found prefix:%.*s -> %s\n",(int)lenapi,api,a->prefix);
+                       v = a->plugin->apis;
+                       for (j = 0 ; v->name ; j++, v++) {
+                               if (!strncasecmp(v->name, verb, lenverb) && !v->name[lenverb]) {
+//fprintf(stderr,"afb_apis_handle found prefix:%.*s verb:%.*s -> %s/%s\n",(int)lenapi,api,(int)lenverb,verb,a->prefix,v->name);
+                                       handle(req, a, v);
+                                       return 1;
+                               }
+                       }
+                       break;
+               }
+       }
+       return 0;
+}
+