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"
60 char *path; /* path of the object for the API */
61 char *api; /* api name of the interface */
62 int fd; /* file descriptor */
67 struct api_ws_event *events;
68 struct api_ws_memo *memos;
71 sd_event_source *listensrc;
72 struct afb_evt_listener *listener; /* listener for broadcasted events */
81 /******************* websocket interface for client part **********************************/
83 static void api_ws_client_on_binary(void *closure, char *data, size_t size);
85 static const struct afb_ws_itf api_ws_client_ws_itf =
89 .on_binary = api_ws_client_on_binary,
94 /******************* event structures for server part **********************************/
96 static void api_ws_server_event_add(void *closure, const char *event, int eventid);
97 static void api_ws_server_event_remove(void *closure, const char *event, int eventid);
98 static void api_ws_server_event_push(void *closure, const char *event, int eventid, struct json_object *object);
99 static void api_ws_server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object);
101 /* the interface for events pushing */
102 static const struct afb_evt_itf api_ws_server_evt_itf = {
103 .broadcast = api_ws_server_event_broadcast,
104 .push = api_ws_server_event_push,
105 .add = api_ws_server_event_add,
106 .remove = api_ws_server_event_remove
109 /******************* client description part for server *****************************/
113 /* the server ws-api */
116 /* count of references */
119 /* listener for events */
120 struct afb_evt_listener *listener;
122 /* file descriptor */
129 /******************* websocket interface for client part **********************************/
131 static void api_ws_server_on_binary(void *closure, char *data, size_t size);
132 static void api_ws_server_on_hangup(void *closure);
134 static const struct afb_ws_itf api_ws_server_ws_itf =
138 .on_binary = api_ws_server_on_binary,
140 .on_hangup = api_ws_server_on_hangup
143 /******************* ws request part for server *****************/
146 * structure for a ws request
148 struct api_ws_server_req {
149 struct afb_context context; /* the context, should be THE FIRST */
150 struct api_ws_client *client; /* the client of the request */
151 char *rcvdata; /* the received data to free */
152 struct json_object *json; /* the readen request as object */
153 const char *request; /* the readen request as string */
154 size_t lenreq; /* the length of the request */
155 int refcount; /* reference count of the request */
156 uint32_t msgid; /* the incoming request msgid */
159 static struct json_object *api_ws_server_req_json(struct api_ws_server_req *wreq);
160 static void api_ws_server_req_unref(struct api_ws_server_req *wreq);
162 static struct json_object *api_ws_server_req_json_cb(void *closure);
163 static struct afb_arg api_ws_server_req_get_cb(void *closure, const char *name);
164 static void api_ws_server_req_success_cb(void *closure, struct json_object *obj, const char *info);
165 static void api_ws_server_req_fail_cb(void *closure, const char *status, const char *info);
166 static const char *api_ws_server_req_raw_cb(void *closure, size_t *size);
167 static void api_ws_server_req_send_cb(void *closure, const char *buffer, size_t size);
168 static void api_ws_server_req_addref_cb(void *closure);
169 static void api_ws_server_req_unref_cb(void *closure);
170 static int api_ws_server_req_subscribe_cb(void *closure, struct afb_event event);
171 static int api_ws_server_req_unsubscribe_cb(void *closure, struct afb_event event);
172 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);
174 const struct afb_req_itf afb_api_ws_req_itf = {
175 .json = api_ws_server_req_json_cb,
176 .get = api_ws_server_req_get_cb,
177 .success = api_ws_server_req_success_cb,
178 .fail = api_ws_server_req_fail_cb,
179 .raw = api_ws_server_req_raw_cb,
180 .send = api_ws_server_req_send_cb,
181 .context_get = (void*)afb_context_get,
182 .context_set = (void*)afb_context_set,
183 .addref = api_ws_server_req_addref_cb,
184 .unref = api_ws_server_req_unref_cb,
185 .session_close = (void*)afb_context_close,
186 .session_set_LOA = (void*)afb_context_change_loa,
187 .subscribe = api_ws_server_req_subscribe_cb,
188 .unsubscribe = api_ws_server_req_unsubscribe_cb,
189 .subcall = api_ws_server_req_subcall_cb
192 /******************* common part **********************************/
195 * create a structure api_ws not connected to the 'path'.
197 static struct api_ws *api_ws_make(const char *path)
202 /* allocates the structure */
203 length = strlen(path);
204 api = calloc(1, sizeof *api + 1 + length);
210 /* path is copied after the struct */
211 api->path = (char*)(api+1);
212 memcpy(api->path, path, length + 1);
214 /* api name is at the end of the path */
215 api->api = strrchr(api->path, '/');
216 if (api->api == NULL || !afb_apis_is_valid_api_name(++api->api)) {
230 static int api_ws_socket_unix(const char *path, int server)
233 struct sockaddr_un addr;
236 length = strlen(path);
238 errno = ENAMETOOLONG;
245 fd = socket(AF_UNIX, SOCK_STREAM, 0);
249 memset(&addr, 0, sizeof addr);
250 addr.sun_family = AF_UNIX;
251 strcpy(addr.sun_path, path);
253 rc = bind(fd, (struct sockaddr *) &addr, (socklen_t)(sizeof addr));
255 rc = connect(fd, (struct sockaddr *) &addr, (socklen_t)(sizeof addr));
264 static int api_ws_socket_inet(const char *path, int server)
267 const char *service, *host, *api;
268 struct addrinfo hint, *rai, *iai;
271 api = strrchr(path, '/');
272 service = strrchr(path, ':');
273 if (api == NULL || service == NULL || api < service) {
277 host = strndupa(path, service++ - path);
278 service = strndupa(service, api - service);
281 memset(&hint, 0, sizeof hint);
282 hint.ai_family = AF_INET;
283 hint.ai_socktype = SOCK_STREAM;
284 rc = getaddrinfo(host, service, &hint, &rai);
292 while (iai != NULL) {
293 fd = socket(iai->ai_family, iai->ai_socktype, iai->ai_protocol);
296 rc = bind(fd, iai->ai_addr, iai->ai_addrlen);
298 rc = connect(fd, iai->ai_addr, iai->ai_addrlen);
313 static int api_ws_socket(const char *path, int server)
317 /* check for unix socket */
318 if (0 == strncmp(path, "unix:", 5))
319 fd = api_ws_socket_unix(path + 5, server);
321 fd = api_ws_socket_inet(path, server);
324 fcntl(fd, F_SETFD, FD_CLOEXEC);
325 fcntl(fd, F_SETFL, O_NONBLOCK);
328 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &rc, sizeof rc);
335 /******************* serialisation part **********************************/
342 #define WRITEBUF_COUNT_MAX 32
345 struct iovec iovec[WRITEBUF_COUNT_MAX];
346 uint32_t uints[WRITEBUF_COUNT_MAX];
350 static char *api_ws_read_get(struct readbuf *rb, uint32_t length)
352 char *before = rb->head;
353 char *after = before + length;
360 static int api_ws_read_uint32(struct readbuf *rb, uint32_t *value)
362 char *after = rb->head + sizeof *value;
365 memcpy(value, rb->head, sizeof *value);
367 *value = le32toh(*value);
371 static int api_ws_read_string(struct readbuf *rb, const char **value, size_t *length)
374 if (!api_ws_read_uint32(rb, &len) || !len)
377 *length = (size_t)(len - 1);
378 return (*value = api_ws_read_get(rb, len)) != NULL && rb->head[-1] == 0;
381 static int api_ws_read_object(struct readbuf *rb, struct json_object **object)
385 return api_ws_read_string(rb, &string, &length) && ((*object = json_tokener_parse(string)) != NULL) == (strcmp(string, "null") != 0);
388 static int api_ws_write_put(struct writebuf *wb, const void *value, size_t length)
391 if (i == WRITEBUF_COUNT_MAX)
393 wb->iovec[i].iov_base = (void*)value;
394 wb->iovec[i].iov_len = length;
399 static int api_ws_write_char(struct writebuf *wb, char value)
402 if (i == WRITEBUF_COUNT_MAX)
404 *(char*)&wb->uints[i] = value;
405 wb->iovec[i].iov_base = &wb->uints[i];
406 wb->iovec[i].iov_len = 1;
411 static int api_ws_write_uint32(struct writebuf *wb, uint32_t value)
414 if (i == WRITEBUF_COUNT_MAX)
416 wb->uints[i] = htole32(value);
417 wb->iovec[i].iov_base = &wb->uints[i];
418 wb->iovec[i].iov_len = sizeof wb->uints[i];
423 static int api_ws_write_string_nz(struct writebuf *wb, const char *value, size_t length)
425 uint32_t len = (uint32_t)length;
426 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');
429 static int api_ws_write_string_length(struct writebuf *wb, const char *value, size_t length)
431 uint32_t len = (uint32_t)++length;
432 return (size_t)len == length && len && api_ws_write_uint32(wb, len) && api_ws_write_put(wb, value, length);
435 static int api_ws_write_string(struct writebuf *wb, const char *value)
437 return api_ws_write_string_length(wb, value, strlen(value));
440 static int api_ws_write_object(struct writebuf *wb, struct json_object *object)
442 const char *string = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
443 return string != NULL && api_ws_write_string(wb, string);
449 /******************* client part **********************************/
452 * structure for recording query data
455 struct api_ws_memo *next; /* the next memo */
456 struct api_ws *api; /* the ws api */
457 struct afb_req req; /* the request handle */
458 struct afb_context *context; /* the context of the query */
459 uint32_t msgid; /* the message identifier */
464 struct api_ws_event *next;
465 struct afb_event event;
470 /* search a memorized request */
471 static struct api_ws_memo *api_ws_client_memo_search(struct api_ws *api, uint32_t msgid)
473 struct api_ws_memo *memo;
475 memo = api->client.memos;
476 while (memo != NULL && memo->msgid != msgid)
482 /* search the event */
483 static struct api_ws_event *api_ws_client_event_search(struct api_ws *api, uint32_t eventid, const char *name)
485 struct api_ws_event *ev;
487 ev = api->client.events;
488 while (ev != NULL && (ev->eventid != eventid || 0 != strcmp(afb_evt_event_name(ev->event), name)))
495 /* allocates and init the memorizing data */
496 static struct api_ws_memo *api_ws_client_memo_make(struct api_ws *api, struct afb_req req, struct afb_context *context)
498 struct api_ws_memo *memo;
500 memo = malloc(sizeof *memo);
504 memo->context = context;
505 do { memo->msgid = ++api->client.id; } while(api_ws_client_memo_search(api, memo->msgid) != NULL);
507 memo->next = api->client.memos;
508 api->client.memos = memo;
513 /* free and release the memorizing data */
514 static void api_ws_client_memo_destroy(struct api_ws_memo *memo)
516 struct api_ws_memo **prv;
518 prv = &memo->api->client.memos;
519 while (*prv != NULL) {
527 afb_req_unref(memo->req);
531 /* get event data from the message */
532 static int api_ws_client_msg_event_read(struct readbuf *rb, uint32_t *eventid, const char **name)
534 return api_ws_read_uint32(rb, eventid) && api_ws_read_string(rb, name, NULL);
537 /* get event from the message */
538 static int api_ws_client_msg_event_get(struct api_ws *api, struct readbuf *rb, struct api_ws_event **ev)
543 /* get event data from the message */
544 if (!api_ws_client_msg_event_read(rb, &eventid, &name)) {
545 ERROR("Invalid message");
549 /* check conflicts */
550 *ev = api_ws_client_event_search(api, eventid, name);
552 ERROR("event %s not found", name);
559 /* get event from the message */
560 static int api_ws_client_msg_memo_get(struct api_ws *api, struct readbuf *rb, struct api_ws_memo **memo)
564 /* get event data from the message */
565 if (!api_ws_read_uint32(rb, &msgid)) {
566 ERROR("Invalid message");
571 *memo = api_ws_client_memo_search(api, msgid);
573 ERROR("message not found");
580 /* read a subscrition message */
581 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)
583 return api_ws_client_msg_memo_get(api, rb, memo) && api_ws_client_msg_event_get(api, rb, ev);
587 static void api_ws_client_event_create(struct api_ws *api, struct readbuf *rb)
592 struct api_ws_event *ev;
594 /* get event data from the message */
595 offset = api_ws_client_msg_event_read(rb, &eventid, &name);
597 ERROR("Invalid message");
601 /* check conflicts */
602 ev = api_ws_client_event_search(api, eventid, name);
608 /* no conflict, try to add it */
609 ev = malloc(sizeof *ev);
611 ev->event = afb_evt_create_event(name);
612 if (ev->event.closure == NULL)
616 ev->eventid = eventid;
617 ev->next = api->client.events;
618 api->client.events = ev;
622 ERROR("can't create event %s, out of memory", name);
625 /* removes an event */
626 static void api_ws_client_event_drop(struct api_ws *api, struct readbuf *rb)
628 struct api_ws_event *ev, **prv;
630 /* retrieves the event */
631 if (!api_ws_client_msg_event_get(api, rb, &ev))
634 /* decrease the reference count */
638 /* unlinks the event */
639 prv = &api->client.events;
644 /* destroys the event */
645 afb_event_drop(ev->event);
649 /* subscribes an event */
650 static void api_ws_client_event_subscribe(struct api_ws *api, struct readbuf *rb)
652 struct api_ws_event *ev;
653 struct api_ws_memo *memo;
655 if (api_ws_client_msg_subscription_get(api, rb, &ev, &memo)) {
656 /* subscribe the request from the event */
657 if (afb_req_subscribe(memo->req, ev->event) < 0)
658 ERROR("can't subscribe: %m");
662 /* unsubscribes an event */
663 static void api_ws_client_event_unsubscribe(struct api_ws *api, struct readbuf *rb)
665 struct api_ws_event *ev;
666 struct api_ws_memo *memo;
668 if (api_ws_client_msg_subscription_get(api, rb, &ev, &memo)) {
669 /* unsubscribe the request from the event */
670 if (afb_req_unsubscribe(memo->req, ev->event) < 0)
671 ERROR("can't unsubscribe: %m");
675 /* receives broadcasted events */
676 static void api_ws_client_event_broadcast(struct api_ws *api, struct readbuf *rb)
678 struct json_object *object;
681 if (api_ws_read_string(rb, &event, NULL) && api_ws_read_object(rb, &object))
682 afb_evt_broadcast(event, object);
684 ERROR("unreadable broadcasted event");
688 static void api_ws_client_event_push(struct api_ws *api, struct readbuf *rb)
690 struct api_ws_event *ev;
691 struct json_object *object;
693 if (api_ws_client_msg_event_get(api, rb, &ev) && api_ws_read_object(rb, &object))
694 afb_event_push(ev->event, object);
696 ERROR("unreadable push event");
699 static void api_ws_client_reply_success(struct api_ws *api, struct readbuf *rb)
701 struct api_ws_memo *memo;
702 struct json_object *object;
706 /* retrieve the message data */
707 if (!api_ws_client_msg_memo_get(api, rb, &memo))
710 if (api_ws_read_uint32(rb, &flags)
711 && api_ws_read_string(rb, &info, NULL)
712 && api_ws_read_object(rb, &object)) {
713 memo->context->flags = (unsigned)flags;
714 afb_req_success(memo->req, object, *info ? info : NULL);
716 /* failing to have the answer */
717 afb_req_fail(memo->req, "error", "ws error");
719 api_ws_client_memo_destroy(memo);
722 static void api_ws_client_reply_fail(struct api_ws *api, struct readbuf *rb)
724 struct api_ws_memo *memo;
725 const char *info, *status;
728 /* retrieve the message data */
729 if (!api_ws_client_msg_memo_get(api, rb, &memo))
732 if (api_ws_read_uint32(rb, &flags)
733 && api_ws_read_string(rb, &status, NULL)
734 && api_ws_read_string(rb, &info, NULL)) {
735 memo->context->flags = (unsigned)flags;
736 afb_req_fail(memo->req, status, *info ? info : NULL);
738 /* failing to have the answer */
739 afb_req_fail(memo->req, "error", "ws error");
741 api_ws_client_memo_destroy(memo);
744 static void api_ws_client_reply_send(struct api_ws *api, struct readbuf *rb)
746 struct api_ws_memo *memo;
751 /* retrieve the message data */
752 if (!api_ws_client_msg_memo_get(api, rb, &memo))
755 if (api_ws_read_uint32(rb, &flags)
756 && api_ws_read_string(rb, &data, &length)) {
757 memo->context->flags = (unsigned)flags;
758 afb_req_send(memo->req, data, length);
760 /* failing to have the answer */
761 afb_req_fail(memo->req, "error", "ws error");
763 api_ws_client_memo_destroy(memo);
766 /* callback when receiving binary data */
767 static void api_ws_client_on_binary(void *closure, char *data, size_t size)
770 struct readbuf rb = { .head = data, .end = data + size };
771 switch (*rb.head++) {
772 case 'T': /* success */
773 api_ws_client_reply_success(closure, &rb);
776 api_ws_client_reply_fail(closure, &rb);
779 api_ws_client_reply_send(closure, &rb);
781 case '*': /* broadcast */
782 api_ws_client_event_broadcast(closure, &rb);
784 case '+': /* creates the event */
785 api_ws_client_event_create(closure, &rb);
787 case '-': /* drops the event */
788 api_ws_client_event_drop(closure, &rb);
790 case '!': /* pushs the event */
791 api_ws_client_event_push(closure, &rb);
793 case 'S': /* subscribe event for a request */
794 api_ws_client_event_subscribe(closure, &rb);
796 case 'U': /* unsubscribe event for a request */
797 api_ws_client_event_unsubscribe(closure, &rb);
799 default: /* unexpected message */
806 /* on call, propagate it to the ws service */
807 static void api_ws_client_call_cb(void * closure, struct afb_req req, struct afb_context *context, const char *verb, size_t lenverb)
810 struct api_ws_memo *memo;
811 struct writebuf wb = { .count = 0 };
814 struct api_ws *api = closure;
816 /* create the recording data */
817 memo = api_ws_client_memo_make(api, req, context);
819 afb_req_fail(req, "error", "out of memory");
823 /* creates the call message */
824 raw = afb_req_raw(req, &szraw);
827 if (!api_ws_write_uint32(&wb, memo->msgid)
828 || !api_ws_write_uint32(&wb, (uint32_t)context->flags)
829 || !api_ws_write_string_nz(&wb, verb, lenverb)
830 || !api_ws_write_string(&wb, afb_session_uuid(context->session))
831 || !api_ws_write_string_length(&wb, raw, szraw))
835 rc = afb_ws_binary_v(api->client.ws, wb.iovec, wb.count);
841 afb_req_fail(req, "error", "websocket sending error");
845 afb_req_fail(req, "error", "internal: raw is NULL!");
849 afb_req_fail(req, "error", "overflow: size doesn't match 32 bits!");
852 api_ws_client_memo_destroy(memo);
855 static int api_ws_service_start_cb(void *closure, int share_session, int onneed)
857 struct api_ws *api = closure;
859 /* not an error when onneed */
863 /* already started: it is an error */
864 ERROR("The WS binding %s is not a startable service", api->path);
869 static void api_ws_client_disconnect(struct api_ws *api)
872 afb_ws_destroy(api->client.ws);
873 api->client.ws = NULL;
880 static int api_ws_client_connect(struct api_ws *api)
885 fd = api_ws_socket(api->path, 0);
887 ws = afb_ws_create(afb_common_get_event_loop(), fd, &api_ws_client_ws_itf, api);
898 /* adds a afb-ws-service client api */
899 int afb_api_ws_add_client(const char *path)
903 struct afb_api afb_api;
905 /* create the ws client api */
906 api = api_ws_make(path);
910 /* connect to the service */
911 rc = api_ws_client_connect(api);
913 ERROR("can't connect to ws service %s", api->path);
917 /* record it as an API */
918 afb_api.closure = api;
919 afb_api.call = api_ws_client_call_cb;
920 afb_api.service_start = api_ws_service_start_cb;
921 if (afb_apis_add(api->api, afb_api) < 0)
927 api_ws_client_disconnect(api);
934 /******************* client description part for server *****************************/
936 static void api_ws_server_client_unref(struct api_ws_client *client)
938 if (!--client->refcount) {
939 afb_evt_listener_unref(client->listener);
940 afb_ws_destroy(client->ws);
945 /* on call, propagate it to the ws service */
946 static void api_ws_server_called(struct api_ws_client *client, struct readbuf *rb, char *data, size_t size)
948 struct api_ws_server_req *wreq;
950 const char *uuid, *verb;
955 /* create the request */
956 wreq = calloc(1 , sizeof *wreq);
960 wreq->client = client;
961 wreq->rcvdata = data;
964 /* reads the call message data */
965 if (!api_ws_read_uint32(rb, &wreq->msgid)
966 || !api_ws_read_uint32(rb, &flags)
967 || !api_ws_read_string(rb, &verb, NULL)
968 || !api_ws_read_string(rb, &uuid, NULL)
969 || !api_ws_read_string(rb, &wreq->request, &wreq->lenreq))
972 /* init the context */
973 if (afb_context_connect(&wreq->context, uuid, NULL) < 0)
975 wreq->context.flags = flags;
978 areq.itf = &afb_api_ws_req_itf;
980 afb_apis_call_(areq, &wreq->context, client->api, verb);
981 api_ws_server_req_unref(wreq);
988 api_ws_server_client_unref(client);
991 /* callback when receiving binary data */
992 static void api_ws_server_on_binary(void *closure, char *data, size_t size)
994 struct readbuf rb = { .head = data, .end = data + size };
995 api_ws_server_called(closure, &rb, data, size);
998 /* callback when receiving a hangup */
999 static void api_ws_server_on_hangup(void *closure)
1001 struct api_ws_client *client = closure;
1003 /* close the socket */
1004 if (client->fd >= 0) {
1009 /* release the client */
1010 api_ws_server_client_unref(client);
1013 static void api_ws_server_accept(struct api_ws *api)
1015 struct api_ws_client *client;
1016 struct sockaddr addr;
1019 client = calloc(1, sizeof *client);
1020 if (client != NULL) {
1021 client->listener = afb_evt_listener_create(&api_ws_server_evt_itf, client);
1022 if (client->listener != NULL) {
1023 lenaddr = (socklen_t)sizeof addr;
1024 client->fd = accept(api->fd, &addr, &lenaddr);
1025 if (client->fd >= 0) {
1026 fcntl(client->fd, F_SETFD, FD_CLOEXEC);
1027 fcntl(client->fd, F_SETFL, O_NONBLOCK);
1028 client->ws = afb_ws_create(afb_common_get_event_loop(), client->fd, &api_ws_server_ws_itf, client);
1029 if (client->ws != NULL) {
1030 client->api = api->api;
1031 client->refcount = 1;
1036 afb_evt_listener_unref(client->listener);
1042 /******************* server part: manage events **********************************/
1044 static void api_ws_server_event_send(struct api_ws_client *client, char order, const char *event, int eventid, const char *data)
1047 struct writebuf wb = { .count = 0 };
1049 if (api_ws_write_char(&wb, order)
1050 && api_ws_write_uint32(&wb, eventid)
1051 && api_ws_write_string(&wb, event)
1052 && (data == NULL || api_ws_write_string(&wb, data))) {
1053 rc = afb_ws_binary_v(client->ws, wb.iovec, wb.count);
1057 ERROR("error while sending %c for event %s", order, event);
1060 static void api_ws_server_event_add(void *closure, const char *event, int eventid)
1062 api_ws_server_event_send(closure, '+', event, eventid, NULL);
1065 static void api_ws_server_event_remove(void *closure, const char *event, int eventid)
1067 api_ws_server_event_send(closure, '-', event, eventid, NULL);
1070 static void api_ws_server_event_push(void *closure, const char *event, int eventid, struct json_object *object)
1072 const char *data = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
1073 api_ws_server_event_send(closure, '!', event, eventid, data ? : "null");
1074 json_object_put(object);
1077 static void api_ws_server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object)
1080 struct api_ws_client *client = closure;
1082 struct writebuf wb = { .count = 0 };
1084 if (api_ws_write_char(&wb, '*') && api_ws_write_string(&wb, event) && api_ws_write_object(&wb, object)) {
1085 rc = afb_ws_binary_v(client->ws, wb.iovec, wb.count);
1087 ERROR("error while broadcasting event %s", event);
1089 ERROR("error while broadcasting event %s", event);
1090 json_object_put(object);
1093 /******************* ws request part for server *****************/
1095 /* increment the reference count of the request */
1096 static void api_ws_server_req_addref_cb(void *closure)
1098 struct api_ws_server_req *wreq = closure;
1102 /* decrement the reference count of the request and free/release it on falling to null */
1103 static void api_ws_server_req_unref_cb(void *closure)
1105 api_ws_server_req_unref(closure);
1108 static void api_ws_server_req_unref(struct api_ws_server_req *wreq)
1110 if (wreq == NULL || --wreq->refcount)
1113 afb_context_disconnect(&wreq->context);
1114 json_object_put(wreq->json);
1115 free(wreq->rcvdata);
1116 api_ws_server_client_unref(wreq->client);
1120 /* get the object of the request */
1121 static struct json_object *api_ws_server_req_json_cb(void *closure)
1123 return api_ws_server_req_json(closure);
1126 static struct json_object *api_ws_server_req_json(struct api_ws_server_req *wreq)
1128 if (wreq->json == NULL) {
1129 wreq->json = json_tokener_parse(wreq->request);
1130 if (wreq->json == NULL && strcmp(wreq->request, "null")) {
1131 /* lazy error detection of json request. Is it to improve? */
1132 wreq->json = json_object_new_string(wreq->request);
1138 /* get the argument of the request of 'name' */
1139 static struct afb_arg api_ws_server_req_get_cb(void *closure, const char *name)
1141 struct api_ws_server_req *wreq = closure;
1142 return afb_msg_json_get_arg(api_ws_server_req_json(wreq), name);
1145 static void api_ws_server_req_success_cb(void *closure, struct json_object *obj, const char *info)
1148 struct writebuf wb = { .count = 0 };
1149 struct api_ws_server_req *wreq = closure;
1151 if (api_ws_write_char(&wb, 'T')
1152 && api_ws_write_uint32(&wb, wreq->msgid)
1153 && api_ws_write_uint32(&wb, (uint32_t)wreq->context.flags)
1154 && api_ws_write_string(&wb, info ? : "")
1155 && api_ws_write_object(&wb, obj)) {
1156 rc = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1160 ERROR("error while sending success");
1162 json_object_put(obj);
1165 static void api_ws_server_req_fail_cb(void *closure, const char *status, const char *info)
1168 struct writebuf wb = { .count = 0 };
1169 struct api_ws_server_req *wreq = closure;
1171 if (api_ws_write_char(&wb, 'F')
1172 && api_ws_write_uint32(&wb, wreq->msgid)
1173 && api_ws_write_uint32(&wb, (uint32_t)wreq->context.flags)
1174 && api_ws_write_string(&wb, status)
1175 && api_ws_write_string(&wb, info ? : "")) {
1176 rc = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1180 ERROR("error while sending fail");
1183 static const char *api_ws_server_req_raw_cb(void *closure, size_t *size)
1185 struct api_ws_server_req *wreq = closure;
1187 *size = wreq->lenreq;
1188 return wreq->request;
1191 static void api_ws_server_req_send_cb(void *closure, const char *buffer, size_t size)
1193 /* TODO: how to put sized buffer as strings? things aren't clear here!!! */
1195 struct writebuf wb = { .count = 0 };
1196 struct api_ws_server_req *wreq = closure;
1198 if (api_ws_write_char(&wb, 'X')
1199 && api_ws_write_uint32(&wb, wreq->msgid)
1200 && api_ws_write_uint32(&wb, (uint32_t)wreq->context.flags)
1201 && api_ws_write_string_length(&wb, buffer, size)) {
1202 rc = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1206 ERROR("error while sending raw");
1209 static int api_ws_server_req_subscribe_cb(void *closure, struct afb_event event)
1212 struct writebuf wb = { .count = 0 };
1213 struct api_ws_server_req *wreq = closure;
1215 rc = afb_evt_add_watch(wreq->client->listener, event);
1219 if (api_ws_write_char(&wb, 'S')
1220 && api_ws_write_uint32(&wb, wreq->msgid)
1221 && api_ws_write_uint32(&wb, (uint32_t)afb_evt_event_id(event))
1222 && api_ws_write_string(&wb, afb_evt_event_name(event))) {
1223 rc2 = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1227 ERROR("error while subscribing event");
1232 static int api_ws_server_req_unsubscribe_cb(void *closure, struct afb_event event)
1235 struct writebuf wb = { .count = 0 };
1236 struct api_ws_server_req *wreq = closure;
1238 if (api_ws_write_char(&wb, 'U')
1239 && api_ws_write_uint32(&wb, wreq->msgid)
1240 && api_ws_write_uint32(&wb, (uint32_t)afb_evt_event_id(event))
1241 && api_ws_write_string(&wb, afb_evt_event_name(event))) {
1242 rc2 = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1246 ERROR("error while subscribing event");
1248 rc = afb_evt_remove_watch(wreq->client->listener, event);
1252 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)
1254 struct api_ws_server_req *wreq = closure;
1255 afb_subcall(&wreq->context, api, verb, args, callback, cb_closure, (struct afb_req){ .itf = &afb_api_ws_req_itf, .closure = wreq });
1258 /******************* server part **********************************/
1260 static int api_ws_server_connect(struct api_ws *api);
1262 static int api_ws_server_listen_callback(sd_event_source *src, int fd, uint32_t revents, void *closure)
1264 if ((revents & EPOLLIN) != 0)
1265 api_ws_server_accept(closure);
1266 if ((revents & EPOLLHUP) != 0)
1267 api_ws_server_connect(closure);
1271 static void api_ws_server_disconnect(struct api_ws *api)
1273 if (api->server.listensrc != NULL) {
1274 sd_event_source_unref(api->server.listensrc);
1275 api->server.listensrc = NULL;
1283 static int api_ws_server_connect(struct api_ws *api)
1287 /* ensure disconnected */
1288 api_ws_server_disconnect(api);
1290 /* request the service object name */
1291 api->fd = api_ws_socket(api->path, 1);
1293 ERROR("can't create socket %s", api->path);
1295 /* listen for service */
1296 rc = sd_event_add_io(afb_common_get_event_loop(), &api->server.listensrc, api->fd, EPOLLIN, api_ws_server_listen_callback, api);
1301 ERROR("can't add ws object %s", api->path);
1306 /* create the service */
1307 int afb_api_ws_add_server(const char *path)
1312 /* creates the ws api object */
1313 api = api_ws_make(path);
1317 /* connect for serving */
1318 rc = api_ws_server_connect(api);