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"
56 #define CHAR_FOR_ANSWER_SUCCESS 'T'
57 #define CHAR_FOR_ANSWER_FAIL 'F'
58 #define CHAR_FOR_EVT_BROADCAST '*'
59 #define CHAR_FOR_EVT_ADD '+'
60 #define CHAR_FOR_EVT_DEL '-'
61 #define CHAR_FOR_EVT_PUSH '!'
62 #define CHAR_FOR_EVT_SUBSCRIBE 'S'
63 #define CHAR_FOR_EVT_UNSUBSCRIBE 'U'
69 char *path; /* path of the object for the API */
70 char *api; /* api name of the interface */
71 int fd; /* file descriptor */
76 struct api_ws_event *events;
77 struct api_ws_memo *memos;
80 sd_event_source *listensrc;
81 struct afb_evt_listener *listener; /* listener for broadcasted events */
90 /******************* websocket interface for client part **********************************/
92 static void api_ws_client_on_binary(void *closure, char *data, size_t size);
94 static const struct afb_ws_itf api_ws_client_ws_itf =
98 .on_binary = api_ws_client_on_binary,
103 /******************* event structures for server part **********************************/
105 static void api_ws_server_event_add(void *closure, const char *event, int eventid);
106 static void api_ws_server_event_remove(void *closure, const char *event, int eventid);
107 static void api_ws_server_event_push(void *closure, const char *event, int eventid, struct json_object *object);
108 static void api_ws_server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object);
110 /* the interface for events pushing */
111 static const struct afb_evt_itf api_ws_server_evt_itf = {
112 .broadcast = api_ws_server_event_broadcast,
113 .push = api_ws_server_event_push,
114 .add = api_ws_server_event_add,
115 .remove = api_ws_server_event_remove
118 /******************* client description part for server *****************************/
122 /* the server ws-api */
125 /* count of references */
128 /* listener for events */
129 struct afb_evt_listener *listener;
131 /* file descriptor */
138 struct afb_cred *cred;
141 /******************* websocket interface for client part **********************************/
143 static void api_ws_server_on_binary(void *closure, char *data, size_t size);
144 static void api_ws_server_on_hangup(void *closure);
146 static const struct afb_ws_itf api_ws_server_ws_itf =
150 .on_binary = api_ws_server_on_binary,
152 .on_hangup = api_ws_server_on_hangup
155 /******************* ws request part for server *****************/
158 * structure for a ws request
160 struct api_ws_server_req {
161 struct afb_xreq xreq; /* the xreq */
162 struct api_ws_client *client; /* the client of the request */
163 char *rcvdata; /* the received data to free */
164 const char *request; /* the readen request as string */
165 size_t lenreq; /* the length of the request */
166 uint32_t msgid; /* the incoming request msgid */
169 static void api_ws_server_req_success_cb(struct afb_xreq *xreq, struct json_object *obj, const char *info);
170 static void api_ws_server_req_fail_cb(struct afb_xreq *xreq, const char *status, const char *info);
171 static void api_ws_server_req_destroy_cb(struct afb_xreq *xreq);
172 static int api_ws_server_req_subscribe_cb(struct afb_xreq *xreq, struct afb_event event);
173 static int api_ws_server_req_unsubscribe_cb(struct afb_xreq *xreq, struct afb_event event);
175 const struct afb_xreq_query_itf afb_api_ws_xreq_itf = {
176 .success = api_ws_server_req_success_cb,
177 .fail = api_ws_server_req_fail_cb,
178 .unref = api_ws_server_req_destroy_cb,
179 .subscribe = api_ws_server_req_subscribe_cb,
180 .unsubscribe = api_ws_server_req_unsubscribe_cb
183 /******************* common part **********************************/
186 * create a structure api_ws not connected to the 'path'.
188 static struct api_ws *api_ws_make(const char *path)
193 /* allocates the structure */
194 length = strlen(path);
195 api = calloc(1, sizeof *api + 1 + length);
201 /* path is copied after the struct */
202 api->path = (char*)(api+1);
203 memcpy(api->path, path, length + 1);
205 /* api name is at the end of the path */
206 while (length && path[length - 1] != '/' && path[length - 1] != ':')
208 api->api = &api->path[length];
209 if (api->api == NULL || !afb_apis_is_valid_api_name(api->api)) {
223 static int api_ws_socket_unix(const char *path, int server)
226 struct sockaddr_un addr;
229 length = strlen(path);
231 errno = ENAMETOOLONG;
238 fd = socket(AF_UNIX, SOCK_STREAM, 0);
242 memset(&addr, 0, sizeof addr);
243 addr.sun_family = AF_UNIX;
244 strcpy(addr.sun_path, path);
246 rc = bind(fd, (struct sockaddr *) &addr, (socklen_t)(sizeof addr));
248 rc = connect(fd, (struct sockaddr *) &addr, (socklen_t)(sizeof addr));
257 static int api_ws_socket_inet(const char *path, int server)
260 const char *service, *host, *api;
261 struct addrinfo hint, *rai, *iai;
264 api = strrchr(path, '/');
265 service = strrchr(path, ':');
266 if (api == NULL || service == NULL || api < service) {
270 host = strndupa(path, service++ - path);
271 service = strndupa(service, api - service);
274 memset(&hint, 0, sizeof hint);
275 hint.ai_family = AF_INET;
276 hint.ai_socktype = SOCK_STREAM;
277 rc = getaddrinfo(host, service, &hint, &rai);
285 while (iai != NULL) {
286 fd = socket(iai->ai_family, iai->ai_socktype, iai->ai_protocol);
289 rc = bind(fd, iai->ai_addr, iai->ai_addrlen);
291 rc = connect(fd, iai->ai_addr, iai->ai_addrlen);
306 static int api_ws_socket(const char *path, int server)
310 /* check for systemd socket */
311 if (0 == strncmp(path, "sd:", 3))
312 fd = sd_fds_for(path + 3);
314 /* check for unix socket */
315 if (0 == strncmp(path, "unix:", 5))
317 fd = api_ws_socket_unix(path + 5, server);
320 fd = api_ws_socket_inet(path, server);
322 if (fd >= 0 && server) {
324 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &rc, sizeof rc);
328 /* configure the socket */
330 fcntl(fd, F_SETFD, FD_CLOEXEC);
331 fcntl(fd, F_SETFL, O_NONBLOCK);
336 /******************* serialisation part **********************************/
343 #define WRITEBUF_COUNT_MAX 32
346 struct iovec iovec[WRITEBUF_COUNT_MAX];
347 uint32_t uints[WRITEBUF_COUNT_MAX];
351 static char *api_ws_read_get(struct readbuf *rb, uint32_t length)
353 char *before = rb->head;
354 char *after = before + length;
361 static int api_ws_read_uint32(struct readbuf *rb, uint32_t *value)
363 char *after = rb->head + sizeof *value;
366 memcpy(value, rb->head, sizeof *value);
368 *value = le32toh(*value);
372 static int api_ws_read_string(struct readbuf *rb, const char **value, size_t *length)
375 if (!api_ws_read_uint32(rb, &len) || !len)
378 *length = (size_t)(len - 1);
379 return (*value = api_ws_read_get(rb, len)) != NULL && rb->head[-1] == 0;
382 static int api_ws_read_object(struct readbuf *rb, struct json_object **object)
386 return api_ws_read_string(rb, &string, &length) && ((*object = json_tokener_parse(string)) != NULL) == (strcmp(string, "null") != 0);
389 static int api_ws_write_put(struct writebuf *wb, const void *value, size_t length)
392 if (i == WRITEBUF_COUNT_MAX)
394 wb->iovec[i].iov_base = (void*)value;
395 wb->iovec[i].iov_len = length;
400 static int api_ws_write_char(struct writebuf *wb, char value)
403 if (i == WRITEBUF_COUNT_MAX)
405 *(char*)&wb->uints[i] = value;
406 wb->iovec[i].iov_base = &wb->uints[i];
407 wb->iovec[i].iov_len = 1;
412 static int api_ws_write_uint32(struct writebuf *wb, uint32_t value)
415 if (i == WRITEBUF_COUNT_MAX)
417 wb->uints[i] = htole32(value);
418 wb->iovec[i].iov_base = &wb->uints[i];
419 wb->iovec[i].iov_len = sizeof wb->uints[i];
424 static int api_ws_write_string_length(struct writebuf *wb, const char *value, size_t length)
426 uint32_t len = (uint32_t)++length;
427 return (size_t)len == length && len && api_ws_write_uint32(wb, len) && api_ws_write_put(wb, value, length);
430 static int api_ws_write_string(struct writebuf *wb, const char *value)
432 return api_ws_write_string_length(wb, value, strlen(value));
435 static int api_ws_write_object(struct writebuf *wb, struct json_object *object)
437 const char *string = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
438 return string != NULL && api_ws_write_string(wb, string);
444 /******************* client part **********************************/
447 * structure for recording query data
450 struct api_ws_memo *next; /* the next memo */
451 struct api_ws *api; /* the ws api */
452 struct afb_xreq *xreq; /* the request handle */
454 struct afb_req req; /* the request handle */
455 struct afb_context *context; /* the context of the query */
457 uint32_t msgid; /* the message identifier */
462 struct api_ws_event *next;
463 struct afb_event event;
468 /* search a memorized request */
469 static struct api_ws_memo *api_ws_client_memo_search(struct api_ws *api, uint32_t msgid)
471 struct api_ws_memo *memo;
473 memo = api->client.memos;
474 while (memo != NULL && memo->msgid != msgid)
480 /* search the event */
481 static struct api_ws_event *api_ws_client_event_search(struct api_ws *api, uint32_t eventid, const char *name)
483 struct api_ws_event *ev;
485 ev = api->client.events;
486 while (ev != NULL && (ev->eventid != eventid || 0 != strcmp(afb_evt_event_name(ev->event), name)))
493 /* allocates and init the memorizing data */
494 static struct api_ws_memo *api_ws_client_memo_make(struct api_ws *api, struct afb_xreq *xreq)
496 struct api_ws_memo *memo;
498 memo = malloc(sizeof *memo);
500 afb_xreq_addref(xreq);
502 do { memo->msgid = ++api->client.id; } while(api_ws_client_memo_search(api, memo->msgid) != NULL);
504 memo->next = api->client.memos;
505 api->client.memos = memo;
510 /* free and release the memorizing data */
511 static void api_ws_client_memo_destroy(struct api_ws_memo *memo)
513 struct api_ws_memo **prv;
515 prv = &memo->api->client.memos;
516 while (*prv != NULL) {
524 afb_xreq_unref(memo->xreq);
528 /* get event data from the message */
529 static int api_ws_client_msg_event_read(struct readbuf *rb, uint32_t *eventid, const char **name)
531 return api_ws_read_uint32(rb, eventid) && api_ws_read_string(rb, name, NULL);
534 /* get event from the message */
535 static int api_ws_client_msg_event_get(struct api_ws *api, struct readbuf *rb, struct api_ws_event **ev)
540 /* get event data from the message */
541 if (!api_ws_client_msg_event_read(rb, &eventid, &name)) {
542 ERROR("Invalid message");
546 /* check conflicts */
547 *ev = api_ws_client_event_search(api, eventid, name);
549 ERROR("event %s not found", name);
556 /* get event from the message */
557 static int api_ws_client_msg_memo_get(struct api_ws *api, struct readbuf *rb, struct api_ws_memo **memo)
561 /* get event data from the message */
562 if (!api_ws_read_uint32(rb, &msgid)) {
563 ERROR("Invalid message");
568 *memo = api_ws_client_memo_search(api, msgid);
570 ERROR("message not found");
577 /* read a subscrition message */
578 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)
580 return api_ws_client_msg_memo_get(api, rb, memo) && api_ws_client_msg_event_get(api, rb, ev);
584 static void api_ws_client_event_create(struct api_ws *api, struct readbuf *rb)
589 struct api_ws_event *ev;
591 /* get event data from the message */
592 offset = api_ws_client_msg_event_read(rb, &eventid, &name);
594 ERROR("Invalid message");
598 /* check conflicts */
599 ev = api_ws_client_event_search(api, eventid, name);
605 /* no conflict, try to add it */
606 ev = malloc(sizeof *ev);
608 ev->event = afb_evt_create_event(name);
609 if (ev->event.closure == NULL)
613 ev->eventid = eventid;
614 ev->next = api->client.events;
615 api->client.events = ev;
619 ERROR("can't create event %s, out of memory", name);
622 /* removes an event */
623 static void api_ws_client_event_drop(struct api_ws *api, struct readbuf *rb)
625 struct api_ws_event *ev, **prv;
627 /* retrieves the event */
628 if (!api_ws_client_msg_event_get(api, rb, &ev))
631 /* decrease the reference count */
635 /* unlinks the event */
636 prv = &api->client.events;
641 /* destroys the event */
642 afb_event_drop(ev->event);
646 /* subscribes an event */
647 static void api_ws_client_event_subscribe(struct api_ws *api, struct readbuf *rb)
649 struct api_ws_event *ev;
650 struct api_ws_memo *memo;
652 if (api_ws_client_msg_subscription_get(api, rb, &ev, &memo)) {
653 /* subscribe the request from the event */
654 if (afb_xreq_subscribe(memo->xreq, ev->event) < 0)
655 ERROR("can't subscribe: %m");
659 /* unsubscribes an event */
660 static void api_ws_client_event_unsubscribe(struct api_ws *api, struct readbuf *rb)
662 struct api_ws_event *ev;
663 struct api_ws_memo *memo;
665 if (api_ws_client_msg_subscription_get(api, rb, &ev, &memo)) {
666 /* unsubscribe the request from the event */
667 if (afb_xreq_unsubscribe(memo->xreq, ev->event) < 0)
668 ERROR("can't unsubscribe: %m");
672 /* receives broadcasted events */
673 static void api_ws_client_event_broadcast(struct api_ws *api, struct readbuf *rb)
675 struct json_object *object;
678 if (api_ws_read_string(rb, &event, NULL) && api_ws_read_object(rb, &object))
679 afb_evt_broadcast(event, object);
681 ERROR("unreadable broadcasted event");
685 static void api_ws_client_event_push(struct api_ws *api, struct readbuf *rb)
687 struct api_ws_event *ev;
688 struct json_object *object;
690 if (api_ws_client_msg_event_get(api, rb, &ev) && api_ws_read_object(rb, &object))
691 afb_event_push(ev->event, object);
693 ERROR("unreadable push event");
696 static void api_ws_client_reply_success(struct api_ws *api, struct readbuf *rb)
698 struct api_ws_memo *memo;
699 struct json_object *object;
703 /* retrieve the message data */
704 if (!api_ws_client_msg_memo_get(api, rb, &memo))
707 if (api_ws_read_uint32(rb, &flags)
708 && api_ws_read_string(rb, &info, NULL)
709 && api_ws_read_object(rb, &object)) {
710 memo->xreq->context.flags = (unsigned)flags;
711 afb_xreq_success(memo->xreq, object, *info ? info : NULL);
713 /* failing to have the answer */
714 afb_xreq_fail(memo->xreq, "error", "ws error");
716 api_ws_client_memo_destroy(memo);
719 static void api_ws_client_reply_fail(struct api_ws *api, struct readbuf *rb)
721 struct api_ws_memo *memo;
722 const char *info, *status;
725 /* retrieve the message data */
726 if (!api_ws_client_msg_memo_get(api, rb, &memo))
729 if (api_ws_read_uint32(rb, &flags)
730 && api_ws_read_string(rb, &status, NULL)
731 && api_ws_read_string(rb, &info, NULL)) {
732 memo->xreq->context.flags = (unsigned)flags;
733 afb_xreq_fail(memo->xreq, status, *info ? info : NULL);
735 /* failing to have the answer */
736 afb_xreq_fail(memo->xreq, "error", "ws error");
738 api_ws_client_memo_destroy(memo);
741 /* callback when receiving binary data */
742 static void api_ws_client_on_binary(void *closure, char *data, size_t size)
745 struct readbuf rb = { .head = data, .end = data + size };
746 switch (*rb.head++) {
747 case CHAR_FOR_ANSWER_SUCCESS: /* success */
748 api_ws_client_reply_success(closure, &rb);
750 case CHAR_FOR_ANSWER_FAIL: /* fail */
751 api_ws_client_reply_fail(closure, &rb);
753 case CHAR_FOR_EVT_BROADCAST: /* broadcast */
754 api_ws_client_event_broadcast(closure, &rb);
756 case CHAR_FOR_EVT_ADD: /* creates the event */
757 api_ws_client_event_create(closure, &rb);
759 case CHAR_FOR_EVT_DEL: /* drops the event */
760 api_ws_client_event_drop(closure, &rb);
762 case CHAR_FOR_EVT_PUSH: /* pushs the event */
763 api_ws_client_event_push(closure, &rb);
765 case CHAR_FOR_EVT_SUBSCRIBE: /* subscribe event for a request */
766 api_ws_client_event_subscribe(closure, &rb);
768 case CHAR_FOR_EVT_UNSUBSCRIBE: /* unsubscribe event for a request */
769 api_ws_client_event_unsubscribe(closure, &rb);
771 default: /* unexpected message */
778 /* on call, propagate it to the ws service */
779 static void api_ws_client_call_cb(void * closure, struct afb_xreq *xreq)
782 struct api_ws_memo *memo;
783 struct writebuf wb = { .count = 0 };
786 struct api_ws *api = closure;
788 /* create the recording data */
789 memo = api_ws_client_memo_make(api, xreq);
791 afb_xreq_fail_f(xreq, "error", "out of memory");
795 /* creates the call message */
796 raw = afb_xreq_raw(xreq, &szraw);
799 if (!api_ws_write_uint32(&wb, memo->msgid)
800 || !api_ws_write_uint32(&wb, (uint32_t)xreq->context.flags)
801 || !api_ws_write_string(&wb, xreq->verb)
802 || !api_ws_write_string(&wb, afb_session_uuid(xreq->context.session))
803 || !api_ws_write_string_length(&wb, raw, szraw))
807 rc = afb_ws_binary_v(api->client.ws, wb.iovec, wb.count);
813 afb_xreq_fail(xreq, "error", "websocket sending error");
817 afb_xreq_fail(xreq, "error", "internal: raw is NULL!");
821 afb_xreq_fail(xreq, "error", "overflow: size doesn't match 32 bits!");
824 api_ws_client_memo_destroy(memo);
827 static int api_ws_service_start_cb(void *closure, int share_session, int onneed)
829 struct api_ws *api = closure;
831 /* not an error when onneed */
835 /* already started: it is an error */
836 ERROR("The WS binding %s is not a startable service", api->path);
841 static void api_ws_client_disconnect(struct api_ws *api)
844 afb_ws_destroy(api->client.ws);
845 api->client.ws = NULL;
852 static int api_ws_client_connect(struct api_ws *api)
857 fd = api_ws_socket(api->path, 0);
859 ws = afb_ws_create(afb_common_get_event_loop(), fd, &api_ws_client_ws_itf, api);
870 static struct afb_api_itf ws_api_itf = {
871 .call = api_ws_client_call_cb,
872 .service_start = api_ws_service_start_cb
875 /* adds a afb-ws-service client api */
876 int afb_api_ws_add_client(const char *path)
880 struct afb_api afb_api;
882 /* create the ws client api */
883 api = api_ws_make(path);
887 /* connect to the service */
888 rc = api_ws_client_connect(api);
890 ERROR("can't connect to ws service %s", api->path);
894 /* record it as an API */
895 afb_api.closure = api;
896 afb_api.itf = &ws_api_itf;
897 if (afb_apis_add(api->api, afb_api) < 0)
903 api_ws_client_disconnect(api);
910 /******************* client description part for server *****************************/
912 static void api_ws_server_client_unref(struct api_ws_client *client)
914 if (!--client->refcount) {
915 afb_evt_listener_unref(client->listener);
916 afb_ws_destroy(client->ws);
917 afb_cred_unref(client->cred);
922 /* on call, propagate it to the ws service */
923 static void api_ws_server_called(struct api_ws_client *client, struct readbuf *rb, char *data, size_t size)
925 struct api_ws_server_req *wreq;
926 const char *uuid, *verb;
931 /* create the request */
932 wreq = calloc(1 , sizeof *wreq);
936 wreq->client = client;
937 wreq->rcvdata = data;
939 /* reads the call message data */
940 if (!api_ws_read_uint32(rb, &wreq->msgid)
941 || !api_ws_read_uint32(rb, &flags)
942 || !api_ws_read_string(rb, &verb, NULL)
943 || !api_ws_read_string(rb, &uuid, NULL)
944 || !api_ws_read_string(rb, &wreq->request, &wreq->lenreq))
947 afb_xreq_init(&wreq->xreq, &afb_api_ws_xreq_itf);
948 wreq->xreq.json = json_tokener_parse(wreq->request);
949 if (wreq->xreq.json == NULL && strcmp(wreq->request, "null")) {
950 wreq->xreq.json = json_object_new_string(wreq->request);
953 /* init the context */
954 if (afb_context_connect(&wreq->xreq.context, uuid, NULL) < 0)
956 wreq->xreq.context.flags = flags;
959 wreq->xreq.cred = afb_cred_addref(client->cred);
960 wreq->xreq.api = client->api;
961 wreq->xreq.verb = verb;
962 afb_apis_call(&wreq->xreq);
963 afb_xreq_unref(&wreq->xreq);
970 api_ws_server_client_unref(client);
973 /* callback when receiving binary data */
974 static void api_ws_server_on_binary(void *closure, char *data, size_t size)
976 struct readbuf rb = { .head = data, .end = data + size };
977 api_ws_server_called(closure, &rb, data, size);
980 /* callback when receiving a hangup */
981 static void api_ws_server_on_hangup(void *closure)
983 struct api_ws_client *client = closure;
985 /* close the socket */
986 if (client->fd >= 0) {
991 /* release the client */
992 api_ws_server_client_unref(client);
995 static void api_ws_server_accept(struct api_ws *api)
997 struct api_ws_client *client;
998 struct sockaddr addr;
1001 client = calloc(1, sizeof *client);
1002 if (client != NULL) {
1003 client->listener = afb_evt_listener_create(&api_ws_server_evt_itf, client);
1004 if (client->listener != NULL) {
1005 lenaddr = (socklen_t)sizeof addr;
1006 client->fd = accept(api->fd, &addr, &lenaddr);
1007 if (client->fd >= 0) {
1008 client->cred = afb_cred_create_for_socket(client->fd);
1009 fcntl(client->fd, F_SETFD, FD_CLOEXEC);
1010 fcntl(client->fd, F_SETFL, O_NONBLOCK);
1011 client->ws = afb_ws_create(afb_common_get_event_loop(), client->fd, &api_ws_server_ws_itf, client);
1012 if (client->ws != NULL) {
1013 client->api = api->api;
1014 client->refcount = 1;
1017 afb_cred_unref(client->cred);
1020 afb_evt_listener_unref(client->listener);
1026 /******************* server part: manage events **********************************/
1028 static void api_ws_server_event_send(struct api_ws_client *client, char order, const char *event, int eventid, const char *data)
1031 struct writebuf wb = { .count = 0 };
1033 if (api_ws_write_char(&wb, order)
1034 && api_ws_write_uint32(&wb, eventid)
1035 && api_ws_write_string(&wb, event)
1036 && (data == NULL || api_ws_write_string(&wb, data))) {
1037 rc = afb_ws_binary_v(client->ws, wb.iovec, wb.count);
1041 ERROR("error while sending %c for event %s", order, event);
1044 static void api_ws_server_event_add(void *closure, const char *event, int eventid)
1046 api_ws_server_event_send(closure, CHAR_FOR_EVT_ADD, event, eventid, NULL);
1049 static void api_ws_server_event_remove(void *closure, const char *event, int eventid)
1051 api_ws_server_event_send(closure, CHAR_FOR_EVT_DEL, event, eventid, NULL);
1054 static void api_ws_server_event_push(void *closure, const char *event, int eventid, struct json_object *object)
1056 const char *data = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
1057 api_ws_server_event_send(closure, CHAR_FOR_EVT_PUSH, event, eventid, data ? : "null");
1058 json_object_put(object);
1061 static void api_ws_server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object)
1064 struct api_ws_client *client = closure;
1066 struct writebuf wb = { .count = 0 };
1068 if (api_ws_write_char(&wb, CHAR_FOR_EVT_BROADCAST) && api_ws_write_string(&wb, event) && api_ws_write_object(&wb, object)) {
1069 rc = afb_ws_binary_v(client->ws, wb.iovec, wb.count);
1071 ERROR("error while broadcasting event %s", event);
1073 ERROR("error while broadcasting event %s", event);
1074 json_object_put(object);
1077 /******************* ws request part for server *****************/
1079 /* decrement the reference count of the request and free/release it on falling to null */
1080 static void api_ws_server_req_destroy_cb(struct afb_xreq *xreq)
1082 struct api_ws_server_req *wreq = CONTAINER_OF_XREQ(struct api_ws_server_req, xreq);
1084 afb_context_disconnect(&wreq->xreq.context);
1085 afb_cred_unref(wreq->xreq.cred);
1086 json_object_put(wreq->xreq.json);
1087 free(wreq->rcvdata);
1088 api_ws_server_client_unref(wreq->client);
1092 static void api_ws_server_req_success_cb(struct afb_xreq *xreq, struct json_object *obj, const char *info)
1095 struct writebuf wb = { .count = 0 };
1096 struct api_ws_server_req *wreq = CONTAINER_OF_XREQ(struct api_ws_server_req, xreq);
1098 if (api_ws_write_char(&wb, CHAR_FOR_ANSWER_SUCCESS)
1099 && api_ws_write_uint32(&wb, wreq->msgid)
1100 && api_ws_write_uint32(&wb, (uint32_t)wreq->xreq.context.flags)
1101 && api_ws_write_string(&wb, info ? : "")
1102 && api_ws_write_object(&wb, obj)) {
1103 rc = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1107 ERROR("error while sending success");
1109 json_object_put(obj);
1112 static void api_ws_server_req_fail_cb(struct afb_xreq *xreq, const char *status, const char *info)
1115 struct writebuf wb = { .count = 0 };
1116 struct api_ws_server_req *wreq = CONTAINER_OF_XREQ(struct api_ws_server_req, xreq);
1118 if (api_ws_write_char(&wb, CHAR_FOR_ANSWER_FAIL)
1119 && api_ws_write_uint32(&wb, wreq->msgid)
1120 && api_ws_write_uint32(&wb, (uint32_t)wreq->xreq.context.flags)
1121 && api_ws_write_string(&wb, status)
1122 && api_ws_write_string(&wb, info ? : "")) {
1123 rc = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1127 ERROR("error while sending fail");
1130 static int api_ws_server_req_subscribe_cb(struct afb_xreq *xreq, struct afb_event event)
1133 struct writebuf wb = { .count = 0 };
1134 struct api_ws_server_req *wreq = CONTAINER_OF_XREQ(struct api_ws_server_req, xreq);
1136 rc = afb_evt_add_watch(wreq->client->listener, event);
1140 if (api_ws_write_char(&wb, CHAR_FOR_EVT_SUBSCRIBE)
1141 && api_ws_write_uint32(&wb, wreq->msgid)
1142 && api_ws_write_uint32(&wb, (uint32_t)afb_evt_event_id(event))
1143 && api_ws_write_string(&wb, afb_evt_event_name(event))) {
1144 rc2 = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1148 ERROR("error while subscribing event");
1153 static int api_ws_server_req_unsubscribe_cb(struct afb_xreq *xreq, struct afb_event event)
1156 struct writebuf wb = { .count = 0 };
1157 struct api_ws_server_req *wreq = CONTAINER_OF_XREQ(struct api_ws_server_req, xreq);
1159 if (api_ws_write_char(&wb, CHAR_FOR_EVT_UNSUBSCRIBE)
1160 && api_ws_write_uint32(&wb, wreq->msgid)
1161 && api_ws_write_uint32(&wb, (uint32_t)afb_evt_event_id(event))
1162 && api_ws_write_string(&wb, afb_evt_event_name(event))) {
1163 rc2 = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1167 ERROR("error while subscribing event");
1169 rc = afb_evt_remove_watch(wreq->client->listener, event);
1173 /******************* server part **********************************/
1175 static int api_ws_server_connect(struct api_ws *api);
1177 static int api_ws_server_listen_callback(sd_event_source *src, int fd, uint32_t revents, void *closure)
1179 if ((revents & EPOLLIN) != 0)
1180 api_ws_server_accept(closure);
1181 if ((revents & EPOLLHUP) != 0)
1182 api_ws_server_connect(closure);
1186 static void api_ws_server_disconnect(struct api_ws *api)
1188 if (api->server.listensrc != NULL) {
1189 sd_event_source_unref(api->server.listensrc);
1190 api->server.listensrc = NULL;
1198 static int api_ws_server_connect(struct api_ws *api)
1202 /* ensure disconnected */
1203 api_ws_server_disconnect(api);
1205 /* request the service object name */
1206 api->fd = api_ws_socket(api->path, 1);
1208 ERROR("can't create socket %s", api->path);
1210 /* listen for service */
1211 rc = sd_event_add_io(afb_common_get_event_loop(), &api->server.listensrc, api->fd, EPOLLIN, api_ws_server_listen_callback, api);
1216 ERROR("can't add ws object %s", api->path);
1221 /* create the service */
1222 int afb_api_ws_add_server(const char *path)
1227 /* creates the ws api object */
1228 api = api_ws_make(path);
1232 /* connect for serving */
1233 rc = api_ws_server_connect(api);