5e57dba869ed658f586351d65a209583835192a5
[src/app-framework-binder.git] / src / afb-stub-ws.c
1 /*
2  * Copyright (C) 2015-2018 "IoT.bzh"
3  * Author José Bollo <jose.bollo@iot.bzh>
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #define _GNU_SOURCE
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <assert.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <endian.h>
27 #include <netdb.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <sys/un.h>
31 #include <pthread.h>
32
33 #include <json-c/json.h>
34
35 #include <afb/afb-event-x2.h>
36
37 #include "afb-session.h"
38 #include "afb-cred.h"
39 #include "afb-api.h"
40 #include "afb-apiset.h"
41 #include "afb-proto-ws.h"
42 #include "afb-stub-ws.h"
43 #include "afb-context.h"
44 #include "afb-evt.h"
45 #include "afb-xreq.h"
46 #include "verbose.h"
47 #include "fdev.h"
48 #include "jobs.h"
49
50 struct afb_stub_ws;
51
52
53 /**
54  * structure for a ws request: requests on server side
55  */
56 struct server_req {
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 */
60 };
61
62 /**
63  * structure for recording events on client side
64  */
65 struct client_event
66 {
67         struct client_event *next;      /**< link to the next */
68         struct afb_event_x2 *event;     /**< the local event */
69         int id;                         /**< the identifier */
70         int refcount;                   /**< a reference count */
71 };
72
73 /**
74  * structure for recording describe requests on the client side
75  */
76 struct client_describe
77 {
78         struct afb_stub_ws *stubws;     /**< the stub */
79         struct jobloop *jobloop;        /**< the jobloop to leave */
80         struct json_object *result;     /**< result */
81 };
82
83 /**
84  * structure for jobs of describing
85  */
86 struct server_describe
87 {
88         struct afb_stub_ws *stubws;
89         struct afb_proto_ws_describe *describe;
90 };
91
92 /**
93  * structure for recording sessions
94  */
95 struct server_session
96 {
97         struct server_session *next;
98         struct afb_session *session;
99 };
100
101 /******************* stub description for client or servers ******************/
102
103 struct afb_stub_ws
104 {
105         /* protocol */
106         struct afb_proto_ws *proto;
107
108         /* apiset */
109         struct afb_apiset *apiset;
110
111         /* on hangup callback */
112         void (*on_hangup)(struct afb_stub_ws *);
113
114         union {
115                 /* server side */
116                 struct {
117                         /* listener for events */
118                         struct afb_evt_listener *listener;
119
120                         /* sessions */
121                         struct server_session *sessions;
122
123                         /* credentials of the client */
124                         struct afb_cred *cred;
125                 };
126
127                 /* client side */
128                 struct {
129                         /* event replica  */
130                         struct client_event *events;
131
132                         /* robustify */
133                         struct {
134                                 struct fdev *(*reopen)(void*);
135                                 void *closure;
136                                 void (*release)(void*);
137                         } robust;
138                 };
139         };
140
141         /* count of references */
142         unsigned refcount;
143
144         /* type of the stub: 0=server, 1=client */
145         uint8_t is_client;
146
147         /* the api name */
148         char apiname[1];
149 };
150
151 static struct afb_proto_ws *afb_stub_ws_create_proto(struct afb_stub_ws *stubws, struct fdev *fdev, uint8_t server);
152
153 /******************* ws request part for server *****************/
154
155 /* decrement the reference count of the request and free/release it on falling to null */
156 static void server_req_destroy_cb(struct afb_xreq *xreq)
157 {
158         struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
159
160         afb_context_disconnect(&wreq->xreq.context);
161         afb_cred_unref(wreq->xreq.cred);
162         json_object_put(wreq->xreq.json);
163         afb_proto_ws_call_unref(wreq->call);
164         afb_stub_ws_unref(wreq->stubws);
165         free(wreq);
166 }
167
168 static void server_req_reply_cb(struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info)
169 {
170         int rc;
171         struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
172
173         rc = afb_proto_ws_call_reply(wreq->call, obj, error, info);
174         if (rc < 0)
175                 ERROR("error while sending reply");
176         json_object_put(obj);
177 }
178
179 static int server_req_subscribe_cb(struct afb_xreq *xreq, struct afb_event_x2 *event)
180 {
181         int rc;
182         struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
183
184         rc = afb_evt_event_x2_add_watch(wreq->stubws->listener, event);
185         if (rc >= 0)
186                 rc = afb_proto_ws_call_subscribe(wreq->call,  afb_evt_event_x2_fullname(event), afb_evt_event_x2_id(event));
187         if (rc < 0)
188                 ERROR("error while subscribing event");
189         return rc;
190 }
191
192 static int server_req_unsubscribe_cb(struct afb_xreq *xreq, struct afb_event_x2 *event)
193 {
194         int rc, rc2;
195         struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
196
197         rc = afb_proto_ws_call_unsubscribe(wreq->call,  afb_evt_event_x2_fullname(event), afb_evt_event_x2_id(event));
198         rc2 = afb_evt_event_x2_remove_watch(wreq->stubws->listener, event);
199         if (rc >= 0 && rc2 < 0)
200                 rc = rc2;
201         if (rc < 0)
202                 ERROR("error while unsubscribing event");
203         return rc;
204 }
205
206 static const struct afb_xreq_query_itf server_req_xreq_itf = {
207         .reply = server_req_reply_cb,
208         .unref = server_req_destroy_cb,
209         .subscribe = server_req_subscribe_cb,
210         .unsubscribe = server_req_unsubscribe_cb
211 };
212
213 /******************* client part **********************************/
214
215 /* destroy all events */
216 static void client_drop_all_events(struct afb_stub_ws *stubws)
217 {
218         struct client_event *ev, *nxt;
219
220         nxt = __atomic_exchange_n(&stubws->events, NULL, __ATOMIC_RELAXED);
221         while (nxt) {
222                 ev = nxt;
223                 nxt = ev->next;
224                 afb_evt_event_x2_unref(ev->event);
225                 free(ev);
226         }
227 }
228
229 /* search the event */
230 static struct client_event *client_event_search(struct afb_stub_ws *stubws, uint32_t eventid, const char *name)
231 {
232         struct client_event *ev;
233
234         ev = stubws->events;
235         while (ev != NULL && (ev->id != eventid || 0 != strcmp(afb_evt_event_x2_fullname(ev->event), name)))
236                 ev = ev->next;
237
238         DEBUG("searching event %s[%d]: %s", name, eventid, ev ? "found" : "not found");
239         return ev;
240 }
241
242 static struct afb_proto_ws *client_get_proto(struct afb_stub_ws *stubws)
243 {
244         struct fdev *fdev;
245         struct afb_proto_ws *proto;
246
247         proto = stubws->proto;
248         if (proto == NULL && stubws->robust.reopen) {
249                 fdev = stubws->robust.reopen(stubws->robust.closure);
250                 if (fdev != NULL)
251                         proto = afb_stub_ws_create_proto(stubws, fdev, 0);
252         }
253         return proto;
254 }
255
256 /* on call, propagate it to the ws service */
257 static void client_api_call_cb(void * closure, struct afb_xreq *xreq)
258 {
259         int rc;
260         struct afb_stub_ws *stubws = closure;
261         struct afb_proto_ws *proto;
262
263         proto = client_get_proto(stubws);
264         if (proto == NULL) {
265                 afb_xreq_reply(xreq, NULL, "disconnected", "server hung up");
266                 return;
267         }
268
269         afb_xreq_unhooked_addref(xreq);
270         rc = afb_proto_ws_client_call(
271                         proto,
272                         xreq->request.called_verb,
273                         afb_xreq_json(xreq),
274                         afb_session_uuid(xreq->context.session),
275                         xreq,
276                         xreq_on_behalf_cred_export(xreq));
277         if (rc < 0) {
278                 afb_xreq_reply(xreq, NULL, "internal", "can't send message");
279                 afb_xreq_unhooked_unref(xreq);
280         }
281 }
282
283 static void client_on_description_cb(void *closure, struct json_object *data)
284 {
285         struct client_describe *desc = closure;
286
287         desc->result = data;
288         jobs_leave(desc->jobloop);
289 }
290
291 static void client_send_describe_cb(int signum, void *closure, struct jobloop *jobloop)
292 {
293         struct client_describe *desc = closure;
294         struct afb_proto_ws *proto;
295
296         proto = client_get_proto(desc->stubws);
297         if (signum || proto == NULL)
298                 jobs_leave(jobloop);
299         else {
300                 desc->jobloop = jobloop;
301                 afb_proto_ws_client_describe(proto, client_on_description_cb, desc);
302         }
303 }
304
305 /* get the description */
306 static struct json_object *client_api_describe_cb(void * closure)
307 {
308         struct client_describe desc;
309
310         /* synchronous job: send the request and wait its result */
311         desc.stubws = closure;
312         desc.result = NULL;
313         jobs_enter(NULL, 0, client_send_describe_cb, &desc);
314         return desc.result;
315 }
316
317 /******************* server part: manage events **********************************/
318
319 static void server_event_add_cb(void *closure, const char *event, int eventid)
320 {
321         struct afb_stub_ws *stubws = closure;
322
323         if (stubws->proto != NULL)
324                 afb_proto_ws_server_event_create(stubws->proto, event, eventid);
325 }
326
327 static void server_event_remove_cb(void *closure, const char *event, int eventid)
328 {
329         struct afb_stub_ws *stubws = closure;
330
331         if (stubws->proto != NULL)
332                 afb_proto_ws_server_event_remove(stubws->proto, event, eventid);
333 }
334
335 static void server_event_push_cb(void *closure, const char *event, int eventid, struct json_object *object)
336 {
337         struct afb_stub_ws *stubws = closure;
338
339         if (stubws->proto != NULL)
340                 afb_proto_ws_server_event_push(stubws->proto, event, eventid, object);
341         json_object_put(object);
342 }
343
344 static void server_event_broadcast_cb(void *closure, const char *event, int eventid, struct json_object *object)
345 {
346         struct afb_stub_ws *stubws = closure;
347
348         if (stubws->proto != NULL)
349                 afb_proto_ws_server_event_broadcast(stubws->proto, event, object);
350         json_object_put(object);
351 }
352
353 /*****************************************************/
354
355 static void client_on_reply_cb(void *closure, void *request, struct json_object *object, const char *error, const char *info)
356 {
357         struct afb_xreq *xreq = request;
358
359         afb_xreq_reply(xreq, object, error, info);
360         afb_xreq_unhooked_unref(xreq);
361 }
362
363 static void client_on_event_create_cb(void *closure, const char *event_name, int event_id)
364 {
365         struct afb_stub_ws *stubws = closure;
366         struct client_event *ev;
367
368         /* check conflicts */
369         ev = client_event_search(stubws, event_id, event_name);
370         if (ev != NULL) {
371                 __atomic_add_fetch(&ev->refcount, 1, __ATOMIC_RELAXED);
372                 return;
373         }
374
375         /* no conflict, try to add it */
376         ev = malloc(sizeof *ev);
377         if (ev != NULL) {
378                 ev->event = afb_evt_event_x2_create(event_name);
379                 if (ev->event != NULL) {
380                         ev->refcount = 1;
381                         ev->id = event_id;
382                         ev->next = stubws->events;
383                         stubws->events = ev;
384                         return;
385                 }
386                 free(ev);
387         }
388         ERROR("can't create event %s, out of memory", event_name);
389 }
390
391 static void client_on_event_remove_cb(void *closure, const char *event_name, int event_id)
392 {
393         struct afb_stub_ws *stubws = closure;
394         struct client_event *ev, **prv;
395
396         /* check conflicts */
397         ev = client_event_search(stubws, event_id, event_name);
398         if (ev == NULL)
399                 return;
400
401         /* decrease the reference count */
402
403         if (__atomic_sub_fetch(&ev->refcount, 1, __ATOMIC_RELAXED))
404                 return;
405
406         /* unlinks the event */
407         prv = &stubws->events;
408         while (*prv != ev)
409                 prv = &(*prv)->next;
410         *prv = ev->next;
411
412         /* destroys the event */
413         afb_evt_event_x2_unref(ev->event);
414         free(ev);
415 }
416
417 static void client_on_event_subscribe_cb(void *closure, void *request, const char *event_name, int event_id)
418 {
419         struct afb_stub_ws *stubws = closure;
420         struct afb_xreq *xreq = request;
421         struct client_event *ev;
422
423         /* check conflicts */
424         ev = client_event_search(stubws, event_id, event_name);
425         if (ev == NULL)
426                 return;
427
428         if (afb_xreq_subscribe(xreq, ev->event) < 0)
429                 ERROR("can't subscribe: %m");
430 }
431
432 static void client_on_event_unsubscribe_cb(void *closure, void *request, const char *event_name, int event_id)
433 {
434         struct afb_stub_ws *stubws = closure;
435         struct afb_xreq *xreq = request;
436         struct client_event *ev;
437
438         /* check conflicts */
439         ev = client_event_search(stubws, event_id, event_name);
440         if (ev == NULL)
441                 return;
442
443         if (afb_xreq_unsubscribe(xreq, ev->event) < 0)
444                 ERROR("can't unsubscribe: %m");
445 }
446
447 static void client_on_event_push_cb(void *closure, const char *event_name, int event_id, struct json_object *data)
448 {
449         struct afb_stub_ws *stubws = closure;
450         struct client_event *ev;
451
452         /* check conflicts */
453         ev = client_event_search(stubws, event_id, event_name);
454         if (ev)
455                 afb_evt_event_x2_push(ev->event, data);
456         else
457                 ERROR("unreadable push event");
458 }
459
460 static void client_on_event_broadcast_cb(void *closure, const char *event_name, struct json_object *data)
461 {
462         afb_evt_broadcast(event_name, data);
463 }
464
465 /*****************************************************/
466
467 static void server_record_session(struct afb_stub_ws *stubws, struct afb_session *session)
468 {
469         struct server_session *s, **prv;
470
471         /* search */
472         prv = &stubws->sessions;
473         while ((s = *prv)) {
474                 if (s->session == session)
475                         return;
476                 if (afb_session_is_closed(s->session)) {
477                         *prv = s->next;
478                         afb_session_unref(s->session);
479                         free(s);
480                 }
481                 else
482                         prv = &s->next;
483         }
484
485         /* create */
486         s = malloc(sizeof *s);
487         if (s) {
488                 s->session = afb_session_addref(session);
489                 s->next = stubws->sessions;
490                 stubws->sessions = s;
491         }
492 }
493
494 static void server_release_all_sessions(struct afb_stub_ws *stubws)
495 {
496         struct server_session *ses, *nses;
497
498         nses = __atomic_exchange_n(&stubws->sessions, NULL, __ATOMIC_RELAXED);
499         while(nses) {
500                 ses = nses;
501                 nses = ses->next;
502                 afb_session_unref(ses->session);
503                 free(ses);
504         }
505 }
506
507 /*****************************************************/
508
509 static void server_on_call_cb(void *closure, struct afb_proto_ws_call *call, const char *verb, struct json_object *args, const char *sessionid, const char *user_creds)
510 {
511         struct afb_stub_ws *stubws = closure;
512         struct server_req *wreq;
513
514         afb_stub_ws_addref(stubws);
515
516         /* create the request */
517         wreq = malloc(sizeof *wreq);
518         if (wreq == NULL)
519                 goto out_of_memory;
520
521         afb_xreq_init(&wreq->xreq, &server_req_xreq_itf);
522         wreq->stubws = stubws;
523         wreq->call = call;
524
525         /* init the context */
526         if (afb_context_connect(&wreq->xreq.context, sessionid, NULL) < 0)
527                 goto unconnected;
528         wreq->xreq.context.validated = 1;
529         server_record_session(stubws, wreq->xreq.context.session);
530         if (wreq->xreq.context.created)
531                 afb_session_set_autoclose(wreq->xreq.context.session, 1);
532
533         /* makes the call */
534         wreq->xreq.cred = afb_cred_mixed_on_behalf_import(stubws->cred, sessionid, user_creds);
535         wreq->xreq.request.called_api = stubws->apiname;
536         wreq->xreq.request.called_verb = verb;
537         wreq->xreq.json = args;
538         afb_xreq_process(&wreq->xreq, stubws->apiset);
539         return;
540
541 unconnected:
542         free(wreq);
543 out_of_memory:
544         json_object_put(args);
545         afb_stub_ws_unref(stubws);
546         afb_proto_ws_call_reply(call, NULL, "internal-error", NULL);
547         afb_proto_ws_call_unref(call);
548 }
549
550 static void server_describe_cb(int signum, void *closure)
551 {
552         struct json_object *obj;
553         struct server_describe *desc = closure;
554
555         /* get the description if possible */
556         obj = !signum ? afb_apiset_describe(desc->stubws->apiset, desc->stubws->apiname) : NULL;
557
558         /* send it */
559         afb_proto_ws_describe_put(desc->describe, obj);
560         json_object_put(obj);
561         afb_stub_ws_unref(desc->stubws);
562 }
563
564 static void server_describe_job(int signum, void *closure)
565 {
566         server_describe_cb(signum, closure);
567         free(closure);
568 }
569
570 static void server_on_describe_cb(void *closure, struct afb_proto_ws_describe *describe)
571 {
572         struct server_describe *desc, sdesc;
573         struct afb_stub_ws *stubws = closure;
574
575         /* allocate (if possible) and init */
576         desc = malloc(sizeof *desc);
577         if (desc == NULL)
578                 desc = &sdesc;
579         desc->stubws = stubws;
580         desc->describe = describe;
581         afb_stub_ws_addref(stubws);
582
583         /* process */
584         if (desc == &sdesc || jobs_queue(NULL, 0, server_describe_job, desc) < 0)
585                 jobs_call(NULL, 0, server_describe_cb, desc);
586 }
587
588 /*****************************************************/
589
590 static const struct afb_proto_ws_client_itf client_itf =
591 {
592         .on_reply = client_on_reply_cb,
593         .on_event_create = client_on_event_create_cb,
594         .on_event_remove = client_on_event_remove_cb,
595         .on_event_subscribe = client_on_event_subscribe_cb,
596         .on_event_unsubscribe = client_on_event_unsubscribe_cb,
597         .on_event_push = client_on_event_push_cb,
598         .on_event_broadcast = client_on_event_broadcast_cb,
599 };
600
601 static struct afb_api_itf client_api_itf = {
602         .call = client_api_call_cb,
603         .describe = client_api_describe_cb
604 };
605
606 static const struct afb_proto_ws_server_itf server_itf =
607 {
608         .on_call = server_on_call_cb,
609         .on_describe = server_on_describe_cb
610 };
611
612 /* the interface for events pushing */
613 static const struct afb_evt_itf server_event_itf = {
614         .broadcast = server_event_broadcast_cb,
615         .push = server_event_push_cb,
616         .add = server_event_add_cb,
617         .remove = server_event_remove_cb
618 };
619
620 /*****************************************************/
621
622 /* disconnect */
623 static void disconnect(struct afb_stub_ws *stubws)
624 {
625         afb_proto_ws_unref(__atomic_exchange_n(&stubws->proto, NULL, __ATOMIC_RELAXED));
626         if (stubws->is_client)
627                 client_drop_all_events(stubws);
628         else {
629                 afb_evt_listener_unref(__atomic_exchange_n(&stubws->listener, NULL, __ATOMIC_RELAXED));
630                 afb_cred_unref(__atomic_exchange_n(&stubws->cred, NULL, __ATOMIC_RELAXED));
631                 server_release_all_sessions(stubws);
632         }
633 }
634
635
636 /* callback when receiving a hangup */
637 static void on_hangup(void *closure)
638 {
639         struct afb_stub_ws *stubws = closure;
640
641         if (stubws->proto) {
642                 afb_stub_ws_addref(stubws);
643                 disconnect(stubws);
644                 if (stubws->on_hangup)
645                         stubws->on_hangup(stubws);
646                 afb_stub_ws_unref(stubws);
647         }
648 }
649
650 static int enqueue_processing(struct afb_proto_ws *proto, void (*callback)(int signum, void* arg), void *arg)
651 {
652         return jobs_queue(proto, 0, callback, arg);
653 }
654
655 /*****************************************************/
656
657 static struct afb_proto_ws *afb_stub_ws_create_proto(struct afb_stub_ws *stubws, struct fdev *fdev, uint8_t is_client)
658 {
659         struct afb_proto_ws *proto;
660
661         stubws->proto = proto = is_client
662                   ? afb_proto_ws_create_client(fdev, &client_itf, stubws)
663                   : afb_proto_ws_create_server(fdev, &server_itf, stubws);
664         if (proto) {
665                 afb_proto_ws_on_hangup(proto, on_hangup);
666                 afb_proto_ws_set_queuing(proto, enqueue_processing);
667         }
668
669         return proto;
670 }
671
672 static struct afb_stub_ws *afb_stub_ws_create(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset, uint8_t is_client)
673 {
674         struct afb_stub_ws *stubws;
675
676         stubws = calloc(1, sizeof *stubws + strlen(apiname));
677         if (stubws == NULL)
678                 errno = ENOMEM;
679         else {
680                 if (afb_stub_ws_create_proto(stubws, fdev, is_client)) {
681                         stubws->refcount = 1;
682                         stubws->is_client = is_client;
683                         strcpy(stubws->apiname, apiname);
684                         stubws->apiset = afb_apiset_addref(apiset);
685                         return stubws;
686                 }
687                 free(stubws);
688         }
689         fdev_unref(fdev);
690         return NULL;
691 }
692
693 struct afb_stub_ws *afb_stub_ws_create_client(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset)
694 {
695         return afb_stub_ws_create(fdev, apiname, apiset, 1);
696 }
697
698 struct afb_stub_ws *afb_stub_ws_create_server(struct fdev *fdev, const char *apiname, struct afb_apiset *apiset)
699 {
700         struct afb_stub_ws *stubws;
701
702         stubws = afb_stub_ws_create(fdev, apiname, apiset, 0);
703         if (stubws) {
704                 stubws->cred = afb_cred_create_for_socket(fdev_fd(fdev));
705                 stubws->listener = afb_evt_listener_create(&server_event_itf, stubws);
706                 if (stubws->listener != NULL)
707                         return stubws;
708                 afb_stub_ws_unref(stubws);
709         }
710         return NULL;
711 }
712
713 void afb_stub_ws_unref(struct afb_stub_ws *stubws)
714 {
715         if (stubws && !__atomic_sub_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED)) {
716
717                 if (stubws->is_client) {
718                         stubws->robust.reopen = NULL;
719                         if (stubws->robust.release)
720                                 stubws->robust.release(stubws->robust.closure);
721                 }
722
723                 disconnect(stubws);
724                 afb_apiset_unref(stubws->apiset);
725                 free(stubws);
726         }
727 }
728
729 void afb_stub_ws_addref(struct afb_stub_ws *stubws)
730 {
731         __atomic_add_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED);
732 }
733
734 void afb_stub_ws_set_on_hangup(struct afb_stub_ws *stubws, void (*on_hangup)(struct afb_stub_ws*))
735 {
736         stubws->on_hangup = on_hangup;
737 }
738
739 const char *afb_stub_ws_name(struct afb_stub_ws *stubws)
740 {
741         return stubws->apiname;
742 }
743
744 struct afb_api_item afb_stub_ws_client_api(struct afb_stub_ws *stubws)
745 {
746         struct afb_api_item api;
747
748         assert(stubws->is_client); /* check client */
749         api.closure = stubws;
750         api.itf = &client_api_itf;
751         api.group = stubws; /* serialize for reconnections */
752         return api;
753 }
754
755 int afb_stub_ws_client_add(struct afb_stub_ws *stubws, struct afb_apiset *apiset)
756 {
757         return afb_apiset_add(apiset, stubws->apiname, afb_stub_ws_client_api(stubws));
758 }
759
760 void afb_stub_ws_client_robustify(struct afb_stub_ws *stubws, struct fdev *(*reopen)(void*), void *closure, void (*release)(void*))
761 {
762         assert(stubws->is_client); /* check client */
763
764         if (stubws->robust.release)
765                 stubws->robust.release(stubws->robust.closure);
766
767         stubws->robust.reopen = reopen;
768         stubws->robust.closure = closure;
769         stubws->robust.release = release;
770 }