-static inline struct afb_request *to_request(struct afb_xreq *xreq)
-{
- return &xreq->request;
-}
-
-static inline struct afb_req to_req(struct afb_xreq *xreq)
-{
- return (struct afb_req){ .itf = xreq->request.itf, .closure = &xreq->request };
-}
-
-static inline struct afb_xreq *from_request(struct afb_request *request)
-{
- return CONTAINER_OF(struct afb_xreq, request, request);
-}
-
-/******************************************************************************/
-
-struct subcall
-{
- struct afb_xreq xreq;
-
- 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 (*callback_req)(void*, int, struct json_object*, struct afb_req);
- void (*callback_request)(void*, int, struct json_object*, struct afb_request*);
- };
- void *closure;
- };
- };
-};
-
-static int subcall_subscribe_cb(struct afb_xreq *xreq, struct afb_eventid *eventid)
-{
- struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
-
- return afb_xreq_subscribe(subcall->xreq.caller, eventid);
-}
-
-static int subcall_unsubscribe_cb(struct afb_xreq *xreq, struct afb_eventid *eventid)
-{
- struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
-
- return afb_xreq_unsubscribe(subcall->xreq.caller, eventid);
-}
-
-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_unhooked_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);
- 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.request.api = api;
- subcall->xreq.request.verb = verb;
- subcall->xreq.caller = caller;
- afb_xreq_unhooked_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->callback_req(subcall->closure, status, result, to_req(subcall->xreq.caller));
-}
-
-static void subcall_request_on_reply(struct subcall *subcall, int status, struct json_object *result)
-{
- subcall->callback_request(subcall->closure, status, result, to_request(subcall->xreq.caller));
-}
-
-static void subcall_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
-{
- afb_hook_xreq_subcall_result(subcall->xreq.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->xreq.caller, status, result);
- subcall_req_on_reply(subcall, status, result);
-}
-
-static void subcall_request_hooked_on_reply(struct subcall *subcall, int status, struct json_object *result)
-{
- afb_hook_xreq_subcall_result(subcall->xreq.caller, status, result);
- subcall_request_on_reply(subcall, status, result);
-}
-
-static void subcall_reply_direct_cb(void *closure, int status, struct json_object *result)