2 * Copyright (C) 2015-2018 "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.
28 #include <sys/types.h>
29 #include <sys/socket.h>
33 #include <json-c/json.h>
35 #include <afb/afb-event-x2.h>
37 #include "afb-session.h"
40 #include "afb-apiset.h"
41 #include "afb-proto-ws.h"
42 #include "afb-stub-ws.h"
43 #include "afb-context.h"
54 * structure for a ws request
57 struct afb_xreq xreq; /* the xreq */
58 struct afb_stub_ws *stubws; /* the client of the request */
59 struct afb_proto_ws_call *call; /* the incoming call */
63 * structure for recording events on client side
67 struct client_event *next;
68 struct afb_event_x2 *event;
74 * structure for recording describe requests
76 struct client_describe
78 struct afb_stub_ws *stubws;
79 struct jobloop *jobloop;
80 struct json_object *result;
84 * structure for jobs of describing
86 struct server_describe
88 struct afb_stub_ws *stubws;
89 struct afb_proto_ws_describe *describe;
93 * structure for recording sessions
97 struct server_session *next;
98 struct afb_session *session;
101 /******************* stub description for client or servers ******************/
105 /* count of references */
108 /* resource control */
109 pthread_mutex_t mutex;
112 struct afb_proto_ws *proto;
114 /* listener for events (server side) */
115 struct afb_evt_listener *listener;
117 /* event replica (client side) */
118 struct client_event *events;
120 /* credentials of the client (server side) */
121 struct afb_cred *cred;
123 /* sessions (server side) */
124 struct server_session *sessions;
127 struct afb_apiset *apiset;
129 /* on hangup callback */
130 void (*on_hangup)(struct afb_stub_ws *);
136 /******************* ws request part for server *****************/
138 /* decrement the reference count of the request and free/release it on falling to null */
139 static void server_req_destroy_cb(struct afb_xreq *xreq)
141 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
143 afb_context_disconnect(&wreq->xreq.context);
144 afb_cred_unref(wreq->xreq.cred);
145 json_object_put(wreq->xreq.json);
146 afb_proto_ws_call_unref(wreq->call);
147 afb_stub_ws_unref(wreq->stubws);
151 static void server_req_reply_cb(struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info)
154 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
156 rc = afb_proto_ws_call_reply(wreq->call, obj, error, info);
158 ERROR("error while sending reply");
159 json_object_put(obj);
162 static int server_req_subscribe_cb(struct afb_xreq *xreq, struct afb_event_x2 *event)
165 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
167 rc = afb_evt_event_x2_add_watch(wreq->stubws->listener, event);
169 rc = afb_proto_ws_call_subscribe(wreq->call, afb_evt_event_x2_fullname(event), afb_evt_event_x2_id(event));
171 ERROR("error while subscribing event");
175 static int server_req_unsubscribe_cb(struct afb_xreq *xreq, struct afb_event_x2 *event)
178 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
180 rc = afb_proto_ws_call_unsubscribe(wreq->call, afb_evt_event_x2_fullname(event), afb_evt_event_x2_id(event));
181 rc2 = afb_evt_event_x2_remove_watch(wreq->stubws->listener, event);
182 if (rc >= 0 && rc2 < 0)
185 ERROR("error while unsubscribing event");
189 static const struct afb_xreq_query_itf server_req_xreq_itf = {
190 .reply = server_req_reply_cb,
191 .unref = server_req_destroy_cb,
192 .subscribe = server_req_subscribe_cb,
193 .unsubscribe = server_req_unsubscribe_cb
196 /******************* client part **********************************/
198 /* search the event */
199 static struct client_event *client_event_search(struct afb_stub_ws *stubws, uint32_t eventid, const char *name)
201 struct client_event *ev;
204 while (ev != NULL && (ev->id != eventid || 0 != strcmp(afb_evt_event_x2_fullname(ev->event), name)))
210 /* on call, propagate it to the ws service */
211 static void client_call_cb(void * closure, struct afb_xreq *xreq)
213 struct afb_stub_ws *stubws = closure;
215 afb_proto_ws_client_call(
217 xreq->request.called_verb,
219 afb_session_uuid(xreq->context.session),
221 xreq_on_behalf_cred_export(xreq));
222 afb_xreq_unhooked_addref(xreq);
225 static void client_on_description_cb(void *closure, struct json_object *data)
227 struct client_describe *desc = closure;
230 jobs_leave(desc->jobloop);
233 static void client_send_describe_cb(int signum, void *closure, struct jobloop *jobloop)
235 struct client_describe *desc = closure;
240 desc->jobloop = jobloop;
241 afb_proto_ws_client_describe(desc->stubws->proto, client_on_description_cb, desc);
245 /* get the description */
246 static struct json_object *client_describe_cb(void * closure)
248 struct client_describe desc;
250 /* synchronous job: send the request and wait its result */
251 desc.stubws = closure;
253 jobs_enter(NULL, 0, client_send_describe_cb, &desc);
257 /******************* server part: manage events **********************************/
259 static void server_event_add(void *closure, const char *event, int eventid)
261 struct afb_stub_ws *stubws = closure;
263 afb_proto_ws_server_event_create(stubws->proto, event, eventid);
266 static void server_event_remove(void *closure, const char *event, int eventid)
268 struct afb_stub_ws *stubws = closure;
270 afb_proto_ws_server_event_remove(stubws->proto, event, eventid);
273 static void server_event_push(void *closure, const char *event, int eventid, struct json_object *object)
275 struct afb_stub_ws *stubws = closure;
277 afb_proto_ws_server_event_push(stubws->proto, event, eventid, object);
278 json_object_put(object);
281 static void server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object)
283 struct afb_stub_ws *stubws = closure;
285 afb_proto_ws_server_event_broadcast(stubws->proto, event, object);
286 json_object_put(object);
289 /*****************************************************/
291 static void on_reply(void *closure, void *request, struct json_object *object, const char *error, const char *info)
293 struct afb_xreq *xreq = request;
295 afb_xreq_reply(xreq, object, error, info);
296 afb_xreq_unhooked_unref(xreq);
299 static void on_event_create(void *closure, const char *event_name, int event_id)
301 struct afb_stub_ws *stubws = closure;
302 struct client_event *ev;
304 /* check conflicts */
305 ev = client_event_search(stubws, event_id, event_name);
311 /* no conflict, try to add it */
312 ev = malloc(sizeof *ev);
314 ev->event = afb_evt_event_x2_create(event_name);
315 if (ev->event != NULL) {
318 ev->next = stubws->events;
324 ERROR("can't create event %s, out of memory", event_name);
327 static void on_event_remove(void *closure, const char *event_name, int event_id)
329 struct afb_stub_ws *stubws = closure;
330 struct client_event *ev, **prv;
332 /* check conflicts */
333 ev = client_event_search(stubws, event_id, event_name);
337 /* decrease the reference count */
341 /* unlinks the event */
342 prv = &stubws->events;
347 /* destroys the event */
348 afb_evt_event_x2_unref(ev->event);
352 static void on_event_subscribe(void *closure, void *request, const char *event_name, int event_id)
354 struct afb_stub_ws *stubws = closure;
355 struct afb_xreq *xreq = request;
356 struct client_event *ev;
358 /* check conflicts */
359 ev = client_event_search(stubws, event_id, event_name);
363 if (afb_xreq_subscribe(xreq, ev->event) < 0)
364 ERROR("can't subscribe: %m");
367 static void on_event_unsubscribe(void *closure, void *request, const char *event_name, int event_id)
369 struct afb_stub_ws *stubws = closure;
370 struct afb_xreq *xreq = request;
371 struct client_event *ev;
373 /* check conflicts */
374 ev = client_event_search(stubws, event_id, event_name);
378 if (afb_xreq_unsubscribe(xreq, ev->event) < 0)
379 ERROR("can't unsubscribe: %m");
382 static void on_event_push(void *closure, const char *event_name, int event_id, struct json_object *data)
384 struct afb_stub_ws *stubws = closure;
385 struct client_event *ev;
387 /* check conflicts */
388 ev = client_event_search(stubws, event_id, event_name);
390 afb_evt_event_x2_push(ev->event, data);
392 ERROR("unreadable push event");
395 static void on_event_broadcast(void *closure, const char *event_name, struct json_object *data)
397 afb_evt_broadcast(event_name, data);
400 /*****************************************************/
402 static void record_session(struct afb_stub_ws *stubws, struct afb_session *session)
404 struct server_session *s, **prv;
407 prv = &stubws->sessions;
409 if (s->session == session)
411 if (afb_session_is_closed(s->session)) {
413 afb_session_unref(s->session);
421 s = malloc(sizeof *s);
423 s->session = afb_session_addref(session);
424 s->next = stubws->sessions;
425 stubws->sessions = s;
429 static void release_all_sessions(struct afb_stub_ws *stubws)
431 struct server_session *s, *n;
433 s = stubws->sessions;
434 stubws->sessions = NULL;
437 afb_session_unref(s->session);
443 /*****************************************************/
445 static void on_call(void *closure, struct afb_proto_ws_call *call, const char *verb, struct json_object *args, const char *sessionid, const char *user_creds)
447 struct afb_stub_ws *stubws = closure;
448 struct server_req *wreq;
450 afb_stub_ws_addref(stubws);
452 /* create the request */
453 wreq = malloc(sizeof *wreq);
457 afb_xreq_init(&wreq->xreq, &server_req_xreq_itf);
458 wreq->stubws = stubws;
461 /* init the context */
462 if (afb_context_connect(&wreq->xreq.context, sessionid, NULL) < 0)
464 wreq->xreq.context.validated = 1;
465 record_session(stubws, wreq->xreq.context.session);
466 if (wreq->xreq.context.created)
467 afb_session_set_autoclose(wreq->xreq.context.session, 1);
470 wreq->xreq.cred = afb_cred_mixed_on_behalf_import(stubws->cred, sessionid, user_creds);
471 wreq->xreq.request.called_api = stubws->apiname;
472 wreq->xreq.request.called_verb = verb;
473 wreq->xreq.json = args;
474 afb_xreq_process(&wreq->xreq, stubws->apiset);
480 json_object_put(args);
481 afb_stub_ws_unref(stubws);
482 afb_proto_ws_call_reply(call, NULL, "internal-error", NULL);
483 afb_proto_ws_call_unref(call);
486 static void server_describe_sjob(int signum, void *closure)
488 struct json_object *obj;
489 struct server_describe *desc = closure;
491 /* get the description if possible */
492 obj = !signum ? afb_apiset_describe(desc->stubws->apiset, desc->stubws->apiname) : NULL;
495 afb_proto_ws_describe_put(desc->describe, obj);
496 json_object_put(obj);
497 afb_stub_ws_unref(desc->stubws);
500 static void server_describe_job(int signum, void *closure)
502 server_describe_sjob(signum, closure);
506 static void on_describe(void *closure, struct afb_proto_ws_describe *describe)
508 struct server_describe *desc, sdesc;
509 struct afb_stub_ws *stubws = closure;
511 /* allocate (if possible) and init */
512 desc = malloc(sizeof *desc);
515 desc->stubws = stubws;
516 desc->describe = describe;
517 afb_stub_ws_addref(stubws);
521 jobs_call(NULL, 0, server_describe_sjob, desc);
523 if (jobs_queue(NULL, 0, server_describe_job, desc) < 0)
524 jobs_call(NULL, 0, server_describe_job, desc);
528 /*****************************************************/
530 static const struct afb_proto_ws_client_itf client_itf =
532 .on_reply = on_reply,
533 .on_event_create = on_event_create,
534 .on_event_remove = on_event_remove,
535 .on_event_subscribe = on_event_subscribe,
536 .on_event_unsubscribe = on_event_unsubscribe,
537 .on_event_push = on_event_push,
538 .on_event_broadcast = on_event_broadcast,
541 static const struct afb_proto_ws_server_itf server_itf =
544 .on_describe = on_describe
547 static struct afb_api_itf ws_api_itf = {
548 .call = client_call_cb,
549 .describe = client_describe_cb
552 /* the interface for events pushing */
553 static const struct afb_evt_itf server_evt_itf = {
554 .broadcast = server_event_broadcast,
555 .push = server_event_push,
556 .add = server_event_add,
557 .remove = server_event_remove
560 /*****************************************************/
562 static void drop_all_events(struct afb_stub_ws *stubws)
564 struct client_event *ev, *nxt;
567 stubws->events = NULL;
571 afb_evt_event_x2_unref(ev->event);
577 /* callback when receiving a hangup */
578 static void on_hangup(void *closure)
580 struct afb_stub_ws *stubws = closure;
582 afb_stub_ws_addref(stubws);
583 if (stubws->on_hangup)
584 stubws->on_hangup(stubws);
586 release_all_sessions(stubws);
587 afb_stub_ws_unref(stubws);
590 static int enqueue_processing(void (*callback)(int signum, void* arg), void *arg)
592 return jobs_queue(NULL, 0, callback, arg);
595 /*****************************************************/
597 static struct afb_stub_ws *afb_stub_ws_create(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset, int client)
599 struct afb_stub_ws *stubws;
601 stubws = calloc(1, sizeof *stubws + strlen(apiname));
606 stubws->proto = afb_proto_ws_create_client(fdev, &client_itf, stubws);
608 stubws->proto = afb_proto_ws_create_server(fdev, &server_itf, stubws);
611 strcpy(stubws->apiname, apiname);
612 stubws->apiset = afb_apiset_addref(apiset);
613 stubws->refcount = 1;
614 afb_proto_ws_on_hangup(stubws->proto, on_hangup);
615 afb_proto_ws_set_queuing(stubws->proto, enqueue_processing);
624 struct afb_stub_ws *afb_stub_ws_create_client(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset)
626 return afb_stub_ws_create(fdev, apiname, apiset, 1);
629 struct afb_stub_ws *afb_stub_ws_create_server(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset)
631 struct afb_stub_ws *stubws;
633 stubws = afb_stub_ws_create(fdev, apiname, apiset, 0);
635 stubws->cred = afb_cred_create_for_socket(fdev_fd(fdev));
636 stubws->listener = afb_evt_listener_create(&server_evt_itf, stubws);
637 if (stubws->listener != NULL)
639 afb_stub_ws_unref(stubws);
644 void afb_stub_ws_unref(struct afb_stub_ws *stubws)
646 if (!__atomic_sub_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED)) {
647 drop_all_events(stubws);
648 if (stubws->listener)
649 afb_evt_listener_unref(stubws->listener);
650 release_all_sessions(stubws);
651 afb_proto_ws_unref(stubws->proto);
652 afb_cred_unref(stubws->cred);
653 afb_apiset_unref(stubws->apiset);
658 void afb_stub_ws_addref(struct afb_stub_ws *stubws)
660 __atomic_add_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED);
663 void afb_stub_ws_set_on_hangup(struct afb_stub_ws *stubws, void (*on_hangup)(struct afb_stub_ws*))
665 stubws->on_hangup = on_hangup;
668 const char *afb_stub_ws_name(struct afb_stub_ws *stubws)
670 return stubws->apiname;
673 struct afb_api_item afb_stub_ws_client_api(struct afb_stub_ws *stubws)
675 struct afb_api_item api;
677 assert(!stubws->listener); /* check client */
678 api.closure = stubws;
679 api.itf = &ws_api_itf;
684 int afb_stub_ws_client_add(struct afb_stub_ws *stubws, struct afb_apiset *apiset)
686 return afb_apiset_add(apiset, stubws->apiname, afb_stub_ws_client_api(stubws));