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 recording calls on client side
57 struct client_call *next; /* the next call */
58 struct afb_stub_ws *stubws; /* the stub_ws */
59 struct afb_xreq *xreq; /* the request handle */
60 uint32_t msgid; /* the message identifier */
64 * structure for a ws request
67 struct afb_xreq xreq; /* the xreq */
68 struct afb_stub_ws *stubws; /* the client of the request */
69 struct afb_proto_ws_call *call; /* the incoming call */
73 * structure for recording events on client side
77 struct client_event *next;
78 struct afb_event_x2 *event;
84 * structure for recording describe requests
86 struct client_describe
88 struct afb_stub_ws *stubws;
89 struct jobloop *jobloop;
90 struct json_object *result;
94 * structure for jobs of describing
96 struct server_describe
98 struct afb_stub_ws *stubws;
99 struct afb_proto_ws_describe *describe;
103 * structure for recording sessions
105 struct server_session
107 struct server_session *next;
108 struct afb_session *session;
111 /******************* stub description for client or servers ******************/
115 /* count of references */
118 /* resource control */
119 pthread_mutex_t mutex;
122 struct afb_proto_ws *proto;
124 /* listener for events (server side) */
125 struct afb_evt_listener *listener;
127 /* event replica (client side) */
128 struct client_event *events;
130 /* credentials of the client (server side) */
131 struct afb_cred *cred;
133 /* sessions (server side) */
134 struct server_session *sessions;
137 struct afb_apiset *apiset;
139 /* on hangup callback */
140 void (*on_hangup)(struct afb_stub_ws *);
146 /******************* ws request part for server *****************/
148 /* decrement the reference count of the request and free/release it on falling to null */
149 static void server_req_destroy_cb(struct afb_xreq *xreq)
151 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
153 afb_context_disconnect(&wreq->xreq.context);
154 afb_cred_unref(wreq->xreq.cred);
155 json_object_put(wreq->xreq.json);
156 afb_proto_ws_call_unref(wreq->call);
157 afb_stub_ws_unref(wreq->stubws);
161 static void server_req_reply_cb(struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info)
164 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
166 rc = afb_proto_ws_call_reply(wreq->call, obj, error, info);
168 ERROR("error while sending reply");
169 json_object_put(obj);
172 static int server_req_subscribe_cb(struct afb_xreq *xreq, struct afb_event_x2 *event)
175 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
177 rc = afb_evt_event_x2_add_watch(wreq->stubws->listener, event);
179 rc = afb_proto_ws_call_subscribe(wreq->call, afb_evt_event_x2_fullname(event), afb_evt_event_x2_id(event));
181 ERROR("error while subscribing event");
185 static int server_req_unsubscribe_cb(struct afb_xreq *xreq, struct afb_event_x2 *event)
188 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
190 rc = afb_proto_ws_call_unsubscribe(wreq->call, afb_evt_event_x2_fullname(event), afb_evt_event_x2_id(event));
191 rc2 = afb_evt_event_x2_remove_watch(wreq->stubws->listener, event);
192 if (rc >= 0 && rc2 < 0)
195 ERROR("error while unsubscribing event");
199 static const struct afb_xreq_query_itf server_req_xreq_itf = {
200 .reply = server_req_reply_cb,
201 .unref = server_req_destroy_cb,
202 .subscribe = server_req_subscribe_cb,
203 .unsubscribe = server_req_unsubscribe_cb
206 /******************* client part **********************************/
208 /* search the event */
209 static struct client_event *client_event_search(struct afb_stub_ws *stubws, uint32_t eventid, const char *name)
211 struct client_event *ev;
214 while (ev != NULL && (ev->id != eventid || 0 != strcmp(afb_evt_event_x2_fullname(ev->event), name)))
220 /* on call, propagate it to the ws service */
221 static void client_call_cb(void * closure, struct afb_xreq *xreq)
223 struct afb_stub_ws *stubws = closure;
225 afb_proto_ws_client_call(
227 xreq->request.called_verb,
229 afb_session_uuid(xreq->context.session),
231 xreq_on_behalf_cred_export(xreq));
232 afb_xreq_unhooked_addref(xreq);
235 static void client_on_description_cb(void *closure, struct json_object *data)
237 struct client_describe *desc = closure;
240 jobs_leave(desc->jobloop);
243 static void client_send_describe_cb(int signum, void *closure, struct jobloop *jobloop)
245 struct client_describe *desc = closure;
250 desc->jobloop = jobloop;
251 afb_proto_ws_client_describe(desc->stubws->proto, client_on_description_cb, desc);
255 /* get the description */
256 static struct json_object *client_describe_cb(void * closure)
258 struct client_describe desc;
260 /* synchronous job: send the request and wait its result */
261 desc.stubws = closure;
263 jobs_enter(NULL, 0, client_send_describe_cb, &desc);
267 /******************* server part: manage events **********************************/
269 static void server_event_add(void *closure, const char *event, int eventid)
271 struct afb_stub_ws *stubws = closure;
273 afb_proto_ws_server_event_create(stubws->proto, event, eventid);
276 static void server_event_remove(void *closure, const char *event, int eventid)
278 struct afb_stub_ws *stubws = closure;
280 afb_proto_ws_server_event_remove(stubws->proto, event, eventid);
283 static void server_event_push(void *closure, const char *event, int eventid, struct json_object *object)
285 struct afb_stub_ws *stubws = closure;
287 afb_proto_ws_server_event_push(stubws->proto, event, eventid, object);
288 json_object_put(object);
291 static void server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object)
293 struct afb_stub_ws *stubws = closure;
295 afb_proto_ws_server_event_broadcast(stubws->proto, event, object);
296 json_object_put(object);
299 /*****************************************************/
301 static void on_reply(void *closure, void *request, struct json_object *object, const char *error, const char *info)
303 struct afb_xreq *xreq = request;
305 afb_xreq_reply(xreq, object, error, info);
306 afb_xreq_unhooked_unref(xreq);
309 static void on_event_create(void *closure, const char *event_name, int event_id)
311 struct afb_stub_ws *stubws = closure;
312 struct client_event *ev;
314 /* check conflicts */
315 ev = client_event_search(stubws, event_id, event_name);
321 /* no conflict, try to add it */
322 ev = malloc(sizeof *ev);
324 ev->event = afb_evt_event_x2_create(event_name);
325 if (ev->event != NULL) {
328 ev->next = stubws->events;
334 ERROR("can't create event %s, out of memory", event_name);
337 static void on_event_remove(void *closure, const char *event_name, int event_id)
339 struct afb_stub_ws *stubws = closure;
340 struct client_event *ev, **prv;
342 /* check conflicts */
343 ev = client_event_search(stubws, event_id, event_name);
347 /* decrease the reference count */
351 /* unlinks the event */
352 prv = &stubws->events;
357 /* destroys the event */
358 afb_evt_event_x2_unref(ev->event);
362 static void on_event_subscribe(void *closure, void *request, const char *event_name, int event_id)
364 struct afb_stub_ws *stubws = closure;
365 struct afb_xreq *xreq = request;
366 struct client_event *ev;
368 /* check conflicts */
369 ev = client_event_search(stubws, event_id, event_name);
373 if (afb_xreq_subscribe(xreq, ev->event) < 0)
374 ERROR("can't subscribe: %m");
377 static void on_event_unsubscribe(void *closure, void *request, const char *event_name, int event_id)
379 struct afb_stub_ws *stubws = closure;
380 struct afb_xreq *xreq = request;
381 struct client_event *ev;
383 /* check conflicts */
384 ev = client_event_search(stubws, event_id, event_name);
388 if (afb_xreq_unsubscribe(xreq, ev->event) < 0)
389 ERROR("can't unsubscribe: %m");
392 static void on_event_push(void *closure, const char *event_name, int event_id, struct json_object *data)
394 struct afb_stub_ws *stubws = closure;
395 struct client_event *ev;
397 /* check conflicts */
398 ev = client_event_search(stubws, event_id, event_name);
400 afb_evt_event_x2_push(ev->event, data);
402 ERROR("unreadable push event");
405 static void on_event_broadcast(void *closure, const char *event_name, struct json_object *data)
407 afb_evt_broadcast(event_name, data);
410 /*****************************************************/
412 static void record_session(struct afb_stub_ws *stubws, struct afb_session *session)
414 struct server_session *s, **prv;
417 prv = &stubws->sessions;
419 if (s->session == session)
421 if (afb_session_is_closed(s->session)) {
423 afb_session_unref(s->session);
431 s = malloc(sizeof *s);
433 s->session = afb_session_addref(session);
434 s->next = stubws->sessions;
435 stubws->sessions = s;
439 static void release_all_sessions(struct afb_stub_ws *stubws)
441 struct server_session *s, *n;
443 s = stubws->sessions;
444 stubws->sessions = NULL;
447 afb_session_unref(s->session);
453 /*****************************************************/
455 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)
457 struct afb_stub_ws *stubws = closure;
458 struct server_req *wreq;
460 afb_stub_ws_addref(stubws);
462 /* create the request */
463 wreq = malloc(sizeof *wreq);
467 afb_xreq_init(&wreq->xreq, &server_req_xreq_itf);
468 wreq->stubws = stubws;
471 /* init the context */
472 if (afb_context_connect(&wreq->xreq.context, sessionid, NULL) < 0)
474 wreq->xreq.context.validated = 1;
475 record_session(stubws, wreq->xreq.context.session);
476 if (wreq->xreq.context.created)
477 afb_session_set_autoclose(wreq->xreq.context.session, 1);
480 wreq->xreq.cred = afb_cred_mixed_on_behalf_import(stubws->cred, sessionid, user_creds);
481 wreq->xreq.request.called_api = stubws->apiname;
482 wreq->xreq.request.called_verb = verb;
483 wreq->xreq.json = args;
484 afb_xreq_process(&wreq->xreq, stubws->apiset);
490 json_object_put(args);
491 afb_stub_ws_unref(stubws);
492 afb_proto_ws_call_reply(call, NULL, "internal-error", NULL);
493 afb_proto_ws_call_unref(call);
496 static void server_describe_sjob(int signum, void *closure)
498 struct json_object *obj;
499 struct server_describe *desc = closure;
501 /* get the description if possible */
502 obj = !signum ? afb_apiset_describe(desc->stubws->apiset, desc->stubws->apiname) : NULL;
505 afb_proto_ws_describe_put(desc->describe, obj);
506 json_object_put(obj);
507 afb_stub_ws_unref(desc->stubws);
510 static void server_describe_job(int signum, void *closure)
512 server_describe_sjob(signum, closure);
516 static void on_describe(void *closure, struct afb_proto_ws_describe *describe)
518 struct server_describe *desc, sdesc;
519 struct afb_stub_ws *stubws = closure;
521 /* allocate (if possible) and init */
522 desc = malloc(sizeof *desc);
525 desc->stubws = stubws;
526 desc->describe = describe;
527 afb_stub_ws_addref(stubws);
531 jobs_call(NULL, 0, server_describe_sjob, desc);
533 if (jobs_queue(NULL, 0, server_describe_job, desc) < 0)
534 jobs_call(NULL, 0, server_describe_job, desc);
538 /*****************************************************/
540 static const struct afb_proto_ws_client_itf client_itf =
542 .on_reply = on_reply,
543 .on_event_create = on_event_create,
544 .on_event_remove = on_event_remove,
545 .on_event_subscribe = on_event_subscribe,
546 .on_event_unsubscribe = on_event_unsubscribe,
547 .on_event_push = on_event_push,
548 .on_event_broadcast = on_event_broadcast,
551 static const struct afb_proto_ws_server_itf server_itf =
554 .on_describe = on_describe
557 static struct afb_api_itf ws_api_itf = {
558 .call = client_call_cb,
559 .describe = client_describe_cb
562 /* the interface for events pushing */
563 static const struct afb_evt_itf server_evt_itf = {
564 .broadcast = server_event_broadcast,
565 .push = server_event_push,
566 .add = server_event_add,
567 .remove = server_event_remove
570 /*****************************************************/
572 static void drop_all_events(struct afb_stub_ws *stubws)
574 struct client_event *ev, *nxt;
577 stubws->events = NULL;
581 afb_evt_event_x2_unref(ev->event);
587 /* callback when receiving a hangup */
588 static void on_hangup(void *closure)
590 struct afb_stub_ws *stubws = closure;
592 afb_stub_ws_addref(stubws);
593 if (stubws->on_hangup)
594 stubws->on_hangup(stubws);
596 release_all_sessions(stubws);
597 afb_stub_ws_unref(stubws);
600 static int enqueue_processing(void (*callback)(int signum, void* arg), void *arg)
602 return jobs_queue(NULL, 0, callback, arg);
605 /*****************************************************/
607 static struct afb_stub_ws *afb_stub_ws_create(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset, int client)
609 struct afb_stub_ws *stubws;
611 stubws = calloc(1, sizeof *stubws + strlen(apiname));
616 stubws->proto = afb_proto_ws_create_client(fdev, &client_itf, stubws);
618 stubws->proto = afb_proto_ws_create_server(fdev, &server_itf, stubws);
621 strcpy(stubws->apiname, apiname);
622 stubws->apiset = afb_apiset_addref(apiset);
623 stubws->refcount = 1;
624 afb_proto_ws_on_hangup(stubws->proto, on_hangup);
625 afb_proto_ws_set_queuing(stubws->proto, enqueue_processing);
634 struct afb_stub_ws *afb_stub_ws_create_client(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset)
636 return afb_stub_ws_create(fdev, apiname, apiset, 1);
639 struct afb_stub_ws *afb_stub_ws_create_server(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset)
641 struct afb_stub_ws *stubws;
643 stubws = afb_stub_ws_create(fdev, apiname, apiset, 0);
645 stubws->cred = afb_cred_create_for_socket(fdev_fd(fdev));
646 stubws->listener = afb_evt_listener_create(&server_evt_itf, stubws);
647 if (stubws->listener != NULL)
649 afb_stub_ws_unref(stubws);
654 void afb_stub_ws_unref(struct afb_stub_ws *stubws)
656 if (!__atomic_sub_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED)) {
657 drop_all_events(stubws);
658 if (stubws->listener)
659 afb_evt_listener_unref(stubws->listener);
660 release_all_sessions(stubws);
661 afb_proto_ws_unref(stubws->proto);
662 afb_cred_unref(stubws->cred);
663 afb_apiset_unref(stubws->apiset);
668 void afb_stub_ws_addref(struct afb_stub_ws *stubws)
670 __atomic_add_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED);
673 void afb_stub_ws_on_hangup(struct afb_stub_ws *stubws, void (*on_hangup)(struct afb_stub_ws*))
675 stubws->on_hangup = on_hangup;
678 const char *afb_stub_ws_name(struct afb_stub_ws *stubws)
680 return stubws->apiname;
683 struct afb_api_item afb_stub_ws_client_api(struct afb_stub_ws *stubws)
685 struct afb_api_item api;
687 assert(!stubws->listener); /* check client */
688 api.closure = stubws;
689 api.itf = &ws_api_itf;
694 int afb_stub_ws_client_add(struct afb_stub_ws *stubws, struct afb_apiset *apiset)
696 return afb_apiset_add(apiset, stubws->apiname, afb_stub_ws_client_api(stubws));