-static inline void xreq_addref(struct afb_xreq *xreq)
-{
- __atomic_add_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED);
-}
-
-static inline void xreq_unref(struct afb_xreq *xreq)
-{
- if (!__atomic_sub_fetch(&xreq->refcount, 1, __ATOMIC_RELAXED)) {
- if (!xreq->replied)
- afb_xreq_fail(xreq, "error", "no reply");
- if (xreq->hookflags)
- afb_hook_xreq_end(xreq);
- xreq->queryitf->unref(xreq);
- }
-}
-
-/******************************************************************************/
-
-extern const struct afb_req_itf xreq_itf;
-extern const struct afb_req_itf xreq_hooked_itf;
-
-static inline struct afb_req to_req(struct afb_xreq *xreq)
-{
- return (struct afb_req){ .itf = xreq->hookflags ? &xreq_hooked_itf : &xreq_itf, .closure = xreq };
-}
-
-/******************************************************************************/
-
-struct subcall
-{
- struct afb_xreq xreq;
- struct afb_xreq *caller;
-
- void (*completion)(struct subcall*, int, struct json_object*);
-
- union {
- struct {
- struct jobloop *jobloop;
- struct json_object *result;
- int status;
- };
- struct {
- union {
- void (*callback)(void*, int, struct json_object*);
- void (*callback2)(void*, int, struct json_object*, struct afb_req);
- };
- void *closure;
- };
- };
-};
-
-static int subcall_subscribe_cb(struct afb_xreq *xreq, struct afb_event event)
-{
- struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
-
- return afb_xreq_subscribe(subcall->caller, event);
-}
-
-static int subcall_unsubscribe_cb(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_cb(struct afb_xreq *xreq, int status, struct json_object *result)
-{
- struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
-
- subcall->completion(subcall, status, result);
- json_object_put(result);
- afb_xreq_unref(&subcall->xreq);
-}
-
-static void subcall_destroy_cb(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_cb,
- .unref = subcall_destroy_cb,
- .subscribe = subcall_subscribe_cb,
- .unsubscribe = subcall_unsubscribe_cb
-};
-
-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_on_reply(struct subcall *subcall, int status, struct json_object *result)
-{
- subcall->callback(subcall->closure, status, result);
-}
-
-static void subcall_req_on_reply(struct subcall *subcall, int status, struct json_object *result)
-{
- subcall->callback2(subcall->closure, status, result, to_req(subcall->caller));
-}
-
-static void subcall_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
-{
- afb_hook_xreq_subcall_result(subcall->caller, status, result);
- subcall_on_reply(subcall, status, result);
-}
-
-static void subcall_req_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
-{
- afb_hook_xreq_subcall_req_result(subcall->caller, status, result);
- subcall_req_on_reply(subcall, status, result);
-}
-
-static void subcall_reply_direct_cb(void *closure, int status, struct json_object *result)
-{
- struct afb_xreq *xreq = closure;
-
- if (xreq->replied) {
- ERROR("subcall replied more than one time!!");
- json_object_put(result);
- } else {
- xreq->replied = 1;
- subcall_reply_cb(xreq, status, result);
- }
-}
-
-static void subcall_process(struct subcall *subcall, void (*completion)(struct subcall*, int, struct json_object*))
-{
- subcall->completion = completion;
- if (subcall->caller->queryitf->subcall) {
- subcall->caller->queryitf->subcall(
- subcall->caller, subcall->xreq.api, subcall->xreq.verb,
- subcall->xreq.json, subcall_reply_direct_cb, &subcall->xreq);
- } else {
- afb_xreq_addref(&subcall->xreq);
- afb_xreq_process(&subcall->xreq, subcall->caller->apiset);
- }
-}
-
-static void subcall(struct subcall *subcall, void (*callback)(void*, int, struct json_object*), void *cb_closure)
-{
- subcall->callback = callback;
- subcall->closure = cb_closure;
- subcall_process(subcall, subcall_on_reply);
-}
-
-static void subcall_req(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
-{
- subcall->callback2 = callback;
- subcall->closure = cb_closure;
- subcall_process(subcall, subcall_req_on_reply);
-}
-
-static void subcall_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*), void *cb_closure)
-{
- subcall->callback = callback;
- subcall->closure = cb_closure;
- subcall_process(subcall, subcall_hooked_on_reply);
-}
-
-static void subcall_req_hooked(struct subcall *subcall, void (*callback)(void*, int, struct json_object*, struct afb_req), void *cb_closure)
-{
- subcall->callback2 = callback;
- subcall->closure = cb_closure;
- subcall_process(subcall, subcall_req_hooked_on_reply);
-}
-
-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(struct subcall *subcall, int status, struct json_object *result)
-{
- subcall->status = status;
- subcall->result = json_object_get(result);
- subcall_sync_leave(subcall);
-}
-
-static void subcall_sync_enter(int signum, void *closure, struct jobloop *jobloop)