- struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
-
- return afb_xreq_subscribe(subcall->caller, event);
-}
-
-static int subcall_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
-{
- struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
-
- return afb_xreq_unsubscribe(subcall->caller, event);
-}
-
-static void subcall_reply(struct afb_xreq *xreq, int status, struct json_object *obj)
-{
- struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
-
- if (subcall->callback)
- subcall->callback(subcall->closure, status, obj);
- json_object_put(obj);
-}
-
-static void subcall_destroy(struct afb_xreq *xreq)
-{
- struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
-
- json_object_put(subcall->xreq.json);
- afb_cred_unref(subcall->xreq.cred);
- xreq_unref(subcall->caller);
- free(subcall);
-}
-
-const struct afb_xreq_query_itf afb_xreq_subcall_itf = {
- .reply = subcall_reply,
- .unref = subcall_destroy,
- .subscribe = subcall_subscribe,
- .unsubscribe = subcall_unsubscribe
-};
-
-static struct subcall *subcall_alloc(
- struct afb_xreq *caller,
- const char *api,
- const char *verb,
- struct json_object *args
-)
-{
- struct subcall *subcall;
- size_t lenapi, lenverb;
- char *copy;
-
- lenapi = 1 + strlen(api);
- lenverb = 1 + strlen(verb);
- subcall = malloc(lenapi + lenverb + sizeof *subcall);
- if (!subcall)
- ERROR("out of memory");
- else {
- copy = (char*)&subcall[1];
- memcpy(copy, api, lenapi);
- api = copy;
- copy = ©[lenapi];
- memcpy(copy, verb, lenverb);
- verb = copy;
-
- afb_xreq_init(&subcall->xreq, &afb_xreq_subcall_itf);
- afb_context_subinit(&subcall->xreq.context, &caller->context);
- subcall->xreq.cred = afb_cred_addref(caller->cred);
- subcall->xreq.json = args;
- subcall->xreq.api = api;
- subcall->xreq.verb = verb;
- subcall->caller = caller;
- xreq_addref(caller);
- }
- return subcall;
-}
-
-static void subcall_process(struct subcall *subcall)
-{
- if (subcall->caller->queryitf->subcall) {
- subcall->caller->queryitf->subcall(
- subcall->caller, subcall->xreq.api, subcall->xreq.verb,
- subcall->xreq.json, subcall->callback, subcall->closure);
- xreq_unref(&subcall->xreq);
- } else
- afb_xreq_process(&subcall->xreq, subcall->caller->apiset);
-}
-
-static void subcall_sync_leave(struct subcall *subcall)
-{
- struct jobloop *jobloop = __atomic_exchange_n(&subcall->jobloop, NULL, __ATOMIC_RELAXED);
- if (jobloop)
- jobs_leave(jobloop);
-}
-
-static void subcall_sync_reply(void *closure, int status, struct json_object *obj)
-{
- struct subcall *subcall = closure;
-
- subcall->status = status;
- subcall->result = json_object_get(obj);
- subcall_sync_leave(subcall);