Finalize hooking of requests
[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
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
46 };
47
48 struct subcall
49 {
50         struct afb_xreq xreq;
51         struct afb_xreq *caller;
52         void (*callback)(void*, int, struct json_object*);
53         void *closure;
54 };
55
56 static void subcall_destroy(void *closure)
57 {
58         struct subcall *subcall = closure;
59
60         json_object_put(subcall->xreq.json);
61         afb_xreq_unref(subcall->caller);
62         free(subcall);
63 }
64
65 static void subcall_reply(void *closure, int iserror, struct json_object *obj)
66 {
67         struct subcall *subcall = closure;
68
69         subcall->callback(subcall->closure, iserror, obj);
70         json_object_put(obj);
71 }
72
73 static int subcall_subscribe(void *closure, struct afb_event event)
74 {
75         struct subcall *subcall = closure;
76
77         return afb_xreq_subscribe(subcall->caller, event);
78 }
79
80 static int subcall_unsubscribe(void *closure, struct afb_event event)
81 {
82         struct subcall *subcall = closure;
83
84         return afb_xreq_unsubscribe(subcall->caller, event);
85 }
86
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)
88 {
89         struct subcall *subcall;
90
91         subcall = calloc(1, sizeof *subcall);
92         if (subcall == NULL) {
93                 return NULL;
94         }
95
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);
108         return subcall;
109 }
110
111 void afb_subcall(
112                 struct afb_xreq *caller,
113                 const char *api,
114                 const char *verb,
115                 struct json_object *args,
116                 void (*callback)(void*, int, struct json_object*),
117                 void *closure
118 )
119 {
120         struct subcall *subcall;
121
122         subcall = create_subcall(caller, api, verb, args, callback, closure);
123         if (subcall == NULL) {
124                 callback(closure, 1, afb_msg_json_internal_error());
125                 return;
126         }
127
128         afb_apis_call(&subcall->xreq);
129         afb_xreq_unref(&subcall->xreq);
130 }
131
132 struct subcall_sync
133 {
134         struct afb_xreq *caller;
135         const char *api;
136         const char *verb;
137         struct json_object *args;
138         struct jobloop *jobloop;
139         struct json_object *result;
140         int iserror;
141 };
142
143 static void subcall_sync_leave(struct subcall_sync *sync)
144 {
145         struct jobloop *jobloop = sync->jobloop;
146         sync->jobloop = NULL;
147         if (jobloop)
148                 jobs_leave(jobloop);
149 }
150
151 static void subcall_sync_reply(void *closure, int iserror, struct json_object *obj)
152 {
153         struct subcall_sync *sync = closure;
154
155         sync->iserror = iserror;
156         sync->result = obj;
157         json_object_get(obj);
158         subcall_sync_leave(sync);
159 }
160
161 static void subcall_sync_enter(int signum, void *closure, struct jobloop *jobloop)
162 {
163         struct subcall_sync *sync = closure;
164
165         if (!signum) {
166                 sync->jobloop = jobloop;
167                 afb_xreq_unhooked_subcall(sync->caller, sync->api, sync->verb, sync->args, subcall_sync_reply, sync);
168         } else {
169                 sync->result = json_object_get(afb_msg_json_internal_error());
170                 sync->iserror = 1;
171                 subcall_sync_leave(sync);
172         }
173 }
174
175 int afb_subcall_sync(
176                 struct afb_xreq *caller,
177                 const char *api,
178                 const char *verb,
179                 struct json_object *args,
180                 struct json_object **result
181 )
182 {
183         int rc;
184         struct subcall_sync sync;
185
186         sync.caller = caller;
187         sync.api = api;
188         sync.verb = verb;
189         sync.args = args;
190         sync.jobloop = NULL;
191         sync.result = NULL;
192         sync.iserror = 1;
193
194         rc = jobs_enter(NULL, 0, subcall_sync_enter, &sync);
195         if (rc < 0) {
196                 sync.result = json_object_get(afb_msg_json_internal_error());
197                 sync.iserror = 1;
198         }
199         rc = !sync.iserror;
200         *result = sync.result;
201         return rc;
202 }
203
204