diff options
author | José Bollo <jose.bollo@iot.bzh> | 2017-05-29 14:16:13 +0200 |
---|---|---|
committer | José Bollo <jose.bollo@iot.bzh> | 2017-05-29 14:16:13 +0200 |
commit | 090379fdaf6ed1860dcff21424135ad71ead0cd2 (patch) | |
tree | b7c9bde501e86a9fcf4a801444a670de3339208a /bindings/samples/HelloWorld.c | |
parent | ce8d87d91aa710702b02a371278f4e1f39ea195c (diff) |
Add 'afb_service_call_sync' function
This new function allows to call features for the
services synchronously.
Also refactoring how are handled arguments to
calls. The call to 'json_object_put' is now always
done by the binder.
Change-Id: I910517da75b179aeafc824da4ce29bc299711990
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
Diffstat (limited to 'bindings/samples/HelloWorld.c')
-rw-r--r-- | bindings/samples/HelloWorld.c | 55 |
1 files changed, 49 insertions, 6 deletions
diff --git a/bindings/samples/HelloWorld.c b/bindings/samples/HelloWorld.c index 505aee30..6c904bde 100644 --- a/bindings/samples/HelloWorld.c +++ b/bindings/samples/HelloWorld.c @@ -182,10 +182,8 @@ static void subcall (struct afb_req request) if (object == NULL) afb_req_fail(request, "failed", "bad arguments"); - else { + else afb_req_subcall(request, api, verb, object, subcallcb, afb_req_store(request)); - json_object_put(object); - } } static void subcallsync (struct afb_req request) @@ -200,13 +198,12 @@ static void subcallsync (struct afb_req request) afb_req_fail(request, "failed", "bad arguments"); else { rc = afb_req_subcall_sync(request, api, verb, object, &result); - if (rc) { + if (rc) afb_req_success(request, result, NULL); - } else { + else { afb_req_fail(request, "failed", json_object_to_json_string(result)); json_object_put(result); } - json_object_put(object); } } @@ -284,6 +281,50 @@ static void eventpush (struct afb_req request) json_object_put(object); } +static void callcb (void *prequest, int iserror, json_object *object) +{ + struct afb_req request = afb_req_unstore(prequest); + if (iserror) + afb_req_fail(request, "failed", json_object_to_json_string(object)); + else + afb_req_success(request, json_object_get(object), NULL); + afb_req_unref(request); +} + +static void call (struct afb_req request) +{ + const char *api = afb_req_value(request, "api"); + const char *verb = afb_req_value(request, "verb"); + const char *args = afb_req_value(request, "args"); + json_object *object = api && verb && args ? json_tokener_parse(args) : NULL; + + if (object == NULL) + afb_req_fail(request, "failed", "bad arguments"); + else + afb_service_call(api, verb, object, callcb, afb_req_store(request)); +} + +static void callsync (struct afb_req request) +{ + int rc; + const char *api = afb_req_value(request, "api"); + const char *verb = afb_req_value(request, "verb"); + const char *args = afb_req_value(request, "args"); + json_object *result, *object = api && verb && args ? json_tokener_parse(args) : NULL; + + if (object == NULL) + afb_req_fail(request, "failed", "bad arguments"); + else { + rc = afb_service_call_sync(api, verb, object, &result); + if (rc) + afb_req_success(request, result, NULL); + else { + afb_req_fail(request, "failed", json_object_to_json_string(result)); + json_object_put(result); + } + } +} + static void exitnow (struct afb_req request) { exit(0); @@ -317,6 +358,8 @@ static const struct afb_verb_v2 verbs[]= { { "eventsub", eventsub , NULL, AFB_SESSION_NONE }, { "eventunsub", eventunsub , NULL, AFB_SESSION_NONE }, { "eventpush", eventpush , NULL, AFB_SESSION_NONE }, + { "call", call , NULL, AFB_SESSION_NONE }, + { "callsync", callsync , NULL, AFB_SESSION_NONE }, { "exit", exitnow , NULL, AFB_SESSION_NONE }, { NULL} }; |