Simplify functions for calls
[src/app-framework-binder.git] / src / afb-ws-json1.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 <unistd.h>
22 #include <assert.h>
23 #include <errno.h>
24 #include <string.h>
25
26 #include <json-c/json.h>
27
28 #include <afb/afb-req-itf.h>
29
30 #include "afb-wsj1.h"
31 #include "afb-ws-json1.h"
32 #include "afb-common.h"
33 #include "afb-msg-json.h"
34 #include "afb-session.h"
35 #include "afb-apis.h"
36 #include "afb-context.h"
37 #include "afb-evt.h"
38 #include "afb-subcall.h"
39 #include "verbose.h"
40
41 /* predeclaration of structures */
42 struct afb_ws_json1;
43 struct afb_wsreq;
44
45 /* predeclaration of websocket callbacks */
46 static void aws_on_hangup(struct afb_ws_json1 *ws, struct afb_wsj1 *wsj1);
47 static void aws_on_call(struct afb_ws_json1 *ws, const char *api, const char *verb, struct afb_wsj1_msg *msg);
48 static void aws_on_event(struct afb_ws_json1 *ws, const char *event, int eventid, struct json_object *object);
49
50 /* predeclaration of wsreq callbacks */
51 static void wsreq_addref(struct afb_wsreq *wsreq);
52 static void wsreq_unref(struct afb_wsreq *wsreq);
53 static struct json_object *wsreq_json(struct afb_wsreq *wsreq);
54 static struct afb_arg wsreq_get(struct afb_wsreq *wsreq, const char *name);
55 static void wsreq_fail(struct afb_wsreq *wsreq, const char *status, const char *info);
56 static void wsreq_success(struct afb_wsreq *wsreq, struct json_object *obj, const char *info);
57 static const char *wsreq_raw(struct afb_wsreq *wsreq, size_t *size);
58 static void wsreq_send(struct afb_wsreq *wsreq, const char *buffer, size_t size);
59 static int wsreq_subscribe(struct afb_wsreq *wsreq, struct afb_event event);
60 static int wsreq_unsubscribe(struct afb_wsreq *wsreq, struct afb_event event);
61 static void wsreq_subcall(struct afb_wsreq *wsreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *closure);
62
63 /* declaration of websocket structure */
64 struct afb_ws_json1
65 {
66         int refcount;
67         void (*cleanup)(void*);
68         void *cleanup_closure;
69         struct afb_session *session;
70         struct afb_evt_listener *listener;
71         struct afb_wsj1 *wsj1;
72         int new_session;
73 };
74
75 /* declaration of wsreq structure */
76 struct afb_wsreq
77 {
78         /*
79          * CAUTION: 'context' field should be the first because there
80          * is an implicit convertion to struct afb_context
81          */
82         struct afb_context context;
83         int refcount;
84         struct afb_ws_json1 *aws;
85         struct afb_wsreq *next;
86         struct afb_wsj1_msg *msgj1;
87 };
88
89 /* interface for afb_ws_json1 / afb_wsj1 */
90 static struct afb_wsj1_itf wsj1_itf = {
91         .on_hangup = (void*)aws_on_hangup,
92         .on_call = (void*)aws_on_call
93 };
94
95 /* interface for wsreq / afb_req */
96 const struct afb_req_itf afb_ws_json1_req_itf = {
97         .json = (void*)wsreq_json,
98         .get = (void*)wsreq_get,
99         .success = (void*)wsreq_success,
100         .fail = (void*)wsreq_fail,
101         .raw = (void*)wsreq_raw,
102         .send = (void*)wsreq_send,
103         .context_get = (void*)afb_context_get,
104         .context_set = (void*)afb_context_set,
105         .addref = (void*)wsreq_addref,
106         .unref = (void*)wsreq_unref,
107         .session_close = (void*)afb_context_close,
108         .session_set_LOA = (void*)afb_context_change_loa,
109         .subscribe = (void*)wsreq_subscribe,
110         .unsubscribe = (void*)wsreq_unsubscribe,
111         .subcall = (void*)wsreq_subcall
112 };
113
114 /* the interface for events */
115 static const struct afb_evt_itf evt_itf = {
116         .broadcast = (void*)aws_on_event,
117         .push = (void*)aws_on_event
118 };
119
120 /***************************************************************
121 ****************************************************************
122 **
123 **  functions of afb_ws_json1 / afb_wsj1
124 **
125 ****************************************************************
126 ***************************************************************/
127
128 struct afb_ws_json1 *afb_ws_json1_create(int fd, struct afb_context *context, void (*cleanup)(void*), void *cleanup_closure)
129 {
130         struct afb_ws_json1 *result;
131
132         assert(fd >= 0);
133         assert(context != NULL);
134
135         result = malloc(sizeof * result);
136         if (result == NULL)
137                 goto error;
138
139         result->refcount = 1;
140         result->cleanup = cleanup;
141         result->cleanup_closure = cleanup_closure;
142         result->session = afb_session_addref(context->session);
143         result->new_session = context->created != 0;
144         if (result->session == NULL)
145                 goto error2;
146
147         result->wsj1 = afb_wsj1_create(afb_common_get_event_loop(), fd, &wsj1_itf, result);
148         if (result->wsj1 == NULL)
149                 goto error3;
150
151         result->listener = afb_evt_listener_create(&evt_itf, result);
152         if (result->listener == NULL)
153                 goto error4;
154
155         return result;
156
157 error4:
158         afb_wsj1_unref(result->wsj1);
159 error3:
160         afb_session_unref(result->session);
161 error2:
162         free(result);
163 error:
164         close(fd);
165         return NULL;
166 }
167
168 static struct afb_ws_json1 *aws_addref(struct afb_ws_json1 *ws)
169 {
170         ws->refcount++;
171         return ws;
172 }
173
174 static void aws_unref(struct afb_ws_json1 *ws)
175 {
176         if (--ws->refcount == 0) {
177                 afb_evt_listener_unref(ws->listener);
178                 afb_wsj1_unref(ws->wsj1);
179                 if (ws->cleanup != NULL)
180                         ws->cleanup(ws->cleanup_closure);
181                 afb_session_unref(ws->session);
182                 free(ws);
183         }
184 }
185
186 static void aws_on_hangup(struct afb_ws_json1 *ws, struct afb_wsj1 *wsj1)
187 {
188         aws_unref(ws);
189 }
190
191 static void aws_on_call(struct afb_ws_json1 *ws, const char *api, const char *verb, struct afb_wsj1_msg *msg)
192 {
193         struct afb_req r;
194         struct afb_wsreq *wsreq;
195
196         DEBUG("received websocket request for %s/%s: %s", api, verb, afb_wsj1_msg_object_s(msg));
197
198         /* allocate */
199         wsreq = calloc(1, sizeof *wsreq);
200         if (wsreq == NULL) {
201                 afb_wsj1_close(ws->wsj1, 1008, NULL);
202                 return;
203         }
204
205         /* init the context */
206         afb_context_init(&wsreq->context, ws->session, afb_wsj1_msg_token(msg));
207         if (!wsreq->context.invalidated)
208                 wsreq->context.validated = 1;
209         if (ws->new_session != 0) {
210                 wsreq->context.created = 1;
211                 ws->new_session = 0;
212         }
213
214         /* fill and record the request */
215         afb_wsj1_msg_addref(msg);
216         wsreq->msgj1 = msg;
217         wsreq->refcount = 1;
218         wsreq->aws = aws_addref(ws);
219
220         /* emits the call */
221         r.closure = wsreq;
222         r.itf = &afb_ws_json1_req_itf;
223         afb_apis_call(r, &wsreq->context, api, verb);
224         wsreq_unref(wsreq);
225 }
226
227 static void aws_on_event(struct afb_ws_json1 *aws, const char *event, int eventid, struct json_object *object)
228 {
229         afb_wsj1_send_event_j(aws->wsj1, event, afb_msg_json_event(event, object));
230 }
231
232 /***************************************************************
233 ****************************************************************
234 **
235 **  functions of wsreq / afb_req
236 **
237 ****************************************************************
238 ***************************************************************/
239
240 static void wsreq_addref(struct afb_wsreq *wsreq)
241 {
242         wsreq->refcount++;
243 }
244
245 static void wsreq_unref(struct afb_wsreq *wsreq)
246 {
247         if (--wsreq->refcount == 0) {
248                 afb_context_disconnect(&wsreq->context);
249                 afb_wsj1_msg_unref(wsreq->msgj1);
250                 aws_unref(wsreq->aws);
251                 free(wsreq);
252         }
253 }
254
255 static struct json_object *wsreq_json(struct afb_wsreq *wsreq)
256 {
257         return afb_wsj1_msg_object_j(wsreq->msgj1);
258 }
259
260 static struct afb_arg wsreq_get(struct afb_wsreq *wsreq, const char *name)
261 {
262         return afb_msg_json_get_arg(wsreq_json(wsreq), name);
263 }
264
265 static void wsreq_fail(struct afb_wsreq *wsreq, const char *status, const char *info)
266 {
267         int rc;
268         rc = afb_wsj1_reply_error_j(wsreq->msgj1, afb_msg_json_reply_error(status, info, &wsreq->context, NULL), afb_context_sent_token(&wsreq->context));
269         if (rc)
270                 ERROR("Can't send fail reply: %m");
271 }
272
273 static void wsreq_success(struct afb_wsreq *wsreq, json_object *obj, const char *info)
274 {
275         int rc;
276         rc = afb_wsj1_reply_ok_j(wsreq->msgj1, afb_msg_json_reply_ok(info, obj, &wsreq->context, NULL), afb_context_sent_token(&wsreq->context));
277         if (rc)
278                 ERROR("Can't send success reply: %m");
279 }
280
281 static const char *wsreq_raw(struct afb_wsreq *wsreq, size_t *size)
282 {
283         const char *result = afb_wsj1_msg_object_s(wsreq->msgj1);
284         if (size != NULL)
285                 *size = strlen(result);
286         return result;
287 }
288
289 static void wsreq_send(struct afb_wsreq *wsreq, const char *buffer, size_t size)
290 {
291         int rc;
292         rc = afb_wsj1_reply_ok_s(wsreq->msgj1, buffer, afb_context_sent_token(&wsreq->context));
293         if (rc)
294                 ERROR("Can't send raw reply: %m");
295 }
296
297 static int wsreq_subscribe(struct afb_wsreq *wsreq, struct afb_event event)
298 {
299         return afb_evt_add_watch(wsreq->aws->listener, event);
300 }
301
302 static int wsreq_unsubscribe(struct afb_wsreq *wsreq, struct afb_event event)
303 {
304         return afb_evt_remove_watch(wsreq->aws->listener, event);
305 }
306
307 static void wsreq_subcall(struct afb_wsreq *wsreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *closure)
308 {
309         afb_subcall(&wsreq->context, api, verb, args, callback, closure, (struct afb_req){ .itf = &afb_ws_json1_req_itf, .closure = wsreq });
310 }
311