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.
19 #define NO_PLUGIN_VERBOSE_MACRO
29 #include <sys/types.h>
30 #include <sys/socket.h>
33 #include <json-c/json.h>
34 #include <systemd/sd-event.h>
36 #include <afb/afb-req-itf.h>
38 #include "afb-common.h"
40 #include "afb-session.h"
42 #include "afb-msg-json.h"
44 #include "afb-api-so.h"
45 #include "afb-context.h"
61 char *path; /* path of the object for the API */
62 char *api; /* api name of the interface */
63 int fd; /* file descriptor */
68 struct api_ws_event *events;
69 struct api_ws_memo *memos;
72 sd_event_source *listensrc;
73 struct afb_evt_listener *listener; /* listener for broadcasted events */
82 /******************* websocket interface for client part **********************************/
84 static void api_ws_client_on_binary(void *closure, char *data, size_t size);
86 static const struct afb_ws_itf api_ws_client_ws_itf =
90 .on_binary = api_ws_client_on_binary,
95 /******************* event structures for server part **********************************/
97 static void api_ws_server_event_add(void *closure, const char *event, int eventid);
98 static void api_ws_server_event_remove(void *closure, const char *event, int eventid);
99 static void api_ws_server_event_push(void *closure, const char *event, int eventid, struct json_object *object);
100 static void api_ws_server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object);
102 /* the interface for events pushing */
103 static const struct afb_evt_itf api_ws_server_evt_itf = {
104 .broadcast = api_ws_server_event_broadcast,
105 .push = api_ws_server_event_push,
106 .add = api_ws_server_event_add,
107 .remove = api_ws_server_event_remove
110 /******************* client description part for server *****************************/
114 /* the server ws-api */
117 /* count of references */
120 /* listener for events */
121 struct afb_evt_listener *listener;
123 /* file descriptor */
130 /******************* websocket interface for client part **********************************/
132 static void api_ws_server_on_binary(void *closure, char *data, size_t size);
133 static void api_ws_server_on_hangup(void *closure);
135 static const struct afb_ws_itf api_ws_server_ws_itf =
139 .on_binary = api_ws_server_on_binary,
141 .on_hangup = api_ws_server_on_hangup
144 /******************* ws request part for server *****************/
147 * structure for a ws request
149 struct api_ws_server_req {
150 struct afb_xreq xreq; /* the xreq */
151 struct api_ws_client *client; /* the client of the request */
152 char *rcvdata; /* the received data to free */
153 const char *request; /* the readen request as string */
154 size_t lenreq; /* the length of the request */
155 uint32_t msgid; /* the incoming request msgid */
158 static void api_ws_server_req_success_cb(void *closure, struct json_object *obj, const char *info);
159 static void api_ws_server_req_fail_cb(void *closure, const char *status, const char *info);
160 static void api_ws_server_req_destroy_cb(void *closure);
161 static int api_ws_server_req_subscribe_cb(void *closure, struct afb_event event);
162 static int api_ws_server_req_unsubscribe_cb(void *closure, struct afb_event event);
164 const struct afb_xreq_query_itf afb_api_ws_xreq_itf = {
165 .success = api_ws_server_req_success_cb,
166 .fail = api_ws_server_req_fail_cb,
167 .unref = api_ws_server_req_destroy_cb,
168 .subscribe = api_ws_server_req_subscribe_cb,
169 .unsubscribe = api_ws_server_req_unsubscribe_cb
172 /******************* common part **********************************/
175 * create a structure api_ws not connected to the 'path'.
177 static struct api_ws *api_ws_make(const char *path)
182 /* allocates the structure */
183 length = strlen(path);
184 api = calloc(1, sizeof *api + 1 + length);
190 /* path is copied after the struct */
191 api->path = (char*)(api+1);
192 memcpy(api->path, path, length + 1);
194 /* api name is at the end of the path */
195 while (length && path[length - 1] != '/' && path[length - 1] != ':')
197 api->api = &api->path[length];
198 if (api->api == NULL || !afb_apis_is_valid_api_name(api->api)) {
212 static int api_ws_socket_unix(const char *path, int server)
215 struct sockaddr_un addr;
218 length = strlen(path);
220 errno = ENAMETOOLONG;
227 fd = socket(AF_UNIX, SOCK_STREAM, 0);
231 memset(&addr, 0, sizeof addr);
232 addr.sun_family = AF_UNIX;
233 strcpy(addr.sun_path, path);
235 rc = bind(fd, (struct sockaddr *) &addr, (socklen_t)(sizeof addr));
237 rc = connect(fd, (struct sockaddr *) &addr, (socklen_t)(sizeof addr));
246 static int api_ws_socket_inet(const char *path, int server)
249 const char *service, *host, *api;
250 struct addrinfo hint, *rai, *iai;
253 api = strrchr(path, '/');
254 service = strrchr(path, ':');
255 if (api == NULL || service == NULL || api < service) {
259 host = strndupa(path, service++ - path);
260 service = strndupa(service, api - service);
263 memset(&hint, 0, sizeof hint);
264 hint.ai_family = AF_INET;
265 hint.ai_socktype = SOCK_STREAM;
266 rc = getaddrinfo(host, service, &hint, &rai);
274 while (iai != NULL) {
275 fd = socket(iai->ai_family, iai->ai_socktype, iai->ai_protocol);
278 rc = bind(fd, iai->ai_addr, iai->ai_addrlen);
280 rc = connect(fd, iai->ai_addr, iai->ai_addrlen);
295 static int api_ws_socket(const char *path, int server)
299 /* check for systemd socket */
300 if (0 == strncmp(path, "sd:", 3))
301 fd = sd_fds_for(path + 3);
303 /* check for unix socket */
304 if (0 == strncmp(path, "unix:", 5))
306 fd = api_ws_socket_unix(path + 5, server);
309 fd = api_ws_socket_inet(path, server);
311 if (fd >= 0 && server) {
313 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &rc, sizeof rc);
317 /* configure the socket */
319 fcntl(fd, F_SETFD, FD_CLOEXEC);
320 fcntl(fd, F_SETFL, O_NONBLOCK);
325 /******************* serialisation part **********************************/
332 #define WRITEBUF_COUNT_MAX 32
335 struct iovec iovec[WRITEBUF_COUNT_MAX];
336 uint32_t uints[WRITEBUF_COUNT_MAX];
340 static char *api_ws_read_get(struct readbuf *rb, uint32_t length)
342 char *before = rb->head;
343 char *after = before + length;
350 static int api_ws_read_uint32(struct readbuf *rb, uint32_t *value)
352 char *after = rb->head + sizeof *value;
355 memcpy(value, rb->head, sizeof *value);
357 *value = le32toh(*value);
361 static int api_ws_read_string(struct readbuf *rb, const char **value, size_t *length)
364 if (!api_ws_read_uint32(rb, &len) || !len)
367 *length = (size_t)(len - 1);
368 return (*value = api_ws_read_get(rb, len)) != NULL && rb->head[-1] == 0;
371 static int api_ws_read_object(struct readbuf *rb, struct json_object **object)
375 return api_ws_read_string(rb, &string, &length) && ((*object = json_tokener_parse(string)) != NULL) == (strcmp(string, "null") != 0);
378 static int api_ws_write_put(struct writebuf *wb, const void *value, size_t length)
381 if (i == WRITEBUF_COUNT_MAX)
383 wb->iovec[i].iov_base = (void*)value;
384 wb->iovec[i].iov_len = length;
389 static int api_ws_write_char(struct writebuf *wb, char value)
392 if (i == WRITEBUF_COUNT_MAX)
394 *(char*)&wb->uints[i] = value;
395 wb->iovec[i].iov_base = &wb->uints[i];
396 wb->iovec[i].iov_len = 1;
401 static int api_ws_write_uint32(struct writebuf *wb, uint32_t value)
404 if (i == WRITEBUF_COUNT_MAX)
406 wb->uints[i] = htole32(value);
407 wb->iovec[i].iov_base = &wb->uints[i];
408 wb->iovec[i].iov_len = sizeof wb->uints[i];
413 static int api_ws_write_string_length(struct writebuf *wb, const char *value, size_t length)
415 uint32_t len = (uint32_t)++length;
416 return (size_t)len == length && len && api_ws_write_uint32(wb, len) && api_ws_write_put(wb, value, length);
419 static int api_ws_write_string(struct writebuf *wb, const char *value)
421 return api_ws_write_string_length(wb, value, strlen(value));
424 static int api_ws_write_object(struct writebuf *wb, struct json_object *object)
426 const char *string = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
427 return string != NULL && api_ws_write_string(wb, string);
433 /******************* client part **********************************/
436 * structure for recording query data
439 struct api_ws_memo *next; /* the next memo */
440 struct api_ws *api; /* the ws api */
441 struct afb_xreq *xreq; /* the request handle */
443 struct afb_req req; /* the request handle */
444 struct afb_context *context; /* the context of the query */
446 uint32_t msgid; /* the message identifier */
451 struct api_ws_event *next;
452 struct afb_event event;
457 /* search a memorized request */
458 static struct api_ws_memo *api_ws_client_memo_search(struct api_ws *api, uint32_t msgid)
460 struct api_ws_memo *memo;
462 memo = api->client.memos;
463 while (memo != NULL && memo->msgid != msgid)
469 /* search the event */
470 static struct api_ws_event *api_ws_client_event_search(struct api_ws *api, uint32_t eventid, const char *name)
472 struct api_ws_event *ev;
474 ev = api->client.events;
475 while (ev != NULL && (ev->eventid != eventid || 0 != strcmp(afb_evt_event_name(ev->event), name)))
482 /* allocates and init the memorizing data */
483 static struct api_ws_memo *api_ws_client_memo_make(struct api_ws *api, struct afb_xreq *xreq)
485 struct api_ws_memo *memo;
487 memo = malloc(sizeof *memo);
489 afb_xreq_addref(xreq);
491 do { memo->msgid = ++api->client.id; } while(api_ws_client_memo_search(api, memo->msgid) != NULL);
493 memo->next = api->client.memos;
494 api->client.memos = memo;
499 /* free and release the memorizing data */
500 static void api_ws_client_memo_destroy(struct api_ws_memo *memo)
502 struct api_ws_memo **prv;
504 prv = &memo->api->client.memos;
505 while (*prv != NULL) {
513 afb_xreq_unref(memo->xreq);
517 /* get event data from the message */
518 static int api_ws_client_msg_event_read(struct readbuf *rb, uint32_t *eventid, const char **name)
520 return api_ws_read_uint32(rb, eventid) && api_ws_read_string(rb, name, NULL);
523 /* get event from the message */
524 static int api_ws_client_msg_event_get(struct api_ws *api, struct readbuf *rb, struct api_ws_event **ev)
529 /* get event data from the message */
530 if (!api_ws_client_msg_event_read(rb, &eventid, &name)) {
531 ERROR("Invalid message");
535 /* check conflicts */
536 *ev = api_ws_client_event_search(api, eventid, name);
538 ERROR("event %s not found", name);
545 /* get event from the message */
546 static int api_ws_client_msg_memo_get(struct api_ws *api, struct readbuf *rb, struct api_ws_memo **memo)
550 /* get event data from the message */
551 if (!api_ws_read_uint32(rb, &msgid)) {
552 ERROR("Invalid message");
557 *memo = api_ws_client_memo_search(api, msgid);
559 ERROR("message not found");
566 /* read a subscrition message */
567 static int api_ws_client_msg_subscription_get(struct api_ws *api, struct readbuf *rb, struct api_ws_event **ev, struct api_ws_memo **memo)
569 return api_ws_client_msg_memo_get(api, rb, memo) && api_ws_client_msg_event_get(api, rb, ev);
573 static void api_ws_client_event_create(struct api_ws *api, struct readbuf *rb)
578 struct api_ws_event *ev;
580 /* get event data from the message */
581 offset = api_ws_client_msg_event_read(rb, &eventid, &name);
583 ERROR("Invalid message");
587 /* check conflicts */
588 ev = api_ws_client_event_search(api, eventid, name);
594 /* no conflict, try to add it */
595 ev = malloc(sizeof *ev);
597 ev->event = afb_evt_create_event(name);
598 if (ev->event.closure == NULL)
602 ev->eventid = eventid;
603 ev->next = api->client.events;
604 api->client.events = ev;
608 ERROR("can't create event %s, out of memory", name);
611 /* removes an event */
612 static void api_ws_client_event_drop(struct api_ws *api, struct readbuf *rb)
614 struct api_ws_event *ev, **prv;
616 /* retrieves the event */
617 if (!api_ws_client_msg_event_get(api, rb, &ev))
620 /* decrease the reference count */
624 /* unlinks the event */
625 prv = &api->client.events;
630 /* destroys the event */
631 afb_event_drop(ev->event);
635 /* subscribes an event */
636 static void api_ws_client_event_subscribe(struct api_ws *api, struct readbuf *rb)
638 struct api_ws_event *ev;
639 struct api_ws_memo *memo;
641 if (api_ws_client_msg_subscription_get(api, rb, &ev, &memo)) {
642 /* subscribe the request from the event */
643 if (afb_xreq_subscribe(memo->xreq, ev->event) < 0)
644 ERROR("can't subscribe: %m");
648 /* unsubscribes an event */
649 static void api_ws_client_event_unsubscribe(struct api_ws *api, struct readbuf *rb)
651 struct api_ws_event *ev;
652 struct api_ws_memo *memo;
654 if (api_ws_client_msg_subscription_get(api, rb, &ev, &memo)) {
655 /* unsubscribe the request from the event */
656 if (afb_xreq_unsubscribe(memo->xreq, ev->event) < 0)
657 ERROR("can't unsubscribe: %m");
661 /* receives broadcasted events */
662 static void api_ws_client_event_broadcast(struct api_ws *api, struct readbuf *rb)
664 struct json_object *object;
667 if (api_ws_read_string(rb, &event, NULL) && api_ws_read_object(rb, &object))
668 afb_evt_broadcast(event, object);
670 ERROR("unreadable broadcasted event");
674 static void api_ws_client_event_push(struct api_ws *api, struct readbuf *rb)
676 struct api_ws_event *ev;
677 struct json_object *object;
679 if (api_ws_client_msg_event_get(api, rb, &ev) && api_ws_read_object(rb, &object))
680 afb_event_push(ev->event, object);
682 ERROR("unreadable push event");
685 static void api_ws_client_reply_success(struct api_ws *api, struct readbuf *rb)
687 struct api_ws_memo *memo;
688 struct json_object *object;
692 /* retrieve the message data */
693 if (!api_ws_client_msg_memo_get(api, rb, &memo))
696 if (api_ws_read_uint32(rb, &flags)
697 && api_ws_read_string(rb, &info, NULL)
698 && api_ws_read_object(rb, &object)) {
699 memo->xreq->context.flags = (unsigned)flags;
700 afb_xreq_success(memo->xreq, object, *info ? info : NULL);
702 /* failing to have the answer */
703 afb_xreq_fail(memo->xreq, "error", "ws error");
705 api_ws_client_memo_destroy(memo);
708 static void api_ws_client_reply_fail(struct api_ws *api, struct readbuf *rb)
710 struct api_ws_memo *memo;
711 const char *info, *status;
714 /* retrieve the message data */
715 if (!api_ws_client_msg_memo_get(api, rb, &memo))
718 if (api_ws_read_uint32(rb, &flags)
719 && api_ws_read_string(rb, &status, NULL)
720 && api_ws_read_string(rb, &info, NULL)) {
721 memo->xreq->context.flags = (unsigned)flags;
722 afb_xreq_fail(memo->xreq, status, *info ? info : NULL);
724 /* failing to have the answer */
725 afb_xreq_fail(memo->xreq, "error", "ws error");
727 api_ws_client_memo_destroy(memo);
730 /* callback when receiving binary data */
731 static void api_ws_client_on_binary(void *closure, char *data, size_t size)
734 struct readbuf rb = { .head = data, .end = data + size };
735 switch (*rb.head++) {
736 case 'T': /* success */
737 api_ws_client_reply_success(closure, &rb);
740 api_ws_client_reply_fail(closure, &rb);
742 case '*': /* broadcast */
743 api_ws_client_event_broadcast(closure, &rb);
745 case '+': /* creates the event */
746 api_ws_client_event_create(closure, &rb);
748 case '-': /* drops the event */
749 api_ws_client_event_drop(closure, &rb);
751 case '!': /* pushs the event */
752 api_ws_client_event_push(closure, &rb);
754 case 'S': /* subscribe event for a request */
755 api_ws_client_event_subscribe(closure, &rb);
757 case 'U': /* unsubscribe event for a request */
758 api_ws_client_event_unsubscribe(closure, &rb);
760 default: /* unexpected message */
767 /* on call, propagate it to the ws service */
768 static void api_ws_client_call_cb(void * closure, struct afb_xreq *xreq)
771 struct api_ws_memo *memo;
772 struct writebuf wb = { .count = 0 };
775 struct api_ws *api = closure;
777 /* create the recording data */
778 memo = api_ws_client_memo_make(api, xreq);
780 afb_xreq_fail_f(xreq, "error", "out of memory");
784 /* creates the call message */
785 raw = afb_xreq_raw(xreq, &szraw);
788 if (!api_ws_write_uint32(&wb, memo->msgid)
789 || !api_ws_write_uint32(&wb, (uint32_t)xreq->context.flags)
790 || !api_ws_write_string(&wb, xreq->verb)
791 || !api_ws_write_string(&wb, afb_session_uuid(xreq->context.session))
792 || !api_ws_write_string_length(&wb, raw, szraw))
796 rc = afb_ws_binary_v(api->client.ws, wb.iovec, wb.count);
802 afb_xreq_fail(xreq, "error", "websocket sending error");
806 afb_xreq_fail(xreq, "error", "internal: raw is NULL!");
810 afb_xreq_fail(xreq, "error", "overflow: size doesn't match 32 bits!");
813 api_ws_client_memo_destroy(memo);
816 static int api_ws_service_start_cb(void *closure, int share_session, int onneed)
818 struct api_ws *api = closure;
820 /* not an error when onneed */
824 /* already started: it is an error */
825 ERROR("The WS binding %s is not a startable service", api->path);
830 static void api_ws_client_disconnect(struct api_ws *api)
833 afb_ws_destroy(api->client.ws);
834 api->client.ws = NULL;
841 static int api_ws_client_connect(struct api_ws *api)
846 fd = api_ws_socket(api->path, 0);
848 ws = afb_ws_create(afb_common_get_event_loop(), fd, &api_ws_client_ws_itf, api);
859 static struct afb_api_itf ws_api_itf = {
860 .call = api_ws_client_call_cb,
861 .service_start = api_ws_service_start_cb
864 /* adds a afb-ws-service client api */
865 int afb_api_ws_add_client(const char *path)
869 struct afb_api afb_api;
871 /* create the ws client api */
872 api = api_ws_make(path);
876 /* connect to the service */
877 rc = api_ws_client_connect(api);
879 ERROR("can't connect to ws service %s", api->path);
883 /* record it as an API */
884 afb_api.closure = api;
885 afb_api.itf = &ws_api_itf;
886 if (afb_apis_add(api->api, afb_api) < 0)
892 api_ws_client_disconnect(api);
899 /******************* client description part for server *****************************/
901 static void api_ws_server_client_unref(struct api_ws_client *client)
903 if (!--client->refcount) {
904 afb_evt_listener_unref(client->listener);
905 afb_ws_destroy(client->ws);
910 /* on call, propagate it to the ws service */
911 static void api_ws_server_called(struct api_ws_client *client, struct readbuf *rb, char *data, size_t size)
913 struct api_ws_server_req *wreq;
914 const char *uuid, *verb;
919 /* create the request */
920 wreq = calloc(1 , sizeof *wreq);
924 wreq->client = client;
925 wreq->rcvdata = data;
927 /* reads the call message data */
928 if (!api_ws_read_uint32(rb, &wreq->msgid)
929 || !api_ws_read_uint32(rb, &flags)
930 || !api_ws_read_string(rb, &verb, NULL)
931 || !api_ws_read_string(rb, &uuid, NULL)
932 || !api_ws_read_string(rb, &wreq->request, &wreq->lenreq))
935 wreq->xreq.json = json_tokener_parse(wreq->request);
936 if (wreq->xreq.json == NULL && strcmp(wreq->request, "null")) {
937 wreq->xreq.json = json_object_new_string(wreq->request);
940 /* init the context */
941 if (afb_context_connect(&wreq->xreq.context, uuid, NULL) < 0)
943 wreq->xreq.context.flags = flags;
946 wreq->xreq.refcount = 1;
947 wreq->xreq.api = client->api;
948 wreq->xreq.verb = verb;
949 wreq->xreq.query = wreq;
950 wreq->xreq.queryitf = &afb_api_ws_xreq_itf;
951 afb_apis_call(&wreq->xreq);
952 afb_xreq_unref(&wreq->xreq);
959 api_ws_server_client_unref(client);
962 /* callback when receiving binary data */
963 static void api_ws_server_on_binary(void *closure, char *data, size_t size)
965 struct readbuf rb = { .head = data, .end = data + size };
966 api_ws_server_called(closure, &rb, data, size);
969 /* callback when receiving a hangup */
970 static void api_ws_server_on_hangup(void *closure)
972 struct api_ws_client *client = closure;
974 /* close the socket */
975 if (client->fd >= 0) {
980 /* release the client */
981 api_ws_server_client_unref(client);
984 static void api_ws_server_accept(struct api_ws *api)
986 struct api_ws_client *client;
987 struct sockaddr addr;
990 client = calloc(1, sizeof *client);
991 if (client != NULL) {
992 client->listener = afb_evt_listener_create(&api_ws_server_evt_itf, client);
993 if (client->listener != NULL) {
994 lenaddr = (socklen_t)sizeof addr;
995 client->fd = accept(api->fd, &addr, &lenaddr);
996 if (client->fd >= 0) {
997 fcntl(client->fd, F_SETFD, FD_CLOEXEC);
998 fcntl(client->fd, F_SETFL, O_NONBLOCK);
999 client->ws = afb_ws_create(afb_common_get_event_loop(), client->fd, &api_ws_server_ws_itf, client);
1000 if (client->ws != NULL) {
1001 client->api = api->api;
1002 client->refcount = 1;
1007 afb_evt_listener_unref(client->listener);
1013 /******************* server part: manage events **********************************/
1015 static void api_ws_server_event_send(struct api_ws_client *client, char order, const char *event, int eventid, const char *data)
1018 struct writebuf wb = { .count = 0 };
1020 if (api_ws_write_char(&wb, order)
1021 && api_ws_write_uint32(&wb, eventid)
1022 && api_ws_write_string(&wb, event)
1023 && (data == NULL || api_ws_write_string(&wb, data))) {
1024 rc = afb_ws_binary_v(client->ws, wb.iovec, wb.count);
1028 ERROR("error while sending %c for event %s", order, event);
1031 static void api_ws_server_event_add(void *closure, const char *event, int eventid)
1033 api_ws_server_event_send(closure, '+', event, eventid, NULL);
1036 static void api_ws_server_event_remove(void *closure, const char *event, int eventid)
1038 api_ws_server_event_send(closure, '-', event, eventid, NULL);
1041 static void api_ws_server_event_push(void *closure, const char *event, int eventid, struct json_object *object)
1043 const char *data = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
1044 api_ws_server_event_send(closure, '!', event, eventid, data ? : "null");
1045 json_object_put(object);
1048 static void api_ws_server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object)
1051 struct api_ws_client *client = closure;
1053 struct writebuf wb = { .count = 0 };
1055 if (api_ws_write_char(&wb, '*') && api_ws_write_string(&wb, event) && api_ws_write_object(&wb, object)) {
1056 rc = afb_ws_binary_v(client->ws, wb.iovec, wb.count);
1058 ERROR("error while broadcasting event %s", event);
1060 ERROR("error while broadcasting event %s", event);
1061 json_object_put(object);
1064 /******************* ws request part for server *****************/
1066 /* decrement the reference count of the request and free/release it on falling to null */
1067 static void api_ws_server_req_destroy_cb(void *closure)
1069 struct api_ws_server_req *wreq = closure;
1071 afb_context_disconnect(&wreq->xreq.context);
1072 json_object_put(wreq->xreq.json);
1073 free(wreq->rcvdata);
1074 api_ws_server_client_unref(wreq->client);
1078 static void api_ws_server_req_success_cb(void *closure, struct json_object *obj, const char *info)
1081 struct writebuf wb = { .count = 0 };
1082 struct api_ws_server_req *wreq = closure;
1084 if (api_ws_write_char(&wb, 'T')
1085 && api_ws_write_uint32(&wb, wreq->msgid)
1086 && api_ws_write_uint32(&wb, (uint32_t)wreq->xreq.context.flags)
1087 && api_ws_write_string(&wb, info ? : "")
1088 && api_ws_write_object(&wb, obj)) {
1089 rc = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1093 ERROR("error while sending success");
1095 json_object_put(obj);
1098 static void api_ws_server_req_fail_cb(void *closure, const char *status, const char *info)
1101 struct writebuf wb = { .count = 0 };
1102 struct api_ws_server_req *wreq = closure;
1104 if (api_ws_write_char(&wb, 'F')
1105 && api_ws_write_uint32(&wb, wreq->msgid)
1106 && api_ws_write_uint32(&wb, (uint32_t)wreq->xreq.context.flags)
1107 && api_ws_write_string(&wb, status)
1108 && api_ws_write_string(&wb, info ? : "")) {
1109 rc = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1113 ERROR("error while sending fail");
1116 static int api_ws_server_req_subscribe_cb(void *closure, struct afb_event event)
1119 struct writebuf wb = { .count = 0 };
1120 struct api_ws_server_req *wreq = closure;
1122 rc = afb_evt_add_watch(wreq->client->listener, event);
1126 if (api_ws_write_char(&wb, 'S')
1127 && api_ws_write_uint32(&wb, wreq->msgid)
1128 && api_ws_write_uint32(&wb, (uint32_t)afb_evt_event_id(event))
1129 && api_ws_write_string(&wb, afb_evt_event_name(event))) {
1130 rc2 = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1134 ERROR("error while subscribing event");
1139 static int api_ws_server_req_unsubscribe_cb(void *closure, struct afb_event event)
1142 struct writebuf wb = { .count = 0 };
1143 struct api_ws_server_req *wreq = closure;
1145 if (api_ws_write_char(&wb, 'U')
1146 && api_ws_write_uint32(&wb, wreq->msgid)
1147 && api_ws_write_uint32(&wb, (uint32_t)afb_evt_event_id(event))
1148 && api_ws_write_string(&wb, afb_evt_event_name(event))) {
1149 rc2 = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1153 ERROR("error while subscribing event");
1155 rc = afb_evt_remove_watch(wreq->client->listener, event);
1159 /******************* server part **********************************/
1161 static int api_ws_server_connect(struct api_ws *api);
1163 static int api_ws_server_listen_callback(sd_event_source *src, int fd, uint32_t revents, void *closure)
1165 if ((revents & EPOLLIN) != 0)
1166 api_ws_server_accept(closure);
1167 if ((revents & EPOLLHUP) != 0)
1168 api_ws_server_connect(closure);
1172 static void api_ws_server_disconnect(struct api_ws *api)
1174 if (api->server.listensrc != NULL) {
1175 sd_event_source_unref(api->server.listensrc);
1176 api->server.listensrc = NULL;
1184 static int api_ws_server_connect(struct api_ws *api)
1188 /* ensure disconnected */
1189 api_ws_server_disconnect(api);
1191 /* request the service object name */
1192 api->fd = api_ws_socket(api->path, 1);
1194 ERROR("can't create socket %s", api->path);
1196 /* listen for service */
1197 rc = sd_event_add_io(afb_common_get_event_loop(), &api->server.listensrc, api->fd, EPOLLIN, api_ws_server_listen_callback, api);
1202 ERROR("can't add ws object %s", api->path);
1207 /* create the service */
1208 int afb_api_ws_add_server(const char *path)
1213 /* creates the ws api object */
1214 api = api_ws_make(path);
1218 /* connect for serving */
1219 rc = api_ws_server_connect(api);