2 * Copyright (C) 2016 "IoT.bzh"
3 * Author: José Bollo <jose.bollo@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include <json-c/json.h>
29 #include "afb-ws-json1.h"
30 #include "afb-msg-json.h"
32 #include <afb/afb-req-itf.h>
34 #include "afb-context.h"
37 static void aws_on_hangup(struct afb_ws_json1 *ws, struct afb_wsj1 *wsj1);
38 static void aws_on_call(struct afb_ws_json1 *ws, const char *api, const char *verb, struct afb_wsj1_msg *msg);
40 static struct afb_wsj1_itf wsj1_itf = {
41 .on_hangup = (void*)aws_on_hangup,
42 .on_call = (void*)aws_on_call
50 void (*cleanup)(void*);
51 void *cleanup_closure;
52 struct AFB_clientCtx *session;
53 struct afb_wsj1 *wsj1;
57 static void aws_send_event(struct afb_ws_json1 *ws, const char *event, struct json_object *object);
59 static const struct afb_event_listener_itf event_listener_itf = {
60 .send = (void*)aws_send_event,
64 static inline struct afb_event_listener listener_for(struct afb_ws_json1 *aws)
66 return (struct afb_event_listener){ .itf = &event_listener_itf, .closure = aws };
69 struct afb_ws_json1 *afb_ws_json1_create(int fd, struct afb_context *context, void (*cleanup)(void*), void *cleanup_closure)
71 struct afb_ws_json1 *result;
74 assert(context != NULL);
76 result = malloc(sizeof * result);
81 result->cleanup = cleanup;
82 result->cleanup_closure = cleanup_closure;
83 result->session = ctxClientAddRef(context->session);
84 result->new_session = context->created != 0;
85 if (result->session == NULL)
88 result->wsj1 = afb_wsj1_create(fd, &wsj1_itf, result);
89 if (result->wsj1 == NULL)
92 if (0 > ctxClientEventListenerAdd(result->session, listener_for(result)))
98 afb_wsj1_unref(result->wsj1);
100 ctxClientUnref(result->session);
108 static struct afb_ws_json1 *aws_addref(struct afb_ws_json1 *ws)
114 static void aws_unref(struct afb_ws_json1 *ws)
116 if (--ws->refcount == 0) {
117 ctxClientEventListenerRemove(ws->session, listener_for(ws));
118 afb_wsj1_unref(ws->wsj1);
119 if (ws->cleanup != NULL)
120 ws->cleanup(ws->cleanup_closure);
121 ctxClientUnref(ws->session);
126 static void aws_on_hangup(struct afb_ws_json1 *ws, struct afb_wsj1 *wsj1)
134 * CAUTION: 'context' field should be the first because there
135 * is an implicit convertion to struct afb_context
137 struct afb_context context;
139 struct afb_ws_json1 *aws;
140 struct afb_wsreq *next;
141 struct afb_wsj1_msg *msgj1;
144 static void wsreq_addref(struct afb_wsreq *wsreq);
145 static void wsreq_unref(struct afb_wsreq *wsreq);
146 static struct json_object *wsreq_json(struct afb_wsreq *wsreq);
147 static struct afb_arg wsreq_get(struct afb_wsreq *wsreq, const char *name);
148 static void wsreq_fail(struct afb_wsreq *wsreq, const char *status, const char *info);
149 static void wsreq_success(struct afb_wsreq *wsreq, struct json_object *obj, const char *info);
150 static const char *wsreq_raw(struct afb_wsreq *wsreq, size_t *size);
151 static void wsreq_send(struct afb_wsreq *wsreq, const char *buffer, size_t size);
154 static const struct afb_req_itf wsreq_itf = {
155 .json = (void*)wsreq_json,
156 .get = (void*)wsreq_get,
157 .success = (void*)wsreq_success,
158 .fail = (void*)wsreq_fail,
159 .raw = (void*)wsreq_raw,
160 .send = (void*)wsreq_send,
161 .context_get = (void*)afb_context_get,
162 .context_set = (void*)afb_context_set,
163 .addref = (void*)wsreq_addref,
164 .unref = (void*)wsreq_unref,
165 .session_close = (void*)afb_context_close,
166 .session_set_LOA = (void*)afb_context_change_loa
169 static void aws_on_call(struct afb_ws_json1 *ws, const char *api, const char *verb, struct afb_wsj1_msg *msg)
172 struct afb_wsreq *wsreq;
174 DEBUG("received websocket request for %s/%s: %s", api, verb, afb_wsj1_msg_object_s(msg));
177 wsreq = calloc(1, sizeof *wsreq);
179 afb_wsj1_close(ws->wsj1, 1008, NULL);
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;
192 /* fill and record the request */
193 afb_wsj1_msg_addref(msg);
196 wsreq->aws = aws_addref(ws);
201 afb_apis_call_(r, &wsreq->context, api, verb);
205 static void wsreq_addref(struct afb_wsreq *wsreq)
210 static void wsreq_unref(struct afb_wsreq *wsreq)
212 if (--wsreq->refcount == 0) {
213 afb_context_disconnect(&wsreq->context);
214 afb_wsj1_msg_unref(wsreq->msgj1);
215 aws_unref(wsreq->aws);
220 static struct json_object *wsreq_json(struct afb_wsreq *wsreq)
222 return afb_wsj1_msg_object_j(wsreq->msgj1);
225 static struct afb_arg wsreq_get(struct afb_wsreq *wsreq, const char *name)
228 struct json_object *value, *root;
230 root = wsreq_json(wsreq);
231 if (json_object_object_get_ex(root, name, &value)) {
233 arg.value = json_object_get_string(value);
242 static void wsreq_fail(struct afb_wsreq *wsreq, const char *status, const char *info)
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));
247 ERROR("Can't send fail reply: %m");
250 static void wsreq_success(struct afb_wsreq *wsreq, json_object *obj, const char *info)
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));
255 ERROR("Can't send success reply: %m");
258 static const char *wsreq_raw(struct afb_wsreq *wsreq, size_t *size)
260 const char *result = afb_wsj1_msg_object_s(wsreq->msgj1);
262 *size = strlen(result);
266 static void wsreq_send(struct afb_wsreq *wsreq, const char *buffer, size_t size)
269 rc = afb_wsj1_reply_ok_s(wsreq->msgj1, buffer, afb_context_sent_token(&wsreq->context));
271 ERROR("Can't send raw reply: %m");
274 static void aws_send_event(struct afb_ws_json1 *aws, const char *event, struct json_object *object)
276 afb_wsj1_send_event_j(aws->wsj1, event, afb_msg_json_event(event, object));