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