Definitive switch to internal's xreq
authorJosé Bollo <jose.bollo@iot.bzh>
Tue, 4 Apr 2017 08:10:13 +0000 (10:10 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Tue, 4 Apr 2017 08:10:13 +0000 (10:10 +0200)
This switch allows to work on a common base for
the requests.

Change-Id: I94e7fdda80c1966af399309334c41c7c8c8259dc
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
src/CMakeLists.txt
src/afb-api-dbus.c
src/afb-api-so-v1.c
src/afb-api-so-v2.c
src/afb-api-ws.c
src/afb-apis.c
src/afb-apis.h
src/afb-hswitch.c
src/afb-subcall.c
src/afb-svc.c
src/afb-ws-json1.c

index 919e0d5..1f46550 100644 (file)
@@ -73,7 +73,6 @@ ADD_LIBRARY(afb-lib STATIC
        afb-session.c
        afb-svc.c
        afb-subcall.c
-       afb-thread.c
        afb-websock.c
        afb-ws-client.c
        afb-ws-json1.c
index 09af983..dc5273a 100644 (file)
@@ -311,7 +311,7 @@ static int api_dbus_client_on_reply(sd_bus_message *message, void *userdata, sd_
 }
 
 /* on call, propagate it to the dbus service */
-static void api_dbus_client_xcall(void *closure, struct afb_xreq *xreq)
+static void api_dbus_client_call(void *closure, struct afb_xreq *xreq)
 {
        struct api_dbus *api = closure;
        size_t size;
@@ -614,7 +614,7 @@ int afb_api_dbus_add_client(const char *path)
 
        /* record it as an API */
        afb_api.closure = api;
-       afb_api.xcall = api_dbus_client_xcall;
+       afb_api.call = api_dbus_client_call;
        afb_api.service_start = api_dbus_service_start;
        if (afb_apis_add(api->api, afb_api) < 0)
                goto error2;
@@ -963,7 +963,7 @@ static int api_dbus_server_on_object_called(sd_bus_message *message, void *userd
        dreq->xreq.queryitf = &afb_api_dbus_xreq_itf;
        dreq->xreq.api = api->api;
        dreq->xreq.verb = method;
-       afb_apis_xcall(&dreq->xreq);
+       afb_apis_call(&dreq->xreq);
        afb_xreq_unref(&dreq->xreq);
        return 1;
 
index a09b0f5..63ea3f8 100644 (file)
@@ -30,7 +30,6 @@
 #include "afb-common.h"
 #include "afb-context.h"
 #include "afb-api-so.h"
-#include "afb-thread.h"
 #include "afb-xreq.h"
 #include "verbose.h"
 
@@ -127,53 +126,6 @@ static int afb_api_so_rootdir_open_locale(void *closure, const char *filename, i
        return afb_common_rootdir_open_locale(filename, flags, locale);
 }
 
-static int call_check(struct afb_req req, struct afb_context *context, const struct afb_verb_desc_v1 *verb)
-{
-       int stag = (int)verb->session;
-
-       if ((stag & (AFB_SESSION_CREATE|AFB_SESSION_CLOSE|AFB_SESSION_RENEW|AFB_SESSION_CHECK|AFB_SESSION_LOA_EQ)) != 0) {
-               if (!afb_context_check(context)) {
-                       afb_context_close(context);
-                       afb_req_fail(req, "failed", "invalid token's identity");
-                       return 0;
-               }
-       }
-
-       if ((stag & AFB_SESSION_CREATE) != 0) {
-               if (afb_context_check_loa(context, 1)) {
-                       afb_req_fail(req, "failed", "invalid creation state");
-                       return 0;
-               }
-               afb_context_change_loa(context, 1);
-               afb_context_refresh(context);
-       }
-
-       if ((stag & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0)
-               afb_context_refresh(context);
-
-       if ((stag & AFB_SESSION_CLOSE) != 0) {
-               afb_context_change_loa(context, 0);
-               afb_context_close(context);
-       }
-
-       if ((stag & AFB_SESSION_LOA_GE) != 0) {
-               int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
-               if (!afb_context_check_loa(context, loa)) {
-                       afb_req_fail(req, "failed", "invalid LOA");
-                       return 0;
-               }
-       }
-
-       if ((stag & AFB_SESSION_LOA_LE) != 0) {
-               int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
-               if (afb_context_check_loa(context, loa + 1)) {
-                       afb_req_fail(req, "failed", "invalid LOA");
-                       return 0;
-               }
-       }
-       return 1;
-}
-
 static const struct afb_verb_desc_v1 *search(struct api_so_v1 *desc, const char *name)
 {
        const struct afb_verb_desc_v1 *verb;
@@ -184,20 +136,7 @@ static const struct afb_verb_desc_v1 *search(struct api_so_v1 *desc, const char
        return verb->name ? verb : NULL;
 }
 
-static void call_cb(void *closure, struct afb_req req, struct afb_context *context, const char *strverb)
-{
-       const struct afb_verb_desc_v1 *verb;
-       struct api_so_v1 *desc = closure;
-
-       verb = search(desc, strverb);
-       if (!verb)
-               afb_req_fail_f(req, "unknown-verb", "verb %s unknown within api %s", strverb, desc->binding->v1.prefix);
-       else if (call_check(req, context, verb)) {
-               afb_thread_req_call(req, verb->callback, afb_api_so_timeout, desc);
-       }
-}
-
-static void xcall_cb(void *closure, struct afb_xreq *xreq)
+static void call_cb(void *closure, struct afb_xreq *xreq)
 {
        const struct afb_verb_desc_v1 *verb;
        struct api_so_v1 *desc = closure;
@@ -326,7 +265,6 @@ int afb_api_so_v1_add(const char *path, void *handle)
        if (afb_apis_add(desc->binding->v1.prefix, (struct afb_api){
                        .closure = desc,
                        .call = call_cb,
-                       .xcall = xcall_cb,
                        .service_start = service_start_cb }) < 0) {
                ERROR("binding [%s] can't be registered...", path);
                goto error2;
index 2770f64..401aa5c 100644 (file)
@@ -30,7 +30,6 @@
 #include "afb-common.h"
 #include "afb-context.h"
 #include "afb-api-so.h"
-#include "afb-thread.h"
 #include "afb-xreq.h"
 #include "verbose.h"
 
@@ -125,53 +124,6 @@ static int afb_api_so_rootdir_open_locale(void *closure, const char *filename, i
        return afb_common_rootdir_open_locale(filename, flags, locale);
 }
 
-static int call_check(struct afb_req req, struct afb_context *context, const struct afb_verb_v2 *verb)
-{
-       int stag = (int)verb->session;
-
-       if ((stag & (AFB_SESSION_CREATE|AFB_SESSION_CLOSE|AFB_SESSION_RENEW|AFB_SESSION_CHECK|AFB_SESSION_LOA_EQ)) != 0) {
-               if (!afb_context_check(context)) {
-                       afb_context_close(context);
-                       afb_req_fail(req, "failed", "invalid token's identity");
-                       return 0;
-               }
-       }
-
-       if ((stag & AFB_SESSION_CREATE) != 0) {
-               if (afb_context_check_loa(context, 1)) {
-                       afb_req_fail(req, "failed", "invalid creation state");
-                       return 0;
-               }
-               afb_context_change_loa(context, 1);
-               afb_context_refresh(context);
-       }
-
-       if ((stag & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0)
-               afb_context_refresh(context);
-
-       if ((stag & AFB_SESSION_CLOSE) != 0) {
-               afb_context_change_loa(context, 0);
-               afb_context_close(context);
-       }
-
-       if ((stag & AFB_SESSION_LOA_GE) != 0) {
-               int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
-               if (!afb_context_check_loa(context, loa)) {
-                       afb_req_fail(req, "failed", "invalid LOA");
-                       return 0;
-               }
-       }
-
-       if ((stag & AFB_SESSION_LOA_LE) != 0) {
-               int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
-               if (afb_context_check_loa(context, loa + 1)) {
-                       afb_req_fail(req, "failed", "invalid LOA");
-                       return 0;
-               }
-       }
-       return 1;
-}
-
 static const struct afb_verb_v2 *search(struct api_so_v2 *desc, const char *verb)
 {
        const struct afb_verb_v2 *result;
@@ -182,20 +134,7 @@ static const struct afb_verb_v2 *search(struct api_so_v2 *desc, const char *verb
        return result->verb ? result : NULL;
 }
 
-static void call_cb(void *closure, struct afb_req req, struct afb_context *context, const char *name)
-{
-       struct api_so_v2 *desc = closure;
-       const struct afb_verb_v2 *verb;
-
-       verb = search(desc, name);
-       if (!verb)
-               afb_req_fail_f(req, "unknown-verb", "verb %s unknown within api %s", name, desc->binding->api);
-       else if (call_check(req, context, verb)) {
-               afb_thread_req_call(req, verb->callback, afb_api_so_timeout, desc);
-       }
-}
-
-static void xcall_cb(void *closure, struct afb_xreq *xreq)
+static void call_cb(void *closure, struct afb_xreq *xreq)
 {
        struct api_so_v2 *desc = closure;
        const struct afb_verb_v2 *verb;
@@ -317,7 +256,6 @@ int afb_api_so_v2_add(const char *path, void *handle)
        if (afb_apis_add(binding->api, (struct afb_api){
                        .closure = desc,
                        .call = call_cb,
-                       .xcall = xcall_cb,
                        .service_start = service_start_cb }) < 0) {
                ERROR("binding [%s] can't be registered...", path);
                goto error2;
index 8267187..5c3dcd4 100644 (file)
@@ -765,7 +765,7 @@ static void api_ws_client_on_binary(void *closure, char *data, size_t size)
 }
 
 /* on call, propagate it to the ws service */
-static void api_ws_client_xcall_cb(void * closure, struct afb_xreq *xreq)
+static void api_ws_client_call_cb(void * closure, struct afb_xreq *xreq)
 {
        int rc;
        struct api_ws_memo *memo;
@@ -877,7 +877,7 @@ int afb_api_ws_add_client(const char *path)
 
        /* record it as an API */
        afb_api.closure = api;
-       afb_api.xcall = api_ws_client_xcall_cb;
+       afb_api.call = api_ws_client_call_cb;
        afb_api.service_start = api_ws_service_start_cb;
        if (afb_apis_add(api->api, afb_api) < 0)
                goto error3;
@@ -944,7 +944,7 @@ static void api_ws_server_called(struct api_ws_client *client, struct readbuf *r
        wreq->xreq.verb = verb;
        wreq->xreq.query = wreq;
        wreq->xreq.queryitf = &afb_api_ws_xreq_itf;
-       afb_apis_xcall(&wreq->xreq);
+       afb_apis_call(&wreq->xreq);
        afb_xreq_unref(&wreq->xreq);
        return;
 
index cce21e0..de122b6 100644 (file)
@@ -180,31 +180,6 @@ static const struct api_desc *search(const char *api)
        }
 }
 
-/**
- * Dispatch the request 'req' with the 'context' to the
- * method of 'api' and 'verb'.
- * @param req the request to dispatch
- * @param context the context of the request
- * @param api the api of the verb
- * @param verb the verb within the api
- */
-void afb_apis_call(struct afb_req req, struct afb_context *context, const char *api, const char *verb)
-{
-       const struct api_desc *a;
-
-       /* init hooking the request */
-       req = afb_hook_req_call(req, context, api, verb);
-
-       /* search the api */
-       a = search(api);
-       if (!a)
-               afb_req_fail(req, "fail", "api not found");
-       else {
-               context->api_key = a->api.closure;
-               a->api.call(a->api.closure, req, context, verb);
-       }
-}
-
 /**
  * Starts a service by its 'api' name.
  * @param api name of the service to start
@@ -253,7 +228,7 @@ int afb_apis_start_all_services(int share_session)
  * @param api the api of the verb
  * @param verb the verb within the api
  */
-void afb_apis_xcall(struct afb_xreq *xreq)
+void afb_apis_call(struct afb_xreq *xreq)
 {
        const struct api_desc *a;
 
@@ -266,7 +241,7 @@ void afb_apis_xcall(struct afb_xreq *xreq)
                afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
        else {
                xreq->context.api_key = a->api.closure;
-               a->api.xcall(a->api.closure, xreq);
+               a->api.call(a->api.closure, xreq);
        }
 }
 
index f211bda..540b644 100644 (file)
@@ -24,9 +24,8 @@ struct afb_xreq;
 struct afb_api
 {
        void *closure;
-       void (*call)(void *closure, struct afb_req req, struct afb_context *context, const char *verb);
+       void (*call)(void *closure, struct afb_xreq *xreq);
        int (*service_start)(void *closure, int share_session, int onneed);
-       void (*xcall)(void *closure, struct afb_xreq *xreq);
 };
 
 
@@ -37,7 +36,6 @@ extern int afb_apis_add(const char *name, struct afb_api api);
 extern int afb_apis_start_all_services(int share_session);
 extern int afb_apis_start_service(const char *name, int share_session, int onneed);
 
-extern void afb_apis_call(struct afb_req req, struct afb_context *context, const char *api, const char *verb);
-extern void afb_apis_xcall(struct afb_xreq *xreq);
+extern void afb_apis_call(struct afb_xreq *xreq);
 
 
index eab5b9b..0eae18f 100644 (file)
@@ -47,7 +47,7 @@ int afb_hswitch_apis(struct afb_hreq *hreq, void *data)
        if (afb_hreq_init_req_call(hreq, api, lenapi, verb, lenverb) < 0)
                afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
        else
-               afb_apis_xcall(&hreq->xreq);
+               afb_apis_call(&hreq->xreq);
        return 1;
 }
 
index 82128d8..81361a0 100644 (file)
@@ -134,7 +134,7 @@ void afb_subcall(
        }
 
        afb_xreq_addref(caller);
-       afb_apis_xcall(&subcall->xreq);
+       afb_apis_call(&subcall->xreq);
        afb_xreq_unref(&subcall->xreq);
 }
 
index 58e2b6d..93e8553 100644 (file)
@@ -246,7 +246,7 @@ static void svc_call(void *closure, const char *api, const char *verb, struct js
        svcreq->svc = svc;
 
        /* terminates and frees ressources if needed */
-       afb_apis_xcall(&svcreq->xreq);
+       afb_apis_call(&svcreq->xreq);
        afb_xreq_unref(&svcreq->xreq);
 }
 
index fdd3bae..29078e2 100644 (file)
@@ -198,7 +198,7 @@ static void aws_on_call(struct afb_ws_json1 *ws, const char *api, const char *ve
        wsreq->xreq.listener = wsreq->aws->listener;
 
        /* emits the call */
-       afb_apis_xcall(&wsreq->xreq);
+       afb_apis_call(&wsreq->xreq);
        afb_xreq_unref(&wsreq->xreq);
 }