Definitive switch to internal's xreq
[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-xreq.h"
37 #include "afb-context.h"
38 #include "afb-evt.h"
39 #include "afb-subcall.h"
40 #include "verbose.h"
41
42 /* predeclaration of structures */
43 struct afb_ws_json1;
44 struct afb_wsreq;
45
46 /* predeclaration of websocket callbacks */
47 static void aws_on_hangup(struct afb_ws_json1 *ws, struct afb_wsj1 *wsj1);
48 static void aws_on_call(struct afb_ws_json1 *ws, const char *api, const char *verb, struct afb_wsj1_msg *msg);
49 static void aws_on_event(struct afb_ws_json1 *ws, const char *event, int eventid, struct json_object *object);
50
51 /* predeclaration of wsreq callbacks */
52 static void wsreq_destroy(struct afb_wsreq *wsreq);
53 static struct json_object *wsreq_json(struct afb_wsreq *wsreq);
54 static void wsreq_reply(struct afb_wsreq *wsreq, int iserror, json_object *obj);
55
56 /* declaration of websocket structure */
57 struct afb_ws_json1
58 {
59         int refcount;
60         void (*cleanup)(void*);
61         void *cleanup_closure;
62         struct afb_session *session;
63         struct afb_evt_listener *listener;
64         struct afb_wsj1 *wsj1;
65         int new_session;
66 };
67
68 /* declaration of wsreq structure */
69 struct afb_wsreq
70 {
71         struct afb_xreq xreq;
72         struct afb_ws_json1 *aws;
73         struct afb_wsreq *next;
74         struct afb_wsj1_msg *msgj1;
75 };
76
77 /* interface for afb_ws_json1 / afb_wsj1 */
78 static struct afb_wsj1_itf wsj1_itf = {
79         .on_hangup = (void*)aws_on_hangup,
80         .on_call = (void*)aws_on_call
81 };
82
83 /* interface for xreq */
84 const struct afb_xreq_query_itf afb_ws_json1_xreq_itf = {
85         .json = (void*)wsreq_json,
86         .reply = (void*)wsreq_reply,
87         .unref = (void*)wsreq_destroy
88 };
89
90 /* the interface for events */
91 static const struct afb_evt_itf evt_itf = {
92         .broadcast = (void*)aws_on_event,
93         .push = (void*)aws_on_event
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(int fd, struct afb_context *context, void (*cleanup)(void*), void *cleanup_closure)
105 {
106         struct afb_ws_json1 *result;
107
108         assert(fd >= 0);
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(afb_common_get_event_loop(), fd, &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         return result;
132
133 error4:
134         afb_wsj1_unref(result->wsj1);
135 error3:
136         afb_session_unref(result->session);
137 error2:
138         free(result);
139 error:
140         close(fd);
141         return NULL;
142 }
143
144 static struct afb_ws_json1 *aws_addref(struct afb_ws_json1 *ws)
145 {
146         ws->refcount++;
147         return ws;
148 }
149
150 static void aws_unref(struct afb_ws_json1 *ws)
151 {
152         if (--ws->refcount == 0) {
153                 afb_evt_listener_unref(ws->listener);
154                 afb_wsj1_unref(ws->wsj1);
155                 if (ws->cleanup != NULL)
156                         ws->cleanup(ws->cleanup_closure);
157                 afb_session_unref(ws->session);
158                 free(ws);
159         }
160 }
161
162 static void aws_on_hangup(struct afb_ws_json1 *ws, struct afb_wsj1 *wsj1)
163 {
164         aws_unref(ws);
165 }
166
167 static void aws_on_call(struct afb_ws_json1 *ws, const char *api, const char *verb, struct afb_wsj1_msg *msg)
168 {
169         struct afb_wsreq *wsreq;
170
171         DEBUG("received websocket request for %s/%s: %s", api, verb, afb_wsj1_msg_object_s(msg));
172
173         /* allocate */
174         wsreq = calloc(1, sizeof *wsreq);
175         if (wsreq == NULL) {
176                 afb_wsj1_close(ws->wsj1, 1008, NULL);
177                 return;
178         }
179
180         /* init the context */
181         afb_context_init(&wsreq->xreq.context, ws->session, afb_wsj1_msg_token(msg));
182         if (!wsreq->xreq.context.invalidated)
183                 wsreq->xreq.context.validated = 1;
184         if (ws->new_session != 0) {
185                 wsreq->xreq.context.created = 1;
186                 ws->new_session = 0;
187         }
188
189         /* fill and record the request */
190         afb_wsj1_msg_addref(msg);
191         wsreq->msgj1 = msg;
192         wsreq->xreq.refcount = 1;
193         wsreq->xreq.query = wsreq;
194         wsreq->xreq.queryitf = &afb_ws_json1_xreq_itf;
195         wsreq->xreq.api = api;
196         wsreq->xreq.verb = verb;
197         wsreq->aws = aws_addref(ws);
198         wsreq->xreq.listener = wsreq->aws->listener;
199
200         /* emits the call */
201         afb_apis_call(&wsreq->xreq);
202         afb_xreq_unref(&wsreq->xreq);
203 }
204
205 static void aws_on_event(struct afb_ws_json1 *aws, const char *event, int eventid, struct json_object *object)
206 {
207         afb_wsj1_send_event_j(aws->wsj1, event, afb_msg_json_event(event, object));
208 }
209
210 /***************************************************************
211 ****************************************************************
212 **
213 **  functions of wsreq / afb_req
214 **
215 ****************************************************************
216 ***************************************************************/
217
218 static void wsreq_destroy(struct afb_wsreq *wsreq)
219 {
220         afb_context_disconnect(&wsreq->xreq.context);
221         afb_wsj1_msg_unref(wsreq->msgj1);
222         aws_unref(wsreq->aws);
223         free(wsreq);
224 }
225
226 static struct json_object *wsreq_json(struct afb_wsreq *wsreq)
227 {
228         return afb_wsj1_msg_object_j(wsreq->msgj1);
229 }
230
231 static void wsreq_reply(struct afb_wsreq *wsreq, int iserror, json_object *obj)
232 {
233         int rc;
234         rc = (iserror ? afb_wsj1_reply_error_j : afb_wsj1_reply_ok_j)(
235                         wsreq->msgj1, obj, afb_context_sent_token(&wsreq->xreq.context));
236         if (rc)
237                 ERROR("Can't send reply: %m (was %s)", json_object_get_string(obj));
238 }
239