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"
43 #include "afb-msg-json.h"
45 #include "afb-api-so.h"
46 #include "afb-context.h"
62 char *path; /* path of the object for the API */
63 char *api; /* api name of the interface */
64 int fd; /* file descriptor */
69 struct api_ws_event *events;
70 struct api_ws_memo *memos;
73 sd_event_source *listensrc;
74 struct afb_evt_listener *listener; /* listener for broadcasted events */
83 /******************* websocket interface for client part **********************************/
85 static void api_ws_client_on_binary(void *closure, char *data, size_t size);
87 static const struct afb_ws_itf api_ws_client_ws_itf =
91 .on_binary = api_ws_client_on_binary,
96 /******************* event structures for server part **********************************/
98 static void api_ws_server_event_add(void *closure, const char *event, int eventid);
99 static void api_ws_server_event_remove(void *closure, const char *event, int eventid);
100 static void api_ws_server_event_push(void *closure, const char *event, int eventid, struct json_object *object);
101 static void api_ws_server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object);
103 /* the interface for events pushing */
104 static const struct afb_evt_itf api_ws_server_evt_itf = {
105 .broadcast = api_ws_server_event_broadcast,
106 .push = api_ws_server_event_push,
107 .add = api_ws_server_event_add,
108 .remove = api_ws_server_event_remove
111 /******************* client description part for server *****************************/
115 /* the server ws-api */
118 /* count of references */
121 /* listener for events */
122 struct afb_evt_listener *listener;
124 /* file descriptor */
131 struct afb_cred *cred;
134 /******************* websocket interface for client part **********************************/
136 static void api_ws_server_on_binary(void *closure, char *data, size_t size);
137 static void api_ws_server_on_hangup(void *closure);
139 static const struct afb_ws_itf api_ws_server_ws_itf =
143 .on_binary = api_ws_server_on_binary,
145 .on_hangup = api_ws_server_on_hangup
148 /******************* ws request part for server *****************/
151 * structure for a ws request
153 struct api_ws_server_req {
154 struct afb_xreq xreq; /* the xreq */
155 struct api_ws_client *client; /* the client of the request */
156 char *rcvdata; /* the received data to free */
157 const char *request; /* the readen request as string */
158 size_t lenreq; /* the length of the request */
159 uint32_t msgid; /* the incoming request msgid */
162 static void api_ws_server_req_success_cb(struct afb_xreq *xreq, struct json_object *obj, const char *info);
163 static void api_ws_server_req_fail_cb(struct afb_xreq *xreq, const char *status, const char *info);
164 static void api_ws_server_req_destroy_cb(struct afb_xreq *xreq);
165 static int api_ws_server_req_subscribe_cb(struct afb_xreq *xreq, struct afb_event event);
166 static int api_ws_server_req_unsubscribe_cb(struct afb_xreq *xreq, struct afb_event event);
168 const struct afb_xreq_query_itf afb_api_ws_xreq_itf = {
169 .success = api_ws_server_req_success_cb,
170 .fail = api_ws_server_req_fail_cb,
171 .unref = api_ws_server_req_destroy_cb,
172 .subscribe = api_ws_server_req_subscribe_cb,
173 .unsubscribe = api_ws_server_req_unsubscribe_cb
176 /******************* common part **********************************/
179 * create a structure api_ws not connected to the 'path'.
181 static struct api_ws *api_ws_make(const char *path)
186 /* allocates the structure */
187 length = strlen(path);
188 api = calloc(1, sizeof *api + 1 + length);
194 /* path is copied after the struct */
195 api->path = (char*)(api+1);
196 memcpy(api->path, path, length + 1);
198 /* api name is at the end of the path */
199 while (length && path[length - 1] != '/' && path[length - 1] != ':')
201 api->api = &api->path[length];
202 if (api->api == NULL || !afb_apis_is_valid_api_name(api->api)) {
216 static int api_ws_socket_unix(const char *path, int server)
219 struct sockaddr_un addr;
222 length = strlen(path);
224 errno = ENAMETOOLONG;
231 fd = socket(AF_UNIX, SOCK_STREAM, 0);
235 memset(&addr, 0, sizeof addr);
236 addr.sun_family = AF_UNIX;
237 strcpy(addr.sun_path, path);
239 rc = bind(fd, (struct sockaddr *) &addr, (socklen_t)(sizeof addr));
241 rc = connect(fd, (struct sockaddr *) &addr, (socklen_t)(sizeof addr));
250 static int api_ws_socket_inet(const char *path, int server)
253 const char *service, *host, *api;
254 struct addrinfo hint, *rai, *iai;
257 api = strrchr(path, '/');
258 service = strrchr(path, ':');
259 if (api == NULL || service == NULL || api < service) {
263 host = strndupa(path, service++ - path);
264 service = strndupa(service, api - service);
267 memset(&hint, 0, sizeof hint);
268 hint.ai_family = AF_INET;
269 hint.ai_socktype = SOCK_STREAM;
270 rc = getaddrinfo(host, service, &hint, &rai);
278 while (iai != NULL) {
279 fd = socket(iai->ai_family, iai->ai_socktype, iai->ai_protocol);
282 rc = bind(fd, iai->ai_addr, iai->ai_addrlen);
284 rc = connect(fd, iai->ai_addr, iai->ai_addrlen);
299 static int api_ws_socket(const char *path, int server)
303 /* check for systemd socket */
304 if (0 == strncmp(path, "sd:", 3))
305 fd = sd_fds_for(path + 3);
307 /* check for unix socket */
308 if (0 == strncmp(path, "unix:", 5))
310 fd = api_ws_socket_unix(path + 5, server);
313 fd = api_ws_socket_inet(path, server);
315 if (fd >= 0 && server) {
317 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &rc, sizeof rc);
321 /* configure the socket */
323 fcntl(fd, F_SETFD, FD_CLOEXEC);
324 fcntl(fd, F_SETFL, O_NONBLOCK);
329 /******************* serialisation part **********************************/
336 #define WRITEBUF_COUNT_MAX 32
339 struct iovec iovec[WRITEBUF_COUNT_MAX];
340 uint32_t uints[WRITEBUF_COUNT_MAX];
344 static char *api_ws_read_get(struct readbuf *rb, uint32_t length)
346 char *before = rb->head;
347 char *after = before + length;
354 static int api_ws_read_uint32(struct readbuf *rb, uint32_t *value)
356 char *after = rb->head + sizeof *value;
359 memcpy(value, rb->head, sizeof *value);
361 *value = le32toh(*value);
365 static int api_ws_read_string(struct readbuf *rb, const char **value, size_t *length)
368 if (!api_ws_read_uint32(rb, &len) || !len)
371 *length = (size_t)(len - 1);
372 return (*value = api_ws_read_get(rb, len)) != NULL && rb->head[-1] == 0;
375 static int api_ws_read_object(struct readbuf *rb, struct json_object **object)
379 return api_ws_read_string(rb, &string, &length) && ((*object = json_tokener_parse(string)) != NULL) == (strcmp(string, "null") != 0);
382 static int api_ws_write_put(struct writebuf *wb, const void *value, size_t length)
385 if (i == WRITEBUF_COUNT_MAX)
387 wb->iovec[i].iov_base = (void*)value;
388 wb->iovec[i].iov_len = length;
393 static int api_ws_write_char(struct writebuf *wb, char value)
396 if (i == WRITEBUF_COUNT_MAX)
398 *(char*)&wb->uints[i] = value;
399 wb->iovec[i].iov_base = &wb->uints[i];
400 wb->iovec[i].iov_len = 1;
405 static int api_ws_write_uint32(struct writebuf *wb, uint32_t value)
408 if (i == WRITEBUF_COUNT_MAX)
410 wb->uints[i] = htole32(value);
411 wb->iovec[i].iov_base = &wb->uints[i];
412 wb->iovec[i].iov_len = sizeof wb->uints[i];
417 static int api_ws_write_string_length(struct writebuf *wb, const char *value, size_t length)
419 uint32_t len = (uint32_t)++length;
420 return (size_t)len == length && len && api_ws_write_uint32(wb, len) && api_ws_write_put(wb, value, length);
423 static int api_ws_write_string(struct writebuf *wb, const char *value)
425 return api_ws_write_string_length(wb, value, strlen(value));
428 static int api_ws_write_object(struct writebuf *wb, struct json_object *object)
430 const char *string = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
431 return string != NULL && api_ws_write_string(wb, string);
437 /******************* client part **********************************/
440 * structure for recording query data
443 struct api_ws_memo *next; /* the next memo */
444 struct api_ws *api; /* the ws api */
445 struct afb_xreq *xreq; /* the request handle */
447 struct afb_req req; /* the request handle */
448 struct afb_context *context; /* the context of the query */
450 uint32_t msgid; /* the message identifier */
455 struct api_ws_event *next;
456 struct afb_event event;
461 /* search a memorized request */
462 static struct api_ws_memo *api_ws_client_memo_search(struct api_ws *api, uint32_t msgid)
464 struct api_ws_memo *memo;
466 memo = api->client.memos;
467 while (memo != NULL && memo->msgid != msgid)
473 /* search the event */
474 static struct api_ws_event *api_ws_client_event_search(struct api_ws *api, uint32_t eventid, const char *name)
476 struct api_ws_event *ev;
478 ev = api->client.events;
479 while (ev != NULL && (ev->eventid != eventid || 0 != strcmp(afb_evt_event_name(ev->event), name)))
486 /* allocates and init the memorizing data */
487 static struct api_ws_memo *api_ws_client_memo_make(struct api_ws *api, struct afb_xreq *xreq)
489 struct api_ws_memo *memo;
491 memo = malloc(sizeof *memo);
493 afb_xreq_addref(xreq);
495 do { memo->msgid = ++api->client.id; } while(api_ws_client_memo_search(api, memo->msgid) != NULL);
497 memo->next = api->client.memos;
498 api->client.memos = memo;
503 /* free and release the memorizing data */
504 static void api_ws_client_memo_destroy(struct api_ws_memo *memo)
506 struct api_ws_memo **prv;
508 prv = &memo->api->client.memos;
509 while (*prv != NULL) {
517 afb_xreq_unref(memo->xreq);
521 /* get event data from the message */
522 static int api_ws_client_msg_event_read(struct readbuf *rb, uint32_t *eventid, const char **name)
524 return api_ws_read_uint32(rb, eventid) && api_ws_read_string(rb, name, NULL);
527 /* get event from the message */
528 static int api_ws_client_msg_event_get(struct api_ws *api, struct readbuf *rb, struct api_ws_event **ev)
533 /* get event data from the message */
534 if (!api_ws_client_msg_event_read(rb, &eventid, &name)) {
535 ERROR("Invalid message");
539 /* check conflicts */
540 *ev = api_ws_client_event_search(api, eventid, name);
542 ERROR("event %s not found", name);
549 /* get event from the message */
550 static int api_ws_client_msg_memo_get(struct api_ws *api, struct readbuf *rb, struct api_ws_memo **memo)
554 /* get event data from the message */
555 if (!api_ws_read_uint32(rb, &msgid)) {
556 ERROR("Invalid message");
561 *memo = api_ws_client_memo_search(api, msgid);
563 ERROR("message not found");
570 /* read a subscrition message */
571 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)
573 return api_ws_client_msg_memo_get(api, rb, memo) && api_ws_client_msg_event_get(api, rb, ev);
577 static void api_ws_client_event_create(struct api_ws *api, struct readbuf *rb)
582 struct api_ws_event *ev;
584 /* get event data from the message */
585 offset = api_ws_client_msg_event_read(rb, &eventid, &name);
587 ERROR("Invalid message");
591 /* check conflicts */
592 ev = api_ws_client_event_search(api, eventid, name);
598 /* no conflict, try to add it */
599 ev = malloc(sizeof *ev);
601 ev->event = afb_evt_create_event(name);
602 if (ev->event.closure == NULL)
606 ev->eventid = eventid;
607 ev->next = api->client.events;
608 api->client.events = ev;
612 ERROR("can't create event %s, out of memory", name);
615 /* removes an event */
616 static void api_ws_client_event_drop(struct api_ws *api, struct readbuf *rb)
618 struct api_ws_event *ev, **prv;
620 /* retrieves the event */
621 if (!api_ws_client_msg_event_get(api, rb, &ev))
624 /* decrease the reference count */
628 /* unlinks the event */
629 prv = &api->client.events;
634 /* destroys the event */
635 afb_event_drop(ev->event);
639 /* subscribes an event */
640 static void api_ws_client_event_subscribe(struct api_ws *api, struct readbuf *rb)
642 struct api_ws_event *ev;
643 struct api_ws_memo *memo;
645 if (api_ws_client_msg_subscription_get(api, rb, &ev, &memo)) {
646 /* subscribe the request from the event */
647 if (afb_xreq_subscribe(memo->xreq, ev->event) < 0)
648 ERROR("can't subscribe: %m");
652 /* unsubscribes an event */
653 static void api_ws_client_event_unsubscribe(struct api_ws *api, struct readbuf *rb)
655 struct api_ws_event *ev;
656 struct api_ws_memo *memo;
658 if (api_ws_client_msg_subscription_get(api, rb, &ev, &memo)) {
659 /* unsubscribe the request from the event */
660 if (afb_xreq_unsubscribe(memo->xreq, ev->event) < 0)
661 ERROR("can't unsubscribe: %m");
665 /* receives broadcasted events */
666 static void api_ws_client_event_broadcast(struct api_ws *api, struct readbuf *rb)
668 struct json_object *object;
671 if (api_ws_read_string(rb, &event, NULL) && api_ws_read_object(rb, &object))
672 afb_evt_broadcast(event, object);
674 ERROR("unreadable broadcasted event");
678 static void api_ws_client_event_push(struct api_ws *api, struct readbuf *rb)
680 struct api_ws_event *ev;
681 struct json_object *object;
683 if (api_ws_client_msg_event_get(api, rb, &ev) && api_ws_read_object(rb, &object))
684 afb_event_push(ev->event, object);
686 ERROR("unreadable push event");
689 static void api_ws_client_reply_success(struct api_ws *api, struct readbuf *rb)
691 struct api_ws_memo *memo;
692 struct json_object *object;
696 /* retrieve the message data */
697 if (!api_ws_client_msg_memo_get(api, rb, &memo))
700 if (api_ws_read_uint32(rb, &flags)
701 && api_ws_read_string(rb, &info, NULL)
702 && api_ws_read_object(rb, &object)) {
703 memo->xreq->context.flags = (unsigned)flags;
704 afb_xreq_success(memo->xreq, object, *info ? info : NULL);
706 /* failing to have the answer */
707 afb_xreq_fail(memo->xreq, "error", "ws error");
709 api_ws_client_memo_destroy(memo);
712 static void api_ws_client_reply_fail(struct api_ws *api, struct readbuf *rb)
714 struct api_ws_memo *memo;
715 const char *info, *status;
718 /* retrieve the message data */
719 if (!api_ws_client_msg_memo_get(api, rb, &memo))
722 if (api_ws_read_uint32(rb, &flags)
723 && api_ws_read_string(rb, &status, NULL)
724 && api_ws_read_string(rb, &info, NULL)) {
725 memo->xreq->context.flags = (unsigned)flags;
726 afb_xreq_fail(memo->xreq, status, *info ? info : NULL);
728 /* failing to have the answer */
729 afb_xreq_fail(memo->xreq, "error", "ws error");
731 api_ws_client_memo_destroy(memo);
734 /* callback when receiving binary data */
735 static void api_ws_client_on_binary(void *closure, char *data, size_t size)
738 struct readbuf rb = { .head = data, .end = data + size };
739 switch (*rb.head++) {
740 case 'T': /* success */
741 api_ws_client_reply_success(closure, &rb);
744 api_ws_client_reply_fail(closure, &rb);
746 case '*': /* broadcast */
747 api_ws_client_event_broadcast(closure, &rb);
749 case '+': /* creates the event */
750 api_ws_client_event_create(closure, &rb);
752 case '-': /* drops the event */
753 api_ws_client_event_drop(closure, &rb);
755 case '!': /* pushs the event */
756 api_ws_client_event_push(closure, &rb);
758 case 'S': /* subscribe event for a request */
759 api_ws_client_event_subscribe(closure, &rb);
761 case 'U': /* unsubscribe event for a request */
762 api_ws_client_event_unsubscribe(closure, &rb);
764 default: /* unexpected message */
771 /* on call, propagate it to the ws service */
772 static void api_ws_client_call_cb(void * closure, struct afb_xreq *xreq)
775 struct api_ws_memo *memo;
776 struct writebuf wb = { .count = 0 };
779 struct api_ws *api = closure;
781 /* create the recording data */
782 memo = api_ws_client_memo_make(api, xreq);
784 afb_xreq_fail_f(xreq, "error", "out of memory");
788 /* creates the call message */
789 raw = afb_xreq_raw(xreq, &szraw);
792 if (!api_ws_write_uint32(&wb, memo->msgid)
793 || !api_ws_write_uint32(&wb, (uint32_t)xreq->context.flags)
794 || !api_ws_write_string(&wb, xreq->verb)
795 || !api_ws_write_string(&wb, afb_session_uuid(xreq->context.session))
796 || !api_ws_write_string_length(&wb, raw, szraw))
800 rc = afb_ws_binary_v(api->client.ws, wb.iovec, wb.count);
806 afb_xreq_fail(xreq, "error", "websocket sending error");
810 afb_xreq_fail(xreq, "error", "internal: raw is NULL!");
814 afb_xreq_fail(xreq, "error", "overflow: size doesn't match 32 bits!");
817 api_ws_client_memo_destroy(memo);
820 static int api_ws_service_start_cb(void *closure, int share_session, int onneed)
822 struct api_ws *api = closure;
824 /* not an error when onneed */
828 /* already started: it is an error */
829 ERROR("The WS binding %s is not a startable service", api->path);
834 static void api_ws_client_disconnect(struct api_ws *api)
837 afb_ws_destroy(api->client.ws);
838 api->client.ws = NULL;
845 static int api_ws_client_connect(struct api_ws *api)
850 fd = api_ws_socket(api->path, 0);
852 ws = afb_ws_create(afb_common_get_event_loop(), fd, &api_ws_client_ws_itf, api);
863 static struct afb_api_itf ws_api_itf = {
864 .call = api_ws_client_call_cb,
865 .service_start = api_ws_service_start_cb
868 /* adds a afb-ws-service client api */
869 int afb_api_ws_add_client(const char *path)
873 struct afb_api afb_api;
875 /* create the ws client api */
876 api = api_ws_make(path);
880 /* connect to the service */
881 rc = api_ws_client_connect(api);
883 ERROR("can't connect to ws service %s", api->path);
887 /* record it as an API */
888 afb_api.closure = api;
889 afb_api.itf = &ws_api_itf;
890 if (afb_apis_add(api->api, afb_api) < 0)
896 api_ws_client_disconnect(api);
903 /******************* client description part for server *****************************/
905 static void api_ws_server_client_unref(struct api_ws_client *client)
907 if (!--client->refcount) {
908 afb_evt_listener_unref(client->listener);
909 afb_ws_destroy(client->ws);
910 afb_cred_unref(client->cred);
915 /* on call, propagate it to the ws service */
916 static void api_ws_server_called(struct api_ws_client *client, struct readbuf *rb, char *data, size_t size)
918 struct api_ws_server_req *wreq;
919 const char *uuid, *verb;
924 /* create the request */
925 wreq = calloc(1 , sizeof *wreq);
929 wreq->client = client;
930 wreq->rcvdata = data;
932 /* reads the call message data */
933 if (!api_ws_read_uint32(rb, &wreq->msgid)
934 || !api_ws_read_uint32(rb, &flags)
935 || !api_ws_read_string(rb, &verb, NULL)
936 || !api_ws_read_string(rb, &uuid, NULL)
937 || !api_ws_read_string(rb, &wreq->request, &wreq->lenreq))
940 afb_xreq_init(&wreq->xreq, &afb_api_ws_xreq_itf);
941 wreq->xreq.json = json_tokener_parse(wreq->request);
942 if (wreq->xreq.json == NULL && strcmp(wreq->request, "null")) {
943 wreq->xreq.json = json_object_new_string(wreq->request);
946 /* init the context */
947 if (afb_context_connect(&wreq->xreq.context, uuid, NULL) < 0)
949 wreq->xreq.context.flags = flags;
952 wreq->xreq.cred = afb_cred_addref(client->cred);
953 wreq->xreq.api = client->api;
954 wreq->xreq.verb = verb;
955 afb_apis_call(&wreq->xreq);
956 afb_xreq_unref(&wreq->xreq);
963 api_ws_server_client_unref(client);
966 /* callback when receiving binary data */
967 static void api_ws_server_on_binary(void *closure, char *data, size_t size)
969 struct readbuf rb = { .head = data, .end = data + size };
970 api_ws_server_called(closure, &rb, data, size);
973 /* callback when receiving a hangup */
974 static void api_ws_server_on_hangup(void *closure)
976 struct api_ws_client *client = closure;
978 /* close the socket */
979 if (client->fd >= 0) {
984 /* release the client */
985 api_ws_server_client_unref(client);
988 static void api_ws_server_accept(struct api_ws *api)
990 struct api_ws_client *client;
991 struct sockaddr addr;
994 client = calloc(1, sizeof *client);
995 if (client != NULL) {
996 client->listener = afb_evt_listener_create(&api_ws_server_evt_itf, client);
997 if (client->listener != NULL) {
998 lenaddr = (socklen_t)sizeof addr;
999 client->fd = accept(api->fd, &addr, &lenaddr);
1000 if (client->fd >= 0) {
1001 client->cred = afb_cred_create_for_socket(client->fd);
1002 fcntl(client->fd, F_SETFD, FD_CLOEXEC);
1003 fcntl(client->fd, F_SETFL, O_NONBLOCK);
1004 client->ws = afb_ws_create(afb_common_get_event_loop(), client->fd, &api_ws_server_ws_itf, client);
1005 if (client->ws != NULL) {
1006 client->api = api->api;
1007 client->refcount = 1;
1010 afb_cred_unref(client->cred);
1013 afb_evt_listener_unref(client->listener);
1019 /******************* server part: manage events **********************************/
1021 static void api_ws_server_event_send(struct api_ws_client *client, char order, const char *event, int eventid, const char *data)
1024 struct writebuf wb = { .count = 0 };
1026 if (api_ws_write_char(&wb, order)
1027 && api_ws_write_uint32(&wb, eventid)
1028 && api_ws_write_string(&wb, event)
1029 && (data == NULL || api_ws_write_string(&wb, data))) {
1030 rc = afb_ws_binary_v(client->ws, wb.iovec, wb.count);
1034 ERROR("error while sending %c for event %s", order, event);
1037 static void api_ws_server_event_add(void *closure, const char *event, int eventid)
1039 api_ws_server_event_send(closure, '+', event, eventid, NULL);
1042 static void api_ws_server_event_remove(void *closure, const char *event, int eventid)
1044 api_ws_server_event_send(closure, '-', event, eventid, NULL);
1047 static void api_ws_server_event_push(void *closure, const char *event, int eventid, struct json_object *object)
1049 const char *data = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
1050 api_ws_server_event_send(closure, '!', event, eventid, data ? : "null");
1051 json_object_put(object);
1054 static void api_ws_server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object)
1057 struct api_ws_client *client = closure;
1059 struct writebuf wb = { .count = 0 };
1061 if (api_ws_write_char(&wb, '*') && api_ws_write_string(&wb, event) && api_ws_write_object(&wb, object)) {
1062 rc = afb_ws_binary_v(client->ws, wb.iovec, wb.count);
1064 ERROR("error while broadcasting event %s", event);
1066 ERROR("error while broadcasting event %s", event);
1067 json_object_put(object);
1070 /******************* ws request part for server *****************/
1072 /* decrement the reference count of the request and free/release it on falling to null */
1073 static void api_ws_server_req_destroy_cb(struct afb_xreq *xreq)
1075 struct api_ws_server_req *wreq = CONTAINER_OF_XREQ(struct api_ws_server_req, xreq);
1077 afb_context_disconnect(&wreq->xreq.context);
1078 afb_cred_unref(wreq->xreq.cred);
1079 json_object_put(wreq->xreq.json);
1080 free(wreq->rcvdata);
1081 api_ws_server_client_unref(wreq->client);
1085 static void api_ws_server_req_success_cb(struct afb_xreq *xreq, struct json_object *obj, const char *info)
1088 struct writebuf wb = { .count = 0 };
1089 struct api_ws_server_req *wreq = CONTAINER_OF_XREQ(struct api_ws_server_req, xreq);
1091 if (api_ws_write_char(&wb, 'T')
1092 && api_ws_write_uint32(&wb, wreq->msgid)
1093 && api_ws_write_uint32(&wb, (uint32_t)wreq->xreq.context.flags)
1094 && api_ws_write_string(&wb, info ? : "")
1095 && api_ws_write_object(&wb, obj)) {
1096 rc = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1100 ERROR("error while sending success");
1102 json_object_put(obj);
1105 static void api_ws_server_req_fail_cb(struct afb_xreq *xreq, const char *status, const char *info)
1108 struct writebuf wb = { .count = 0 };
1109 struct api_ws_server_req *wreq = CONTAINER_OF_XREQ(struct api_ws_server_req, xreq);
1111 if (api_ws_write_char(&wb, 'F')
1112 && api_ws_write_uint32(&wb, wreq->msgid)
1113 && api_ws_write_uint32(&wb, (uint32_t)wreq->xreq.context.flags)
1114 && api_ws_write_string(&wb, status)
1115 && api_ws_write_string(&wb, info ? : "")) {
1116 rc = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1120 ERROR("error while sending fail");
1123 static int api_ws_server_req_subscribe_cb(struct afb_xreq *xreq, struct afb_event event)
1126 struct writebuf wb = { .count = 0 };
1127 struct api_ws_server_req *wreq = CONTAINER_OF_XREQ(struct api_ws_server_req, xreq);
1129 rc = afb_evt_add_watch(wreq->client->listener, event);
1133 if (api_ws_write_char(&wb, 'S')
1134 && api_ws_write_uint32(&wb, wreq->msgid)
1135 && api_ws_write_uint32(&wb, (uint32_t)afb_evt_event_id(event))
1136 && api_ws_write_string(&wb, afb_evt_event_name(event))) {
1137 rc2 = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1141 ERROR("error while subscribing event");
1146 static int api_ws_server_req_unsubscribe_cb(struct afb_xreq *xreq, struct afb_event event)
1149 struct writebuf wb = { .count = 0 };
1150 struct api_ws_server_req *wreq = CONTAINER_OF_XREQ(struct api_ws_server_req, xreq);
1152 if (api_ws_write_char(&wb, 'U')
1153 && api_ws_write_uint32(&wb, wreq->msgid)
1154 && api_ws_write_uint32(&wb, (uint32_t)afb_evt_event_id(event))
1155 && api_ws_write_string(&wb, afb_evt_event_name(event))) {
1156 rc2 = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1160 ERROR("error while subscribing event");
1162 rc = afb_evt_remove_watch(wreq->client->listener, event);
1166 /******************* server part **********************************/
1168 static int api_ws_server_connect(struct api_ws *api);
1170 static int api_ws_server_listen_callback(sd_event_source *src, int fd, uint32_t revents, void *closure)
1172 if ((revents & EPOLLIN) != 0)
1173 api_ws_server_accept(closure);
1174 if ((revents & EPOLLHUP) != 0)
1175 api_ws_server_connect(closure);
1179 static void api_ws_server_disconnect(struct api_ws *api)
1181 if (api->server.listensrc != NULL) {
1182 sd_event_source_unref(api->server.listensrc);
1183 api->server.listensrc = NULL;
1191 static int api_ws_server_connect(struct api_ws *api)
1195 /* ensure disconnected */
1196 api_ws_server_disconnect(api);
1198 /* request the service object name */
1199 api->fd = api_ws_socket(api->path, 1);
1201 ERROR("can't create socket %s", api->path);
1203 /* listen for service */
1204 rc = sd_event_add_io(afb_common_get_event_loop(), &api->server.listensrc, api->fd, EPOLLIN, api_ws_server_listen_callback, api);
1209 ERROR("can't add ws object %s", api->path);
1214 /* create the service */
1215 int afb_api_ws_add_server(const char *path)
1220 /* creates the ws api object */
1221 api = api_ws_make(path);
1225 /* connect for serving */
1226 rc = api_ws_server_connect(api);