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)
214 struct afb_stub_ws *stubws = closure;
216 rc = afb_proto_ws_client_call(
218 xreq->request.called_verb,
220 afb_session_uuid(xreq->context.session),
222 xreq_on_behalf_cred_export(xreq));
224 afb_xreq_unhooked_addref(xreq);
226 afb_xreq_reply(xreq, NULL, "internal", "can't send message");
229 static void client_on_description_cb(void *closure, struct json_object *data)
231 struct client_describe *desc = closure;
234 jobs_leave(desc->jobloop);
237 static void client_send_describe_cb(int signum, void *closure, struct jobloop *jobloop)
239 struct client_describe *desc = closure;
244 desc->jobloop = jobloop;
245 afb_proto_ws_client_describe(desc->stubws->proto, client_on_description_cb, desc);
249 /* get the description */
250 static struct json_object *client_describe_cb(void * closure)
252 struct client_describe desc;
254 /* synchronous job: send the request and wait its result */
255 desc.stubws = closure;
257 jobs_enter(NULL, 0, client_send_describe_cb, &desc);
261 /******************* server part: manage events **********************************/
263 static void server_event_add(void *closure, const char *event, int eventid)
265 struct afb_stub_ws *stubws = closure;
267 afb_proto_ws_server_event_create(stubws->proto, event, eventid);
270 static void server_event_remove(void *closure, const char *event, int eventid)
272 struct afb_stub_ws *stubws = closure;
274 afb_proto_ws_server_event_remove(stubws->proto, event, eventid);
277 static void server_event_push(void *closure, const char *event, int eventid, struct json_object *object)
279 struct afb_stub_ws *stubws = closure;
281 afb_proto_ws_server_event_push(stubws->proto, event, eventid, object);
282 json_object_put(object);
285 static void server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object)
287 struct afb_stub_ws *stubws = closure;
289 afb_proto_ws_server_event_broadcast(stubws->proto, event, object);
290 json_object_put(object);
293 /*****************************************************/
295 static void on_reply(void *closure, void *request, struct json_object *object, const char *error, const char *info)
297 struct afb_xreq *xreq = request;
299 afb_xreq_reply(xreq, object, error, info);
300 afb_xreq_unhooked_unref(xreq);
303 static void on_event_create(void *closure, const char *event_name, int event_id)
305 struct afb_stub_ws *stubws = closure;
306 struct client_event *ev;
308 /* check conflicts */
309 ev = client_event_search(stubws, event_id, event_name);
315 /* no conflict, try to add it */
316 ev = malloc(sizeof *ev);
318 ev->event = afb_evt_event_x2_create(event_name);
319 if (ev->event != NULL) {
322 ev->next = stubws->events;
328 ERROR("can't create event %s, out of memory", event_name);
331 static void on_event_remove(void *closure, const char *event_name, int event_id)
333 struct afb_stub_ws *stubws = closure;
334 struct client_event *ev, **prv;
336 /* check conflicts */
337 ev = client_event_search(stubws, event_id, event_name);
341 /* decrease the reference count */
345 /* unlinks the event */
346 prv = &stubws->events;
351 /* destroys the event */
352 afb_evt_event_x2_unref(ev->event);
356 static void on_event_subscribe(void *closure, void *request, const char *event_name, int event_id)
358 struct afb_stub_ws *stubws = closure;
359 struct afb_xreq *xreq = request;
360 struct client_event *ev;
362 /* check conflicts */
363 ev = client_event_search(stubws, event_id, event_name);
367 if (afb_xreq_subscribe(xreq, ev->event) < 0)
368 ERROR("can't subscribe: %m");
371 static void on_event_unsubscribe(void *closure, void *request, const char *event_name, int event_id)
373 struct afb_stub_ws *stubws = closure;
374 struct afb_xreq *xreq = request;
375 struct client_event *ev;
377 /* check conflicts */
378 ev = client_event_search(stubws, event_id, event_name);
382 if (afb_xreq_unsubscribe(xreq, ev->event) < 0)
383 ERROR("can't unsubscribe: %m");
386 static void on_event_push(void *closure, const char *event_name, int event_id, struct json_object *data)
388 struct afb_stub_ws *stubws = closure;
389 struct client_event *ev;
391 /* check conflicts */
392 ev = client_event_search(stubws, event_id, event_name);
394 afb_evt_event_x2_push(ev->event, data);
396 ERROR("unreadable push event");
399 static void on_event_broadcast(void *closure, const char *event_name, struct json_object *data)
401 afb_evt_broadcast(event_name, data);
404 /*****************************************************/
406 static void record_session(struct afb_stub_ws *stubws, struct afb_session *session)
408 struct server_session *s, **prv;
411 prv = &stubws->sessions;
413 if (s->session == session)
415 if (afb_session_is_closed(s->session)) {
417 afb_session_unref(s->session);
425 s = malloc(sizeof *s);
427 s->session = afb_session_addref(session);
428 s->next = stubws->sessions;
429 stubws->sessions = s;
433 static void release_all_sessions(struct afb_stub_ws *stubws)
435 struct server_session *s, *n;
437 s = __atomic_exchange_n(&stubws->sessions, NULL, __ATOMIC_RELAXED);
440 afb_session_unref(s->session);
446 /*****************************************************/
448 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)
450 struct afb_stub_ws *stubws = closure;
451 struct server_req *wreq;
453 afb_stub_ws_addref(stubws);
455 /* create the request */
456 wreq = malloc(sizeof *wreq);
460 afb_xreq_init(&wreq->xreq, &server_req_xreq_itf);
461 wreq->stubws = stubws;
464 /* init the context */
465 if (afb_context_connect(&wreq->xreq.context, sessionid, NULL) < 0)
467 wreq->xreq.context.validated = 1;
468 record_session(stubws, wreq->xreq.context.session);
469 if (wreq->xreq.context.created)
470 afb_session_set_autoclose(wreq->xreq.context.session, 1);
473 wreq->xreq.cred = afb_cred_mixed_on_behalf_import(stubws->cred, sessionid, user_creds);
474 wreq->xreq.request.called_api = stubws->apiname;
475 wreq->xreq.request.called_verb = verb;
476 wreq->xreq.json = args;
477 afb_xreq_process(&wreq->xreq, stubws->apiset);
483 json_object_put(args);
484 afb_stub_ws_unref(stubws);
485 afb_proto_ws_call_reply(call, NULL, "internal-error", NULL);
486 afb_proto_ws_call_unref(call);
489 static void server_describe_sjob(int signum, void *closure)
491 struct json_object *obj;
492 struct server_describe *desc = closure;
494 /* get the description if possible */
495 obj = !signum ? afb_apiset_describe(desc->stubws->apiset, desc->stubws->apiname) : NULL;
498 afb_proto_ws_describe_put(desc->describe, obj);
499 json_object_put(obj);
500 afb_stub_ws_unref(desc->stubws);
503 static void server_describe_job(int signum, void *closure)
505 server_describe_sjob(signum, closure);
509 static void on_describe(void *closure, struct afb_proto_ws_describe *describe)
511 struct server_describe *desc, sdesc;
512 struct afb_stub_ws *stubws = closure;
514 /* allocate (if possible) and init */
515 desc = malloc(sizeof *desc);
518 desc->stubws = stubws;
519 desc->describe = describe;
520 afb_stub_ws_addref(stubws);
524 jobs_call(NULL, 0, server_describe_sjob, desc);
526 if (jobs_queue(NULL, 0, server_describe_job, desc) < 0)
527 jobs_call(NULL, 0, server_describe_job, desc);
531 /*****************************************************/
533 static const struct afb_proto_ws_client_itf client_itf =
535 .on_reply = on_reply,
536 .on_event_create = on_event_create,
537 .on_event_remove = on_event_remove,
538 .on_event_subscribe = on_event_subscribe,
539 .on_event_unsubscribe = on_event_unsubscribe,
540 .on_event_push = on_event_push,
541 .on_event_broadcast = on_event_broadcast,
544 static const struct afb_proto_ws_server_itf server_itf =
547 .on_describe = on_describe
550 static struct afb_api_itf ws_api_itf = {
551 .call = client_call_cb,
552 .describe = client_describe_cb
555 /* the interface for events pushing */
556 static const struct afb_evt_itf server_evt_itf = {
557 .broadcast = server_event_broadcast,
558 .push = server_event_push,
559 .add = server_event_add,
560 .remove = server_event_remove
563 /*****************************************************/
565 static void drop_all_events(struct afb_stub_ws *stubws)
567 struct client_event *ev, *nxt;
570 stubws->events = NULL;
574 afb_evt_event_x2_unref(ev->event);
580 /* callback when receiving a hangup */
581 static void on_hangup(void *closure)
583 struct afb_stub_ws *stubws = closure;
585 afb_stub_ws_addref(stubws);
586 if (stubws->on_hangup)
587 stubws->on_hangup(stubws);
589 release_all_sessions(stubws);
590 afb_stub_ws_unref(stubws);
593 static int enqueue_processing(void (*callback)(int signum, void* arg), void *arg)
595 return jobs_queue(NULL, 0, callback, arg);
598 /*****************************************************/
600 static struct afb_stub_ws *afb_stub_ws_create(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset, int client)
602 struct afb_stub_ws *stubws;
604 stubws = calloc(1, sizeof *stubws + strlen(apiname));
609 stubws->proto = afb_proto_ws_create_client(fdev, &client_itf, stubws);
611 stubws->proto = afb_proto_ws_create_server(fdev, &server_itf, stubws);
614 strcpy(stubws->apiname, apiname);
615 stubws->apiset = afb_apiset_addref(apiset);
616 stubws->refcount = 1;
617 afb_proto_ws_on_hangup(stubws->proto, on_hangup);
618 afb_proto_ws_set_queuing(stubws->proto, enqueue_processing);
627 struct afb_stub_ws *afb_stub_ws_create_client(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset)
629 return afb_stub_ws_create(fdev, apiname, apiset, 1);
632 struct afb_stub_ws *afb_stub_ws_create_server(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset)
634 struct afb_stub_ws *stubws;
636 stubws = afb_stub_ws_create(fdev, apiname, apiset, 0);
638 stubws->cred = afb_cred_create_for_socket(fdev_fd(fdev));
639 stubws->listener = afb_evt_listener_create(&server_evt_itf, stubws);
640 if (stubws->listener != NULL)
642 afb_stub_ws_unref(stubws);
647 void afb_stub_ws_unref(struct afb_stub_ws *stubws)
649 if (!__atomic_sub_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED)) {
650 drop_all_events(stubws);
651 if (stubws->listener)
652 afb_evt_listener_unref(stubws->listener);
653 release_all_sessions(stubws);
654 afb_proto_ws_unref(stubws->proto);
655 afb_cred_unref(stubws->cred);
656 afb_apiset_unref(stubws->apiset);
661 void afb_stub_ws_addref(struct afb_stub_ws *stubws)
663 __atomic_add_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED);
666 void afb_stub_ws_set_on_hangup(struct afb_stub_ws *stubws, void (*on_hangup)(struct afb_stub_ws*))
668 stubws->on_hangup = on_hangup;
671 const char *afb_stub_ws_name(struct afb_stub_ws *stubws)
673 return stubws->apiname;
676 struct afb_api_item afb_stub_ws_client_api(struct afb_stub_ws *stubws)
678 struct afb_api_item api;
680 assert(!stubws->listener); /* check client */
681 api.closure = stubws;
682 api.itf = &ws_api_itf;
687 int afb_stub_ws_client_add(struct afb_stub_ws *stubws, struct afb_apiset *apiset)
689 return afb_apiset_add(apiset, stubws->apiname, afb_stub_ws_client_api(stubws));