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"
36 static void subcall_destroy(void *closure);
37 static void subcall_reply(void *closure, int iserror, struct json_object *obj);
38 static int subcall_subscribe(void *closure, struct afb_event event);
39 static int subcall_unsubscribe(void *closure, struct afb_event event);
41 const struct afb_xreq_query_itf afb_subcall_xreq_itf = {
42 .reply = subcall_reply,
43 .unref = subcall_destroy,
44 .subscribe = subcall_subscribe,
45 .unsubscribe = subcall_unsubscribe
51 struct afb_xreq *caller;
52 void (*callback)(void*, int, struct json_object*);
56 static void subcall_destroy(void *closure)
58 struct subcall *subcall = closure;
60 json_object_put(subcall->xreq.json);
61 afb_xreq_unref(subcall->caller);
65 static void subcall_reply(void *closure, int iserror, struct json_object *obj)
67 struct subcall *subcall = closure;
69 subcall->callback(subcall->closure, iserror, obj);
73 static int subcall_subscribe(void *closure, struct afb_event event)
75 struct subcall *subcall = closure;
77 return afb_xreq_subscribe(subcall->caller, event);
80 static int subcall_unsubscribe(void *closure, struct afb_event event)
82 struct subcall *subcall = closure;
84 return afb_xreq_unsubscribe(subcall->caller, event);
87 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)
89 struct subcall *subcall;
91 subcall = calloc(1, sizeof *subcall);
92 if (subcall == NULL) {
96 afb_context_subinit(&subcall->xreq.context, &caller->context);
97 subcall->xreq.refcount = 1;
98 subcall->xreq.json = args;
99 subcall->xreq.api = api; /* TODO: alloc ? */
100 subcall->xreq.verb = verb; /* TODO: alloc ? */
101 subcall->xreq.query = subcall;
102 subcall->xreq.queryitf = &afb_subcall_xreq_itf;
103 subcall->caller = caller;
104 subcall->callback = callback;
105 subcall->closure = closure;
106 afb_xreq_addref(caller);
107 json_object_get(args);
112 struct afb_xreq *caller,
115 struct json_object *args,
116 void (*callback)(void*, int, struct json_object*),
120 struct subcall *subcall;
122 subcall = create_subcall(caller, api, verb, args, callback, closure);
123 if (subcall == NULL) {
124 callback(closure, 1, afb_msg_json_internal_error());
128 afb_apis_call(&subcall->xreq);
129 afb_xreq_unref(&subcall->xreq);
134 struct afb_xreq *caller;
137 struct json_object *args;
138 struct jobloop *jobloop;
139 struct json_object *result;
143 static void subcall_sync_leave(struct subcall_sync *sync)
145 struct jobloop *jobloop = sync->jobloop;
146 sync->jobloop = NULL;
151 static void subcall_sync_reply(void *closure, int iserror, struct json_object *obj)
153 struct subcall_sync *sync = closure;
155 sync->iserror = iserror;
157 json_object_get(obj);
158 subcall_sync_leave(sync);
161 static void subcall_sync_enter(int signum, void *closure, struct jobloop *jobloop)
163 struct subcall_sync *sync = closure;
166 sync->jobloop = jobloop;
167 afb_xreq_unhooked_subcall(sync->caller, sync->api, sync->verb, sync->args, subcall_sync_reply, sync);
169 sync->result = json_object_get(afb_msg_json_internal_error());
171 subcall_sync_leave(sync);
175 int afb_subcall_sync(
176 struct afb_xreq *caller,
179 struct json_object *args,
180 struct json_object **result
184 struct subcall_sync sync;
186 sync.caller = caller;
194 rc = jobs_enter(NULL, 0, subcall_sync_enter, &sync);
196 sync.result = json_object_get(afb_msg_json_internal_error());
200 *result = sync.result;