2 * Copyright (C) 2015, 2016 "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"
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 struct afb_arg api_ws_server_req_get(struct api_ws_server_req *wreq, const char *name);
161 static void api_ws_server_req_success(struct api_ws_server_req *wreq, struct json_object *obj, const char *info);
162 static void api_ws_server_req_fail(struct api_ws_server_req *wreq, const char *status, const char *info);
163 static const char *api_ws_server_req_raw(struct api_ws_server_req *wreq, size_t *size);
164 static void api_ws_server_req_send(struct api_ws_server_req *wreq, const char *buffer, size_t size);
165 static void api_ws_server_req_addref(struct api_ws_server_req *wreq);
166 static void api_ws_server_req_unref(struct api_ws_server_req *wreq);
167 static int api_ws_server_req_subscribe(struct api_ws_server_req *wreq, struct afb_event event);
168 static int api_ws_server_req_unsubscribe(struct api_ws_server_req *wreq, struct afb_event event);
169 static void api_ws_server_req_subcall(struct api_ws_server_req *wreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *closure);
171 const struct afb_req_itf afb_api_ws_req_itf = {
172 .json = (void*)api_ws_server_req_json,
173 .get = (void*)api_ws_server_req_get,
174 .success = (void*)api_ws_server_req_success,
175 .fail = (void*)api_ws_server_req_fail,
176 .raw = (void*)api_ws_server_req_raw,
177 .send = (void*)api_ws_server_req_send,
178 .context_get = (void*)afb_context_get,
179 .context_set = (void*)afb_context_set,
180 .addref = (void*)api_ws_server_req_addref,
181 .unref = (void*)api_ws_server_req_unref,
182 .session_close = (void*)afb_context_close,
183 .session_set_LOA = (void*)afb_context_change_loa,
184 .subscribe = (void*)api_ws_server_req_subscribe,
185 .unsubscribe = (void*)api_ws_server_req_unsubscribe,
186 .subcall = (void*)api_ws_server_req_subcall
189 /******************* common part **********************************/
192 * create a structure api_ws not connected to the 'path'.
194 static struct api_ws *api_ws_make(const char *path)
199 /* allocates the structure */
200 length = strlen(path);
201 api = calloc(1, sizeof *api + 1 + length);
207 /* path is copied after the struct */
208 api->path = (void*)(api+1);
209 memcpy(api->path, path, length + 1);
211 /* api name is at the end of the path */
212 api->api = strrchr(api->path, '/');
213 if (api->api == NULL || !afb_apis_is_valid_api_name(++api->api)) {
227 static int api_ws_socket_unix(const char *path, int server)
230 struct sockaddr_un addr;
233 length = strlen(path);
235 errno = ENAMETOOLONG;
242 fd = socket(AF_UNIX, SOCK_STREAM, 0);
246 memset(&addr, 0, sizeof addr);
247 addr.sun_family = AF_UNIX;
248 strcpy(addr.sun_path, path);
250 rc = bind(fd, (struct sockaddr *) &addr, (socklen_t)(sizeof addr));
252 rc = connect(fd, (struct sockaddr *) &addr, (socklen_t)(sizeof addr));
261 static int api_ws_socket_inet(const char *path, int server)
264 const char *service, *host, *api;
265 struct addrinfo hint, *rai, *iai;
268 api = strrchr(path, '/');
269 service = strrchr(path, ':');
270 if (api == NULL || service == NULL || api < service) {
274 host = strndupa(path, service++ - path);
275 service = strndupa(service, api - service);
278 memset(&hint, 0, sizeof hint);
279 hint.ai_family = AF_INET;
280 hint.ai_socktype = SOCK_STREAM;
281 rc = getaddrinfo(host, service, &hint, &rai);
289 while (iai != NULL) {
290 fd = socket(iai->ai_family, iai->ai_socktype, iai->ai_protocol);
293 rc = bind(fd, iai->ai_addr, iai->ai_addrlen);
295 rc = connect(fd, iai->ai_addr, iai->ai_addrlen);
310 static int api_ws_socket(const char *path, int server)
314 /* check for unix socket */
315 if (0 == strncmp(path, "unix:", 5))
316 fd = api_ws_socket_unix(path + 5, server);
318 fd = api_ws_socket_inet(path, server);
321 fcntl(fd, F_SETFD, FD_CLOEXEC);
322 fcntl(fd, F_SETFL, O_NONBLOCK);
325 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &rc, sizeof rc);
332 /******************* serialisation part **********************************/
339 #define WRITEBUF_COUNT_MAX 32
342 struct iovec iovec[WRITEBUF_COUNT_MAX];
343 uint32_t uints[WRITEBUF_COUNT_MAX];
347 static char *api_ws_read_get(struct readbuf *rb, uint32_t length)
349 char *before = rb->head;
350 char *after = before + length;
357 static int api_ws_read_uint32(struct readbuf *rb, uint32_t *value)
359 char *after = rb->head + sizeof *value;
362 memcpy(value, rb->head, sizeof *value);
364 *value = le32toh(*value);
368 static int api_ws_read_string(struct readbuf *rb, const char **value, size_t *length)
371 if (!api_ws_read_uint32(rb, &len) || !len)
374 *length = (size_t)(len - 1);
375 return (*value = api_ws_read_get(rb, len)) != NULL && rb->head[-1] == 0;
378 static int api_ws_read_object(struct readbuf *rb, struct json_object **object)
382 return api_ws_read_string(rb, &string, &length) && ((*object = json_tokener_parse(string)) != NULL) == (strcmp(string, "null") != 0);
385 static int api_ws_write_put(struct writebuf *wb, const void *value, size_t length)
388 if (i == WRITEBUF_COUNT_MAX)
390 wb->iovec[i].iov_base = (void*)value;
391 wb->iovec[i].iov_len = length;
396 static int api_ws_write_char(struct writebuf *wb, char value)
399 if (i == WRITEBUF_COUNT_MAX)
401 *(char*)&wb->uints[i] = value;
402 wb->iovec[i].iov_base = &wb->uints[i];
403 wb->iovec[i].iov_len = 1;
408 static int api_ws_write_uint32(struct writebuf *wb, uint32_t value)
411 if (i == WRITEBUF_COUNT_MAX)
413 wb->uints[i] = htole32(value);
414 wb->iovec[i].iov_base = &wb->uints[i];
415 wb->iovec[i].iov_len = sizeof wb->uints[i];
420 static int api_ws_write_string_nz(struct writebuf *wb, const char *value, size_t length)
422 uint32_t len = (uint32_t)length;
423 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');
426 static int api_ws_write_string_length(struct writebuf *wb, const char *value, size_t length)
428 uint32_t len = (uint32_t)++length;
429 return (size_t)len == length && len && api_ws_write_uint32(wb, len) && api_ws_write_put(wb, value, length);
432 static int api_ws_write_string(struct writebuf *wb, const char *value)
434 return api_ws_write_string_length(wb, value, strlen(value));
437 static int api_ws_write_object(struct writebuf *wb, struct json_object *object)
439 const char *string = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
440 return string != NULL && api_ws_write_string(wb, string);
446 /******************* client part **********************************/
449 * structure for recording query data
452 struct api_ws_memo *next; /* the next memo */
453 struct api_ws *api; /* the ws api */
454 struct afb_req req; /* the request handle */
455 struct afb_context *context; /* the context of the query */
456 uint32_t msgid; /* the message identifier */
461 struct api_ws_event *next;
462 struct afb_event event;
467 /* search a memorized request */
468 static struct api_ws_memo *api_ws_client_memo_search(struct api_ws *api, uint32_t msgid)
470 struct api_ws_memo *memo;
472 memo = api->client.memos;
473 while (memo != NULL && memo->msgid != msgid)
479 /* search the event */
480 static struct api_ws_event *api_ws_client_event_search(struct api_ws *api, uint32_t eventid, const char *name)
482 struct api_ws_event *ev;
484 ev = api->client.events;
485 while (ev != NULL && (ev->eventid != eventid || 0 != strcmp(afb_evt_event_name(ev->event), name)))
492 /* allocates and init the memorizing data */
493 static struct api_ws_memo *api_ws_client_memo_make(struct api_ws *api, struct afb_req req, struct afb_context *context)
495 struct api_ws_memo *memo;
497 memo = malloc(sizeof *memo);
501 memo->context = context;
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_req_unref(memo->req);
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_req_subscribe(memo->req, 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_req_unsubscribe(memo->req, 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->context->flags = (unsigned)flags;
711 afb_req_success(memo->req, object, *info ? info : NULL);
713 /* failing to have the answer */
714 afb_req_fail(memo->req, "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->context->flags = (unsigned)flags;
733 afb_req_fail(memo->req, status, *info ? info : NULL);
735 /* failing to have the answer */
736 afb_req_fail(memo->req, "error", "ws error");
738 api_ws_client_memo_destroy(memo);
741 static void api_ws_client_reply_send(struct api_ws *api, struct readbuf *rb)
743 struct api_ws_memo *memo;
748 /* retrieve the message data */
749 if (!api_ws_client_msg_memo_get(api, rb, &memo))
752 if (api_ws_read_uint32(rb, &flags)
753 && api_ws_read_string(rb, &data, &length)) {
754 memo->context->flags = (unsigned)flags;
755 afb_req_send(memo->req, data, length);
757 /* failing to have the answer */
758 afb_req_fail(memo->req, "error", "ws error");
760 api_ws_client_memo_destroy(memo);
763 /* callback when receiving binary data */
764 static void api_ws_client_on_binary(void *closure, char *data, size_t size)
767 struct readbuf rb = { .head = data, .end = data + size };
768 switch (*rb.head++) {
769 case 'T': /* success */
770 api_ws_client_reply_success(closure, &rb);
773 api_ws_client_reply_fail(closure, &rb);
776 api_ws_client_reply_send(closure, &rb);
778 case '*': /* broadcast */
779 api_ws_client_event_broadcast(closure, &rb);
781 case '+': /* creates the event */
782 api_ws_client_event_create(closure, &rb);
784 case '-': /* drops the event */
785 api_ws_client_event_drop(closure, &rb);
787 case '!': /* pushs the event */
788 api_ws_client_event_push(closure, &rb);
790 case 'S': /* subscribe event for a request */
791 api_ws_client_event_subscribe(closure, &rb);
793 case 'U': /* unsubscribe event for a request */
794 api_ws_client_event_unsubscribe(closure, &rb);
796 default: /* unexpected message */
802 /* on call, propagate it to the ws service */
803 static void api_ws_client_call(struct api_ws *api, struct afb_req req, struct afb_context *context, const char *verb, size_t lenverb)
806 struct api_ws_memo *memo;
807 struct writebuf wb = { .count = 0 };
811 /* create the recording data */
812 memo = api_ws_client_memo_make(api, req, context);
814 afb_req_fail(req, "error", "out of memory");
818 /* creates the call message */
819 raw = afb_req_raw(req, &szraw);
822 if (!api_ws_write_uint32(&wb, memo->msgid)
823 || !api_ws_write_uint32(&wb, (uint32_t)context->flags)
824 || !api_ws_write_string_nz(&wb, verb, lenverb)
825 || !api_ws_write_string(&wb, ctxClientGetUuid(context->session))
826 || !api_ws_write_string_length(&wb, raw, szraw))
830 rc = afb_ws_binary_v(api->client.ws, wb.iovec, wb.count);
836 afb_req_fail(req, "error", "websocket sending error");
840 afb_req_fail(req, "error", "internal: raw is NULL!");
844 afb_req_fail(req, "error", "overflow: size doesn't match 32 bits!");
847 api_ws_client_memo_destroy(memo);
850 static int api_ws_service_start(struct api_ws *api, int share_session, int onneed)
852 /* not an error when onneed */
856 /* already started: it is an error */
857 ERROR("The WS binding %s is not a startable service", api->path);
862 static void api_ws_client_disconnect(struct api_ws *api)
865 afb_ws_destroy(api->client.ws);
866 api->client.ws = NULL;
873 static int api_ws_client_connect(struct api_ws *api)
878 fd = api_ws_socket(api->path, 0);
880 ws = afb_ws_create(afb_common_get_event_loop(), fd, &api_ws_client_ws_itf, api);
891 /* adds a afb-ws-service client api */
892 int afb_api_ws_add_client(const char *path)
896 struct afb_api afb_api;
898 /* create the ws client api */
899 api = api_ws_make(path);
903 /* connect to the service */
904 rc = api_ws_client_connect(api);
906 ERROR("can't connect to ws service %s", api->path);
910 /* record it as an API */
911 afb_api.closure = api;
912 afb_api.call = (void*)api_ws_client_call;
913 afb_api.service_start = (void*)api_ws_service_start;
914 if (afb_apis_add(api->api, afb_api) < 0)
920 api_ws_client_disconnect(api);
927 /******************* client description part for server *****************************/
929 static void api_ws_server_client_unref(struct api_ws_client *client)
931 if (!--client->refcount) {
932 afb_evt_listener_unref(client->listener);
933 afb_ws_destroy(client->ws);
938 /* on call, propagate it to the ws service */
939 static void api_ws_server_called(struct api_ws_client *client, struct readbuf *rb, char *data, size_t size)
941 struct api_ws_server_req *wreq;
943 const char *uuid, *verb;
948 /* create the request */
949 wreq = calloc(1 , sizeof *wreq);
953 wreq->client = client;
954 wreq->rcvdata = data;
957 /* reads the call message data */
958 if (!api_ws_read_uint32(rb, &wreq->msgid)
959 || !api_ws_read_uint32(rb, &flags)
960 || !api_ws_read_string(rb, &verb, NULL)
961 || !api_ws_read_string(rb, &uuid, NULL)
962 || !api_ws_read_string(rb, &wreq->request, &wreq->lenreq))
965 /* init the context */
966 if (afb_context_connect(&wreq->context, uuid, NULL) < 0)
968 wreq->context.flags = flags;
971 areq.itf = &afb_api_ws_req_itf;
973 afb_apis_call_(areq, &wreq->context, client->api, verb);
974 api_ws_server_req_unref(wreq);
981 api_ws_server_client_unref(client);
984 /* callback when receiving binary data */
985 static void api_ws_server_on_binary(void *closure, char *data, size_t size)
987 struct readbuf rb = { .head = data, .end = data + size };
988 api_ws_server_called(closure, &rb, data, size);
991 /* callback when receiving a hangup */
992 static void api_ws_server_on_hangup(void *closure)
994 struct api_ws_client *client = closure;
996 /* close the socket */
997 if (client->fd >= 0) {
1002 /* release the client */
1003 api_ws_server_client_unref(client);
1006 static void api_ws_server_accept(struct api_ws *api)
1008 struct api_ws_client *client;
1009 struct sockaddr addr;
1012 client = calloc(1, sizeof *client);
1013 if (client != NULL) {
1014 client->listener = afb_evt_listener_create(&api_ws_server_evt_itf, client);
1015 if (client->listener != NULL) {
1016 lenaddr = (socklen_t)sizeof addr;
1017 client->fd = accept(api->fd, &addr, &lenaddr);
1018 if (client->fd >= 0) {
1019 client->ws = afb_ws_create(afb_common_get_event_loop(), client->fd, &api_ws_server_ws_itf, client);
1020 if (client->ws != NULL) {
1021 client->api = api->api;
1022 client->refcount = 1;
1027 afb_evt_listener_unref(client->listener);
1033 /******************* server part: manage events **********************************/
1035 static void api_ws_server_event_send(struct api_ws_client *client, char order, const char *event, int eventid, const char *data)
1038 struct writebuf wb = { .count = 0 };
1040 if (api_ws_write_char(&wb, order)
1041 && api_ws_write_uint32(&wb, eventid)
1042 && api_ws_write_string(&wb, event)
1043 && (data == NULL || api_ws_write_string(&wb, data))) {
1044 rc = afb_ws_binary_v(client->ws, wb.iovec, wb.count);
1048 ERROR("error while sending %c for event %s", order, event);
1051 static void api_ws_server_event_add(void *closure, const char *event, int eventid)
1053 api_ws_server_event_send(closure, '+', event, eventid, NULL);
1056 static void api_ws_server_event_remove(void *closure, const char *event, int eventid)
1058 api_ws_server_event_send(closure, '-', event, eventid, NULL);
1061 static void api_ws_server_event_push(void *closure, const char *event, int eventid, struct json_object *object)
1063 const char *data = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
1064 api_ws_server_event_send(closure, '!', event, eventid, data ? : "null");
1065 json_object_put(object);
1068 static void api_ws_server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object)
1071 struct api_ws_client *client = closure;
1073 struct writebuf wb = { .count = 0 };
1075 if (api_ws_write_char(&wb, '*') && api_ws_write_string(&wb, event) && api_ws_write_object(&wb, object)) {
1076 rc = afb_ws_binary_v(client->ws, wb.iovec, wb.count);
1078 ERROR("error while broadcasting event %s", event);
1080 ERROR("error while broadcasting event %s", event);
1081 json_object_put(object);
1084 /******************* ws request part for server *****************/
1086 /* increment the reference count of the request */
1087 static void api_ws_server_req_addref(struct api_ws_server_req *wreq)
1092 /* decrement the reference count of the request and free/release it on falling to null */
1093 static void api_ws_server_req_unref(struct api_ws_server_req *wreq)
1095 if (wreq == NULL || --wreq->refcount)
1098 afb_context_disconnect(&wreq->context);
1099 json_object_put(wreq->json);
1100 free(wreq->rcvdata);
1101 api_ws_server_client_unref(wreq->client);
1105 /* get the object of the request */
1106 static struct json_object *api_ws_server_req_json(struct api_ws_server_req *wreq)
1108 if (wreq->json == NULL) {
1109 wreq->json = json_tokener_parse(wreq->request);
1110 if (wreq->json == NULL && strcmp(wreq->request, "null")) {
1111 /* lazy error detection of json request. Is it to improve? */
1112 wreq->json = json_object_new_string(wreq->request);
1118 /* get the argument of the request of 'name' */
1119 static struct afb_arg api_ws_server_req_get(struct api_ws_server_req *wreq, const char *name)
1121 return afb_msg_json_get_arg(api_ws_server_req_json(wreq), name);
1124 static void api_ws_server_req_success(struct api_ws_server_req *wreq, struct json_object *obj, const char *info)
1127 struct writebuf wb = { .count = 0 };
1129 if (api_ws_write_char(&wb, 'T')
1130 && api_ws_write_uint32(&wb, wreq->msgid)
1131 && api_ws_write_uint32(&wb, (uint32_t)wreq->context.flags)
1132 && api_ws_write_string(&wb, info ? : "")
1133 && api_ws_write_object(&wb, obj)) {
1134 rc = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1138 ERROR("error while sending success");
1140 json_object_put(obj);
1143 static void api_ws_server_req_fail(struct api_ws_server_req *wreq, const char *status, const char *info)
1146 struct writebuf wb = { .count = 0 };
1148 if (api_ws_write_char(&wb, 'F')
1149 && api_ws_write_uint32(&wb, wreq->msgid)
1150 && api_ws_write_uint32(&wb, (uint32_t)wreq->context.flags)
1151 && api_ws_write_string(&wb, status)
1152 && api_ws_write_string(&wb, info ? : "")) {
1153 rc = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1157 ERROR("error while sending fail");
1160 static const char *api_ws_server_req_raw(struct api_ws_server_req *wreq, size_t *size)
1163 *size = wreq->lenreq;
1164 return wreq->request;
1167 static void api_ws_server_req_send(struct api_ws_server_req *wreq, const char *buffer, size_t size)
1169 /* TODO: how to put sized buffer as strings? things aren't clear here!!! */
1171 struct writebuf wb = { .count = 0 };
1173 if (api_ws_write_char(&wb, 'X')
1174 && api_ws_write_uint32(&wb, wreq->msgid)
1175 && api_ws_write_uint32(&wb, (uint32_t)wreq->context.flags)
1176 && api_ws_write_string_length(&wb, buffer, size)) {
1177 rc = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1181 ERROR("error while sending raw");
1184 static int api_ws_server_req_subscribe(struct api_ws_server_req *wreq, struct afb_event event)
1187 struct writebuf wb = { .count = 0 };
1189 rc = afb_evt_add_watch(wreq->client->listener, event);
1193 if (api_ws_write_char(&wb, 'S')
1194 && api_ws_write_uint32(&wb, wreq->msgid)
1195 && api_ws_write_uint32(&wb, (uint32_t)afb_evt_event_id(event))
1196 && api_ws_write_string(&wb, afb_evt_event_name(event))) {
1197 rc2 = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1201 ERROR("error while subscribing event");
1206 static int api_ws_server_req_unsubscribe(struct api_ws_server_req *wreq, struct afb_event event)
1209 struct writebuf wb = { .count = 0 };
1211 if (api_ws_write_char(&wb, 'U')
1212 && api_ws_write_uint32(&wb, wreq->msgid)
1213 && api_ws_write_uint32(&wb, (uint32_t)afb_evt_event_id(event))
1214 && api_ws_write_string(&wb, afb_evt_event_name(event))) {
1215 rc2 = afb_ws_binary_v(wreq->client->ws, wb.iovec, wb.count);
1219 ERROR("error while subscribing event");
1221 rc = afb_evt_remove_watch(wreq->client->listener, event);
1225 static void api_ws_server_req_subcall(struct api_ws_server_req *wreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *closure)
1227 afb_subcall(&wreq->context, api, verb, args, callback, closure, (struct afb_req){ .itf = &afb_api_ws_req_itf, .closure = wreq });
1230 /******************* server part **********************************/
1232 static int api_ws_server_connect(struct api_ws *api);
1234 static int api_ws_server_listen_callback(sd_event_source *src, int fd, uint32_t revents, void *closure)
1236 if ((revents & EPOLLIN) != 0)
1237 api_ws_server_accept(closure);
1238 if ((revents & EPOLLHUP) != 0)
1239 api_ws_server_connect(closure);
1243 static void api_ws_server_disconnect(struct api_ws *api)
1245 if (api->server.listensrc != NULL) {
1246 sd_event_source_unref(api->server.listensrc);
1247 api->server.listensrc = NULL;
1255 static int api_ws_server_connect(struct api_ws *api)
1259 /* ensure disconnected */
1260 api_ws_server_disconnect(api);
1262 /* request the service object name */
1263 api->fd = api_ws_socket(api->path, 1);
1265 ERROR("can't create socket %s", api->path);
1267 /* listen for service */
1268 rc = sd_event_add_io(afb_common_get_event_loop(), &api->server.listensrc, api->fd, EPOLLIN, api_ws_server_listen_callback, api);
1273 ERROR("can't add ws object %s", api->path);
1278 /* create the service */
1279 int afb_api_ws_add_server(const char *path)
1284 /* creates the ws api object */
1285 api = api_ws_make(path);
1289 /* connect for serving */
1290 rc = api_ws_server_connect(api);