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;
128 /******************* stub description for client or servers ******************/
132 /* count of references */
135 /* resource control */
136 pthread_mutex_t mutex;
139 struct afb_proto_ws *proto;
141 /* listener for events (server side) */
142 struct afb_evt_listener *listener;
144 /* event replica (client side) */
145 struct client_event *events;
147 /* credentials (server side) */
148 struct afb_cred *cred;
151 struct afb_apiset *apiset;
153 /* on hangup callback */
154 void (*on_hangup)(struct afb_stub_ws *);
160 /******************* ws request part for server *****************/
162 /* decrement the reference count of the request and free/release it on falling to null */
163 static void server_req_destroy_cb(struct afb_xreq *xreq)
165 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
167 afb_context_disconnect(&wreq->xreq.context);
168 afb_cred_unref(wreq->xreq.cred);
169 json_object_put(wreq->xreq.json);
170 afb_proto_ws_call_unref(wreq->call);
171 afb_stub_ws_unref(wreq->stubws);
175 static void server_req_success_cb(struct afb_xreq *xreq, struct json_object *obj, const char *info)
178 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
180 rc = afb_proto_ws_call_success(wreq->call, obj, info);
182 ERROR("error while sending success");
183 json_object_put(obj);
186 static void server_req_fail_cb(struct afb_xreq *xreq, const char *status, const char *info)
189 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
191 rc = afb_proto_ws_call_fail(wreq->call, status, info);
193 ERROR("error while sending fail");
196 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)
199 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
201 rc = afb_proto_ws_call_subcall(wreq->call, api, verb, args, callback, cb_closure);
203 ERROR("error while sending subcall");
206 static int server_req_subscribe_cb(struct afb_xreq *xreq, struct afb_eventid *event)
209 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
211 rc = afb_evt_eventid_add_watch(wreq->stubws->listener, event);
213 rc = afb_proto_ws_call_subscribe(wreq->call, afb_evt_eventid_fullname(event), afb_evt_eventid_id(event));
215 ERROR("error while subscribing event");
219 static int server_req_unsubscribe_cb(struct afb_xreq *xreq, struct afb_eventid *event)
222 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
224 rc = afb_proto_ws_call_unsubscribe(wreq->call, afb_evt_eventid_fullname(event), afb_evt_eventid_id(event));
225 rc2 = afb_evt_eventid_remove_watch(wreq->stubws->listener, event);
226 if (rc >= 0 && rc2 < 0)
229 ERROR("error while unsubscribing event");
233 static const struct afb_xreq_query_itf server_req_xreq_itf = {
234 .success = server_req_success_cb,
235 .fail = server_req_fail_cb,
236 .unref = server_req_destroy_cb,
237 .subcall = server_req_subcall_cb,
238 .subscribe = server_req_subscribe_cb,
239 .unsubscribe = server_req_unsubscribe_cb
242 /******************* client part **********************************/
244 /* search the event */
245 static struct client_event *client_event_search(struct afb_stub_ws *stubws, uint32_t eventid, const char *name)
247 struct client_event *ev;
250 while (ev != NULL && (ev->id != eventid || 0 != strcmp(afb_evt_eventid_fullname(ev->eventid), name)))
256 /* on call, propagate it to the ws service */
257 static void client_call_cb(void * closure, struct afb_xreq *xreq)
259 struct afb_stub_ws *stubws = closure;
261 afb_proto_ws_client_call(stubws->proto, xreq->request.verb, afb_xreq_json(xreq), afb_session_uuid(xreq->context.session), xreq);
262 afb_xreq_unhooked_addref(xreq);
265 static void client_on_description_cb(void *closure, struct json_object *data)
267 struct client_describe *desc = closure;
270 jobs_leave(desc->jobloop);
273 static void client_send_describe_cb(int signum, void *closure, struct jobloop *jobloop)
275 struct client_describe *desc = closure;
280 desc->jobloop = jobloop;
281 afb_proto_ws_client_describe(desc->stubws->proto, client_on_description_cb, desc);
285 /* get the description */
286 static struct json_object *client_describe_cb(void * closure)
288 struct client_describe desc;
290 /* synchronous job: send the request and wait its result */
291 desc.stubws = closure;
293 jobs_enter(NULL, 0, client_send_describe_cb, &desc);
297 /******************* server part: manage events **********************************/
299 static void server_event_add(void *closure, const char *event, int eventid)
301 struct afb_stub_ws *stubws = closure;
303 afb_proto_ws_server_event_create(stubws->proto, event, eventid);
306 static void server_event_remove(void *closure, const char *event, int eventid)
308 struct afb_stub_ws *stubws = closure;
310 afb_proto_ws_server_event_remove(stubws->proto, event, eventid);
313 static void server_event_push(void *closure, const char *event, int eventid, struct json_object *object)
315 struct afb_stub_ws *stubws = closure;
317 afb_proto_ws_server_event_push(stubws->proto, event, eventid, object);
318 json_object_put(object);
321 static void server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object)
323 struct afb_stub_ws *stubws = closure;
325 afb_proto_ws_server_event_broadcast(stubws->proto, event, object);
326 json_object_put(object);
329 /*****************************************************/
331 static void on_reply_success(void *closure, void *request, struct json_object *result, const char *info)
333 struct afb_xreq *xreq = request;
335 afb_xreq_success(xreq, result, *info ? info : NULL);
336 afb_xreq_unhooked_unref(xreq);
339 static void on_reply_fail(void *closure, void *request, const char *status, const char *info)
341 struct afb_xreq *xreq = request;
343 afb_xreq_fail(xreq, status, *info ? info : NULL);
344 afb_xreq_unhooked_unref(xreq);
347 static void on_event_create(void *closure, const char *event_name, int event_id)
349 struct afb_stub_ws *stubws = closure;
350 struct client_event *ev;
352 /* check conflicts */
353 ev = client_event_search(stubws, event_id, event_name);
359 /* no conflict, try to add it */
360 ev = malloc(sizeof *ev);
362 ev->eventid = afb_evt_eventid_create(event_name);
363 if (ev->eventid != NULL) {
366 ev->next = stubws->events;
372 ERROR("can't create event %s, out of memory", event_name);
375 static void on_event_remove(void *closure, const char *event_name, int event_id)
377 struct afb_stub_ws *stubws = closure;
378 struct client_event *ev, **prv;
380 /* check conflicts */
381 ev = client_event_search(stubws, event_id, event_name);
385 /* decrease the reference count */
389 /* unlinks the event */
390 prv = &stubws->events;
395 /* destroys the event */
396 afb_evt_eventid_unref(ev->eventid);
400 static void on_event_subscribe(void *closure, void *request, const char *event_name, int event_id)
402 struct afb_stub_ws *stubws = closure;
403 struct afb_xreq *xreq = request;
404 struct client_event *ev;
406 /* check conflicts */
407 ev = client_event_search(stubws, event_id, event_name);
411 if (afb_xreq_subscribe(xreq, ev->eventid) < 0)
412 ERROR("can't subscribe: %m");
415 static void on_event_unsubscribe(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_unsubscribe(xreq, ev->eventid) < 0)
427 ERROR("can't unsubscribe: %m");
430 static void on_event_push(void *closure, const char *event_name, int event_id, struct json_object *data)
432 struct afb_stub_ws *stubws = closure;
433 struct client_event *ev;
435 /* check conflicts */
436 ev = client_event_search(stubws, event_id, event_name);
438 afb_evt_eventid_push(ev->eventid, data);
440 ERROR("unreadable push event");
443 static void on_event_broadcast(void *closure, const char *event_name, struct json_object *data)
445 afb_evt_broadcast(event_name, data);
448 static void client_subcall_reply_cb(void *closure, int status, json_object *object, struct afb_request *request)
450 struct afb_proto_ws_subcall *subcall = closure;
451 afb_proto_ws_subcall_reply(subcall, status, object);
454 static void on_subcall(void *closure, struct afb_proto_ws_subcall *subcall, void *request, const char *api, const char *verb, struct json_object *args)
456 struct afb_xreq *xreq = request;
458 afb_xreq_subcall(xreq, api, verb, args, client_subcall_reply_cb, subcall);
461 /*****************************************************/
463 static void on_call(void *closure, struct afb_proto_ws_call *call, const char *verb, struct json_object *args, const char *sessionid)
465 struct afb_stub_ws *stubws = closure;
466 struct server_req *wreq;
468 afb_stub_ws_addref(stubws);
470 /* create the request */
471 wreq = malloc(sizeof *wreq);
475 afb_xreq_init(&wreq->xreq, &server_req_xreq_itf);
476 wreq->stubws = stubws;
479 /* init the context */
480 if (afb_context_connect(&wreq->xreq.context, sessionid, NULL) < 0)
482 wreq->xreq.context.validated = 1;
485 wreq->xreq.cred = afb_cred_addref(stubws->cred);
486 wreq->xreq.request.api = stubws->apiname;
487 wreq->xreq.request.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_evt_eventid_unref(ev->eventid);
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(afb_common_get_event_loop(), fd, &client_itf, stubws);
616 stubws->proto = afb_proto_ws_create_server(afb_common_get_event_loop(), 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));