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