2 * Copyright (C) 2015, 2016, 2017 "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.
20 #define NO_PLUGIN_VERBOSE_MACRO
30 #include <sys/types.h>
31 #include <sys/socket.h>
35 #include <json-c/json.h>
37 #include <afb/afb-event.h>
39 #include "afb-common.h"
41 #include "afb-session.h"
44 #include "afb-apiset.h"
45 #include "afb-proto-ws.h"
46 #include "afb-stub-ws.h"
47 #include "afb-context.h"
56 /******************* handling subcalls *****************************/
59 * Structure on server side for recording pending
64 struct server_subcall *next; /**< next subcall for the client */
65 uint32_t subcallid; /**< the subcallid */
66 void (*callback)(void*, int, struct json_object*); /**< callback on completion */
67 void *closure; /**< closure of the callback */
71 * Structure for sending back replies on client side
75 struct afb_stub_ws *stubws; /**< stub descriptor */
76 uint32_t subcallid; /**< subcallid for the reply */
80 * structure for recording calls on client side
83 struct client_call *next; /* the next call */
84 struct afb_stub_ws *stubws; /* the stub_ws */
85 struct afb_xreq *xreq; /* the request handle */
86 uint32_t msgid; /* the message identifier */
90 * structure for a ws request
93 struct afb_xreq xreq; /* the xreq */
94 struct afb_stub_ws *stubws; /* the client of the request */
95 struct afb_proto_ws_call *call; /* the incoming call */
99 * structure for recording events on client side
103 struct client_event *next;
104 struct afb_eventid *eventid;
110 * structure for recording describe requests
112 struct client_describe
114 struct afb_stub_ws *stubws;
115 struct jobloop *jobloop;
116 struct json_object *result;
120 * structure for jobs of describing
122 struct server_describe
124 struct afb_stub_ws *stubws;
125 struct afb_proto_ws_describe *describe;
129 * structure for recording sessions
131 struct server_session
133 struct server_session *next;
134 struct afb_session *session;
137 /******************* stub description for client or servers ******************/
141 /* count of references */
144 /* resource control */
145 pthread_mutex_t mutex;
148 struct afb_proto_ws *proto;
150 /* listener for events (server side) */
151 struct afb_evt_listener *listener;
153 /* event replica (client side) */
154 struct client_event *events;
156 /* credentials (server side) */
157 struct afb_cred *cred;
159 /* sessions (server side) */
160 struct server_session *sessions;
163 struct afb_apiset *apiset;
165 /* on hangup callback */
166 void (*on_hangup)(struct afb_stub_ws *);
172 /******************* ws request part for server *****************/
174 /* decrement the reference count of the request and free/release it on falling to null */
175 static void server_req_destroy_cb(struct afb_xreq *xreq)
177 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
179 afb_context_disconnect(&wreq->xreq.context);
180 afb_cred_unref(wreq->xreq.cred);
181 json_object_put(wreq->xreq.json);
182 afb_proto_ws_call_unref(wreq->call);
183 afb_stub_ws_unref(wreq->stubws);
187 static void server_req_success_cb(struct afb_xreq *xreq, struct json_object *obj, const char *info)
190 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
192 rc = afb_proto_ws_call_success(wreq->call, obj, info);
194 ERROR("error while sending success");
195 json_object_put(obj);
198 static void server_req_fail_cb(struct afb_xreq *xreq, const char *status, const char *info)
201 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
203 rc = afb_proto_ws_call_fail(wreq->call, status, info);
205 ERROR("error while sending fail");
208 static void server_req_subcall_cb(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
211 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
213 rc = afb_proto_ws_call_subcall(wreq->call, api, verb, args, callback, cb_closure);
215 ERROR("error while sending subcall");
218 static int server_req_subscribe_cb(struct afb_xreq *xreq, struct afb_eventid *event)
221 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
223 rc = afb_evt_eventid_add_watch(wreq->stubws->listener, event);
225 rc = afb_proto_ws_call_subscribe(wreq->call, afb_evt_eventid_fullname(event), afb_evt_eventid_id(event));
227 ERROR("error while subscribing event");
231 static int server_req_unsubscribe_cb(struct afb_xreq *xreq, struct afb_eventid *event)
234 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
236 rc = afb_proto_ws_call_unsubscribe(wreq->call, afb_evt_eventid_fullname(event), afb_evt_eventid_id(event));
237 rc2 = afb_evt_eventid_remove_watch(wreq->stubws->listener, event);
238 if (rc >= 0 && rc2 < 0)
241 ERROR("error while unsubscribing event");
245 static const struct afb_xreq_query_itf server_req_xreq_itf = {
246 .success = server_req_success_cb,
247 .fail = server_req_fail_cb,
248 .unref = server_req_destroy_cb,
249 .subcall = server_req_subcall_cb,
250 .subscribe = server_req_subscribe_cb,
251 .unsubscribe = server_req_unsubscribe_cb
254 /******************* client part **********************************/
256 /* search the event */
257 static struct client_event *client_event_search(struct afb_stub_ws *stubws, uint32_t eventid, const char *name)
259 struct client_event *ev;
262 while (ev != NULL && (ev->id != eventid || 0 != strcmp(afb_evt_eventid_fullname(ev->eventid), name)))
268 /* on call, propagate it to the ws service */
269 static void client_call_cb(void * closure, struct afb_xreq *xreq)
271 struct afb_stub_ws *stubws = closure;
273 afb_proto_ws_client_call(stubws->proto, xreq->request.verb, afb_xreq_json(xreq), afb_session_uuid(xreq->context.session), xreq);
274 afb_xreq_unhooked_addref(xreq);
277 static void client_on_description_cb(void *closure, struct json_object *data)
279 struct client_describe *desc = closure;
282 jobs_leave(desc->jobloop);
285 static void client_send_describe_cb(int signum, void *closure, struct jobloop *jobloop)
287 struct client_describe *desc = closure;
292 desc->jobloop = jobloop;
293 afb_proto_ws_client_describe(desc->stubws->proto, client_on_description_cb, desc);
297 /* get the description */
298 static struct json_object *client_describe_cb(void * closure)
300 struct client_describe desc;
302 /* synchronous job: send the request and wait its result */
303 desc.stubws = closure;
305 jobs_enter(NULL, 0, client_send_describe_cb, &desc);
309 /******************* server part: manage events **********************************/
311 static void server_event_add(void *closure, const char *event, int eventid)
313 struct afb_stub_ws *stubws = closure;
315 afb_proto_ws_server_event_create(stubws->proto, event, eventid);
318 static void server_event_remove(void *closure, const char *event, int eventid)
320 struct afb_stub_ws *stubws = closure;
322 afb_proto_ws_server_event_remove(stubws->proto, event, eventid);
325 static void server_event_push(void *closure, const char *event, int eventid, struct json_object *object)
327 struct afb_stub_ws *stubws = closure;
329 afb_proto_ws_server_event_push(stubws->proto, event, eventid, object);
330 json_object_put(object);
333 static void server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object)
335 struct afb_stub_ws *stubws = closure;
337 afb_proto_ws_server_event_broadcast(stubws->proto, event, object);
338 json_object_put(object);
341 /*****************************************************/
343 static void on_reply_success(void *closure, void *request, struct json_object *result, const char *info)
345 struct afb_xreq *xreq = request;
347 afb_xreq_success(xreq, result, *info ? info : NULL);
348 afb_xreq_unhooked_unref(xreq);
351 static void on_reply_fail(void *closure, void *request, const char *status, const char *info)
353 struct afb_xreq *xreq = request;
355 afb_xreq_fail(xreq, status, *info ? info : NULL);
356 afb_xreq_unhooked_unref(xreq);
359 static void on_event_create(void *closure, const char *event_name, int event_id)
361 struct afb_stub_ws *stubws = closure;
362 struct client_event *ev;
364 /* check conflicts */
365 ev = client_event_search(stubws, event_id, event_name);
371 /* no conflict, try to add it */
372 ev = malloc(sizeof *ev);
374 ev->eventid = afb_evt_eventid_create(event_name);
375 if (ev->eventid != NULL) {
378 ev->next = stubws->events;
384 ERROR("can't create event %s, out of memory", event_name);
387 static void on_event_remove(void *closure, const char *event_name, int event_id)
389 struct afb_stub_ws *stubws = closure;
390 struct client_event *ev, **prv;
392 /* check conflicts */
393 ev = client_event_search(stubws, event_id, event_name);
397 /* decrease the reference count */
401 /* unlinks the event */
402 prv = &stubws->events;
407 /* destroys the event */
408 afb_evt_eventid_unref(ev->eventid);
412 static void on_event_subscribe(void *closure, void *request, const char *event_name, int event_id)
414 struct afb_stub_ws *stubws = closure;
415 struct afb_xreq *xreq = request;
416 struct client_event *ev;
418 /* check conflicts */
419 ev = client_event_search(stubws, event_id, event_name);
423 if (afb_xreq_subscribe(xreq, ev->eventid) < 0)
424 ERROR("can't subscribe: %m");
427 static void on_event_unsubscribe(void *closure, void *request, const char *event_name, int event_id)
429 struct afb_stub_ws *stubws = closure;
430 struct afb_xreq *xreq = request;
431 struct client_event *ev;
433 /* check conflicts */
434 ev = client_event_search(stubws, event_id, event_name);
438 if (afb_xreq_unsubscribe(xreq, ev->eventid) < 0)
439 ERROR("can't unsubscribe: %m");
442 static void on_event_push(void *closure, const char *event_name, int event_id, struct json_object *data)
444 struct afb_stub_ws *stubws = closure;
445 struct client_event *ev;
447 /* check conflicts */
448 ev = client_event_search(stubws, event_id, event_name);
450 afb_evt_eventid_push(ev->eventid, data);
452 ERROR("unreadable push event");
455 static void on_event_broadcast(void *closure, const char *event_name, struct json_object *data)
457 afb_evt_broadcast(event_name, data);
460 static void client_subcall_reply_cb(void *closure, int status, json_object *object, struct afb_request *request)
462 struct afb_proto_ws_subcall *subcall = closure;
463 afb_proto_ws_subcall_reply(subcall, status, object);
466 static void on_subcall(void *closure, struct afb_proto_ws_subcall *subcall, void *request, const char *api, const char *verb, struct json_object *args)
468 struct afb_xreq *xreq = request;
470 afb_xreq_subcall(xreq, api, verb, args, client_subcall_reply_cb, subcall);
473 /*****************************************************/
475 static void record_session(struct afb_stub_ws *stubws, struct afb_session *session)
477 struct server_session *s, **prv;
480 prv = &stubws->sessions;
482 if (s->session == session)
484 if (afb_session_is_closed(s->session)) {
486 afb_session_unref(s->session);
494 s = malloc(sizeof *s);
496 s->session = afb_session_addref(session);
497 s->next = stubws->sessions;
498 stubws->sessions = s;
502 static void release_sessions(struct afb_stub_ws *stubws)
504 struct server_session *s;
506 while((s = stubws->sessions)) {
507 stubws->sessions = s->next;
508 afb_session_unref(s->session);
513 /*****************************************************/
515 static void on_call(void *closure, struct afb_proto_ws_call *call, const char *verb, struct json_object *args, const char *sessionid)
517 struct afb_stub_ws *stubws = closure;
518 struct server_req *wreq;
520 afb_stub_ws_addref(stubws);
522 /* create the request */
523 wreq = malloc(sizeof *wreq);
527 afb_xreq_init(&wreq->xreq, &server_req_xreq_itf);
528 wreq->stubws = stubws;
531 /* init the context */
532 if (afb_context_connect(&wreq->xreq.context, sessionid, NULL) < 0)
534 wreq->xreq.context.validated = 1;
535 record_session(stubws, wreq->xreq.context.session);
536 if (wreq->xreq.context.created)
537 afb_session_set_autoclose(wreq->xreq.context.session, 1);
540 wreq->xreq.cred = afb_cred_addref(stubws->cred);
541 wreq->xreq.request.api = stubws->apiname;
542 wreq->xreq.request.verb = verb;
543 wreq->xreq.json = args;
544 afb_xreq_process(&wreq->xreq, stubws->apiset);
550 json_object_put(args);
551 afb_stub_ws_unref(stubws);
552 afb_proto_ws_call_fail(call, "internal-error", NULL);
553 afb_proto_ws_call_unref(call);
556 static void server_describe_sjob(int signum, void *closure)
558 struct json_object *obj;
559 struct server_describe *desc = closure;
561 /* get the description if possible */
562 obj = !signum ? afb_apiset_describe(desc->stubws->apiset, desc->stubws->apiname) : NULL;
565 afb_proto_ws_describe_put(desc->describe, obj);
566 json_object_put(obj);
567 afb_stub_ws_unref(desc->stubws);
570 static void server_describe_job(int signum, void *closure)
572 server_describe_sjob(signum, closure);
576 static void on_describe(void *closure, struct afb_proto_ws_describe *describe)
578 struct server_describe *desc, sdesc;
579 struct afb_stub_ws *stubws = closure;
581 /* allocate (if possible) and init */
582 desc = malloc(sizeof *desc);
585 desc->stubws = stubws;
586 desc->describe = describe;
587 afb_stub_ws_addref(stubws);
591 jobs_call(NULL, 0, server_describe_sjob, desc);
593 if (jobs_queue(NULL, 0, server_describe_job, desc) < 0)
594 jobs_call(NULL, 0, server_describe_job, desc);
598 /*****************************************************/
600 static const struct afb_proto_ws_client_itf client_itf =
602 .on_reply_success = on_reply_success,
603 .on_reply_fail = on_reply_fail,
604 .on_event_create = on_event_create,
605 .on_event_remove = on_event_remove,
606 .on_event_subscribe = on_event_subscribe,
607 .on_event_unsubscribe = on_event_unsubscribe,
608 .on_event_push = on_event_push,
609 .on_event_broadcast = on_event_broadcast,
610 .on_subcall = on_subcall
613 static const struct afb_proto_ws_server_itf server_itf =
616 .on_describe = on_describe
619 static struct afb_api_itf ws_api_itf = {
620 .call = client_call_cb,
621 .describe = client_describe_cb
624 /* the interface for events pushing */
625 static const struct afb_evt_itf server_evt_itf = {
626 .broadcast = server_event_broadcast,
627 .push = server_event_push,
628 .add = server_event_add,
629 .remove = server_event_remove
632 /*****************************************************/
634 static void drop_all_events(struct afb_stub_ws *stubws)
636 struct client_event *ev, *nxt;
639 stubws->events = NULL;
643 afb_evt_eventid_unref(ev->eventid);
649 /* callback when receiving a hangup */
650 static void on_hangup(void *closure)
652 struct afb_stub_ws *stubws = closure;
654 if (stubws->on_hangup)
655 stubws->on_hangup(stubws);
657 release_sessions(stubws);
660 /*****************************************************/
662 static struct afb_stub_ws *afb_stub_ws_create(int fd, const char *apiname, struct afb_apiset *apiset, int client)
664 struct afb_stub_ws *stubws;
666 stubws = calloc(1, sizeof *stubws + strlen(apiname));
671 stubws->proto = afb_proto_ws_create_client(afb_common_get_event_loop(), fd, &client_itf, stubws);
673 stubws->proto = afb_proto_ws_create_server(afb_common_get_event_loop(), fd, &server_itf, stubws);
674 if (stubws->proto != NULL) {
675 strcpy(stubws->apiname, apiname);
676 stubws->apiset = afb_apiset_addref(apiset);
677 stubws->refcount = 1;
678 afb_proto_ws_on_hangup(stubws->proto, on_hangup);
686 struct afb_stub_ws *afb_stub_ws_create_client(int fd, const char *apiname, struct afb_apiset *apiset)
688 return afb_stub_ws_create(fd, apiname, apiset, 1);
691 struct afb_stub_ws *afb_stub_ws_create_server(int fd, const char *apiname, struct afb_apiset *apiset)
693 struct afb_stub_ws *stubws;
695 stubws = afb_stub_ws_create(fd, apiname, apiset, 0);
697 stubws->cred = afb_cred_create_for_socket(fd);
698 stubws->listener = afb_evt_listener_create(&server_evt_itf, stubws);
699 if (stubws->listener != NULL)
701 afb_stub_ws_unref(stubws);
706 void afb_stub_ws_unref(struct afb_stub_ws *stubws)
708 if (!__atomic_sub_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED)) {
709 drop_all_events(stubws);
710 afb_evt_listener_unref(stubws->listener);
711 release_sessions(stubws);
712 afb_proto_ws_unref(stubws->proto);
713 afb_cred_unref(stubws->cred);
714 afb_apiset_unref(stubws->apiset);
719 void afb_stub_ws_addref(struct afb_stub_ws *stubws)
721 __atomic_add_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED);
724 void afb_stub_ws_on_hangup(struct afb_stub_ws *stubws, void (*on_hangup)(struct afb_stub_ws*))
726 stubws->on_hangup = on_hangup;
729 const char *afb_stub_ws_name(struct afb_stub_ws *stubws)
731 return stubws->apiname;
734 struct afb_api afb_stub_ws_client_api(struct afb_stub_ws *stubws)
738 assert(!stubws->listener); /* check client */
739 api.closure = stubws;
740 api.itf = &ws_api_itf;
745 int afb_stub_ws_client_add(struct afb_stub_ws *stubws, struct afb_apiset *apiset)
747 return afb_apiset_add(apiset, stubws->apiname, afb_stub_ws_client_api(stubws));