Add synchronous subcalls
[src/app-framework-binder.git] / src / afb-subcall.c
1 /*
2  * Copyright (C) 2016, 2017 "IoT.bzh"
3  * Author: José Bollo <jose.bollo@iot.bzh>
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #define _GNU_SOURCE
19
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include <json-c/json.h>
24 #include <afb/afb-req-itf.h>
25
26 #include "afb-subcall.h"
27 #include "afb-msg-json.h"
28 #include "afb-apis.h"
29 #include "afb-context.h"
30 #include "afb-xreq.h"
31 #include "verbose.h"
32 #include "jobs.h"
33
34 struct subcall;
35
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);
41
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
47 };
48
49 struct subcall
50 {
51         struct afb_xreq xreq;
52         struct afb_xreq *caller;
53         void (*callback)(void*, int, struct json_object*);
54         void *closure;
55         struct jobloop *jobloop;
56         struct json_object *result;
57         int iserror;
58 };
59
60 static void subcall_destroy(void *closure)
61 {
62         struct subcall *subcall = closure;
63
64         json_object_put(subcall->xreq.json);
65         afb_xreq_unref(subcall->caller);
66         free(subcall);
67 }
68
69 static void subcall_reply(void *closure, int iserror, struct json_object *obj)
70 {
71         struct subcall *subcall = closure;
72
73         if (subcall->callback) {
74                 subcall->callback(subcall->closure, iserror, obj);
75                 json_object_put(obj);
76         } else {
77                 subcall->result = obj;
78                 subcall->iserror = iserror;
79                 subcall_sync_leave(subcall);
80         }
81 }
82
83 static int subcall_subscribe(void *closure, struct afb_event event)
84 {
85         struct subcall *subcall = closure;
86
87         return afb_xreq_subscribe(subcall->caller, event);
88 }
89
90 static int subcall_unsubscribe(void *closure, struct afb_event event)
91 {
92         struct subcall *subcall = closure;
93
94         return afb_xreq_unsubscribe(subcall->caller, event);
95 }
96
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)
98 {
99         struct subcall *subcall;
100
101         subcall = calloc(1, sizeof *subcall);
102         if (subcall == NULL) {
103                 return NULL;
104         }
105
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);
118         return subcall;
119 }
120
121 void afb_subcall(
122                 struct afb_xreq *caller,
123                 const char *api,
124                 const char *verb,
125                 struct json_object *args,
126                 void (*callback)(void*, int, struct json_object*),
127                 void *closure
128 )
129 {
130         struct subcall *subcall;
131
132         subcall = create_subcall(caller, api, verb, args, callback, closure);
133         if (subcall == NULL) {
134                 callback(closure, 1, afb_msg_json_internal_error());
135                 return;
136         }
137
138         afb_apis_call(&subcall->xreq);
139         afb_xreq_unref(&subcall->xreq);
140 }
141
142 static void subcall_sync_leave(struct subcall *subcall)
143 {
144         struct jobloop *jobloop = subcall->jobloop;
145         subcall->jobloop = NULL;
146         if (jobloop)
147                 jobs_leave(jobloop);
148 }
149
150 static void subcall_sync_cb(int signum, void *closure, struct jobloop *jobloop)
151 {
152         struct subcall *subcall = closure;
153
154         if (!signum) {
155                 subcall->jobloop = jobloop;
156                 afb_apis_call_direct(&subcall->xreq);
157         } else {
158                 afb_xreq_fail_f(&subcall->xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
159         }
160         subcall_sync_leave(subcall);
161 }
162
163 int afb_subcall_sync(
164                 struct afb_xreq *caller,
165                 const char *api,
166                 const char *verb,
167                 struct json_object *args,
168                 struct json_object **result
169 )
170 {
171         int rc;
172         struct subcall *subcall;
173
174         subcall = create_subcall(caller, api, verb, args, NULL, NULL);
175         if (subcall == NULL) {
176                 *result = json_object_get(afb_msg_json_internal_error());
177                 return 0;
178         }
179
180         rc = jobs_enter(NULL, 0, subcall_sync_cb, subcall);
181         if (rc < 0) {
182                 subcall->result = json_object_get(afb_msg_json_internal_error());
183                 subcall->iserror = 1;
184         }
185         rc = !subcall->iserror;
186         *result = subcall->result;
187         afb_xreq_unref(&subcall->xreq);
188         return rc;
189 }
190
191