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);
40 static void subcall_sync_leave(struct subcall *subcall);
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*);
55 struct jobloop *jobloop;
56 struct json_object *result;
60 static void subcall_destroy(void *closure)
62 struct subcall *subcall = closure;
64 json_object_put(subcall->xreq.json);
65 afb_xreq_unref(subcall->caller);
69 static void subcall_reply(void *closure, int iserror, struct json_object *obj)
71 struct subcall *subcall = closure;
73 if (subcall->callback) {
74 subcall->callback(subcall->closure, iserror, obj);
77 subcall->result = obj;
78 subcall->iserror = iserror;
79 subcall_sync_leave(subcall);
83 static int subcall_subscribe(void *closure, struct afb_event event)
85 struct subcall *subcall = closure;
87 return afb_xreq_subscribe(subcall->caller, event);
90 static int subcall_unsubscribe(void *closure, struct afb_event event)
92 struct subcall *subcall = closure;
94 return afb_xreq_unsubscribe(subcall->caller, event);
97 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)
99 struct subcall *subcall;
101 subcall = calloc(1, sizeof *subcall);
102 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;
116 afb_xreq_addref(caller);
117 json_object_get(args);
122 struct afb_xreq *caller,
125 struct json_object *args,
126 void (*callback)(void*, int, struct json_object*),
130 struct subcall *subcall;
132 subcall = create_subcall(caller, api, verb, args, callback, closure);
133 if (subcall == NULL) {
134 callback(closure, 1, afb_msg_json_internal_error());
138 afb_apis_call(&subcall->xreq);
139 afb_xreq_unref(&subcall->xreq);
142 static void subcall_sync_leave(struct subcall *subcall)
144 struct jobloop *jobloop = subcall->jobloop;
145 subcall->jobloop = NULL;
150 static void subcall_sync_cb(int signum, void *closure, struct jobloop *jobloop)
152 struct subcall *subcall = closure;
155 subcall->jobloop = jobloop;
156 afb_apis_call_direct(&subcall->xreq);
158 afb_xreq_fail_f(&subcall->xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
160 subcall_sync_leave(subcall);
163 int afb_subcall_sync(
164 struct afb_xreq *caller,
167 struct json_object *args,
168 struct json_object **result
172 struct subcall *subcall;
174 subcall = create_subcall(caller, api, verb, args, NULL, NULL);
175 if (subcall == NULL) {
176 *result = json_object_get(afb_msg_json_internal_error());
180 rc = jobs_enter(NULL, 0, subcall_sync_cb, subcall);
182 subcall->result = json_object_get(afb_msg_json_internal_error());
183 subcall->iserror = 1;
185 rc = !subcall->iserror;
186 *result = subcall->result;
187 afb_xreq_unref(&subcall->xreq);