4f5cb868eba3aa779944f6d42d1d66773d98afc4
[src/app-framework-binder.git] / src / afb-ws-json1.c
1 /*
2  * Copyright (C) 2016, 2017, 2018 "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-systemd.h"
31 #include "afb-msg-json.h"
32 #include "afb-session.h"
33 #include "afb-cred.h"
34 #include "afb-apiset.h"
35 #include "afb-xreq.h"
36 #include "afb-context.h"
37 #include "afb-evt.h"
38 #include "verbose.h"
39 #include "fdev.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_cb(void *closure, struct afb_wsj1 *wsj1);
47 static void aws_on_call_cb(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg);
48 static void aws_on_push_cb(void *closure, const char *event, int eventid, struct json_object *object);
49 static void aws_on_broadcast_cb(void *closure, const char *event, struct json_object *object);
50
51 /* predeclaration of wsreq callbacks */
52 static void wsreq_destroy(struct afb_xreq *xreq);
53 static void wsreq_reply(struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info);
54
55 /* declaration of websocket structure */
56 struct afb_ws_json1
57 {
58         int refcount;
59         void (*cleanup)(void*);
60         void *cleanup_closure;
61         struct afb_session *session;
62         struct afb_evt_listener *listener;
63         struct afb_wsj1 *wsj1;
64         struct afb_cred *cred;
65         struct afb_apiset *apiset;
66         int new_session;
67 };
68
69 /* declaration of wsreq structure */
70 struct afb_wsreq
71 {
72         struct afb_xreq xreq;
73         struct afb_ws_json1 *aws;
74         struct afb_wsreq *next;
75         struct afb_wsj1_msg *msgj1;
76 };
77
78 /* interface for afb_ws_json1 / afb_wsj1 */
79 static struct afb_wsj1_itf wsj1_itf = {
80         .on_hangup = aws_on_hangup_cb,
81         .on_call = aws_on_call_cb
82 };
83
84 /* interface for xreq */
85 const struct afb_xreq_query_itf afb_ws_json1_xreq_itf = {
86         .reply = wsreq_reply,
87         .unref = wsreq_destroy
88 };
89
90 /* the interface for events */
91 static const struct afb_evt_itf evt_itf = {
92         .broadcast = aws_on_broadcast_cb,
93         .push = aws_on_push_cb
94 };
95
96 /***************************************************************
97 ****************************************************************
98 **
99 **  functions of afb_ws_json1 / afb_wsj1
100 **
101 ****************************************************************
102 ***************************************************************/
103
104 struct afb_ws_json1 *afb_ws_json1_create(struct fdev *fdev, struct afb_apiset *apiset, struct afb_context *context, void (*cleanup)(void*), void *cleanup_closure)
105 {
106         struct afb_ws_json1 *result;
107
108         assert(fdev);
109         assert(context != NULL);
110
111         result = malloc(sizeof * result);
112         if (result == NULL)
113                 goto error;
114
115         result->refcount = 1;
116         result->cleanup = cleanup;
117         result->cleanup_closure = cleanup_closure;
118         result->session = afb_session_addref(context->session);
119         result->new_session = context->created != 0;
120         if (result->session == NULL)
121                 goto error2;
122
123         result->wsj1 = afb_wsj1_create(fdev, &wsj1_itf, result);
124         if (result->wsj1 == NULL)
125                 goto error3;
126
127         result->listener = afb_evt_listener_create(&evt_itf, result);
128         if (result->listener == NULL)
129                 goto error4;
130
131         result->cred = afb_cred_create_for_socket(fdev_fd(fdev));
132         result->apiset = afb_apiset_addref(apiset);
133         return result;
134
135 error4:
136         afb_wsj1_unref(result->wsj1);
137 error3:
138         afb_session_unref(result->session);
139 error2:
140         free(result);
141 error:
142         fdev_unref(fdev);
143         return NULL;
144 }
145
146 struct afb_ws_json1 *afb_ws_json1_addref(struct afb_ws_json1 *ws)
147 {
148         __atomic_add_fetch(&ws->refcount, 1, __ATOMIC_RELAXED);
149         return ws;
150 }
151
152 void afb_ws_json1_unref(struct afb_ws_json1 *ws)
153 {
154         if (!__atomic_sub_fetch(&ws->refcount, 1, __ATOMIC_RELAXED)) {
155                 afb_evt_listener_unref(ws->listener);
156                 afb_wsj1_unref(ws->wsj1);
157                 if (ws->cleanup != NULL)
158                         ws->cleanup(ws->cleanup_closure);
159                 afb_session_unref(ws->session);
160                 afb_cred_unref(ws->cred);
161                 afb_apiset_unref(ws->apiset);
162                 free(ws);
163         }
164 }
165
166 static void aws_on_hangup_cb(void *closure, struct afb_wsj1 *wsj1)
167 {
168         struct afb_ws_json1 *ws = closure;
169         afb_ws_json1_unref(ws);
170 }
171
172 static void aws_on_call_cb(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
173 {
174         struct afb_ws_json1 *ws = closure;
175         struct afb_wsreq *wsreq;
176
177         DEBUG("received websocket request for %s/%s: %s", api, verb, afb_wsj1_msg_object_s(msg));
178
179         /* allocate */
180         wsreq = calloc(1, sizeof *wsreq);
181         if (wsreq == NULL) {
182                 afb_wsj1_close(ws->wsj1, 1008, NULL);
183                 return;
184         }
185
186         /* init the context */
187         afb_xreq_init(&wsreq->xreq, &afb_ws_json1_xreq_itf);
188         afb_context_init(&wsreq->xreq.context, ws->session, afb_wsj1_msg_token(msg));
189         if (!wsreq->xreq.context.invalidated)
190                 wsreq->xreq.context.validated = 1;
191         if (ws->new_session != 0) {
192                 wsreq->xreq.context.created = 1;
193                 ws->new_session = 0;
194         }
195
196         /* fill and record the request */
197         afb_wsj1_msg_addref(msg);
198         wsreq->msgj1 = msg;
199         wsreq->xreq.cred = afb_cred_addref(ws->cred);
200         wsreq->xreq.request.called_api = api;
201         wsreq->xreq.request.called_verb = verb;
202         wsreq->xreq.json = afb_wsj1_msg_object_j(wsreq->msgj1);
203         wsreq->aws = afb_ws_json1_addref(ws);
204         wsreq->xreq.listener = wsreq->aws->listener;
205
206         /* emits the call */
207         afb_xreq_process(&wsreq->xreq, ws->apiset);
208 }
209
210 static void aws_on_push_cb(void *closure, const char *event, int eventid, struct json_object *object)
211 {
212         aws_on_broadcast_cb(closure, event, object);
213 }
214
215 static void aws_on_broadcast_cb(void *closure, const char *event, struct json_object *object)
216 {
217         struct afb_ws_json1 *aws = closure;
218         afb_wsj1_send_event_j(aws->wsj1, event, afb_msg_json_event(event, object));
219 }
220
221 /***************************************************************
222 ****************************************************************
223 **
224 **  functions of wsreq / afb_req
225 **
226 ****************************************************************
227 ***************************************************************/
228
229 static void wsreq_destroy(struct afb_xreq *xreq)
230 {
231         struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
232
233         afb_context_disconnect(&wsreq->xreq.context);
234         afb_wsj1_msg_unref(wsreq->msgj1);
235         afb_cred_unref(wsreq->xreq.cred);
236         afb_ws_json1_unref(wsreq->aws);
237         free(wsreq);
238 }
239
240 static void wsreq_reply(struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info)
241 {
242         struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
243         int rc;
244         struct json_object *reply;
245
246         /* create the reply */
247         reply = afb_msg_json_reply(object, error, info, &xreq->context);
248
249         rc = (error ? afb_wsj1_reply_error_j : afb_wsj1_reply_ok_j)(
250                         wsreq->msgj1, reply, afb_context_sent_token(&wsreq->xreq.context));
251         if (rc)
252                 ERROR("Can't send reply: %m");
253 }
254