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