diff options
author | José Bollo <jose.bollo@iot.bzh> | 2016-04-27 21:25:54 +0200 |
---|---|---|
committer | José Bollo <jose.bollo@iot.bzh> | 2016-04-27 21:30:01 +0200 |
commit | f262b0f726ac0577f40525038b779185f144873f (patch) | |
tree | c6c2e3664cb6082fcb74e7ef26e430ce2371f4fa /plugins/afm-main-plugin/afm-main-plugin.c | |
parent | 34acb0f8d191593c9761e027424f13ae42831133 (diff) |
first add of asynchonous handling
Change-Id: Id9159d33937dc23342d32892f77998fb8cef0000
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
Diffstat (limited to 'plugins/afm-main-plugin/afm-main-plugin.c')
-rw-r--r-- | plugins/afm-main-plugin/afm-main-plugin.c | 83 |
1 files changed, 59 insertions, 24 deletions
diff --git a/plugins/afm-main-plugin/afm-main-plugin.c b/plugins/afm-main-plugin/afm-main-plugin.c index 553cffc3..21e27e82 100644 --- a/plugins/afm-main-plugin/afm-main-plugin.c +++ b/plugins/afm-main-plugin/afm-main-plugin.c @@ -51,6 +51,22 @@ static struct afb_evmgr evmgr; static struct jbus *jbus; +struct memo +{ + struct afb_req request; + const char *method; +}; + +static struct memo *make_memo(struct afb_req request, const char *method) +{ + struct memo *memo = malloc(sizeof *memo); + if (memo != NULL) { + memo->request = request; + memo->method = method; + } + return memo; +} + static void application_list_changed(const char *data, void *closure) { afb_evmgr_push(evmgr, "application-list-changed", NULL); @@ -78,49 +94,70 @@ static struct json_object *embed(const char *tag, struct json_object *obj) return result; } -static void embed_call_void(struct afb_req request, const char *method) +static void embed_call_void_callback(int status, struct json_object *obj, struct memo *memo) { - struct json_object *obj = jbus_call_sj_sync(jbus, method, "true"); if (interface->verbosity) - fprintf(stderr, "(afm-main-plugin) %s(true) -> %s\n", method, + fprintf(stderr, "(afm-main-plugin) %s(true) -> %s\n", memo->method, obj ? json_object_to_json_string(obj) : "NULL"); if (obj == NULL) { - afb_req_fail(request, "failed", "framework daemon failure"); - return; + afb_req_fail(memo->request, "failed", "framework daemon failure"); + } else { + obj = json_object_get(obj); + obj = embed(memo->method, obj); + if (obj == NULL) { + afb_req_fail(memo->request, "failed", "framework daemon failure"); + } else { + afb_req_success(memo->request, obj, NULL); + } } - obj = json_object_get(obj); - obj = embed(method, obj); + free(memo); +} + +static void embed_call_void(struct afb_req request, const char *method) +{ + struct memo *memo = make_memo(request, method); + if (memo == NULL) + afb_req_fail(request, "failed", "out of memory"); + else if (jbus_call_sj(jbus, method, "true", (void*)embed_call_void_callback, memo) < 0) { + afb_req_fail(request, "failed", "dbus failure"); + free(memo); + } +} + +static void call_appid_callback(int status, struct json_object *obj, struct memo *memo) +{ + if (interface->verbosity) + fprintf(stderr, "(afm-main-plugin) %s -> %s\n", memo->method, + obj ? json_object_to_json_string(obj) : "NULL"); if (obj == NULL) { - afb_req_fail(request, "failed", "framework daemon failure"); - return; + afb_req_fail(memo->request, "failed", "framework daemon failure"); + } else { + obj = json_object_get(obj); + afb_req_success(memo->request, obj, NULL); } - afb_req_success(request, obj, NULL); + free(memo); } static void call_appid(struct afb_req request, const char *method) { - struct json_object *obj; + struct memo *memo; char *sid; const char *id = afb_req_value(request, _id_); if (id == NULL) { afb_req_fail(request, "bad-request", "missing 'id'"); return; } - if (asprintf(&sid, "\"%s\"", id) <= 0) { + memo = make_memo(request, method); + if (asprintf(&sid, "\"%s\"", id) <= 0 || memo == NULL) { afb_req_fail(request, "server-error", "out of memory"); + free(memo); return; } - obj = jbus_call_sj_sync(jbus, method, sid); - if (interface->verbosity) - fprintf(stderr, "(afm-main-plugin) %s(%s) -> %s\n", method, sid, - obj ? json_object_to_json_string(obj) : "NULL"); - free(sid); - if (obj == NULL) { - afb_req_fail(request, "failed", "framework daemon failure"); - return; + if (jbus_call_sj(jbus, method, sid, (void*)call_appid_callback, memo) < 0) { + afb_req_fail(request, "failed", "dbus failure"); + free(memo); } - obj = json_object_get(obj); - afb_req_success(request, obj, NULL); + free(sid); } static void call_runid(struct afb_req request, const char *method) @@ -143,7 +180,6 @@ static void call_runid(struct afb_req request, const char *method) afb_req_success(request, obj, NULL); } - /************************** entries ******************************/ static void runnables(struct afb_req request) @@ -250,7 +286,6 @@ static void install(struct afb_req request) obj = jbus_call_sj_sync(jbus, _install_, query); if (interface->verbosity) -; fprintf(stderr, "(afm-main-plugin) install(%s) -> %s\n", query, obj ? json_object_to_json_string(obj) : "NULL"); free(query); |