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(struct afb_xreq *xreq);
37 static void subcall_reply(struct afb_xreq *xreq, int iserror, struct json_object *obj);
38 static int subcall_subscribe(struct afb_xreq *xreq, struct afb_event event);
39 static int subcall_unsubscribe(struct afb_xreq *xreq, 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(struct afb_xreq *xreq)
58 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
60 json_object_put(subcall->xreq.json);
61 afb_xreq_unref(subcall->caller);
65 static void subcall_reply(struct afb_xreq *xreq, int iserror, struct json_object *obj)
67 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
69 subcall->callback(subcall->closure, iserror, obj);
73 static int subcall_subscribe(struct afb_xreq *xreq, struct afb_event event)
75 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
77 return afb_xreq_subscribe(subcall->caller, event);
80 static int subcall_unsubscribe(struct afb_xreq *xreq, struct afb_event event)
82 struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
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_xreq_init(&subcall->xreq, &afb_subcall_xreq_itf);
97 afb_context_subinit(&subcall->xreq.context, &caller->context);
98 subcall->xreq.json = args;
99 subcall->xreq.api = api; /* TODO: alloc ? */
100 subcall->xreq.verb = verb; /* TODO: alloc ? */
101 subcall->caller = caller;
102 subcall->callback = callback;
103 subcall->closure = closure;
104 afb_xreq_addref(caller);
105 json_object_get(args);
110 struct afb_xreq *caller,
113 struct json_object *args,
114 void (*callback)(void*, int, struct json_object*),
118 struct subcall *subcall;
120 subcall = create_subcall(caller, api, verb, args, callback, closure);
121 if (subcall == NULL) {
122 callback(closure, 1, afb_msg_json_internal_error());
126 afb_apis_call(&subcall->xreq);
127 afb_xreq_unref(&subcall->xreq);
132 struct afb_xreq *caller;
135 struct json_object *args;
136 struct jobloop *jobloop;
137 struct json_object *result;
141 static void subcall_sync_leave(struct subcall_sync *sync)
143 struct jobloop *jobloop = sync->jobloop;
144 sync->jobloop = NULL;
149 static void subcall_sync_reply(void *closure, int iserror, struct json_object *obj)
151 struct subcall_sync *sync = closure;
153 sync->iserror = iserror;
155 json_object_get(obj);
156 subcall_sync_leave(sync);
159 static void subcall_sync_enter(int signum, void *closure, struct jobloop *jobloop)
161 struct subcall_sync *sync = closure;
164 sync->jobloop = jobloop;
165 afb_xreq_unhooked_subcall(sync->caller, sync->api, sync->verb, sync->args, subcall_sync_reply, sync);
167 sync->result = json_object_get(afb_msg_json_internal_error());
169 subcall_sync_leave(sync);
173 int afb_subcall_sync(
174 struct afb_xreq *caller,
177 struct json_object *args,
178 struct json_object **result
182 struct subcall_sync sync;
184 sync.caller = caller;
192 rc = jobs_enter(NULL, 0, subcall_sync_enter, &sync);
194 sync.result = json_object_get(afb_msg_json_internal_error());
198 *result = sync.result;