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"
37 static void subcall_destroy(struct afb_xreq *xreq);
38 static void subcall_reply(struct afb_xreq *xreq, int iserror, struct json_object *obj);
39 static int subcall_subscribe(struct afb_xreq *xreq, struct afb_event event);
40 static int subcall_unsubscribe(struct afb_xreq *xreq, struct afb_event event);
42 const struct afb_xreq_query_itf afb_subcall_xreq_itf = {
43 .reply = subcall_reply,
44 .unref = subcall_destroy,
45 .subscribe = subcall_subscribe,
46 .unsubscribe = subcall_unsubscribe
52 struct afb_xreq *caller;
53 void (*callback)(void*, int, struct json_object*);
57 static void subcall_destroy(struct afb_xreq *xreq)
59 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
61 json_object_put(subcall->xreq.json);
62 afb_cred_unref(subcall->xreq.cred);
63 afb_xreq_unref(subcall->caller);
67 static void subcall_reply(struct afb_xreq *xreq, int iserror, struct json_object *obj)
69 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
71 subcall->callback(subcall->closure, iserror, obj);
75 static int subcall_subscribe(struct afb_xreq *xreq, struct afb_event event)
77 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
79 return afb_xreq_subscribe(subcall->caller, event);
82 static int subcall_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
84 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
86 return afb_xreq_unsubscribe(subcall->caller, event);
89 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)
91 struct subcall *subcall;
93 subcall = calloc(1, sizeof *subcall);
94 if (subcall == NULL) {
98 afb_xreq_init(&subcall->xreq, &afb_subcall_xreq_itf);
99 afb_context_subinit(&subcall->xreq.context, &caller->context);
100 subcall->xreq.cred = afb_cred_addref(caller->cred);
101 subcall->xreq.json = args;
102 subcall->xreq.api = api; /* TODO: alloc ? */
103 subcall->xreq.verb = verb; /* TODO: alloc ? */
104 subcall->caller = caller;
105 subcall->callback = callback;
106 subcall->closure = closure;
107 afb_xreq_addref(caller);
108 json_object_get(args);
113 struct afb_xreq *caller,
116 struct json_object *args,
117 void (*callback)(void*, int, struct json_object*),
121 struct subcall *subcall;
123 subcall = create_subcall(caller, api, verb, args, callback, closure);
124 if (subcall == NULL) {
125 callback(closure, 1, afb_msg_json_internal_error());
129 afb_apis_call(&subcall->xreq);
130 afb_xreq_unref(&subcall->xreq);
135 struct afb_xreq *caller;
138 struct json_object *args;
139 struct jobloop *jobloop;
140 struct json_object *result;
144 static void subcall_sync_leave(struct subcall_sync *sync)
146 struct jobloop *jobloop = sync->jobloop;
147 sync->jobloop = NULL;
152 static void subcall_sync_reply(void *closure, int iserror, struct json_object *obj)
154 struct subcall_sync *sync = closure;
156 sync->iserror = iserror;
158 json_object_get(obj);
159 subcall_sync_leave(sync);
162 static void subcall_sync_enter(int signum, void *closure, struct jobloop *jobloop)
164 struct subcall_sync *sync = closure;
167 sync->jobloop = jobloop;
168 afb_xreq_unhooked_subcall(sync->caller, sync->api, sync->verb, sync->args, subcall_sync_reply, sync);
170 sync->result = json_object_get(afb_msg_json_internal_error());
172 subcall_sync_leave(sync);
176 int afb_subcall_sync(
177 struct afb_xreq *caller,
180 struct json_object *args,
181 struct json_object **result
185 struct subcall_sync sync;
187 sync.caller = caller;
195 rc = jobs_enter(NULL, 0, subcall_sync_enter, &sync);
197 sync.result = json_object_get(afb_msg_json_internal_error());
201 *result = sync.result;