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