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>
36 #include <systemd/sd-event.h>
38 #include <afb/afb-event.h>
40 #include "afb-common.h"
42 #include "afb-session.h"
45 #include "afb-apiset.h"
46 #include "afb-proto-ws.h"
47 #include "afb-stub-ws.h"
48 #include "afb-context.h"
57 /******************* handling subcalls *****************************/
60 * Structure on server side for recording pending
65 struct server_subcall *next; /**< next subcall for the client */
66 uint32_t subcallid; /**< the subcallid */
67 void (*callback)(void*, int, struct json_object*); /**< callback on completion */
68 void *closure; /**< closure of the callback */
72 * Structure for sending back replies on client side
76 struct afb_stub_ws *stubws; /**< stub descriptor */
77 uint32_t subcallid; /**< subcallid for the reply */
81 * structure for recording calls on client side
84 struct client_call *next; /* the next call */
85 struct afb_stub_ws *stubws; /* the stub_ws */
86 struct afb_xreq *xreq; /* the request handle */
87 uint32_t msgid; /* the message identifier */
91 * structure for a ws request
94 struct afb_xreq xreq; /* the xreq */
95 struct afb_stub_ws *stubws; /* the client of the request */
96 struct afb_proto_ws_call *call; /* the incoming call */
100 * structure for recording events on client side
104 struct client_event *next;
105 struct afb_event event;
111 * structure for recording describe requests
113 struct client_describe
115 struct afb_stub_ws *stubws;
116 struct jobloop *jobloop;
117 struct json_object *result;
121 * structure for jobs of describing
123 struct server_describe
125 struct afb_stub_ws *stubws;
126 struct afb_proto_ws_describe *describe;
129 /******************* stub description for client or servers ******************/
133 /* count of references */
136 /* resource control */
137 pthread_mutex_t mutex;
140 struct afb_proto_ws *proto;
142 /* listener for events (server side) */
143 struct afb_evt_listener *listener;
145 /* event replica (client side) */
146 struct client_event *events;
148 /* credentials (server side) */
149 struct afb_cred *cred;
152 struct afb_apiset *apiset;
154 /* on hangup callback */
155 void (*on_hangup)(struct afb_stub_ws *);
161 /******************* ws request part for server *****************/
163 /* decrement the reference count of the request and free/release it on falling to null */
164 static void server_req_destroy_cb(struct afb_xreq *xreq)
166 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
168 afb_context_disconnect(&wreq->xreq.context);
169 afb_cred_unref(wreq->xreq.cred);
170 json_object_put(wreq->xreq.json);
171 afb_proto_ws_call_unref(wreq->call);
172 afb_stub_ws_unref(wreq->stubws);
176 static void server_req_success_cb(struct afb_xreq *xreq, struct json_object *obj, const char *info)
179 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
181 rc = afb_proto_ws_call_success(wreq->call, obj, info);
183 ERROR("error while sending success");
184 json_object_put(obj);
187 static void server_req_fail_cb(struct afb_xreq *xreq, const char *status, const char *info)
190 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
192 rc = afb_proto_ws_call_fail(wreq->call, status, info);
194 ERROR("error while sending fail");
197 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)
200 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
202 rc = afb_proto_ws_call_subcall(wreq->call, api, verb, args, callback, cb_closure);
204 ERROR("error while sending subcall");
207 static int server_req_subscribe_cb(struct afb_xreq *xreq, struct afb_event event)
210 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
212 rc = afb_evt_add_watch(wreq->stubws->listener, event);
214 rc = afb_proto_ws_call_subscribe(wreq->call, afb_evt_event_fullname(event), afb_evt_event_id(event));
216 ERROR("error while subscribing event");
220 static int server_req_unsubscribe_cb(struct afb_xreq *xreq, struct afb_event event)
223 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
225 rc = afb_proto_ws_call_unsubscribe(wreq->call, afb_evt_event_fullname(event), afb_evt_event_id(event));
226 rc2 = afb_evt_remove_watch(wreq->stubws->listener, event);
227 if (rc >= 0 && rc2 < 0)
230 ERROR("error while unsubscribing event");
234 static const struct afb_xreq_query_itf server_req_xreq_itf = {
235 .success = server_req_success_cb,
236 .fail = server_req_fail_cb,
237 .unref = server_req_destroy_cb,
238 .subcall = server_req_subcall_cb,
239 .subscribe = server_req_subscribe_cb,
240 .unsubscribe = server_req_unsubscribe_cb
243 /******************* client part **********************************/
245 /* search the event */
246 static struct client_event *client_event_search(struct afb_stub_ws *stubws, uint32_t eventid, const char *name)
248 struct client_event *ev;
251 while (ev != NULL && (ev->eventid != eventid || 0 != strcmp(afb_evt_event_fullname(ev->event), name)))
257 /* on call, propagate it to the ws service */
258 static void client_call_cb(void * closure, struct afb_xreq *xreq)
260 struct afb_stub_ws *stubws = closure;
262 afb_proto_ws_client_call(stubws->proto, xreq->verb, afb_xreq_json(xreq), afb_session_uuid(xreq->context.session), xreq);
263 afb_xreq_unhooked_addref(xreq);
266 static void client_on_description_cb(void *closure, struct json_object *data)
268 struct client_describe *desc = closure;
271 jobs_leave(desc->jobloop);
274 static void client_send_describe_cb(int signum, void *closure, struct jobloop *jobloop)
276 struct client_describe *desc = closure;
281 desc->jobloop = jobloop;
282 afb_proto_ws_client_describe(desc->stubws->proto, client_on_description_cb, desc);
286 /* get the description */
287 static struct json_object *client_describe_cb(void * closure)
289 struct client_describe desc;
291 /* synchronous job: send the request and wait its result */
292 desc.stubws = closure;
294 jobs_enter(NULL, 0, client_send_describe_cb, &desc);
298 /******************* server part: manage events **********************************/
300 static void server_event_add(void *closure, const char *event, int eventid)
302 struct afb_stub_ws *stubws = closure;
304 afb_proto_ws_server_event_create(stubws->proto, event, eventid);
307 static void server_event_remove(void *closure, const char *event, int eventid)
309 struct afb_stub_ws *stubws = closure;
311 afb_proto_ws_server_event_remove(stubws->proto, event, eventid);
314 static void server_event_push(void *closure, const char *event, int eventid, struct json_object *object)
316 struct afb_stub_ws *stubws = closure;
318 afb_proto_ws_server_event_push(stubws->proto, event, eventid, object);
319 json_object_put(object);
322 static void server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object)
324 struct afb_stub_ws *stubws = closure;
326 afb_proto_ws_server_event_broadcast(stubws->proto, event, object);
327 json_object_put(object);
330 /*****************************************************/
332 static void on_reply_success(void *closure, void *request, struct json_object *result, const char *info)
334 struct afb_xreq *xreq = request;
336 afb_xreq_success(xreq, result, *info ? info : NULL);
337 afb_xreq_unhooked_unref(xreq);
340 static void on_reply_fail(void *closure, void *request, const char *status, const char *info)
342 struct afb_xreq *xreq = request;
344 afb_xreq_fail(xreq, status, *info ? info : NULL);
345 afb_xreq_unhooked_unref(xreq);
348 static void on_event_create(void *closure, const char *event_name, int event_id)
350 struct afb_stub_ws *stubws = closure;
351 struct client_event *ev;
353 /* check conflicts */
354 ev = client_event_search(stubws, event_id, event_name);
360 /* no conflict, try to add it */
361 ev = malloc(sizeof *ev);
363 ev->event = afb_evt_create_event(event_name);
364 if (ev->event.closure != NULL) {
366 ev->eventid = event_id;
367 ev->next = stubws->events;
373 ERROR("can't create event %s, out of memory", event_name);
376 static void on_event_remove(void *closure, const char *event_name, int event_id)
378 struct afb_stub_ws *stubws = closure;
379 struct client_event *ev, **prv;
381 /* check conflicts */
382 ev = client_event_search(stubws, event_id, event_name);
386 /* decrease the reference count */
390 /* unlinks the event */
391 prv = &stubws->events;
396 /* destroys the event */
397 afb_event_unref(ev->event);
401 static void on_event_subscribe(void *closure, void *request, const char *event_name, int event_id)
403 struct afb_stub_ws *stubws = closure;
404 struct afb_xreq *xreq = request;
405 struct client_event *ev;
407 /* check conflicts */
408 ev = client_event_search(stubws, event_id, event_name);
412 if (afb_xreq_subscribe(xreq, ev->event) < 0)
413 ERROR("can't subscribe: %m");
416 static void on_event_unsubscribe(void *closure, void *request, const char *event_name, int event_id)
418 struct afb_stub_ws *stubws = closure;
419 struct afb_xreq *xreq = request;
420 struct client_event *ev;
422 /* check conflicts */
423 ev = client_event_search(stubws, event_id, event_name);
427 if (afb_xreq_unsubscribe(xreq, ev->event) < 0)
428 ERROR("can't unsubscribe: %m");
431 static void on_event_push(void *closure, const char *event_name, int event_id, struct json_object *data)
433 struct afb_stub_ws *stubws = closure;
434 struct client_event *ev;
436 /* check conflicts */
437 ev = client_event_search(stubws, event_id, event_name);
439 afb_event_push(ev->event, data);
441 ERROR("unreadable push event");
444 static void on_event_broadcast(void *closure, const char *event_name, struct json_object *data)
446 afb_evt_broadcast(event_name, data);
449 static void client_subcall_reply_cb(void *closure, int status, json_object *object)
451 struct afb_proto_ws_subcall *subcall = closure;
452 afb_proto_ws_subcall_reply(subcall, status, object);
455 static void on_subcall(void *closure, struct afb_proto_ws_subcall *subcall, void *request, const char *api, const char *verb, struct json_object *args)
457 struct afb_xreq *xreq = request;
459 afb_xreq_subcall(xreq, api, verb, args, client_subcall_reply_cb, subcall);
462 /*****************************************************/
464 static void on_call(void *closure, struct afb_proto_ws_call *call, const char *verb, struct json_object *args, const char *sessionid)
466 struct afb_stub_ws *stubws = closure;
467 struct server_req *wreq;
469 afb_stub_ws_addref(stubws);
471 /* create the request */
472 wreq = malloc(sizeof *wreq);
476 afb_xreq_init(&wreq->xreq, &server_req_xreq_itf);
477 wreq->stubws = stubws;
480 /* init the context */
481 if (afb_context_connect(&wreq->xreq.context, sessionid, NULL) < 0)
485 wreq->xreq.cred = afb_cred_addref(stubws->cred);
486 wreq->xreq.api = stubws->apiname;
487 wreq->xreq.verb = verb;
488 wreq->xreq.json = args;
489 afb_xreq_process(&wreq->xreq, stubws->apiset);
495 json_object_put(args);
496 afb_stub_ws_unref(stubws);
497 afb_proto_ws_call_fail(call, "internal-error", NULL);
498 afb_proto_ws_call_unref(call);
501 static void server_describe_sjob(int signum, void *closure)
503 struct json_object *obj;
504 struct server_describe *desc = closure;
506 /* get the description if possible */
507 obj = !signum ? afb_apiset_describe(desc->stubws->apiset, desc->stubws->apiname) : NULL;
510 afb_proto_ws_describe_put(desc->describe, obj);
511 json_object_put(obj);
512 afb_stub_ws_unref(desc->stubws);
515 static void server_describe_job(int signum, void *closure)
517 server_describe_sjob(signum, closure);
521 static void on_describe(void *closure, struct afb_proto_ws_describe *describe)
523 struct server_describe *desc, sdesc;
524 struct afb_stub_ws *stubws = closure;
526 /* allocate (if possible) and init */
527 desc = malloc(sizeof *desc);
530 desc->stubws = stubws;
531 desc->describe = describe;
532 afb_stub_ws_addref(stubws);
536 jobs_call(NULL, 0, server_describe_sjob, desc);
538 if (jobs_queue(NULL, 0, server_describe_job, desc) < 0)
539 jobs_call(NULL, 0, server_describe_job, desc);
543 /*****************************************************/
545 static const struct afb_proto_ws_client_itf client_itf =
547 .on_reply_success = on_reply_success,
548 .on_reply_fail = on_reply_fail,
549 .on_event_create = on_event_create,
550 .on_event_remove = on_event_remove,
551 .on_event_subscribe = on_event_subscribe,
552 .on_event_unsubscribe = on_event_unsubscribe,
553 .on_event_push = on_event_push,
554 .on_event_broadcast = on_event_broadcast,
555 .on_subcall = on_subcall
558 static const struct afb_proto_ws_server_itf server_itf =
561 .on_describe = on_describe
564 static struct afb_api_itf ws_api_itf = {
565 .call = client_call_cb,
566 .describe = client_describe_cb
569 /* the interface for events pushing */
570 static const struct afb_evt_itf server_evt_itf = {
571 .broadcast = server_event_broadcast,
572 .push = server_event_push,
573 .add = server_event_add,
574 .remove = server_event_remove
577 /*****************************************************/
579 static void drop_all_events(struct afb_stub_ws *stubws)
581 struct client_event *ev, *nxt;
584 stubws->events = NULL;
588 afb_event_unref(ev->event);
594 /* callback when receiving a hangup */
595 static void on_hangup(void *closure)
597 struct afb_stub_ws *stubws = closure;
599 if (stubws->on_hangup)
600 stubws->on_hangup(stubws);
603 /*****************************************************/
605 static struct afb_stub_ws *afb_stub_ws_create(int fd, const char *apiname, struct afb_apiset *apiset, int client)
607 struct afb_stub_ws *stubws;
609 stubws = calloc(1, sizeof *stubws + strlen(apiname));
614 stubws->proto = afb_proto_ws_create_client(fd, &client_itf, stubws);
616 stubws->proto = afb_proto_ws_create_server(fd, &server_itf, stubws);
617 if (stubws->proto != NULL) {
618 strcpy(stubws->apiname, apiname);
619 stubws->apiset = afb_apiset_addref(apiset);
620 stubws->refcount = 1;
621 afb_proto_ws_on_hangup(stubws->proto, on_hangup);
629 struct afb_stub_ws *afb_stub_ws_create_client(int fd, const char *apiname, struct afb_apiset *apiset)
631 return afb_stub_ws_create(fd, apiname, apiset, 1);
634 struct afb_stub_ws *afb_stub_ws_create_server(int fd, const char *apiname, struct afb_apiset *apiset)
636 struct afb_stub_ws *stubws;
638 stubws = afb_stub_ws_create(fd, apiname, apiset, 0);
640 stubws->cred = afb_cred_create_for_socket(fd);
641 stubws->listener = afb_evt_listener_create(&server_evt_itf, stubws);
642 if (stubws->listener != NULL)
644 afb_stub_ws_unref(stubws);
649 void afb_stub_ws_unref(struct afb_stub_ws *stubws)
651 if (!__atomic_sub_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED)) {
652 drop_all_events(stubws);
653 afb_evt_listener_unref(stubws->listener);
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_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 afb_stub_ws_client_api(struct afb_stub_ws *stubws)
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));