afb-apiset: refactor access to apis
authorJosé Bollo <jose.bollo@iot.bzh>
Thu, 31 Aug 2017 16:47:50 +0000 (18:47 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Thu, 31 Aug 2017 16:47:50 +0000 (18:47 +0200)
Change-Id: If003067ada5802b7d77f06f560a5d07464909a61
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
src/afb-api-ws.c
src/afb-apiset.c
src/afb-apiset.h
src/afb-ditf.c
src/afb-monitor.c
src/afb-xreq.c

index dffe126..1bd1071 100644 (file)
@@ -318,7 +318,6 @@ int afb_api_ws_add_server(const char *path, struct afb_apiset *apiset)
 {
        int rc;
        struct api_ws *apiws;
-       struct afb_api api;
 
        /* creates the ws api object */
        apiws = api_ws_make(path);
@@ -326,7 +325,7 @@ int afb_api_ws_add_server(const char *path, struct afb_apiset *apiset)
                goto error;
 
        /* check api name */
-       if (afb_apiset_get(apiset, apiws->api, &api)) {
+       if (afb_apiset_has(apiset, apiws->api, 1)) {
                ERROR("Can't provide ws-server for %s: API %s doesn't exist", path, apiws->api);
                goto error2;
        }
index 18104a9..967cd68 100644 (file)
@@ -286,68 +286,46 @@ int afb_apiset_del(struct afb_apiset *set, const char *name)
 }
 
 /**
- * Get from the 'set' the API of 'name' in 'api'
+ * Get from the 'set' the API of 'name' in 'api' with fallback to subset or default api
  * @param set the set of API
  * @param name the name of the API to get
  * @param api the structure where to store data about the API of name
  * @return 0 in case of success or -1 in case of error
  */
-int afb_apiset_lookup(struct afb_apiset *set, const char *name, struct afb_api *api)
+static struct api_desc *lookup(struct afb_apiset *set, const char *name, int rec)
 {
-       const struct api_desc *i;
-
-       i = search(set, name);
-       if (i) {
-               *api = i->api;
-               return 0;
-       }
-
-       errno = ENOENT;
-       return -1;
-}
-
-/**
- * Check whether the 'set' has the API of 'name'
- * @param set the set of API
- * @param name the name of the API to get
- * @return 1 if the api exist or 0 otherwise
- */
-int afb_apiset_has(struct afb_apiset *set, const char *name)
-{
-       return !!search(set, name);
+       struct api_desc *i = search(set, name);
+       return i || !rec || !set->subset ? i : lookup(set->subset, name, rec);
 }
 
 /**
- * Get from the 'set' the API of 'name' in 'api' with fallback to subset or default api
+ * Get from the 'set' the API of 'name' in 'api'
  * @param set the set of API
  * @param name the name of the API to get
- * @param api the structure where to store data about the API of name
+ * @param rec if not zero look also recursively in subsets
  * @return 0 in case of success or -1 in case of error
  */
-static struct api_desc *get_api(struct afb_apiset *set, const char *name)
+const struct afb_api *afb_apiset_lookup(struct afb_apiset *set, const char *name, int rec)
 {
-       struct api_desc *i = search(set, name);
-       return i || !set->subset ? i : get_api(set->subset, name);
+       struct api_desc *i;
+
+       i = lookup(set, name, rec);
+       if (i)
+               return &i->api;
+       errno = ENOENT;
+       return NULL;
 }
 
 /**
- * Get from the 'set' the API of 'name' in 'api' with fallback to subset or default api
+ * Check whether the 'set' has the API of 'name'
  * @param set the set of API
  * @param name the name of the API to get
- * @param api the structure where to store data about the API of name
- * @return 0 in case of success or -1 in case of error
+ * @param rec if not zero look also recursively in subsets
+ * @return 1 if the api exist or 0 otherwise
  */
-int afb_apiset_get(struct afb_apiset *set, const char *name, struct afb_api *api)
+int afb_apiset_has(struct afb_apiset *set, const char *name, int rec)
 {
-       const struct api_desc *i;
-
-       i = get_api(set, name);
-       if (!i) {
-               errno = ENOENT;
-               return -1;
-       }
-       *api = i->api;
-       return 0;
+       return !!afb_apiset_lookup(set, name, rec);
 }
 
 /**
@@ -391,23 +369,21 @@ static int start_api(struct afb_apiset *set, struct api_desc *api, int share_ses
 }
 
 /**
- * Get from the 'set' the API of 'name' in 'api' with fallback to subset or default api
+ * Get from the 'set' the API of 'name' in 'api'
  * @param set the set of API
  * @param name the name of the API to get
- * @param api the structure where to store data about the API of name
+ * @param rec if not zero look also recursively in subsets
  * @return 0 in case of success or -1 in case of error
  */
-int afb_apiset_get_started(struct afb_apiset *set, const char *name, struct afb_api *api)
+const struct afb_api *afb_apiset_lookup_started(struct afb_apiset *set, const char *name, int rec)
 {
        struct api_desc *i;
 
-       i = get_api(set, name);
-       if (!i) {
-               errno = ENOENT;
-               return -1;
-       }
-       *api = i->api;
-       return i->status ? start_api(set, i, 1, 1) : 0;
+       i = lookup(set, name, rec);
+       if (i)
+               return i->status && start_api(set, i, 1, 1) ? NULL : &i->api;
+       errno = ENOENT;
+       return NULL;
 }
 
 /**
index ea1eefc..ff25a7f 100644 (file)
@@ -31,10 +31,9 @@ extern void afb_apiset_subset_set(struct afb_apiset *set, struct afb_apiset *sub
 extern struct afb_apiset *afb_apiset_subset_get(struct afb_apiset *set);
 extern int afb_apiset_add(struct afb_apiset *set, const char *name, struct afb_api api);
 extern int afb_apiset_del(struct afb_apiset *set, const char *name);
-extern int afb_apiset_has(struct afb_apiset *set, const char *name);
-extern int afb_apiset_lookup(struct afb_apiset *set, const char *name, struct afb_api *api);
-extern int afb_apiset_get(struct afb_apiset *set, const char *name, struct afb_api *api);
-extern int afb_apiset_get_started(struct afb_apiset *set, const char *name, struct afb_api *api);
+extern int afb_apiset_has(struct afb_apiset *set, const char *name, int rec);
+extern const struct afb_api *afb_apiset_lookup(struct afb_apiset *set, const char *name, int rec);
+extern const struct afb_api *afb_apiset_lookup_started(struct afb_apiset *set, const char *name, int rec);
 extern int afb_apiset_start_service(struct afb_apiset *set, const char *name, int share_session, int onneed);
 extern int afb_apiset_start_all_services(struct afb_apiset *set, int share_session);
 extern void afb_apiset_update_hooks(struct afb_apiset *set, const char *name);
index 66bdb9d..3fc09e9 100644 (file)
@@ -112,8 +112,7 @@ static struct afb_req unstore_req_cb(void *closure, struct afb_stored_req *sreq)
 
 static int require_api_cb(void *closure, const char *name, int initialized)
 {
-       struct afb_api a;
-       return (initialized ? afb_apiset_get_started : afb_apiset_get)(main_apiset, name, &a);
+       return -!(initialized ? afb_apiset_lookup_started : afb_apiset_lookup)(main_apiset, name, 1);
 }
 
 /**********************************************
index fd334cf..6bd91f9 100644 (file)
@@ -243,7 +243,7 @@ static void get_one_api(struct json_object *resu, const char *name, struct json_
        struct json_object *o;
 
        o = afb_apiset_describe(main_apiset, name);
-       if (o || afb_apiset_has(main_apiset, name))
+       if (o || afb_apiset_has(main_apiset, name, 0))
                json_object_object_add(resu, name, o);
 }
 
index 80dd143..73841af 100644 (file)
@@ -876,7 +876,7 @@ void afb_xreq_fail_unknown_verb(struct afb_xreq *xreq)
 
 static void process_sync(struct afb_xreq *xreq)
 {
-       struct afb_api api;
+       const struct afb_api *api;
 
        /* init hooking */
        afb_hook_init_xreq(xreq);
@@ -884,14 +884,15 @@ static void process_sync(struct afb_xreq *xreq)
                afb_hook_xreq_begin(xreq);
 
        /* search the api */
-       if (afb_apiset_get_started(xreq->apiset, xreq->api, &api) < 0) {
+       api = afb_apiset_lookup_started(xreq->apiset, xreq->api, 1);
+       if (!api) {
                if (errno == ENOENT)
                        afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
                else
                        afb_xreq_fail_f(xreq, "bad-api-state", "api %s not started correctly: %m", xreq->api);
        } else {
-               xreq->context.api_key = api.closure;
-               api.itf->call(api.closure, xreq);
+               xreq->context.api_key = api->closure;
+               api->itf->call(api->closure, xreq);
        }
 }