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.
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-x2.h>
39 #include "afb-session.h"
42 #include "afb-apiset.h"
43 #include "afb-proto-ws.h"
44 #include "afb-stub-ws.h"
45 #include "afb-context.h"
56 * structure for recording calls on client side
59 struct client_call *next; /* the next call */
60 struct afb_stub_ws *stubws; /* the stub_ws */
61 struct afb_xreq *xreq; /* the request handle */
62 uint32_t msgid; /* the message identifier */
66 * structure for a ws request
69 struct afb_xreq xreq; /* the xreq */
70 struct afb_stub_ws *stubws; /* the client of the request */
71 struct afb_proto_ws_call *call; /* the incoming call */
75 * structure for recording events on client side
79 struct client_event *next;
80 struct afb_event_x2 *event;
86 * structure for recording describe requests
88 struct client_describe
90 struct afb_stub_ws *stubws;
91 struct jobloop *jobloop;
92 struct json_object *result;
96 * structure for jobs of describing
98 struct server_describe
100 struct afb_stub_ws *stubws;
101 struct afb_proto_ws_describe *describe;
105 * structure for recording sessions
107 struct server_session
109 struct server_session *next;
110 struct afb_session *session;
113 /******************* stub description for client or servers ******************/
117 /* count of references */
120 /* resource control */
121 pthread_mutex_t mutex;
124 struct afb_proto_ws *proto;
126 /* listener for events (server side) */
127 struct afb_evt_listener *listener;
129 /* event replica (client side) */
130 struct client_event *events;
132 /* credentials of the client (server side) */
133 struct afb_cred *cred;
135 /* sessions (server side) */
136 struct server_session *sessions;
139 struct afb_apiset *apiset;
141 /* on hangup callback */
142 void (*on_hangup)(struct afb_stub_ws *);
148 /******************* ws request part for server *****************/
150 /* decrement the reference count of the request and free/release it on falling to null */
151 static void server_req_destroy_cb(struct afb_xreq *xreq)
153 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
155 afb_context_disconnect(&wreq->xreq.context);
156 afb_cred_unref(wreq->xreq.cred);
157 json_object_put(wreq->xreq.json);
158 afb_proto_ws_call_unref(wreq->call);
159 afb_stub_ws_unref(wreq->stubws);
163 static void server_req_reply_cb(struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info)
166 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
168 rc = afb_proto_ws_call_reply(wreq->call, obj, error, info);
170 ERROR("error while sending reply");
171 json_object_put(obj);
174 static int server_req_subscribe_cb(struct afb_xreq *xreq, struct afb_event_x2 *event)
177 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
179 rc = afb_evt_event_x2_add_watch(wreq->stubws->listener, event);
181 rc = afb_proto_ws_call_subscribe(wreq->call, afb_evt_event_x2_fullname(event), afb_evt_event_x2_id(event));
183 ERROR("error while subscribing event");
187 static int server_req_unsubscribe_cb(struct afb_xreq *xreq, struct afb_event_x2 *event)
190 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
192 rc = afb_proto_ws_call_unsubscribe(wreq->call, afb_evt_event_x2_fullname(event), afb_evt_event_x2_id(event));
193 rc2 = afb_evt_event_x2_remove_watch(wreq->stubws->listener, event);
194 if (rc >= 0 && rc2 < 0)
197 ERROR("error while unsubscribing event");
201 static const struct afb_xreq_query_itf server_req_xreq_itf = {
202 .reply = server_req_reply_cb,
203 .unref = server_req_destroy_cb,
204 .subscribe = server_req_subscribe_cb,
205 .unsubscribe = server_req_unsubscribe_cb
208 /******************* client part **********************************/
210 /* search the event */
211 static struct client_event *client_event_search(struct afb_stub_ws *stubws, uint32_t eventid, const char *name)
213 struct client_event *ev;
216 while (ev != NULL && (ev->id != eventid || 0 != strcmp(afb_evt_event_x2_fullname(ev->event), name)))
222 /* on call, propagate it to the ws service */
223 static void client_call_cb(void * closure, struct afb_xreq *xreq)
225 struct afb_stub_ws *stubws = closure;
227 afb_proto_ws_client_call(
229 xreq->request.called_verb,
231 afb_session_uuid(xreq->context.session),
233 xreq_on_behalf_cred_export(xreq));
234 afb_xreq_unhooked_addref(xreq);
237 static void client_on_description_cb(void *closure, struct json_object *data)
239 struct client_describe *desc = closure;
242 jobs_leave(desc->jobloop);
245 static void client_send_describe_cb(int signum, void *closure, struct jobloop *jobloop)
247 struct client_describe *desc = closure;
252 desc->jobloop = jobloop;
253 afb_proto_ws_client_describe(desc->stubws->proto, client_on_description_cb, desc);
257 /* get the description */
258 static struct json_object *client_describe_cb(void * closure)
260 struct client_describe desc;
262 /* synchronous job: send the request and wait its result */
263 desc.stubws = closure;
265 jobs_enter(NULL, 0, client_send_describe_cb, &desc);
269 /******************* server part: manage events **********************************/
271 static void server_event_add(void *closure, const char *event, int eventid)
273 struct afb_stub_ws *stubws = closure;
275 afb_proto_ws_server_event_create(stubws->proto, event, eventid);
278 static void server_event_remove(void *closure, const char *event, int eventid)
280 struct afb_stub_ws *stubws = closure;
282 afb_proto_ws_server_event_remove(stubws->proto, event, eventid);
285 static void server_event_push(void *closure, const char *event, int eventid, struct json_object *object)
287 struct afb_stub_ws *stubws = closure;
289 afb_proto_ws_server_event_push(stubws->proto, event, eventid, object);
290 json_object_put(object);
293 static void server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object)
295 struct afb_stub_ws *stubws = closure;
297 afb_proto_ws_server_event_broadcast(stubws->proto, event, object);
298 json_object_put(object);
301 /*****************************************************/
303 static void on_reply(void *closure, void *request, struct json_object *object, const char *error, const char *info)
305 struct afb_xreq *xreq = request;
307 afb_xreq_reply(xreq, object, error, info);
308 afb_xreq_unhooked_unref(xreq);
311 static void on_event_create(void *closure, const char *event_name, int event_id)
313 struct afb_stub_ws *stubws = closure;
314 struct client_event *ev;
316 /* check conflicts */
317 ev = client_event_search(stubws, event_id, event_name);
323 /* no conflict, try to add it */
324 ev = malloc(sizeof *ev);
326 ev->event = afb_evt_event_x2_create(event_name);
327 if (ev->event != NULL) {
330 ev->next = stubws->events;
336 ERROR("can't create event %s, out of memory", event_name);
339 static void on_event_remove(void *closure, const char *event_name, int event_id)
341 struct afb_stub_ws *stubws = closure;
342 struct client_event *ev, **prv;
344 /* check conflicts */
345 ev = client_event_search(stubws, event_id, event_name);
349 /* decrease the reference count */
353 /* unlinks the event */
354 prv = &stubws->events;
359 /* destroys the event */
360 afb_evt_event_x2_unref(ev->event);
364 static void on_event_subscribe(void *closure, void *request, const char *event_name, int event_id)
366 struct afb_stub_ws *stubws = closure;
367 struct afb_xreq *xreq = request;
368 struct client_event *ev;
370 /* check conflicts */
371 ev = client_event_search(stubws, event_id, event_name);
375 if (afb_xreq_subscribe(xreq, ev->event) < 0)
376 ERROR("can't subscribe: %m");
379 static void on_event_unsubscribe(void *closure, void *request, const char *event_name, int event_id)
381 struct afb_stub_ws *stubws = closure;
382 struct afb_xreq *xreq = request;
383 struct client_event *ev;
385 /* check conflicts */
386 ev = client_event_search(stubws, event_id, event_name);
390 if (afb_xreq_unsubscribe(xreq, ev->event) < 0)
391 ERROR("can't unsubscribe: %m");
394 static void on_event_push(void *closure, const char *event_name, int event_id, struct json_object *data)
396 struct afb_stub_ws *stubws = closure;
397 struct client_event *ev;
399 /* check conflicts */
400 ev = client_event_search(stubws, event_id, event_name);
402 afb_evt_event_x2_push(ev->event, data);
404 ERROR("unreadable push event");
407 static void on_event_broadcast(void *closure, const char *event_name, struct json_object *data)
409 afb_evt_broadcast(event_name, data);
412 /*****************************************************/
414 static void record_session(struct afb_stub_ws *stubws, struct afb_session *session)
416 struct server_session *s, **prv;
419 prv = &stubws->sessions;
421 if (s->session == session)
423 if (afb_session_is_closed(s->session)) {
425 afb_session_unref(s->session);
433 s = malloc(sizeof *s);
435 s->session = afb_session_addref(session);
436 s->next = stubws->sessions;
437 stubws->sessions = s;
441 static void release_all_sessions(struct afb_stub_ws *stubws)
443 struct server_session *s, *n;
445 s = stubws->sessions;
446 stubws->sessions = NULL;
449 afb_session_unref(s->session);
455 /*****************************************************/
457 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)
459 struct afb_stub_ws *stubws = closure;
460 struct server_req *wreq;
462 afb_stub_ws_addref(stubws);
464 /* create the request */
465 wreq = malloc(sizeof *wreq);
469 afb_xreq_init(&wreq->xreq, &server_req_xreq_itf);
470 wreq->stubws = stubws;
473 /* init the context */
474 if (afb_context_connect(&wreq->xreq.context, sessionid, NULL) < 0)
476 wreq->xreq.context.validated = 1;
477 record_session(stubws, wreq->xreq.context.session);
478 if (wreq->xreq.context.created)
479 afb_session_set_autoclose(wreq->xreq.context.session, 1);
482 wreq->xreq.cred = afb_cred_mixed_on_behalf_import(stubws->cred, sessionid, user_creds);
483 wreq->xreq.request.called_api = stubws->apiname;
484 wreq->xreq.request.called_verb = verb;
485 wreq->xreq.json = args;
486 afb_xreq_process(&wreq->xreq, stubws->apiset);
492 json_object_put(args);
493 afb_stub_ws_unref(stubws);
494 afb_proto_ws_call_reply(call, NULL, "internal-error", NULL);
495 afb_proto_ws_call_unref(call);
498 static void server_describe_sjob(int signum, void *closure)
500 struct json_object *obj;
501 struct server_describe *desc = closure;
503 /* get the description if possible */
504 obj = !signum ? afb_apiset_describe(desc->stubws->apiset, desc->stubws->apiname) : NULL;
507 afb_proto_ws_describe_put(desc->describe, obj);
508 json_object_put(obj);
509 afb_stub_ws_unref(desc->stubws);
512 static void server_describe_job(int signum, void *closure)
514 server_describe_sjob(signum, closure);
518 static void on_describe(void *closure, struct afb_proto_ws_describe *describe)
520 struct server_describe *desc, sdesc;
521 struct afb_stub_ws *stubws = closure;
523 /* allocate (if possible) and init */
524 desc = malloc(sizeof *desc);
527 desc->stubws = stubws;
528 desc->describe = describe;
529 afb_stub_ws_addref(stubws);
533 jobs_call(NULL, 0, server_describe_sjob, desc);
535 if (jobs_queue(NULL, 0, server_describe_job, desc) < 0)
536 jobs_call(NULL, 0, server_describe_job, desc);
540 /*****************************************************/
542 static const struct afb_proto_ws_client_itf client_itf =
544 .on_reply = on_reply,
545 .on_event_create = on_event_create,
546 .on_event_remove = on_event_remove,
547 .on_event_subscribe = on_event_subscribe,
548 .on_event_unsubscribe = on_event_unsubscribe,
549 .on_event_push = on_event_push,
550 .on_event_broadcast = on_event_broadcast,
553 static const struct afb_proto_ws_server_itf server_itf =
556 .on_describe = on_describe
559 static struct afb_api_itf ws_api_itf = {
560 .call = client_call_cb,
561 .describe = client_describe_cb
564 /* the interface for events pushing */
565 static const struct afb_evt_itf server_evt_itf = {
566 .broadcast = server_event_broadcast,
567 .push = server_event_push,
568 .add = server_event_add,
569 .remove = server_event_remove
572 /*****************************************************/
574 static void drop_all_events(struct afb_stub_ws *stubws)
576 struct client_event *ev, *nxt;
579 stubws->events = NULL;
583 afb_evt_event_x2_unref(ev->event);
589 /* callback when receiving a hangup */
590 static void on_hangup(void *closure)
592 struct afb_stub_ws *stubws = closure;
594 afb_stub_ws_addref(stubws);
595 if (stubws->on_hangup)
596 stubws->on_hangup(stubws);
598 release_all_sessions(stubws);
599 afb_stub_ws_unref(stubws);
602 /*****************************************************/
604 static struct afb_stub_ws *afb_stub_ws_create(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset, int client)
606 struct afb_stub_ws *stubws;
608 stubws = calloc(1, sizeof *stubws + strlen(apiname));
613 stubws->proto = afb_proto_ws_create_client(fdev, &client_itf, stubws);
615 stubws->proto = afb_proto_ws_create_server(fdev, &server_itf, stubws);
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);
630 struct afb_stub_ws *afb_stub_ws_create_client(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset)
632 return afb_stub_ws_create(fdev, apiname, apiset, 1);
635 struct afb_stub_ws *afb_stub_ws_create_server(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset)
637 struct afb_stub_ws *stubws;
639 stubws = afb_stub_ws_create(fdev, apiname, apiset, 0);
641 stubws->cred = afb_cred_create_for_socket(fdev_fd(fdev));
642 stubws->listener = afb_evt_listener_create(&server_evt_itf, stubws);
643 if (stubws->listener != NULL)
645 afb_stub_ws_unref(stubws);
650 void afb_stub_ws_unref(struct afb_stub_ws *stubws)
652 if (!__atomic_sub_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED)) {
653 drop_all_events(stubws);
654 if (stubws->listener)
655 afb_evt_listener_unref(stubws->listener);
656 release_all_sessions(stubws);
657 afb_proto_ws_unref(stubws->proto);
658 afb_cred_unref(stubws->cred);
659 afb_apiset_unref(stubws->apiset);
664 void afb_stub_ws_addref(struct afb_stub_ws *stubws)
666 __atomic_add_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED);
669 void afb_stub_ws_on_hangup(struct afb_stub_ws *stubws, void (*on_hangup)(struct afb_stub_ws*))
671 stubws->on_hangup = on_hangup;
674 const char *afb_stub_ws_name(struct afb_stub_ws *stubws)
676 return stubws->apiname;
679 struct afb_api_item afb_stub_ws_client_api(struct afb_stub_ws *stubws)
681 struct afb_api_item api;
683 assert(!stubws->listener); /* check client */
684 api.closure = stubws;
685 api.itf = &ws_api_itf;
690 int afb_stub_ws_client_add(struct afb_stub_ws *stubws, struct afb_apiset *apiset)
692 return afb_apiset_add(apiset, stubws->apiname, afb_stub_ws_client_api(stubws));