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: requests on server side
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; /**< link to the next */
68 struct afb_event_x2 *event; /**< the local event */
69 int id; /**< the identifier */
70 int refcount; /**< a reference count */
74 * structure for recording describe requests on the client side
76 struct client_describe
78 struct afb_stub_ws *stubws; /**< the stub */
79 struct jobloop *jobloop; /**< the jobloop to leave */
80 struct json_object *result; /**< 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 ******************/
106 struct afb_proto_ws *proto;
109 struct afb_apiset *apiset;
111 /* on hangup callback */
112 void (*on_hangup)(struct afb_stub_ws *);
117 /* listener for events */
118 struct afb_evt_listener *listener;
121 struct server_session *sessions;
123 /* credentials of the client */
124 struct afb_cred *cred;
130 struct client_event *events;
134 struct fdev *(*reopen)(void*);
136 void (*release)(void*);
141 /* count of references */
144 /* type of the stub: 0=server, 1=client */
151 static struct afb_proto_ws *afb_stub_ws_create_proto(struct afb_stub_ws *stubws, struct fdev *fdev, uint8_t server);
153 /******************* ws request part for server *****************/
155 /* decrement the reference count of the request and free/release it on falling to null */
156 static void server_req_destroy_cb(struct afb_xreq *xreq)
158 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
160 afb_context_disconnect(&wreq->xreq.context);
161 afb_cred_unref(wreq->xreq.cred);
162 json_object_put(wreq->xreq.json);
163 afb_proto_ws_call_unref(wreq->call);
164 afb_stub_ws_unref(wreq->stubws);
168 static void server_req_reply_cb(struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info)
171 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
173 rc = afb_proto_ws_call_reply(wreq->call, obj, error, info);
175 ERROR("error while sending reply");
176 json_object_put(obj);
179 static int server_req_subscribe_cb(struct afb_xreq *xreq, struct afb_event_x2 *event)
182 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
184 rc = afb_evt_event_x2_add_watch(wreq->stubws->listener, event);
186 rc = afb_proto_ws_call_subscribe(wreq->call, afb_evt_event_x2_fullname(event), afb_evt_event_x2_id(event));
188 ERROR("error while subscribing event");
192 static int server_req_unsubscribe_cb(struct afb_xreq *xreq, struct afb_event_x2 *event)
195 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
197 rc = afb_proto_ws_call_unsubscribe(wreq->call, afb_evt_event_x2_fullname(event), afb_evt_event_x2_id(event));
198 rc2 = afb_evt_event_x2_remove_watch(wreq->stubws->listener, event);
199 if (rc >= 0 && rc2 < 0)
202 ERROR("error while unsubscribing event");
206 static const struct afb_xreq_query_itf server_req_xreq_itf = {
207 .reply = server_req_reply_cb,
208 .unref = server_req_destroy_cb,
209 .subscribe = server_req_subscribe_cb,
210 .unsubscribe = server_req_unsubscribe_cb
213 /******************* client part **********************************/
215 /* destroy all events */
216 static void client_drop_all_events(struct afb_stub_ws *stubws)
218 struct client_event *ev, *nxt;
220 nxt = __atomic_exchange_n(&stubws->events, NULL, __ATOMIC_RELAXED);
224 afb_evt_event_x2_unref(ev->event);
229 /* search the event */
230 static struct client_event *client_event_search(struct afb_stub_ws *stubws, uint32_t eventid, const char *name)
232 struct client_event *ev;
235 while (ev != NULL && (ev->id != eventid || 0 != strcmp(afb_evt_event_x2_fullname(ev->event), name)))
241 static struct afb_proto_ws *client_get_proto(struct afb_stub_ws *stubws)
244 struct afb_proto_ws *proto;
246 proto = stubws->proto;
247 if (proto == NULL && stubws->robust.reopen) {
248 fdev = stubws->robust.reopen(stubws->robust.closure);
250 proto = afb_stub_ws_create_proto(stubws, fdev, 0);
255 /* on call, propagate it to the ws service */
256 static void client_api_call_cb(void * closure, struct afb_xreq *xreq)
259 struct afb_stub_ws *stubws = closure;
260 struct afb_proto_ws *proto;
262 proto = client_get_proto(stubws);
264 afb_xreq_reply(xreq, NULL, "disconnected", "server hung up");
268 rc = afb_proto_ws_client_call(
270 xreq->request.called_verb,
272 afb_session_uuid(xreq->context.session),
274 xreq_on_behalf_cred_export(xreq));
276 afb_xreq_unhooked_addref(xreq);
278 afb_xreq_reply(xreq, NULL, "internal", "can't send message");
281 static void client_on_description_cb(void *closure, struct json_object *data)
283 struct client_describe *desc = closure;
286 jobs_leave(desc->jobloop);
289 static void client_send_describe_cb(int signum, void *closure, struct jobloop *jobloop)
291 struct client_describe *desc = closure;
292 struct afb_proto_ws *proto;
294 proto = client_get_proto(desc->stubws);
295 if (signum || proto == NULL)
298 desc->jobloop = jobloop;
299 afb_proto_ws_client_describe(proto, client_on_description_cb, desc);
303 /* get the description */
304 static struct json_object *client_api_describe_cb(void * closure)
306 struct client_describe desc;
308 /* synchronous job: send the request and wait its result */
309 desc.stubws = closure;
311 jobs_enter(NULL, 0, client_send_describe_cb, &desc);
315 /******************* server part: manage events **********************************/
317 static void server_event_add_cb(void *closure, const char *event, int eventid)
319 struct afb_stub_ws *stubws = closure;
321 if (stubws->proto != NULL)
322 afb_proto_ws_server_event_create(stubws->proto, event, eventid);
325 static void server_event_remove_cb(void *closure, const char *event, int eventid)
327 struct afb_stub_ws *stubws = closure;
329 if (stubws->proto != NULL)
330 afb_proto_ws_server_event_remove(stubws->proto, event, eventid);
333 static void server_event_push_cb(void *closure, const char *event, int eventid, struct json_object *object)
335 struct afb_stub_ws *stubws = closure;
337 if (stubws->proto != NULL)
338 afb_proto_ws_server_event_push(stubws->proto, event, eventid, object);
339 json_object_put(object);
342 static void server_event_broadcast_cb(void *closure, const char *event, int eventid, struct json_object *object)
344 struct afb_stub_ws *stubws = closure;
346 if (stubws->proto != NULL)
347 afb_proto_ws_server_event_broadcast(stubws->proto, event, object);
348 json_object_put(object);
351 /*****************************************************/
353 static void client_on_reply_cb(void *closure, void *request, struct json_object *object, const char *error, const char *info)
355 struct afb_xreq *xreq = request;
357 afb_xreq_reply(xreq, object, error, info);
358 afb_xreq_unhooked_unref(xreq);
361 static void client_on_event_create_cb(void *closure, const char *event_name, int event_id)
363 struct afb_stub_ws *stubws = closure;
364 struct client_event *ev;
366 /* check conflicts */
367 ev = client_event_search(stubws, event_id, event_name);
369 __atomic_add_fetch(&ev->refcount, 1, __ATOMIC_RELAXED);
373 /* no conflict, try to add it */
374 ev = malloc(sizeof *ev);
376 ev->event = afb_evt_event_x2_create(event_name);
377 if (ev->event != NULL) {
380 ev->next = stubws->events;
386 ERROR("can't create event %s, out of memory", event_name);
389 static void client_on_event_remove_cb(void *closure, const char *event_name, int event_id)
391 struct afb_stub_ws *stubws = closure;
392 struct client_event *ev, **prv;
394 /* check conflicts */
395 ev = client_event_search(stubws, event_id, event_name);
399 /* decrease the reference count */
401 if (__atomic_sub_fetch(&ev->refcount, 1, __ATOMIC_RELAXED))
404 /* unlinks the event */
405 prv = &stubws->events;
410 /* destroys the event */
411 afb_evt_event_x2_unref(ev->event);
415 static void client_on_event_subscribe_cb(void *closure, void *request, const char *event_name, int event_id)
417 struct afb_stub_ws *stubws = closure;
418 struct afb_xreq *xreq = request;
419 struct client_event *ev;
421 /* check conflicts */
422 ev = client_event_search(stubws, event_id, event_name);
426 if (afb_xreq_subscribe(xreq, ev->event) < 0)
427 ERROR("can't subscribe: %m");
430 static void client_on_event_unsubscribe_cb(void *closure, void *request, const char *event_name, int event_id)
432 struct afb_stub_ws *stubws = closure;
433 struct afb_xreq *xreq = request;
434 struct client_event *ev;
436 /* check conflicts */
437 ev = client_event_search(stubws, event_id, event_name);
441 if (afb_xreq_unsubscribe(xreq, ev->event) < 0)
442 ERROR("can't unsubscribe: %m");
445 static void client_on_event_push_cb(void *closure, const char *event_name, int event_id, struct json_object *data)
447 struct afb_stub_ws *stubws = closure;
448 struct client_event *ev;
450 /* check conflicts */
451 ev = client_event_search(stubws, event_id, event_name);
453 afb_evt_event_x2_push(ev->event, data);
455 ERROR("unreadable push event");
458 static void client_on_event_broadcast_cb(void *closure, const char *event_name, struct json_object *data)
460 afb_evt_broadcast(event_name, data);
463 /*****************************************************/
465 static void server_record_session(struct afb_stub_ws *stubws, struct afb_session *session)
467 struct server_session *s, **prv;
470 prv = &stubws->sessions;
472 if (s->session == session)
474 if (afb_session_is_closed(s->session)) {
476 afb_session_unref(s->session);
484 s = malloc(sizeof *s);
486 s->session = afb_session_addref(session);
487 s->next = stubws->sessions;
488 stubws->sessions = s;
492 static void server_release_all_sessions(struct afb_stub_ws *stubws)
494 struct server_session *ses, *nses;
496 nses = __atomic_exchange_n(&stubws->sessions, NULL, __ATOMIC_RELAXED);
500 afb_session_unref(ses->session);
505 /*****************************************************/
507 static void server_on_call_cb(void *closure, struct afb_proto_ws_call *call, const char *verb, struct json_object *args, const char *sessionid, const char *user_creds)
509 struct afb_stub_ws *stubws = closure;
510 struct server_req *wreq;
512 afb_stub_ws_addref(stubws);
514 /* create the request */
515 wreq = malloc(sizeof *wreq);
519 afb_xreq_init(&wreq->xreq, &server_req_xreq_itf);
520 wreq->stubws = stubws;
523 /* init the context */
524 if (afb_context_connect(&wreq->xreq.context, sessionid, NULL) < 0)
526 wreq->xreq.context.validated = 1;
527 server_record_session(stubws, wreq->xreq.context.session);
528 if (wreq->xreq.context.created)
529 afb_session_set_autoclose(wreq->xreq.context.session, 1);
532 wreq->xreq.cred = afb_cred_mixed_on_behalf_import(stubws->cred, sessionid, user_creds);
533 wreq->xreq.request.called_api = stubws->apiname;
534 wreq->xreq.request.called_verb = verb;
535 wreq->xreq.json = args;
536 afb_xreq_process(&wreq->xreq, stubws->apiset);
542 json_object_put(args);
543 afb_stub_ws_unref(stubws);
544 afb_proto_ws_call_reply(call, NULL, "internal-error", NULL);
545 afb_proto_ws_call_unref(call);
548 static void server_describe_cb(int signum, void *closure)
550 struct json_object *obj;
551 struct server_describe *desc = closure;
553 /* get the description if possible */
554 obj = !signum ? afb_apiset_describe(desc->stubws->apiset, desc->stubws->apiname) : NULL;
557 afb_proto_ws_describe_put(desc->describe, obj);
558 json_object_put(obj);
559 afb_stub_ws_unref(desc->stubws);
562 static void server_describe_job(int signum, void *closure)
564 server_describe_cb(signum, closure);
568 static void server_on_describe_cb(void *closure, struct afb_proto_ws_describe *describe)
570 struct server_describe *desc, sdesc;
571 struct afb_stub_ws *stubws = closure;
573 /* allocate (if possible) and init */
574 desc = malloc(sizeof *desc);
577 desc->stubws = stubws;
578 desc->describe = describe;
579 afb_stub_ws_addref(stubws);
582 if (desc == &sdesc || jobs_queue(NULL, 0, server_describe_job, desc) < 0)
583 jobs_call(NULL, 0, server_describe_cb, desc);
586 /*****************************************************/
588 static const struct afb_proto_ws_client_itf client_itf =
590 .on_reply = client_on_reply_cb,
591 .on_event_create = client_on_event_create_cb,
592 .on_event_remove = client_on_event_remove_cb,
593 .on_event_subscribe = client_on_event_subscribe_cb,
594 .on_event_unsubscribe = client_on_event_unsubscribe_cb,
595 .on_event_push = client_on_event_push_cb,
596 .on_event_broadcast = client_on_event_broadcast_cb,
599 static struct afb_api_itf client_api_itf = {
600 .call = client_api_call_cb,
601 .describe = client_api_describe_cb
604 static const struct afb_proto_ws_server_itf server_itf =
606 .on_call = server_on_call_cb,
607 .on_describe = server_on_describe_cb
610 /* the interface for events pushing */
611 static const struct afb_evt_itf server_event_itf = {
612 .broadcast = server_event_broadcast_cb,
613 .push = server_event_push_cb,
614 .add = server_event_add_cb,
615 .remove = server_event_remove_cb
618 /*****************************************************/
621 static void disconnect(struct afb_stub_ws *stubws)
623 afb_proto_ws_unref(__atomic_exchange_n(&stubws->proto, NULL, __ATOMIC_RELAXED));
624 if (stubws->is_client)
625 client_drop_all_events(stubws);
627 afb_evt_listener_unref(__atomic_exchange_n(&stubws->listener, NULL, __ATOMIC_RELAXED));
628 afb_cred_unref(__atomic_exchange_n(&stubws->cred, NULL, __ATOMIC_RELAXED));
629 server_release_all_sessions(stubws);
634 /* callback when receiving a hangup */
635 static void on_hangup(void *closure)
637 struct afb_stub_ws *stubws = closure;
640 afb_stub_ws_addref(stubws);
642 if (stubws->on_hangup)
643 stubws->on_hangup(stubws);
644 afb_stub_ws_unref(stubws);
648 static int enqueue_processing(void (*callback)(int signum, void* arg), void *arg)
650 return jobs_queue(NULL, 0, callback, arg);
653 /*****************************************************/
655 static struct afb_proto_ws *afb_stub_ws_create_proto(struct afb_stub_ws *stubws, struct fdev *fdev, uint8_t is_client)
657 struct afb_proto_ws *proto;
659 stubws->proto = proto = is_client
660 ? afb_proto_ws_create_client(fdev, &client_itf, stubws)
661 : afb_proto_ws_create_server(fdev, &server_itf, stubws);
663 afb_proto_ws_on_hangup(proto, on_hangup);
664 afb_proto_ws_set_queuing(proto, enqueue_processing);
670 static struct afb_stub_ws *afb_stub_ws_create(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset, uint8_t is_client)
672 struct afb_stub_ws *stubws;
674 stubws = calloc(1, sizeof *stubws + strlen(apiname));
678 if (afb_stub_ws_create_proto(stubws, fdev, is_client)) {
679 stubws->refcount = 1;
680 stubws->is_client = is_client;
681 strcpy(stubws->apiname, apiname);
682 stubws->apiset = afb_apiset_addref(apiset);
691 struct afb_stub_ws *afb_stub_ws_create_client(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset)
693 return afb_stub_ws_create(fdev, apiname, apiset, 1);
696 struct afb_stub_ws *afb_stub_ws_create_server(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset)
698 struct afb_stub_ws *stubws;
700 stubws = afb_stub_ws_create(fdev, apiname, apiset, 0);
702 stubws->cred = afb_cred_create_for_socket(fdev_fd(fdev));
703 stubws->listener = afb_evt_listener_create(&server_event_itf, stubws);
704 if (stubws->listener != NULL)
706 afb_stub_ws_unref(stubws);
711 void afb_stub_ws_unref(struct afb_stub_ws *stubws)
713 if (stubws && !__atomic_sub_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED)) {
715 if (stubws->is_client) {
716 stubws->robust.reopen = NULL;
717 if (stubws->robust.release)
718 stubws->robust.release(stubws->robust.closure);
722 afb_apiset_unref(stubws->apiset);
727 void afb_stub_ws_addref(struct afb_stub_ws *stubws)
729 __atomic_add_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED);
732 void afb_stub_ws_set_on_hangup(struct afb_stub_ws *stubws, void (*on_hangup)(struct afb_stub_ws*))
734 stubws->on_hangup = on_hangup;
737 const char *afb_stub_ws_name(struct afb_stub_ws *stubws)
739 return stubws->apiname;
742 struct afb_api_item afb_stub_ws_client_api(struct afb_stub_ws *stubws)
744 struct afb_api_item api;
746 assert(stubws->is_client); /* check client */
747 api.closure = stubws;
748 api.itf = &client_api_itf;
749 api.group = stubws; /* serialize for reconnections */
753 int afb_stub_ws_client_add(struct afb_stub_ws *stubws, struct afb_apiset *apiset)
755 return afb_apiset_add(apiset, stubws->apiname, afb_stub_ws_client_api(stubws));
758 void afb_stub_ws_client_robustify(struct afb_stub_ws *stubws, struct fdev *(*reopen)(void*), void *closure, void (*release)(void*))
760 assert(stubws->is_client); /* check client */
762 if (stubws->robust.release)
763 stubws->robust.release(stubws->robust.closure);
765 stubws->robust.reopen = reopen;
766 stubws->robust.closure = closure;
767 stubws->robust.release = release;