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_nz(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) && api_ws_write_char(wb, '\0');
440 static int api_ws_write_string_length(struct writebuf *wb, const char *value, size_t length)
442 uint32_t len = (uint32_t)++length;
443 return (size_t)len == length && len && api_ws_write_uint32(wb, len) && api_ws_write_put(wb, value, length);
446 static int api_ws_write_string(struct writebuf *wb, const char *value)
448 return api_ws_write_string_length(wb, value, strlen(value));
451 static int api_ws_write_object(struct writebuf *wb, struct json_object *object)
453 const char *string = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
454 return string != NULL && api_ws_write_string(wb, string);
460 /******************* client part **********************************/
463 * structure for recording query data
466 struct api_ws_memo *next; /* the next memo */
467 struct api_ws *api; /* the ws api */
468 struct afb_req req; /* the request handle */
469 struct afb_context *context; /* the context of the query */
470 uint32_t msgid; /* the message identifier */
475 struct api_ws_event *next;
476 struct afb_event event;
481 /* search a memorized request */
482 static struct api_ws_memo *api_ws_client_memo_search(struct api_ws *api, uint32_t msgid)
484 struct api_ws_memo *memo;
486 memo = api->client.memos;
487 while (memo != NULL && memo->msgid != msgid)
493 /* search the event */
494 static struct api_ws_event *api_ws_client_event_search(struct api_ws *api, uint32_t eventid, const char *name)
496 struct api_ws_event *ev;
498 ev = api->client.events;
499 while (ev != NULL && (ev->eventid != eventid || 0 != strcmp(afb_evt_event_name(ev->event), name)))
506 /* allocates and init the memorizing data */
507 static struct api_ws_memo *api_ws_client_memo_make(struct api_ws *api, struct afb_req req, struct afb_context *context)
509 struct api_ws_memo *memo;
511 memo = malloc(sizeof *memo);
515 memo->context = context;
516 do { memo->msgid = ++api->client.id; } while(api_ws_client_memo_search(api, memo->msgid) != NULL);
518 memo->next = api->client.memos;
519 api->client.memos = memo;
524 /* free and release the memorizing data */
525 static void api_ws_client_memo_destroy(struct api_ws_memo *memo)
527 struct api_ws_memo **prv;
529 prv = &memo->api->client.memos;
530 while (*prv != NULL) {
538 afb_req_unref(memo->req);
542 /* get event data from the message */
543 static int api_ws_client_msg_event_read(struct readbuf *rb, uint32_t *eventid, const char **name)
545 return api_ws_read_uint32(rb, eventid) && api_ws_read_string(rb, name, NULL);
548 /* get event from the message */
549 static int api_ws_client_msg_event_get(struct api_ws *api, struct readbuf *rb, struct api_ws_event **ev)
554 /* get event data from the message */
555 if (!api_ws_client_msg_event_read(rb, &eventid, &name)) {
556 ERROR("Invalid message");
560 /* check conflicts */
561 *ev = api_ws_client_event_search(api, eventid, name);
563 ERROR("event %s not found", name);
570 /* get event from the message */
571 static int api_ws_client_msg_memo_get(struct api_ws *api, struct readbuf *rb, struct api_ws_memo **memo)
575 /* get event data from the message */
576 if (!api_ws_read_uint32(rb, &msgid)) {
577 ERROR("Invalid message");
582 *memo = api_ws_client_memo_search(api, msgid);
584 ERROR("message not found");
591 /* read a subscrition message */
592 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)
594 return api_ws_client_msg_memo_get(api, rb, memo) && api_ws_client_msg_event_get(api, rb, ev);
598 static void api_ws_client_event_create(struct api_ws *api, struct readbuf *rb)
603 struct api_ws_event *ev;
605 /* get event data from the message */
606 offset = api_ws_client_msg_event_read(rb, &eventid, &name);
608 ERROR("Invalid message");
612 /* check conflicts */
613 ev = api_ws_client_event_search(api, eventid, name);
619 /* no conflict, try to add it */
620 ev = malloc(sizeof *ev);
622 ev->event = afb_evt_create_event(name);
623 if (ev->event.closure == NULL)
627 ev->eventid = eventid;
628 ev->next = api->client.events;
629 api->client.events = ev;
633 ERROR("can't create event %s, out of memory", name);
636 /* removes an event */
637 static void api_ws_client_event_drop(struct api_ws *api, struct readbuf *rb)
639 struct api_ws_event *ev, **prv;
641 /* retrieves the event */
642 if (!api_ws_client_msg_event_get(api, rb, &ev))
645 /* decrease the reference count */
649 /* unlinks the event */
650 prv = &api->client.events;
655 /* destroys the event */
656 afb_event_drop(ev->event);
660 /* subscribes an event */
661 static void api_ws_client_event_subscribe(struct api_ws *api, struct readbuf *rb)
663 struct api_ws_event *ev;
664 struct api_ws_memo *memo;
666 if (api_ws_client_msg_subscription_get(api, rb, &ev, &memo)) {
667 /* subscribe the request from the event */
668 if (afb_req_subscribe(memo->req, ev->event) < 0)
669 ERROR("can't subscribe: %m");
673 /* unsubscribes an event */
674 static void api_ws_client_event_unsubscribe(struct api_ws *api, struct readbuf *rb)
676 struct api_ws_event *ev;
677 struct api_ws_memo *memo;
679 if (api_ws_client_msg_subscription_get(api, rb, &ev, &memo)) {
680 /* unsubscribe the request from the event */
681 if (afb_req_unsubscribe(memo->req, ev->event) < 0)
682 ERROR("can't unsubscribe: %m");
686 /* receives broadcasted events */
687 static void api_ws_client_event_broadcast(struct api_ws *api, struct readbuf *rb)
689 struct json_object *object;
692 if (api_ws_read_string(rb, &event, NULL) && api_ws_read_object(rb, &object))
693 afb_evt_broadcast(event, object);
695 ERROR("unreadable broadcasted event");
699 static void api_ws_client_event_push(struct api_ws *api, struct readbuf *rb)
701 struct api_ws_event *ev;
702 struct json_object *object;
704 if (api_ws_client_msg_event_get(api, rb, &ev) && api_ws_read_object(rb, &object))
705 afb_event_push(ev->event, object);
707 ERROR("unreadable push event");
710 static void api_ws_client_reply_success(struct api_ws *api, struct readbuf *rb)
712 struct api_ws_memo *memo;
713 struct json_object *object;
717 /* retrieve the message data */
718 if (!api_ws_client_msg_memo_get(api, rb, &memo))
721 if (api_ws_read_uint32(rb, &flags)
722 && api_ws_read_string(rb, &info, NULL)
723 && api_ws_read_object(rb, &object)) {
724 memo->context->flags = (unsigned)flags;
725 afb_req_success(memo->req, object, *info ? info : NULL);
727 /* failing to have the answer */
728 afb_req_fail(memo->req, "error", "ws error");
730 api_ws_client_memo_destroy(memo);
733 static void api_ws_client_reply_fail(struct api_ws *api, struct readbuf *rb)
735 struct api_ws_memo *memo;
736 const char *info, *status;
739 /* retrieve the message data */
740 if (!api_ws_client_msg_memo_get(api, rb, &memo))
743 if (api_ws_read_uint32(rb, &flags)
744 && api_ws_read_string(rb, &status, NULL)
745 && api_ws_read_string(rb, &info, NULL)) {
746 memo->context->flags = (unsigned)flags;
747 afb_req_fail(memo->req, status, *info ? info : NULL);
749 /* failing to have the answer */
750 afb_req_fail(memo->req, "error", "ws error");
752 api_ws_client_memo_destroy(memo);
755 static void api_ws_client_reply_send(struct api_ws *api, struct readbuf *rb)
757 struct api_ws_memo *memo;
762 /* retrieve the message data */
763 if (!api_ws_client_msg_memo_get(api, rb, &memo))
766 if (api_ws_read_uint32(rb, &flags)
767 && api_ws_read_string(rb, &data, &length)) {
768 memo->context->flags = (unsigned)flags;
769 afb_req_send(memo->req, data, length);
771 /* failing to have the answer */
772 afb_req_fail(memo->req, "error", "ws error");
774 api_ws_client_memo_destroy(memo);
777 /* callback when receiving binary data */
778 static void api_ws_client_on_binary(void *closure, char *data, size_t size)
781 struct readbuf rb = { .head = data, .end = data + size };
782 switch (*rb.head++) {
783 case 'T': /* success */
784 api_ws_client_reply_success(closure, &rb);
787 api_ws_client_reply_fail(closure, &rb);
790 api_ws_client_reply_send(closure, &rb);
792 case '*': /* broadcast */
793 api_ws_client_event_broadcast(closure, &rb);
795 case '+': /* creates the event */
796 api_ws_client_event_create(closure, &rb);
798 case '-': /* drops the event */
799 api_ws_client_event_drop(closure, &rb);
801 case '!': /* pushs the event */
802 api_ws_client_event_push(closure, &rb);
804 case 'S': /* subscribe event for a request */
805 api_ws_client_event_subscribe(closure, &rb);
807 case 'U': /* unsubscribe event for a request */
808 api_ws_client_event_unsubscribe(closure, &rb);
810 default: /* unexpected message */
817 /* on call, propagate it to the ws service */
818 static void api_ws_client_call_cb(void * closure, struct afb_req req, struct afb_context *context, const char *verb, size_t lenverb)
821 struct api_ws_memo *memo;
822 struct writebuf wb = { .count = 0 };
825 struct api_ws *api = closure;
827 /* create the recording data */
828 memo = api_ws_client_memo_make(api, req, context);
830 afb_req_fail(req, "error", "out of memory");
834 /* creates the call message */
835 raw = afb_req_raw(req, &szraw);
838 if (!api_ws_write_uint32(&wb, memo->msgid)
839 || !api_ws_write_uint32(&wb, (uint32_t)context->flags)
840 || !api_ws_write_string_nz(&wb, verb, lenverb)
841 || !api_ws_write_string(&wb, afb_session_uuid(context->session))
842 || !api_ws_write_string_length(&wb, raw, szraw))
846 rc = afb_ws_binary_v(api->client.ws, wb.iovec, wb.count);
852 afb_req_fail(req, "error", "websocket sending error");
856 afb_req_fail(req, "error", "internal: raw is NULL!");
860 afb_req_fail(req, "error", "overflow: size doesn't match 32 bits!");
863 api_ws_client_memo_destroy(memo);
866 static int api_ws_service_start_cb(void *closure, int share_session, int onneed)
868 struct api_ws *api = closure;
870 /* not an error when onneed */
874 /* already started: it is an error */
875 ERROR("The WS binding %s is not a startable service", api->path);
880 static void api_ws_client_disconnect(struct api_ws *api)
883 afb_ws_destroy(api->client.ws);
884 api->client.ws = NULL;
891 static int api_ws_client_connect(struct api_ws *api)
896 fd = api_ws_socket(api->path, 0);
898 ws = afb_ws_create(afb_common_get_event_loop(), fd, &api_ws_client_ws_itf, api);
909 /* adds a afb-ws-service client api */
910 int afb_api_ws_add_client(const char *path)
914 struct afb_api afb_api;
916 /* create the ws client api */
917 api = api_ws_make(path);
921 /* connect to the service */
922 rc = api_ws_client_connect(api);
924 ERROR("can't connect to ws service %s", api->path);
928 /* record it as an API */
929 afb_api.closure = api;
930 afb_api.call = api_ws_client_call_cb;
931 afb_api.service_start = api_ws_service_start_cb;
932 if (afb_apis_add(api->api, afb_api) < 0)
938 api_ws_client_disconnect(api);
945 /******************* client description part for server *****************************/
947 static void api_ws_server_client_unref(struct api_ws_client *client)
949 if (!--client->refcount) {
950 afb_evt_listener_unref(client->listener);
951 afb_ws_destroy(client->ws);
956 /* on call, propagate it to the ws service */
957 static void api_ws_server_called(struct api_ws_client *client, struct readbuf *rb, char *data, size_t size)
959 struct api_ws_server_req *wreq;
961 const char *uuid, *verb;
966 /* create the request */
967 wreq = calloc(1 , sizeof *wreq);
971 wreq->client = client;
972 wreq->rcvdata = data;
975 /* reads the call message data */
976 if (!api_ws_read_uint32(rb, &wreq->msgid)
977 || !api_ws_read_uint32(rb, &flags)
978 || !api_ws_read_string(rb, &verb, NULL)
979 || !api_ws_read_string(rb, &uuid, NULL)
980 || !api_ws_read_string(rb, &wreq->request, &wreq->lenreq))
983 /* init the context */
984 if (afb_context_connect(&wreq->context, uuid, NULL) < 0)
986 wreq->context.flags = flags;
989 areq.itf = &afb_api_ws_req_itf;
991 afb_apis_call_(areq, &wreq->context, client->api, verb);
992 api_ws_server_req_unref(wreq);
999 api_ws_server_client_unref(client);
1002 /* callback when receiving binary data */
1003 static void api_ws_server_on_binary(void *closure, char *data, size_t size)
1005 struct readbuf rb = { .head = data, .end = data + size };
1006 api_ws_server_called(closure, &rb, data, size);
1009 /* callback when receiving a hangup */
1010 static void api_ws_server_on_hangup(void *closure)
1012 struct api_ws_client *client = closure;
1014 /* close the socket */
1015 if (client->fd >= 0) {
1020 /* release the client */
1021 api_ws_server_client_unref(client);
1024 static void api_ws_server_accept(struct api_ws *api)
1026 struct api_ws_client *client;
1027 struct sockaddr addr;
1030 client = calloc(1, sizeof *client);
1031 if (client != NULL) {
1032 client->listener = afb_evt_listener_create(&api_ws_server_evt_itf, client);
1033 if (client->listener != NULL) {
1034 lenaddr = (socklen_t)sizeof addr;
1035 client->fd = accept(api->fd, &addr, &lenaddr);
1036 if (client->fd >= 0) {
1037 fcntl(client->fd, F_SETFD, FD_CLOEXEC);
1038 fcntl(client->fd, F_SETFL, O_NONBLOCK);
1039 client->ws = afb_ws_create(afb_common_get_event_loop(), client->fd, &api_ws_server_ws_itf, client);
1040 if (client->ws != NULL) {
1041 client->api = api->api;
1042 client->refcount = 1;
1047 afb_evt_listener_unref(client->listener);
1053 /******************* server part: manage events **********************************/
1055 static void api_ws_server_event_send(struct api_ws_client *client, char order, const char *event, int eventid, const char *data)
1058 struct writebuf wb = { .count = 0 };
1060 if (api_ws_write_char(&wb, order)
1061 && api_ws_write_uint32(&wb, eventid)
1062 && api_ws_write_string(&wb, event)
1063 && (data == NULL || api_ws_write_string(&wb, data))) {
1064 rc = afb_ws_binary_v(client->ws, wb.iovec, wb.count);
1068 ERROR("error while sending %c for event %s", order, event);
1071 static void api_ws_server_event_add(void *closure, const char *event, int eventid)
1073 api_ws_server_event_send(closure, '+', event, eventid, NULL);
1076 static void api_ws_server_event_remove(void *closure, const char *event, int eventid)
1078 api_ws_server_event_send(closure, '-', event, eventid, NULL);
1081 static void api_ws_server_event_push(void *closure, const char *event, int eventid, struct json_object *object)
1083 const char *data = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
1084 api_ws_server_event_send(closure, '!', event, eventid, data ? : "null");
1085 json_object_put(object);
1088 static void api_ws_server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object)
1091 struct api_ws_client *client = closure;
1093 struct writebuf wb = { .count = 0 };
1095 if (api_ws_write_char(&wb, '*') && api_ws_write_string(&wb, event) && api_ws_write_object(&wb, object)) {
1096 rc = afb_ws_binary_v(client->ws, wb.iovec, wb.count);
1098 ERROR("error while broadcasting event %s", event);
1100 ERROR("error while broadcasting event %s", event);
1101 json_object_put(object);
1104 /******************* ws request part for server *****************/
1106 /* increment the reference count of the request */
1107 static void api_ws_server_req_addref_cb(void *closure)
1109 struct api_ws_server_req *wreq = closure;
1113 /* decrement the reference count of the request and free/release it on falling to null */
1114 static void api_ws_server_req_unref_cb(void *closure)
1116 api_ws_server_req_unref(closure);
1119 static void api_ws_server_req_unref(struct api_ws_server_req *wreq)
1121 if (wreq == NULL || --wreq->refcount)
1124 afb_context_disconnect(&wreq->context);
1125 json_object_put(wreq->json);
1126 free(wreq->rcvdata);
1127 api_ws_server_client_unref(wreq->client);
1131 /* get the object of the request */
1132 static struct json_object *api_ws_server_req_json_cb(void *closure)
1134 return api_ws_server_req_json(closure);
1137 static struct json_object *api_ws_server_req_json(struct api_ws_server_req *wreq)
1139 if (wreq->json == NULL) {
1140 wreq->json = json_tokener_parse(wreq->request);
1141 if (wreq->json == NULL && strcmp(wreq->request, "null")) {
1142 /* lazy error detection of json request. Is it to improve? */
1143 wreq->json = json_object_new_string(wreq->request);
1149 /* get the argument of the request of 'name' */
1150 static struct afb_arg api_ws_server_req_get_cb(void *closure, const char *name)
1152 struct api_ws_server_req *wreq = closure;
1153 return afb_msg_json_get_arg(api_ws_server_req_json(wreq), name);
1156 static void api_ws_server_req_success_cb(void *closure, struct json_object *obj, const char *info)
1159 struct writebuf wb = { .count = 0 };
1160 struct api_ws_server_req *wreq = closure;
1162 if (api_ws_write_char(&wb, 'T')
1163 && api_ws_write_uint32(&wb, wreq->msgid)
1164 && api_ws_write_uint32(&wb, (uint32_t)wreq->context.flags)
1165 && api_ws_write_string(&wb, info ? : "")
1166 && api_ws_write_object(&wb, obj)) {
1167 rc = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1171 ERROR("error while sending success");
1173 json_object_put(obj);
1176 static void api_ws_server_req_fail_cb(void *closure, const char *status, const char *info)
1179 struct writebuf wb = { .count = 0 };
1180 struct api_ws_server_req *wreq = closure;
1182 if (api_ws_write_char(&wb, 'F')
1183 && api_ws_write_uint32(&wb, wreq->msgid)
1184 && api_ws_write_uint32(&wb, (uint32_t)wreq->context.flags)
1185 && api_ws_write_string(&wb, status)
1186 && api_ws_write_string(&wb, info ? : "")) {
1187 rc = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1191 ERROR("error while sending fail");
1194 static const char *api_ws_server_req_raw_cb(void *closure, size_t *size)
1196 struct api_ws_server_req *wreq = closure;
1198 *size = wreq->lenreq;
1199 return wreq->request;
1202 static void api_ws_server_req_send_cb(void *closure, const char *buffer, size_t size)
1204 /* TODO: how to put sized buffer as strings? things aren't clear here!!! */
1206 struct writebuf wb = { .count = 0 };
1207 struct api_ws_server_req *wreq = closure;
1209 if (api_ws_write_char(&wb, 'X')
1210 && api_ws_write_uint32(&wb, wreq->msgid)
1211 && api_ws_write_uint32(&wb, (uint32_t)wreq->context.flags)
1212 && api_ws_write_string_length(&wb, buffer, size)) {
1213 rc = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1217 ERROR("error while sending raw");
1220 static int api_ws_server_req_subscribe_cb(void *closure, struct afb_event event)
1223 struct writebuf wb = { .count = 0 };
1224 struct api_ws_server_req *wreq = closure;
1226 rc = afb_evt_add_watch(wreq->client->listener, event);
1230 if (api_ws_write_char(&wb, 'S')
1231 && api_ws_write_uint32(&wb, wreq->msgid)
1232 && api_ws_write_uint32(&wb, (uint32_t)afb_evt_event_id(event))
1233 && api_ws_write_string(&wb, afb_evt_event_name(event))) {
1234 rc2 = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1238 ERROR("error while subscribing event");
1243 static int api_ws_server_req_unsubscribe_cb(void *closure, struct afb_event event)
1246 struct writebuf wb = { .count = 0 };
1247 struct api_ws_server_req *wreq = closure;
1249 if (api_ws_write_char(&wb, 'U')
1250 && api_ws_write_uint32(&wb, wreq->msgid)
1251 && api_ws_write_uint32(&wb, (uint32_t)afb_evt_event_id(event))
1252 && api_ws_write_string(&wb, afb_evt_event_name(event))) {
1253 rc2 = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1257 ERROR("error while subscribing event");
1259 rc = afb_evt_remove_watch(wreq->client->listener, event);
1263 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)
1265 struct api_ws_server_req *wreq = closure;
1266 afb_subcall(&wreq->context, api, verb, args, callback, cb_closure, (struct afb_req){ .itf = &afb_api_ws_req_itf, .closure = wreq });
1269 /******************* server part **********************************/
1271 static int api_ws_server_connect(struct api_ws *api);
1273 static int api_ws_server_listen_callback(sd_event_source *src, int fd, uint32_t revents, void *closure)
1275 if ((revents & EPOLLIN) != 0)
1276 api_ws_server_accept(closure);
1277 if ((revents & EPOLLHUP) != 0)
1278 api_ws_server_connect(closure);
1282 static void api_ws_server_disconnect(struct api_ws *api)
1284 if (api->server.listensrc != NULL) {
1285 sd_event_source_unref(api->server.listensrc);
1286 api->server.listensrc = NULL;
1294 static int api_ws_server_connect(struct api_ws *api)
1298 /* ensure disconnected */
1299 api_ws_server_disconnect(api);
1301 /* request the service object name */
1302 api->fd = api_ws_socket(api->path, 1);
1304 ERROR("can't create socket %s", api->path);
1306 /* listen for service */
1307 rc = sd_event_add_io(afb_common_get_event_loop(), &api->server.listensrc, api->fd, EPOLLIN, api_ws_server_listen_callback, api);
1312 ERROR("can't add ws object %s", api->path);
1317 /* create the service */
1318 int afb_api_ws_add_server(const char *path)
1323 /* creates the ws api object */
1324 api = api_ws_make(path);
1328 /* connect for serving */
1329 rc = api_ws_server_connect(api);