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.
20 #define NO_PLUGIN_VERBOSE_MACRO
30 #include <sys/types.h>
31 #include <sys/socket.h>
35 #include <json-c/json.h>
36 #include <systemd/sd-event.h>
38 #include <afb/afb-event-itf.h>
40 #include "afb-common.h"
42 #include "afb-session.h"
45 #include "afb-msg-json.h"
47 #include "afb-apiset.h"
48 #include "afb-stub-ws.h"
49 #include "afb-context.h"
57 /************** constants for protocol definition *************************/
59 #define CHAR_FOR_CALL 'C'
60 #define CHAR_FOR_ANSWER_SUCCESS 'T'
61 #define CHAR_FOR_ANSWER_FAIL 'F'
62 #define CHAR_FOR_EVT_BROADCAST '*'
63 #define CHAR_FOR_EVT_ADD '+'
64 #define CHAR_FOR_EVT_DEL '-'
65 #define CHAR_FOR_EVT_PUSH '!'
66 #define CHAR_FOR_EVT_SUBSCRIBE 'S'
67 #define CHAR_FOR_EVT_UNSUBSCRIBE 'U'
68 #define CHAR_FOR_SUBCALL_CALL 'B'
69 #define CHAR_FOR_SUBCALL_REPLY 'R'
70 #define CHAR_FOR_DESCRIBE 'D'
71 #define CHAR_FOR_DESCRIPTION 'd'
73 /******************* handling subcalls *****************************/
76 * Structure on server side for recording pending
81 struct server_subcall *next; /**< next subcall for the client */
82 uint32_t subcallid; /**< the subcallid */
83 void (*callback)(void*, int, struct json_object*); /**< callback on completion */
84 void *closure; /**< closure of the callback */
88 * Structure for sending back replies on client side
92 struct afb_stub_ws *stubws; /**< stub descriptor */
93 uint32_t subcallid; /**< subcallid for the reply */
97 * structure for recording calls on client side
100 struct client_call *next; /* the next call */
101 struct afb_stub_ws *stubws; /* the stub_ws */
102 struct afb_xreq *xreq; /* the request handle */
103 uint32_t msgid; /* the message identifier */
107 * structure for a ws request
110 struct afb_xreq xreq; /* the xreq */
111 struct afb_stub_ws *stubws; /* the client of the request */
112 uint32_t msgid; /* the incoming request msgid */
116 * structure for recording events on client side
120 struct client_event *next;
121 struct afb_event event;
127 * structure for recording describe requests
129 struct client_describe
131 struct client_describe *next;
132 struct afb_stub_ws *stubws;
133 struct jobloop *jobloop;
134 struct json_object *result;
139 * structure for jobs of describing
141 struct server_describe
143 struct afb_stub_ws *stubws;
147 /******************* stub description for client or servers ******************/
151 /* count of references */
154 /* file descriptor */
157 /* resource control */
158 pthread_mutex_t mutex;
163 /* listener for events (server side) */
164 struct afb_evt_listener *listener;
166 /* event replica (client side) */
167 struct client_event *events;
169 /* emitted calls (client side) */
170 struct client_call *calls;
172 /* credentials (server side) */
173 struct afb_cred *cred;
175 /* pending subcalls (server side) */
176 struct server_subcall *subcalls;
178 /* pending description (client side) */
179 struct client_describe *describes;
182 struct afb_apiset *apiset;
184 /* on hangup callback */
185 void (*on_hangup)(struct afb_stub_ws *);
191 /******************* common useful tools **********************************/
194 * translate a pointer to some integer
195 * @param ptr the pointer to translate
198 static inline uint32_t ptr2id(void *ptr)
200 return (uint32_t)(((intptr_t)ptr) >> 6);
203 /******************* serialisation part **********************************/
210 #define WRITEBUF_COUNT_MAX 32
213 struct iovec iovec[WRITEBUF_COUNT_MAX];
214 uint32_t uints[WRITEBUF_COUNT_MAX];
218 static char *readbuf_get(struct readbuf *rb, uint32_t length)
220 char *before = rb->head;
221 char *after = before + length;
228 static int readbuf_char(struct readbuf *rb, char *value)
230 if (rb->head >= rb->end)
232 *value = *rb->head++;
236 static int readbuf_uint32(struct readbuf *rb, uint32_t *value)
238 char *after = rb->head + sizeof *value;
241 memcpy(value, rb->head, sizeof *value);
243 *value = le32toh(*value);
247 static int readbuf_string(struct readbuf *rb, const char **value, size_t *length)
250 if (!readbuf_uint32(rb, &len) || !len)
253 *length = (size_t)(len - 1);
254 return (*value = readbuf_get(rb, len)) != NULL && rb->head[-1] == 0;
257 static int readbuf_object(struct readbuf *rb, struct json_object **object)
260 struct json_object *o;
261 int rc = readbuf_string(rb, &string, NULL);
263 o = json_tokener_parse(string);
264 if (o == NULL && strcmp(string, "null"))
265 o = json_object_new_string(string);
271 static int writebuf_put(struct writebuf *wb, const void *value, size_t length)
274 if (i == WRITEBUF_COUNT_MAX)
276 wb->iovec[i].iov_base = (void*)value;
277 wb->iovec[i].iov_len = length;
282 static int writebuf_char(struct writebuf *wb, char value)
285 if (i == WRITEBUF_COUNT_MAX)
287 *(char*)&wb->uints[i] = value;
288 wb->iovec[i].iov_base = &wb->uints[i];
289 wb->iovec[i].iov_len = 1;
294 static int writebuf_uint32(struct writebuf *wb, uint32_t value)
297 if (i == WRITEBUF_COUNT_MAX)
299 wb->uints[i] = htole32(value);
300 wb->iovec[i].iov_base = &wb->uints[i];
301 wb->iovec[i].iov_len = sizeof wb->uints[i];
306 static int writebuf_string_length(struct writebuf *wb, const char *value, size_t length)
308 uint32_t len = (uint32_t)++length;
309 return (size_t)len == length && len && writebuf_uint32(wb, len) && writebuf_put(wb, value, length);
312 static int writebuf_string(struct writebuf *wb, const char *value)
314 return writebuf_string_length(wb, value, strlen(value));
317 static int writebuf_object(struct writebuf *wb, struct json_object *object)
319 const char *string = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
320 return string != NULL && writebuf_string(wb, string);
323 /******************* ws request part for server *****************/
325 /* decrement the reference count of the request and free/release it on falling to null */
326 static void server_req_destroy_cb(struct afb_xreq *xreq)
328 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
330 afb_context_disconnect(&wreq->xreq.context);
331 afb_cred_unref(wreq->xreq.cred);
332 json_object_put(wreq->xreq.json);
333 afb_stub_ws_unref(wreq->stubws);
337 static void server_req_success_cb(struct afb_xreq *xreq, struct json_object *obj, const char *info)
340 struct writebuf wb = { .count = 0 };
341 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
343 if (writebuf_char(&wb, CHAR_FOR_ANSWER_SUCCESS)
344 && writebuf_uint32(&wb, wreq->msgid)
345 && writebuf_uint32(&wb, (uint32_t)wreq->xreq.context.flags)
346 && writebuf_string(&wb, info ? : "")
347 && writebuf_object(&wb, obj)) {
348 rc = afb_ws_binary_v(wreq->stubws->ws, wb.iovec, wb.count);
352 ERROR("error while sending success");
354 json_object_put(obj);
357 static void server_req_fail_cb(struct afb_xreq *xreq, const char *status, const char *info)
360 struct writebuf wb = { .count = 0 };
361 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
363 if (writebuf_char(&wb, CHAR_FOR_ANSWER_FAIL)
364 && writebuf_uint32(&wb, wreq->msgid)
365 && writebuf_uint32(&wb, (uint32_t)wreq->xreq.context.flags)
366 && writebuf_string(&wb, status)
367 && writebuf_string(&wb, info ? : "")) {
368 rc = afb_ws_binary_v(wreq->stubws->ws, wb.iovec, wb.count);
372 ERROR("error while sending fail");
375 static void server_req_subcall_cb(struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
378 struct writebuf wb = { .count = 0 };
379 struct server_subcall *sc, *osc;
380 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
381 struct afb_stub_ws *stubws = wreq->stubws;
383 sc = malloc(sizeof *sc);
385 callback(cb_closure, 1, afb_msg_json_internal_error());
387 sc->callback = callback;
388 sc->closure = cb_closure;
390 pthread_mutex_unlock(&stubws->mutex);
391 sc->subcallid = ptr2id(sc);
394 osc = stubws->subcalls;
395 while(osc && osc->subcallid != sc->subcallid)
398 sc->next = stubws->subcalls;
399 stubws->subcalls = sc;
400 pthread_mutex_unlock(&stubws->mutex);
402 if (writebuf_char(&wb, CHAR_FOR_SUBCALL_CALL)
403 && writebuf_uint32(&wb, wreq->msgid)
404 && writebuf_uint32(&wb, sc->subcallid)
405 && writebuf_string(&wb, api)
406 && writebuf_string(&wb, verb)
407 && writebuf_object(&wb, args)) {
408 rc = afb_ws_binary_v(wreq->stubws->ws, wb.iovec, wb.count);
412 ERROR("error while sending fail");
416 static int server_req_subscribe_cb(struct afb_xreq *xreq, struct afb_event event)
419 struct writebuf wb = { .count = 0 };
420 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
422 rc = afb_evt_add_watch(wreq->stubws->listener, event);
426 if (writebuf_char(&wb, CHAR_FOR_EVT_SUBSCRIBE)
427 && writebuf_uint32(&wb, wreq->msgid)
428 && writebuf_uint32(&wb, (uint32_t)afb_evt_event_id(event))
429 && writebuf_string(&wb, afb_evt_event_name(event))) {
430 rc2 = afb_ws_binary_v(wreq->stubws->ws, wb.iovec, wb.count);
434 ERROR("error while subscribing event");
439 static int server_req_unsubscribe_cb(struct afb_xreq *xreq, struct afb_event event)
442 struct writebuf wb = { .count = 0 };
443 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
445 if (writebuf_char(&wb, CHAR_FOR_EVT_UNSUBSCRIBE)
446 && writebuf_uint32(&wb, wreq->msgid)
447 && writebuf_uint32(&wb, (uint32_t)afb_evt_event_id(event))
448 && writebuf_string(&wb, afb_evt_event_name(event))) {
449 rc2 = afb_ws_binary_v(wreq->stubws->ws, wb.iovec, wb.count);
453 ERROR("error while subscribing event");
455 rc = afb_evt_remove_watch(wreq->stubws->listener, event);
459 static const struct afb_xreq_query_itf server_req_xreq_itf = {
460 .success = server_req_success_cb,
461 .fail = server_req_fail_cb,
462 .unref = server_req_destroy_cb,
463 .subcall = server_req_subcall_cb,
464 .subscribe = server_req_subscribe_cb,
465 .unsubscribe = server_req_unsubscribe_cb
468 /******************* client part **********************************/
470 /* search a memorized call */
471 static struct client_call *client_call_search(struct afb_stub_ws *stubws, uint32_t msgid)
473 struct client_call *call;
475 call = stubws->calls;
476 while (call != NULL && call->msgid != msgid)
482 /* search the event */
483 static struct client_event *client_event_search(struct afb_stub_ws *stubws, uint32_t eventid, const char *name)
485 struct client_event *ev;
488 while (ev != NULL && (ev->eventid != eventid || 0 != strcmp(afb_evt_event_name(ev->event), name)))
495 /* allocates and init the memorizing call */
496 static struct client_call *client_call_make(struct afb_stub_ws *stubws, struct afb_xreq *xreq)
498 struct client_call *call;
500 call = malloc(sizeof *call);
502 afb_xreq_addref(xreq);
504 call->msgid = ptr2id(call);
505 while(client_call_search(stubws, call->msgid) != NULL)
507 call->stubws = stubws;
508 call->next = stubws->calls;
509 stubws->calls = call;
514 /* free and release the memorizing call */
515 static void client_call_destroy(struct client_call *call)
517 struct client_call **prv;
519 prv = &call->stubws->calls;
520 while (*prv != NULL) {
528 afb_xreq_unref(call->xreq);
532 /* get event data from the message */
533 static int client_msg_event_read(struct readbuf *rb, uint32_t *eventid, const char **name)
535 return readbuf_uint32(rb, eventid) && readbuf_string(rb, name, NULL);
538 /* get event from the message */
539 static int client_msg_event_get(struct afb_stub_ws *stubws, struct readbuf *rb, struct client_event **ev)
544 /* get event data from the message */
545 if (!client_msg_event_read(rb, &eventid, &name)) {
546 ERROR("Invalid message");
550 /* check conflicts */
551 *ev = client_event_search(stubws, eventid, name);
553 ERROR("event %s not found", name);
560 /* get event from the message */
561 static int client_msg_call_get(struct afb_stub_ws *stubws, struct readbuf *rb, struct client_call **call)
565 /* get event data from the message */
566 if (!readbuf_uint32(rb, &msgid)) {
567 ERROR("Invalid message");
572 *call = client_call_search(stubws, msgid);
574 ERROR("message not found");
581 /* read a subscrition message */
582 static int client_msg_subscription_get(struct afb_stub_ws *stubws, struct readbuf *rb, struct client_call **call, struct client_event **ev)
584 return client_msg_call_get(stubws, rb, call) && client_msg_event_get(stubws, rb, ev);
588 static void client_event_create(struct afb_stub_ws *stubws, struct readbuf *rb)
593 struct client_event *ev;
595 /* get event data from the message */
596 offset = client_msg_event_read(rb, &eventid, &name);
598 ERROR("Invalid message");
602 /* check conflicts */
603 ev = client_event_search(stubws, eventid, name);
609 /* no conflict, try to add it */
610 ev = malloc(sizeof *ev);
612 ev->event = afb_evt_create_event(name);
613 if (ev->event.closure == NULL)
617 ev->eventid = eventid;
618 ev->next = stubws->events;
623 ERROR("can't create event %s, out of memory", name);
626 /* removes an event */
627 static void client_event_drop(struct afb_stub_ws *stubws, struct readbuf *rb)
629 struct client_event *ev, **prv;
631 /* retrieves the event */
632 if (!client_msg_event_get(stubws, rb, &ev))
635 /* decrease the reference count */
639 /* unlinks the event */
640 prv = &stubws->events;
645 /* destroys the event */
646 afb_event_drop(ev->event);
650 /* subscribes an event */
651 static void client_event_subscribe(struct afb_stub_ws *stubws, struct readbuf *rb)
653 struct client_event *ev;
654 struct client_call *call;
656 if (client_msg_subscription_get(stubws, rb, &call, &ev)) {
657 /* subscribe the request from the event */
658 if (afb_xreq_subscribe(call->xreq, ev->event) < 0)
659 ERROR("can't subscribe: %m");
663 /* unsubscribes an event */
664 static void client_event_unsubscribe(struct afb_stub_ws *stubws, struct readbuf *rb)
666 struct client_event *ev;
667 struct client_call *call;
669 if (client_msg_subscription_get(stubws, rb, &call, &ev)) {
670 /* unsubscribe the request from the event */
671 if (afb_xreq_unsubscribe(call->xreq, ev->event) < 0)
672 ERROR("can't unsubscribe: %m");
676 /* receives broadcasted events */
677 static void client_event_broadcast(struct afb_stub_ws *stubws, struct readbuf *rb)
679 struct json_object *object;
682 if (readbuf_string(rb, &event, NULL) && readbuf_object(rb, &object))
683 afb_evt_broadcast(event, object);
685 ERROR("unreadable broadcasted event");
689 static void client_event_push(struct afb_stub_ws *stubws, struct readbuf *rb)
691 struct client_event *ev;
692 struct json_object *object;
694 if (client_msg_event_get(stubws, rb, &ev) && readbuf_object(rb, &object))
695 afb_event_push(ev->event, object);
697 ERROR("unreadable push event");
700 static void client_reply_success(struct afb_stub_ws *stubws, struct readbuf *rb)
702 struct client_call *call;
703 struct json_object *object;
707 /* retrieve the message data */
708 if (!client_msg_call_get(stubws, rb, &call))
711 if (readbuf_uint32(rb, &flags)
712 && readbuf_string(rb, &info, NULL)
713 && readbuf_object(rb, &object)) {
714 call->xreq->context.flags = (unsigned)flags;
715 afb_xreq_success(call->xreq, object, *info ? info : NULL);
717 /* failing to have the answer */
718 afb_xreq_fail(call->xreq, "error", "ws error");
720 client_call_destroy(call);
723 static void client_reply_fail(struct afb_stub_ws *stubws, struct readbuf *rb)
725 struct client_call *call;
726 const char *info, *status;
729 /* retrieve the message data */
730 if (!client_msg_call_get(stubws, rb, &call))
733 if (readbuf_uint32(rb, &flags)
734 && readbuf_string(rb, &status, NULL)
735 && readbuf_string(rb, &info, NULL)) {
736 call->xreq->context.flags = (unsigned)flags;
737 afb_xreq_fail(call->xreq, status, *info ? info : NULL);
739 /* failing to have the answer */
740 afb_xreq_fail(call->xreq, "error", "ws error");
742 client_call_destroy(call);
745 /* send a subcall reply */
746 static void client_send_subcall_reply(struct client_subcall *subcall, int status, json_object *object)
749 struct writebuf wb = { .count = 0 };
750 char ie = status < 0;
752 if (!writebuf_char(&wb, CHAR_FOR_SUBCALL_REPLY)
753 || !writebuf_uint32(&wb, subcall->subcallid)
754 || !writebuf_char(&wb, ie)
755 || !writebuf_object(&wb, object)) {
760 rc = afb_ws_binary_v(subcall->stubws->ws, wb.iovec, wb.count);
763 ERROR("error while sending subcall reply");
766 /* callback for subcall reply */
767 static void client_subcall_reply_cb(void *closure, int status, json_object *object)
769 client_send_subcall_reply(closure, status, object);
773 /* received a subcall request */
774 static void client_subcall(struct afb_stub_ws *stubws, struct readbuf *rb)
776 struct client_subcall *subcall;
777 struct client_call *call;
778 const char *api, *verb;
780 struct json_object *object;
782 subcall = malloc(sizeof *subcall);
786 /* retrieve the message data */
787 if (!client_msg_call_get(stubws, rb, &call))
790 if (readbuf_uint32(rb, &subcallid)
791 && readbuf_string(rb, &api, NULL)
792 && readbuf_string(rb, &verb, NULL)
793 && readbuf_object(rb, &object)) {
794 subcall->stubws = stubws;
795 subcall->subcallid = subcallid;
796 afb_xreq_subcall(call->xreq, api, verb, object, client_subcall_reply_cb, subcall);
801 static void client_on_description(struct afb_stub_ws *stubws, struct readbuf *rb)
804 struct client_describe *desc;
805 struct json_object *object;
807 if (!readbuf_uint32(rb, &descid))
808 ERROR("unreadable description");
810 desc = stubws->describes;
811 while (desc && desc->descid != descid)
814 ERROR("unexpected description");
816 if (readbuf_object(rb, &object))
817 desc->result = object;
819 ERROR("bad description");
820 jobs_leave(desc->jobloop);
825 /* callback when receiving binary data */
826 static void client_on_binary(void *closure, char *data, size_t size)
829 struct afb_stub_ws *stubws = closure;
830 struct readbuf rb = { .head = data, .end = data + size };
832 pthread_mutex_lock(&stubws->mutex);
833 switch (*rb.head++) {
834 case CHAR_FOR_ANSWER_SUCCESS: /* success */
835 client_reply_success(stubws, &rb);
837 case CHAR_FOR_ANSWER_FAIL: /* fail */
838 client_reply_fail(stubws, &rb);
840 case CHAR_FOR_EVT_BROADCAST: /* broadcast */
841 client_event_broadcast(stubws, &rb);
843 case CHAR_FOR_EVT_ADD: /* creates the event */
844 client_event_create(stubws, &rb);
846 case CHAR_FOR_EVT_DEL: /* drops the event */
847 client_event_drop(stubws, &rb);
849 case CHAR_FOR_EVT_PUSH: /* pushs the event */
850 client_event_push(stubws, &rb);
852 case CHAR_FOR_EVT_SUBSCRIBE: /* subscribe event for a request */
853 client_event_subscribe(stubws, &rb);
855 case CHAR_FOR_EVT_UNSUBSCRIBE: /* unsubscribe event for a request */
856 client_event_unsubscribe(stubws, &rb);
858 case CHAR_FOR_SUBCALL_CALL: /* subcall */
859 client_subcall(stubws, &rb);
861 case CHAR_FOR_DESCRIPTION: /* description */
862 client_on_description(stubws, &rb);
864 default: /* unexpected message */
865 /* TODO: close the connection */
868 pthread_mutex_unlock(&stubws->mutex);
873 /* on call, propagate it to the ws service */
874 static void client_call_cb(void * closure, struct afb_xreq *xreq)
877 struct client_call *call;
878 struct writebuf wb = { .count = 0 };
881 struct afb_stub_ws *stubws = closure;
883 pthread_mutex_lock(&stubws->mutex);
885 /* create the recording data */
886 call = client_call_make(stubws, xreq);
888 afb_xreq_fail_f(xreq, "error", "out of memory");
892 /* creates the call message */
893 raw = afb_xreq_raw(xreq, &szraw);
896 if (!writebuf_char(&wb, CHAR_FOR_CALL)
897 || !writebuf_uint32(&wb, call->msgid)
898 || !writebuf_uint32(&wb, (uint32_t)xreq->context.flags)
899 || !writebuf_string(&wb, xreq->verb)
900 || !writebuf_string(&wb, afb_session_uuid(xreq->context.session))
901 || !writebuf_string_length(&wb, raw, szraw))
905 rc = afb_ws_binary_v(stubws->ws, wb.iovec, wb.count);
909 afb_xreq_fail(xreq, "error", "websocket sending error");
913 afb_xreq_fail(xreq, "error", "internal: raw is NULL!");
917 afb_xreq_fail(xreq, "error", "overflow: size doesn't match 32 bits!");
920 client_call_destroy(call);
922 pthread_mutex_unlock(&stubws->mutex);
925 static void client_send_describe_cb(int signum, void *closure, struct jobloop *jobloop)
927 struct client_describe *desc = closure;
928 struct writebuf wb = { .count = 0 };
931 /* record the jobloop */
932 desc->jobloop = jobloop;
935 if (writebuf_char(&wb, CHAR_FOR_DESCRIBE)
936 && writebuf_uint32(&wb, desc->descid)
937 && afb_ws_binary_v(desc->stubws->ws, wb.iovec, wb.count) >= 0)
943 /* get the description */
944 static struct json_object *client_describe_cb(void * closure)
946 struct client_describe desc, *d;
947 struct afb_stub_ws *stubws = closure;
949 /* fill in stack the description of the task */
950 pthread_mutex_lock(&stubws->mutex);
952 desc.descid = ptr2id(&desc);
953 d = stubws->describes;
955 if (d->descid != desc.descid)
959 d = stubws->describes;
962 desc.stubws = stubws;
963 desc.next = stubws->describes;
964 stubws->describes = &desc;
965 pthread_mutex_unlock(&stubws->mutex);
967 /* synchronous job: send the request and wait its result */
968 jobs_enter(NULL, 0, client_send_describe_cb, &desc);
970 /* unlink and send the result */
971 pthread_mutex_lock(&stubws->mutex);
972 d = stubws->describes;
974 stubws->describes = desc.next;
977 if (d->next != &desc)
985 pthread_mutex_unlock(&stubws->mutex);
989 /******************* client description part for server *****************************/
991 /* on call, propagate it to the ws service */
992 static void server_on_call(struct afb_stub_ws *stubws, struct readbuf *rb)
994 struct server_req *wreq;
996 const char *uuid, *verb;
997 uint32_t flags, msgid;
999 struct json_object *object;
1001 afb_stub_ws_addref(stubws);
1003 /* reads the call message data */
1004 if (!readbuf_uint32(rb, &msgid)
1005 || !readbuf_uint32(rb, &flags)
1006 || !readbuf_string(rb, &verb, &lenverb)
1007 || !readbuf_string(rb, &uuid, NULL)
1008 || !readbuf_object(rb, &object))
1011 /* create the request */
1012 wreq = malloc(++lenverb + sizeof *wreq);
1016 afb_xreq_init(&wreq->xreq, &server_req_xreq_itf);
1017 wreq->stubws = stubws;
1018 wreq->msgid = msgid;
1019 cverb = (char*)&wreq[1];
1020 memcpy(cverb, verb, lenverb);
1022 /* init the context */
1023 if (afb_context_connect(&wreq->xreq.context, uuid, NULL) < 0)
1025 wreq->xreq.context.flags = flags;
1027 /* makes the call */
1028 wreq->xreq.cred = afb_cred_addref(stubws->cred);
1029 wreq->xreq.api = stubws->apiname;
1030 wreq->xreq.verb = cverb;
1031 wreq->xreq.json = object;
1032 afb_xreq_process(&wreq->xreq, stubws->apiset);
1038 json_object_put(object);
1040 afb_stub_ws_unref(stubws);
1043 /* on subcall reply */
1044 static void server_on_subcall_reply(struct afb_stub_ws *stubws, struct readbuf *rb)
1048 struct json_object *object;
1049 struct server_subcall *sc, **psc;
1051 /* reads the call message data */
1052 if (!readbuf_uint32(rb, &subcallid)
1053 || !readbuf_char(rb, &ie)
1054 || !readbuf_object(rb, &object)) {
1055 /* TODO bad protocol */
1059 /* search the subcall and unlink it */
1060 pthread_mutex_lock(&stubws->mutex);
1061 psc = &stubws->subcalls;
1062 while ((sc = *psc) && sc->subcallid != subcallid)
1065 pthread_mutex_unlock(&stubws->mutex);
1066 /* TODO subcall not found */
1069 pthread_mutex_unlock(&stubws->mutex);
1070 sc->callback(sc->closure, -(int)ie, object);
1073 json_object_put(object);
1076 static void server_send_description(struct afb_stub_ws *stubws, uint32_t descid, struct json_object *descobj)
1078 struct writebuf wb = { .count = 0 };
1080 if (!writebuf_char(&wb, CHAR_FOR_DESCRIPTION)
1081 || !writebuf_uint32(&wb, descid)
1082 || !writebuf_object(&wb, descobj)
1083 || afb_ws_binary_v(stubws->ws, wb.iovec, wb.count) < 0)
1084 ERROR("can't send description");
1087 static void server_describe_job(int signum, void *closure)
1089 struct json_object *obj;
1090 struct server_describe *desc = closure;
1092 /* get the description if possible */
1093 obj = !signum ? afb_apiset_describe(desc->stubws->apiset, desc->stubws->apiname) : NULL;
1096 server_send_description(desc->stubws, desc->descid, obj);
1097 json_object_put(obj);
1098 afb_stub_ws_unref(desc->stubws);
1102 /* on describe, propagate it to the ws service */
1103 static void server_on_describe(struct afb_stub_ws *stubws, struct readbuf *rb)
1107 struct server_describe *desc;
1109 /* reads the descid */
1110 if (readbuf_uint32(rb, &descid)) {
1111 /* create asynchronous job */
1112 desc = malloc(sizeof *desc);
1114 desc->descid = descid;
1115 desc->stubws = stubws;
1116 afb_stub_ws_addref(stubws);
1117 if (jobs_queue(NULL, 0, server_describe_job, desc) < 0)
1118 server_describe_job(0, desc);
1121 server_send_description(stubws, descid, NULL);
1123 ERROR("can't provide description");
1126 /* callback when receiving binary data */
1127 static void server_on_binary(void *closure, char *data, size_t size)
1130 struct readbuf rb = { .head = data, .end = data + size };
1131 switch (*rb.head++) {
1133 server_on_call(closure, &rb);
1135 case CHAR_FOR_SUBCALL_REPLY:
1136 server_on_subcall_reply(closure, &rb);
1138 case CHAR_FOR_DESCRIBE:
1139 server_on_describe(closure, &rb);
1141 default: /* unexpected message */
1142 /* TODO: close the connection */
1149 /******************* server part: manage events **********************************/
1151 static void server_event_send(struct afb_stub_ws *stubws, char order, const char *event, int eventid, const char *data)
1154 struct writebuf wb = { .count = 0 };
1156 if (writebuf_char(&wb, order)
1157 && writebuf_uint32(&wb, eventid)
1158 && writebuf_string(&wb, event)
1159 && (data == NULL || writebuf_string(&wb, data))) {
1160 rc = afb_ws_binary_v(stubws->ws, wb.iovec, wb.count);
1164 ERROR("error while sending %c for event %s", order, event);
1167 static void server_event_add(void *closure, const char *event, int eventid)
1169 server_event_send(closure, CHAR_FOR_EVT_ADD, event, eventid, NULL);
1172 static void server_event_remove(void *closure, const char *event, int eventid)
1174 server_event_send(closure, CHAR_FOR_EVT_DEL, event, eventid, NULL);
1177 static void server_event_push(void *closure, const char *event, int eventid, struct json_object *object)
1179 const char *data = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
1180 server_event_send(closure, CHAR_FOR_EVT_PUSH, event, eventid, data ? : "null");
1181 json_object_put(object);
1184 static void server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object)
1187 struct afb_stub_ws *stubws = closure;
1189 struct writebuf wb = { .count = 0 };
1191 if (writebuf_char(&wb, CHAR_FOR_EVT_BROADCAST) && writebuf_string(&wb, event) && writebuf_object(&wb, object)) {
1192 rc = afb_ws_binary_v(stubws->ws, wb.iovec, wb.count);
1194 ERROR("error while broadcasting event %s", event);
1196 ERROR("error while broadcasting event %s", event);
1197 json_object_put(object);
1200 /*****************************************************/
1202 /* callback when receiving a hangup */
1203 static void server_on_hangup(void *closure)
1205 struct afb_stub_ws *stubws = closure;
1207 /* close the socket */
1208 if (stubws->fd >= 0) {
1211 if (stubws->on_hangup)
1212 stubws->on_hangup(stubws);
1215 /* release the client */
1216 afb_stub_ws_unref(stubws);
1219 /*****************************************************/
1221 /* the interface for events pushing */
1222 static const struct afb_evt_itf server_evt_itf = {
1223 .broadcast = server_event_broadcast,
1224 .push = server_event_push,
1225 .add = server_event_add,
1226 .remove = server_event_remove
1229 static const struct afb_ws_itf stub_ws_client_ws_itf =
1233 .on_binary = client_on_binary,
1238 static const struct afb_ws_itf server_ws_itf =
1242 .on_binary = server_on_binary,
1244 .on_hangup = server_on_hangup
1247 static struct afb_api_itf ws_api_itf = {
1248 .call = client_call_cb,
1249 .describe = client_describe_cb
1252 /*****************************************************/
1254 static struct afb_stub_ws *afb_stub_ws_create(int fd, const char *apiname, struct afb_apiset *apiset, const struct afb_ws_itf *itf)
1256 struct afb_stub_ws *stubws;
1258 stubws = calloc(1, sizeof *stubws + strlen(apiname));
1262 fcntl(fd, F_SETFD, FD_CLOEXEC);
1263 fcntl(fd, F_SETFL, O_NONBLOCK);
1264 stubws->ws = afb_ws_create(afb_common_get_event_loop(), fd, itf, stubws);
1265 if (stubws->ws != NULL) {
1267 strcpy(stubws->apiname, apiname);
1268 stubws->apiset = afb_apiset_addref(apiset);
1269 stubws->refcount = 1;
1270 stubws->subcalls = NULL;
1278 struct afb_stub_ws *afb_stub_ws_create_client(int fd, const char *apiname, struct afb_apiset *apiset)
1280 return afb_stub_ws_create(fd, apiname, apiset, &stub_ws_client_ws_itf);
1283 struct afb_stub_ws *afb_stub_ws_create_server(int fd, const char *apiname, struct afb_apiset *apiset)
1285 struct afb_stub_ws *stubws;
1287 stubws = afb_stub_ws_create(fd, apiname, apiset, &server_ws_itf);
1289 stubws->cred = afb_cred_create_for_socket(fd);
1290 stubws->listener = afb_evt_listener_create(&server_evt_itf, stubws);
1291 if (stubws->listener != NULL)
1293 afb_stub_ws_unref(stubws);
1298 void afb_stub_ws_unref(struct afb_stub_ws *stubws)
1300 struct server_subcall *sc, *nsc;
1302 if (!__atomic_sub_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED)) {
1303 afb_evt_listener_unref(stubws->listener);
1304 afb_ws_destroy(stubws->ws);
1305 nsc = stubws->subcalls;
1309 sc->callback(sc->closure, 1, NULL);
1312 afb_cred_unref(stubws->cred);
1313 afb_apiset_unref(stubws->apiset);
1318 void afb_stub_ws_addref(struct afb_stub_ws *stubws)
1320 __atomic_add_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED);
1323 void afb_stub_ws_on_hangup(struct afb_stub_ws *stubws, void (*on_hangup)(struct afb_stub_ws*))
1325 stubws->on_hangup = on_hangup;
1328 const char *afb_stub_ws_name(struct afb_stub_ws *stubws)
1330 return stubws->apiname;
1333 struct afb_api afb_stub_ws_client_api(struct afb_stub_ws *stubws)
1337 assert(!stubws->listener); /* check client */
1338 api.closure = stubws;
1339 api.itf = &ws_api_itf;
1340 api.noconcurrency = 0;
1344 int afb_stub_ws_client_add(struct afb_stub_ws *stubws, struct afb_apiset *apiset)
1346 return afb_apiset_add(apiset, stubws->apiname, afb_stub_ws_client_api(stubws));