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"
47 #include "afb-subcall.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_context context; /* the context, should be THE FIRST */
151 struct api_ws_client *client; /* the client of the request */
152 char *rcvdata; /* the received data to free */
153 struct json_object *json; /* the readen request as object */
154 const char *request; /* the readen request as string */
155 size_t lenreq; /* the length of the request */
156 int refcount; /* reference count of the request */
157 uint32_t msgid; /* the incoming request msgid */
160 static struct json_object *api_ws_server_req_json(struct api_ws_server_req *wreq);
161 static void api_ws_server_req_unref(struct api_ws_server_req *wreq);
163 static struct json_object *api_ws_server_req_json_cb(void *closure);
164 static struct afb_arg api_ws_server_req_get_cb(void *closure, const char *name);
165 static void api_ws_server_req_success_cb(void *closure, struct json_object *obj, const char *info);
166 static void api_ws_server_req_fail_cb(void *closure, const char *status, const char *info);
167 static const char *api_ws_server_req_raw_cb(void *closure, size_t *size);
168 static void api_ws_server_req_send_cb(void *closure, const char *buffer, size_t size);
169 static void api_ws_server_req_addref_cb(void *closure);
170 static void api_ws_server_req_unref_cb(void *closure);
171 static int api_ws_server_req_subscribe_cb(void *closure, struct afb_event event);
172 static int api_ws_server_req_unsubscribe_cb(void *closure, struct afb_event event);
173 static void api_ws_server_req_subcall_cb(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure);
175 const struct afb_req_itf afb_api_ws_req_itf = {
176 .json = api_ws_server_req_json_cb,
177 .get = api_ws_server_req_get_cb,
178 .success = api_ws_server_req_success_cb,
179 .fail = api_ws_server_req_fail_cb,
180 .raw = api_ws_server_req_raw_cb,
181 .send = api_ws_server_req_send_cb,
182 .context_get = (void*)afb_context_get,
183 .context_set = (void*)afb_context_set,
184 .addref = api_ws_server_req_addref_cb,
185 .unref = api_ws_server_req_unref_cb,
186 .session_close = (void*)afb_context_close,
187 .session_set_LOA = (void*)afb_context_change_loa,
188 .subscribe = api_ws_server_req_subscribe_cb,
189 .unsubscribe = api_ws_server_req_unsubscribe_cb,
190 .subcall = api_ws_server_req_subcall_cb
193 /******************* common part **********************************/
196 * create a structure api_ws not connected to the 'path'.
198 static struct api_ws *api_ws_make(const char *path)
203 /* allocates the structure */
204 length = strlen(path);
205 api = calloc(1, sizeof *api + 1 + length);
211 /* path is copied after the struct */
212 api->path = (char*)(api+1);
213 memcpy(api->path, path, length + 1);
215 /* api name is at the end of the path */
216 while (length && path[length - 1] != '/' && path[length - 1] != ':')
218 api->api = &api->path[length];
219 if (api->api == NULL || !afb_apis_is_valid_api_name(++api->api)) {
233 static int api_ws_socket_unix(const char *path, int server)
236 struct sockaddr_un addr;
239 length = strlen(path);
241 errno = ENAMETOOLONG;
248 fd = socket(AF_UNIX, SOCK_STREAM, 0);
252 memset(&addr, 0, sizeof addr);
253 addr.sun_family = AF_UNIX;
254 strcpy(addr.sun_path, path);
256 rc = bind(fd, (struct sockaddr *) &addr, (socklen_t)(sizeof addr));
258 rc = connect(fd, (struct sockaddr *) &addr, (socklen_t)(sizeof addr));
267 static int api_ws_socket_inet(const char *path, int server)
270 const char *service, *host, *api;
271 struct addrinfo hint, *rai, *iai;
274 api = strrchr(path, '/');
275 service = strrchr(path, ':');
276 if (api == NULL || service == NULL || api < service) {
280 host = strndupa(path, service++ - path);
281 service = strndupa(service, api - service);
284 memset(&hint, 0, sizeof hint);
285 hint.ai_family = AF_INET;
286 hint.ai_socktype = SOCK_STREAM;
287 rc = getaddrinfo(host, service, &hint, &rai);
295 while (iai != NULL) {
296 fd = socket(iai->ai_family, iai->ai_socktype, iai->ai_protocol);
299 rc = bind(fd, iai->ai_addr, iai->ai_addrlen);
301 rc = connect(fd, iai->ai_addr, iai->ai_addrlen);
316 static int api_ws_socket(const char *path, int server)
320 /* check for systemd socket */
321 if (0 == strncmp(path, "sd:", 3))
322 fd = sd_fds_for(path + 3);
324 /* check for unix socket */
325 if (0 == strncmp(path, "unix:", 5))
327 fd = api_ws_socket_unix(path + 5, server);
330 fd = api_ws_socket_inet(path, server);
332 if (fd >= 0 && server) {
334 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &rc, sizeof rc);
338 /* configure the socket */
340 fcntl(fd, F_SETFD, FD_CLOEXEC);
341 fcntl(fd, F_SETFL, O_NONBLOCK);
346 /******************* serialisation part **********************************/
353 #define WRITEBUF_COUNT_MAX 32
356 struct iovec iovec[WRITEBUF_COUNT_MAX];
357 uint32_t uints[WRITEBUF_COUNT_MAX];
361 static char *api_ws_read_get(struct readbuf *rb, uint32_t length)
363 char *before = rb->head;
364 char *after = before + length;
371 static int api_ws_read_uint32(struct readbuf *rb, uint32_t *value)
373 char *after = rb->head + sizeof *value;
376 memcpy(value, rb->head, sizeof *value);
378 *value = le32toh(*value);
382 static int api_ws_read_string(struct readbuf *rb, const char **value, size_t *length)
385 if (!api_ws_read_uint32(rb, &len) || !len)
388 *length = (size_t)(len - 1);
389 return (*value = api_ws_read_get(rb, len)) != NULL && rb->head[-1] == 0;
392 static int api_ws_read_object(struct readbuf *rb, struct json_object **object)
396 return api_ws_read_string(rb, &string, &length) && ((*object = json_tokener_parse(string)) != NULL) == (strcmp(string, "null") != 0);
399 static int api_ws_write_put(struct writebuf *wb, const void *value, size_t length)
402 if (i == WRITEBUF_COUNT_MAX)
404 wb->iovec[i].iov_base = (void*)value;
405 wb->iovec[i].iov_len = length;
410 static int api_ws_write_char(struct writebuf *wb, char value)
413 if (i == WRITEBUF_COUNT_MAX)
415 *(char*)&wb->uints[i] = value;
416 wb->iovec[i].iov_base = &wb->uints[i];
417 wb->iovec[i].iov_len = 1;
422 static int api_ws_write_uint32(struct writebuf *wb, uint32_t value)
425 if (i == WRITEBUF_COUNT_MAX)
427 wb->uints[i] = htole32(value);
428 wb->iovec[i].iov_base = &wb->uints[i];
429 wb->iovec[i].iov_len = sizeof wb->uints[i];
434 static int api_ws_write_string_length(struct writebuf *wb, const char *value, size_t length)
436 uint32_t len = (uint32_t)++length;
437 return (size_t)len == length && len && api_ws_write_uint32(wb, len) && api_ws_write_put(wb, value, length);
440 static int api_ws_write_string(struct writebuf *wb, const char *value)
442 return api_ws_write_string_length(wb, value, strlen(value));
445 static int api_ws_write_object(struct writebuf *wb, struct json_object *object)
447 const char *string = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
448 return string != NULL && api_ws_write_string(wb, string);
454 /******************* client part **********************************/
457 * structure for recording query data
460 struct api_ws_memo *next; /* the next memo */
461 struct api_ws *api; /* the ws api */
462 struct afb_req req; /* the request handle */
463 struct afb_context *context; /* the context of the query */
464 uint32_t msgid; /* the message identifier */
469 struct api_ws_event *next;
470 struct afb_event event;
475 /* search a memorized request */
476 static struct api_ws_memo *api_ws_client_memo_search(struct api_ws *api, uint32_t msgid)
478 struct api_ws_memo *memo;
480 memo = api->client.memos;
481 while (memo != NULL && memo->msgid != msgid)
487 /* search the event */
488 static struct api_ws_event *api_ws_client_event_search(struct api_ws *api, uint32_t eventid, const char *name)
490 struct api_ws_event *ev;
492 ev = api->client.events;
493 while (ev != NULL && (ev->eventid != eventid || 0 != strcmp(afb_evt_event_name(ev->event), name)))
500 /* allocates and init the memorizing data */
501 static struct api_ws_memo *api_ws_client_memo_make(struct api_ws *api, struct afb_req req, struct afb_context *context)
503 struct api_ws_memo *memo;
505 memo = malloc(sizeof *memo);
509 memo->context = context;
510 do { memo->msgid = ++api->client.id; } while(api_ws_client_memo_search(api, memo->msgid) != NULL);
512 memo->next = api->client.memos;
513 api->client.memos = memo;
518 /* free and release the memorizing data */
519 static void api_ws_client_memo_destroy(struct api_ws_memo *memo)
521 struct api_ws_memo **prv;
523 prv = &memo->api->client.memos;
524 while (*prv != NULL) {
532 afb_req_unref(memo->req);
536 /* get event data from the message */
537 static int api_ws_client_msg_event_read(struct readbuf *rb, uint32_t *eventid, const char **name)
539 return api_ws_read_uint32(rb, eventid) && api_ws_read_string(rb, name, NULL);
542 /* get event from the message */
543 static int api_ws_client_msg_event_get(struct api_ws *api, struct readbuf *rb, struct api_ws_event **ev)
548 /* get event data from the message */
549 if (!api_ws_client_msg_event_read(rb, &eventid, &name)) {
550 ERROR("Invalid message");
554 /* check conflicts */
555 *ev = api_ws_client_event_search(api, eventid, name);
557 ERROR("event %s not found", name);
564 /* get event from the message */
565 static int api_ws_client_msg_memo_get(struct api_ws *api, struct readbuf *rb, struct api_ws_memo **memo)
569 /* get event data from the message */
570 if (!api_ws_read_uint32(rb, &msgid)) {
571 ERROR("Invalid message");
576 *memo = api_ws_client_memo_search(api, msgid);
578 ERROR("message not found");
585 /* read a subscrition message */
586 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)
588 return api_ws_client_msg_memo_get(api, rb, memo) && api_ws_client_msg_event_get(api, rb, ev);
592 static void api_ws_client_event_create(struct api_ws *api, struct readbuf *rb)
597 struct api_ws_event *ev;
599 /* get event data from the message */
600 offset = api_ws_client_msg_event_read(rb, &eventid, &name);
602 ERROR("Invalid message");
606 /* check conflicts */
607 ev = api_ws_client_event_search(api, eventid, name);
613 /* no conflict, try to add it */
614 ev = malloc(sizeof *ev);
616 ev->event = afb_evt_create_event(name);
617 if (ev->event.closure == NULL)
621 ev->eventid = eventid;
622 ev->next = api->client.events;
623 api->client.events = ev;
627 ERROR("can't create event %s, out of memory", name);
630 /* removes an event */
631 static void api_ws_client_event_drop(struct api_ws *api, struct readbuf *rb)
633 struct api_ws_event *ev, **prv;
635 /* retrieves the event */
636 if (!api_ws_client_msg_event_get(api, rb, &ev))
639 /* decrease the reference count */
643 /* unlinks the event */
644 prv = &api->client.events;
649 /* destroys the event */
650 afb_event_drop(ev->event);
654 /* subscribes an event */
655 static void api_ws_client_event_subscribe(struct api_ws *api, struct readbuf *rb)
657 struct api_ws_event *ev;
658 struct api_ws_memo *memo;
660 if (api_ws_client_msg_subscription_get(api, rb, &ev, &memo)) {
661 /* subscribe the request from the event */
662 if (afb_req_subscribe(memo->req, ev->event) < 0)
663 ERROR("can't subscribe: %m");
667 /* unsubscribes an event */
668 static void api_ws_client_event_unsubscribe(struct api_ws *api, struct readbuf *rb)
670 struct api_ws_event *ev;
671 struct api_ws_memo *memo;
673 if (api_ws_client_msg_subscription_get(api, rb, &ev, &memo)) {
674 /* unsubscribe the request from the event */
675 if (afb_req_unsubscribe(memo->req, ev->event) < 0)
676 ERROR("can't unsubscribe: %m");
680 /* receives broadcasted events */
681 static void api_ws_client_event_broadcast(struct api_ws *api, struct readbuf *rb)
683 struct json_object *object;
686 if (api_ws_read_string(rb, &event, NULL) && api_ws_read_object(rb, &object))
687 afb_evt_broadcast(event, object);
689 ERROR("unreadable broadcasted event");
693 static void api_ws_client_event_push(struct api_ws *api, struct readbuf *rb)
695 struct api_ws_event *ev;
696 struct json_object *object;
698 if (api_ws_client_msg_event_get(api, rb, &ev) && api_ws_read_object(rb, &object))
699 afb_event_push(ev->event, object);
701 ERROR("unreadable push event");
704 static void api_ws_client_reply_success(struct api_ws *api, struct readbuf *rb)
706 struct api_ws_memo *memo;
707 struct json_object *object;
711 /* retrieve the message data */
712 if (!api_ws_client_msg_memo_get(api, rb, &memo))
715 if (api_ws_read_uint32(rb, &flags)
716 && api_ws_read_string(rb, &info, NULL)
717 && api_ws_read_object(rb, &object)) {
718 memo->context->flags = (unsigned)flags;
719 afb_req_success(memo->req, object, *info ? info : NULL);
721 /* failing to have the answer */
722 afb_req_fail(memo->req, "error", "ws error");
724 api_ws_client_memo_destroy(memo);
727 static void api_ws_client_reply_fail(struct api_ws *api, struct readbuf *rb)
729 struct api_ws_memo *memo;
730 const char *info, *status;
733 /* retrieve the message data */
734 if (!api_ws_client_msg_memo_get(api, rb, &memo))
737 if (api_ws_read_uint32(rb, &flags)
738 && api_ws_read_string(rb, &status, NULL)
739 && api_ws_read_string(rb, &info, NULL)) {
740 memo->context->flags = (unsigned)flags;
741 afb_req_fail(memo->req, status, *info ? info : NULL);
743 /* failing to have the answer */
744 afb_req_fail(memo->req, "error", "ws error");
746 api_ws_client_memo_destroy(memo);
749 static void api_ws_client_reply_send(struct api_ws *api, struct readbuf *rb)
751 struct api_ws_memo *memo;
756 /* retrieve the message data */
757 if (!api_ws_client_msg_memo_get(api, rb, &memo))
760 if (api_ws_read_uint32(rb, &flags)
761 && api_ws_read_string(rb, &data, &length)) {
762 memo->context->flags = (unsigned)flags;
763 afb_req_send(memo->req, data, length);
765 /* failing to have the answer */
766 afb_req_fail(memo->req, "error", "ws error");
768 api_ws_client_memo_destroy(memo);
771 /* callback when receiving binary data */
772 static void api_ws_client_on_binary(void *closure, char *data, size_t size)
775 struct readbuf rb = { .head = data, .end = data + size };
776 switch (*rb.head++) {
777 case 'T': /* success */
778 api_ws_client_reply_success(closure, &rb);
781 api_ws_client_reply_fail(closure, &rb);
784 api_ws_client_reply_send(closure, &rb);
786 case '*': /* broadcast */
787 api_ws_client_event_broadcast(closure, &rb);
789 case '+': /* creates the event */
790 api_ws_client_event_create(closure, &rb);
792 case '-': /* drops the event */
793 api_ws_client_event_drop(closure, &rb);
795 case '!': /* pushs the event */
796 api_ws_client_event_push(closure, &rb);
798 case 'S': /* subscribe event for a request */
799 api_ws_client_event_subscribe(closure, &rb);
801 case 'U': /* unsubscribe event for a request */
802 api_ws_client_event_unsubscribe(closure, &rb);
804 default: /* unexpected message */
811 /* on call, propagate it to the ws service */
812 static void api_ws_client_call_cb(void * closure, struct afb_req req, struct afb_context *context, const char *verb)
815 struct api_ws_memo *memo;
816 struct writebuf wb = { .count = 0 };
819 struct api_ws *api = closure;
821 /* create the recording data */
822 memo = api_ws_client_memo_make(api, req, context);
824 afb_req_fail(req, "error", "out of memory");
828 /* creates the call message */
829 raw = afb_req_raw(req, &szraw);
832 if (!api_ws_write_uint32(&wb, memo->msgid)
833 || !api_ws_write_uint32(&wb, (uint32_t)context->flags)
834 || !api_ws_write_string(&wb, verb)
835 || !api_ws_write_string(&wb, afb_session_uuid(context->session))
836 || !api_ws_write_string_length(&wb, raw, szraw))
840 rc = afb_ws_binary_v(api->client.ws, wb.iovec, wb.count);
846 afb_req_fail(req, "error", "websocket sending error");
850 afb_req_fail(req, "error", "internal: raw is NULL!");
854 afb_req_fail(req, "error", "overflow: size doesn't match 32 bits!");
857 api_ws_client_memo_destroy(memo);
860 static int api_ws_service_start_cb(void *closure, int share_session, int onneed)
862 struct api_ws *api = closure;
864 /* not an error when onneed */
868 /* already started: it is an error */
869 ERROR("The WS binding %s is not a startable service", api->path);
874 static void api_ws_client_disconnect(struct api_ws *api)
877 afb_ws_destroy(api->client.ws);
878 api->client.ws = NULL;
885 static int api_ws_client_connect(struct api_ws *api)
890 fd = api_ws_socket(api->path, 0);
892 ws = afb_ws_create(afb_common_get_event_loop(), fd, &api_ws_client_ws_itf, api);
903 /* adds a afb-ws-service client api */
904 int afb_api_ws_add_client(const char *path)
908 struct afb_api afb_api;
910 /* create the ws client api */
911 api = api_ws_make(path);
915 /* connect to the service */
916 rc = api_ws_client_connect(api);
918 ERROR("can't connect to ws service %s", api->path);
922 /* record it as an API */
923 afb_api.closure = api;
924 afb_api.call = api_ws_client_call_cb;
925 afb_api.service_start = api_ws_service_start_cb;
926 if (afb_apis_add(api->api, afb_api) < 0)
932 api_ws_client_disconnect(api);
939 /******************* client description part for server *****************************/
941 static void api_ws_server_client_unref(struct api_ws_client *client)
943 if (!--client->refcount) {
944 afb_evt_listener_unref(client->listener);
945 afb_ws_destroy(client->ws);
950 /* on call, propagate it to the ws service */
951 static void api_ws_server_called(struct api_ws_client *client, struct readbuf *rb, char *data, size_t size)
953 struct api_ws_server_req *wreq;
955 const char *uuid, *verb;
960 /* create the request */
961 wreq = calloc(1 , sizeof *wreq);
965 wreq->client = client;
966 wreq->rcvdata = data;
969 /* reads the call message data */
970 if (!api_ws_read_uint32(rb, &wreq->msgid)
971 || !api_ws_read_uint32(rb, &flags)
972 || !api_ws_read_string(rb, &verb, NULL)
973 || !api_ws_read_string(rb, &uuid, NULL)
974 || !api_ws_read_string(rb, &wreq->request, &wreq->lenreq))
977 /* init the context */
978 if (afb_context_connect(&wreq->context, uuid, NULL) < 0)
980 wreq->context.flags = flags;
983 areq.itf = &afb_api_ws_req_itf;
985 afb_apis_call(areq, &wreq->context, client->api, verb);
986 api_ws_server_req_unref(wreq);
993 api_ws_server_client_unref(client);
996 /* callback when receiving binary data */
997 static void api_ws_server_on_binary(void *closure, char *data, size_t size)
999 struct readbuf rb = { .head = data, .end = data + size };
1000 api_ws_server_called(closure, &rb, data, size);
1003 /* callback when receiving a hangup */
1004 static void api_ws_server_on_hangup(void *closure)
1006 struct api_ws_client *client = closure;
1008 /* close the socket */
1009 if (client->fd >= 0) {
1014 /* release the client */
1015 api_ws_server_client_unref(client);
1018 static void api_ws_server_accept(struct api_ws *api)
1020 struct api_ws_client *client;
1021 struct sockaddr addr;
1024 client = calloc(1, sizeof *client);
1025 if (client != NULL) {
1026 client->listener = afb_evt_listener_create(&api_ws_server_evt_itf, client);
1027 if (client->listener != NULL) {
1028 lenaddr = (socklen_t)sizeof addr;
1029 client->fd = accept(api->fd, &addr, &lenaddr);
1030 if (client->fd >= 0) {
1031 fcntl(client->fd, F_SETFD, FD_CLOEXEC);
1032 fcntl(client->fd, F_SETFL, O_NONBLOCK);
1033 client->ws = afb_ws_create(afb_common_get_event_loop(), client->fd, &api_ws_server_ws_itf, client);
1034 if (client->ws != NULL) {
1035 client->api = api->api;
1036 client->refcount = 1;
1041 afb_evt_listener_unref(client->listener);
1047 /******************* server part: manage events **********************************/
1049 static void api_ws_server_event_send(struct api_ws_client *client, char order, const char *event, int eventid, const char *data)
1052 struct writebuf wb = { .count = 0 };
1054 if (api_ws_write_char(&wb, order)
1055 && api_ws_write_uint32(&wb, eventid)
1056 && api_ws_write_string(&wb, event)
1057 && (data == NULL || api_ws_write_string(&wb, data))) {
1058 rc = afb_ws_binary_v(client->ws, wb.iovec, wb.count);
1062 ERROR("error while sending %c for event %s", order, event);
1065 static void api_ws_server_event_add(void *closure, const char *event, int eventid)
1067 api_ws_server_event_send(closure, '+', event, eventid, NULL);
1070 static void api_ws_server_event_remove(void *closure, const char *event, int eventid)
1072 api_ws_server_event_send(closure, '-', event, eventid, NULL);
1075 static void api_ws_server_event_push(void *closure, const char *event, int eventid, struct json_object *object)
1077 const char *data = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
1078 api_ws_server_event_send(closure, '!', event, eventid, data ? : "null");
1079 json_object_put(object);
1082 static void api_ws_server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object)
1085 struct api_ws_client *client = closure;
1087 struct writebuf wb = { .count = 0 };
1089 if (api_ws_write_char(&wb, '*') && api_ws_write_string(&wb, event) && api_ws_write_object(&wb, object)) {
1090 rc = afb_ws_binary_v(client->ws, wb.iovec, wb.count);
1092 ERROR("error while broadcasting event %s", event);
1094 ERROR("error while broadcasting event %s", event);
1095 json_object_put(object);
1098 /******************* ws request part for server *****************/
1100 /* increment the reference count of the request */
1101 static void api_ws_server_req_addref_cb(void *closure)
1103 struct api_ws_server_req *wreq = closure;
1107 /* decrement the reference count of the request and free/release it on falling to null */
1108 static void api_ws_server_req_unref_cb(void *closure)
1110 api_ws_server_req_unref(closure);
1113 static void api_ws_server_req_unref(struct api_ws_server_req *wreq)
1115 if (wreq == NULL || --wreq->refcount)
1118 afb_context_disconnect(&wreq->context);
1119 json_object_put(wreq->json);
1120 free(wreq->rcvdata);
1121 api_ws_server_client_unref(wreq->client);
1125 /* get the object of the request */
1126 static struct json_object *api_ws_server_req_json_cb(void *closure)
1128 return api_ws_server_req_json(closure);
1131 static struct json_object *api_ws_server_req_json(struct api_ws_server_req *wreq)
1133 if (wreq->json == NULL) {
1134 wreq->json = json_tokener_parse(wreq->request);
1135 if (wreq->json == NULL && strcmp(wreq->request, "null")) {
1136 /* lazy error detection of json request. Is it to improve? */
1137 wreq->json = json_object_new_string(wreq->request);
1143 /* get the argument of the request of 'name' */
1144 static struct afb_arg api_ws_server_req_get_cb(void *closure, const char *name)
1146 struct api_ws_server_req *wreq = closure;
1147 return afb_msg_json_get_arg(api_ws_server_req_json(wreq), name);
1150 static void api_ws_server_req_success_cb(void *closure, struct json_object *obj, const char *info)
1153 struct writebuf wb = { .count = 0 };
1154 struct api_ws_server_req *wreq = closure;
1156 if (api_ws_write_char(&wb, 'T')
1157 && api_ws_write_uint32(&wb, wreq->msgid)
1158 && api_ws_write_uint32(&wb, (uint32_t)wreq->context.flags)
1159 && api_ws_write_string(&wb, info ? : "")
1160 && api_ws_write_object(&wb, obj)) {
1161 rc = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1165 ERROR("error while sending success");
1167 json_object_put(obj);
1170 static void api_ws_server_req_fail_cb(void *closure, const char *status, const char *info)
1173 struct writebuf wb = { .count = 0 };
1174 struct api_ws_server_req *wreq = closure;
1176 if (api_ws_write_char(&wb, 'F')
1177 && api_ws_write_uint32(&wb, wreq->msgid)
1178 && api_ws_write_uint32(&wb, (uint32_t)wreq->context.flags)
1179 && api_ws_write_string(&wb, status)
1180 && api_ws_write_string(&wb, info ? : "")) {
1181 rc = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1185 ERROR("error while sending fail");
1188 static const char *api_ws_server_req_raw_cb(void *closure, size_t *size)
1190 struct api_ws_server_req *wreq = closure;
1192 *size = wreq->lenreq;
1193 return wreq->request;
1196 static void api_ws_server_req_send_cb(void *closure, const char *buffer, size_t size)
1198 /* TODO: how to put sized buffer as strings? things aren't clear here!!! */
1200 struct writebuf wb = { .count = 0 };
1201 struct api_ws_server_req *wreq = closure;
1203 if (api_ws_write_char(&wb, 'X')
1204 && api_ws_write_uint32(&wb, wreq->msgid)
1205 && api_ws_write_uint32(&wb, (uint32_t)wreq->context.flags)
1206 && api_ws_write_string_length(&wb, buffer, size)) {
1207 rc = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1211 ERROR("error while sending raw");
1214 static int api_ws_server_req_subscribe_cb(void *closure, struct afb_event event)
1217 struct writebuf wb = { .count = 0 };
1218 struct api_ws_server_req *wreq = closure;
1220 rc = afb_evt_add_watch(wreq->client->listener, event);
1224 if (api_ws_write_char(&wb, 'S')
1225 && api_ws_write_uint32(&wb, wreq->msgid)
1226 && api_ws_write_uint32(&wb, (uint32_t)afb_evt_event_id(event))
1227 && api_ws_write_string(&wb, afb_evt_event_name(event))) {
1228 rc2 = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1232 ERROR("error while subscribing event");
1237 static int api_ws_server_req_unsubscribe_cb(void *closure, struct afb_event event)
1240 struct writebuf wb = { .count = 0 };
1241 struct api_ws_server_req *wreq = closure;
1243 if (api_ws_write_char(&wb, 'U')
1244 && api_ws_write_uint32(&wb, wreq->msgid)
1245 && api_ws_write_uint32(&wb, (uint32_t)afb_evt_event_id(event))
1246 && api_ws_write_string(&wb, afb_evt_event_name(event))) {
1247 rc2 = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1251 ERROR("error while subscribing event");
1253 rc = afb_evt_remove_watch(wreq->client->listener, event);
1257 static void api_ws_server_req_subcall_cb(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
1259 struct api_ws_server_req *wreq = closure;
1260 afb_subcall(&wreq->context, api, verb, args, callback, cb_closure, (struct afb_req){ .itf = &afb_api_ws_req_itf, .closure = wreq });
1263 /******************* server part **********************************/
1265 static int api_ws_server_connect(struct api_ws *api);
1267 static int api_ws_server_listen_callback(sd_event_source *src, int fd, uint32_t revents, void *closure)
1269 if ((revents & EPOLLIN) != 0)
1270 api_ws_server_accept(closure);
1271 if ((revents & EPOLLHUP) != 0)
1272 api_ws_server_connect(closure);
1276 static void api_ws_server_disconnect(struct api_ws *api)
1278 if (api->server.listensrc != NULL) {
1279 sd_event_source_unref(api->server.listensrc);
1280 api->server.listensrc = NULL;
1288 static int api_ws_server_connect(struct api_ws *api)
1292 /* ensure disconnected */
1293 api_ws_server_disconnect(api);
1295 /* request the service object name */
1296 api->fd = api_ws_socket(api->path, 1);
1298 ERROR("can't create socket %s", api->path);
1300 /* listen for service */
1301 rc = sd_event_add_io(afb_common_get_event_loop(), &api->server.listensrc, api->fd, EPOLLIN, api_ws_server_listen_callback, api);
1306 ERROR("can't add ws object %s", api->path);
1311 /* create the service */
1312 int afb_api_ws_add_server(const char *path)
1317 /* creates the ws api object */
1318 api = api_ws_make(path);
1322 /* connect for serving */
1323 rc = api_ws_server_connect(api);