2 * Copyright (C) 2016, 2017 "IoT.bzh"
3 * Author: José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
23 #include <json-c/json.h>
24 #include <afb/afb-req-itf.h>
26 #include "afb-subcall.h"
27 #include "afb-msg-json.h"
29 #include "afb-context.h"
35 static void subcall_destroy(void *closure);
36 static void subcall_reply(void *closure, int iserror, struct json_object *obj);
37 static int subcall_subscribe(void *closure, struct afb_event event);
38 static int subcall_unsubscribe(void *closure, struct afb_event event);
40 const struct afb_xreq_query_itf afb_subcall_xreq_itf = {
41 .reply = subcall_reply,
42 .unref = subcall_destroy,
43 .subscribe = subcall_subscribe,
44 .unsubscribe = subcall_unsubscribe
50 struct afb_xreq *caller;
51 void (*callback)(void*, int, struct json_object*);
55 static void subcall_destroy(void *closure)
57 struct subcall *subcall = closure;
59 json_object_put(subcall->xreq.json);
60 afb_xreq_unref(subcall->caller);
64 static void subcall_reply(void *closure, int iserror, struct json_object *obj)
66 struct subcall *subcall = closure;
68 subcall->callback(subcall->closure, iserror, obj);
72 static int subcall_subscribe(void *closure, struct afb_event event)
74 struct subcall *subcall = closure;
76 return afb_xreq_subscribe(subcall->caller, event);
79 static int subcall_unsubscribe(void *closure, struct afb_event event)
81 struct subcall *subcall = closure;
83 return afb_xreq_unsubscribe(subcall->caller, event);
86 void afb_subcall_internal_error(void (*callback)(void*, int, struct json_object*), void *closure)
88 static struct json_object *obj;
91 obj = afb_msg_json_reply_error("failed", "internal error", NULL, NULL);
93 callback(closure, 1, obj);
96 static struct subcall *create_subcall(struct afb_xreq *caller, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *closure)
98 struct subcall *subcall;
100 subcall = calloc(1, sizeof *subcall);
101 if (subcall == NULL) {
106 afb_context_subinit(&subcall->xreq.context, &caller->context);
107 subcall->xreq.refcount = 1;
108 subcall->xreq.json = args;
109 subcall->xreq.api = api; /* TODO: alloc ? */
110 subcall->xreq.verb = verb; /* TODO: alloc ? */
111 subcall->xreq.query = subcall;
112 subcall->xreq.queryitf = &afb_subcall_xreq_itf;
113 subcall->caller = caller;
114 subcall->callback = callback;
115 subcall->closure = closure;
120 struct afb_xreq *caller,
123 struct json_object *args,
124 void (*callback)(void*, int, struct json_object*),
128 struct subcall *subcall;
130 subcall = create_subcall(caller, api, verb, args, callback, closure);
131 if (subcall == NULL) {
132 afb_subcall_internal_error(callback, closure);
136 afb_xreq_addref(caller);
137 afb_apis_call(&subcall->xreq);
138 afb_xreq_unref(&subcall->xreq);