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 /******************* client description part for server *****************************/
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;
188 /******************* common useful tools **********************************/
191 * translate a pointer to some integer
192 * @param ptr the pointer to translate
195 static inline uint32_t ptr2id(void *ptr)
197 return (uint32_t)(((intptr_t)ptr) >> 6);
200 /******************* serialisation part **********************************/
207 #define WRITEBUF_COUNT_MAX 32
210 struct iovec iovec[WRITEBUF_COUNT_MAX];
211 uint32_t uints[WRITEBUF_COUNT_MAX];
215 static char *readbuf_get(struct readbuf *rb, uint32_t length)
217 char *before = rb->head;
218 char *after = before + length;
225 static int readbuf_char(struct readbuf *rb, char *value)
227 if (rb->head >= rb->end)
229 *value = *rb->head++;
233 static int readbuf_uint32(struct readbuf *rb, uint32_t *value)
235 char *after = rb->head + sizeof *value;
238 memcpy(value, rb->head, sizeof *value);
240 *value = le32toh(*value);
244 static int readbuf_string(struct readbuf *rb, const char **value, size_t *length)
247 if (!readbuf_uint32(rb, &len) || !len)
250 *length = (size_t)(len - 1);
251 return (*value = readbuf_get(rb, len)) != NULL && rb->head[-1] == 0;
254 static int readbuf_object(struct readbuf *rb, struct json_object **object)
257 struct json_object *o;
258 int rc = readbuf_string(rb, &string, NULL);
260 o = json_tokener_parse(string);
261 if (o == NULL && strcmp(string, "null"))
262 o = json_object_new_string(string);
268 static int writebuf_put(struct writebuf *wb, const void *value, size_t length)
271 if (i == WRITEBUF_COUNT_MAX)
273 wb->iovec[i].iov_base = (void*)value;
274 wb->iovec[i].iov_len = length;
279 static int writebuf_char(struct writebuf *wb, char value)
282 if (i == WRITEBUF_COUNT_MAX)
284 *(char*)&wb->uints[i] = value;
285 wb->iovec[i].iov_base = &wb->uints[i];
286 wb->iovec[i].iov_len = 1;
291 static int writebuf_uint32(struct writebuf *wb, uint32_t value)
294 if (i == WRITEBUF_COUNT_MAX)
296 wb->uints[i] = htole32(value);
297 wb->iovec[i].iov_base = &wb->uints[i];
298 wb->iovec[i].iov_len = sizeof wb->uints[i];
303 static int writebuf_string_length(struct writebuf *wb, const char *value, size_t length)
305 uint32_t len = (uint32_t)++length;
306 return (size_t)len == length && len && writebuf_uint32(wb, len) && writebuf_put(wb, value, length);
309 static int writebuf_string(struct writebuf *wb, const char *value)
311 return writebuf_string_length(wb, value, strlen(value));
314 static int writebuf_object(struct writebuf *wb, struct json_object *object)
316 const char *string = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
317 return string != NULL && writebuf_string(wb, string);
320 /******************* ws request part for server *****************/
322 /* decrement the reference count of the request and free/release it on falling to null */
323 static void server_req_destroy_cb(struct afb_xreq *xreq)
325 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
327 afb_context_disconnect(&wreq->xreq.context);
328 afb_cred_unref(wreq->xreq.cred);
329 json_object_put(wreq->xreq.json);
330 afb_stub_ws_unref(wreq->stubws);
334 static void server_req_success_cb(struct afb_xreq *xreq, struct json_object *obj, const char *info)
337 struct writebuf wb = { .count = 0 };
338 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
340 if (writebuf_char(&wb, CHAR_FOR_ANSWER_SUCCESS)
341 && writebuf_uint32(&wb, wreq->msgid)
342 && writebuf_uint32(&wb, (uint32_t)wreq->xreq.context.flags)
343 && writebuf_string(&wb, info ? : "")
344 && writebuf_object(&wb, obj)) {
345 rc = afb_ws_binary_v(wreq->stubws->ws, wb.iovec, wb.count);
349 ERROR("error while sending success");
351 json_object_put(obj);
354 static void server_req_fail_cb(struct afb_xreq *xreq, const char *status, const char *info)
357 struct writebuf wb = { .count = 0 };
358 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
360 if (writebuf_char(&wb, CHAR_FOR_ANSWER_FAIL)
361 && writebuf_uint32(&wb, wreq->msgid)
362 && writebuf_uint32(&wb, (uint32_t)wreq->xreq.context.flags)
363 && writebuf_string(&wb, status)
364 && writebuf_string(&wb, info ? : "")) {
365 rc = afb_ws_binary_v(wreq->stubws->ws, wb.iovec, wb.count);
369 ERROR("error while sending fail");
372 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)
375 struct writebuf wb = { .count = 0 };
376 struct server_subcall *sc, *osc;
377 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
378 struct afb_stub_ws *stubws = wreq->stubws;
380 sc = malloc(sizeof *sc);
382 callback(cb_closure, 1, afb_msg_json_internal_error());
384 sc->callback = callback;
385 sc->closure = cb_closure;
387 pthread_mutex_unlock(&stubws->mutex);
388 sc->subcallid = ptr2id(sc);
391 osc = stubws->subcalls;
392 while(osc && osc->subcallid != sc->subcallid)
395 sc->next = stubws->subcalls;
396 stubws->subcalls = sc;
397 pthread_mutex_unlock(&stubws->mutex);
399 if (writebuf_char(&wb, CHAR_FOR_SUBCALL_CALL)
400 && writebuf_uint32(&wb, wreq->msgid)
401 && writebuf_uint32(&wb, sc->subcallid)
402 && writebuf_string(&wb, api)
403 && writebuf_string(&wb, verb)
404 && writebuf_object(&wb, args)) {
405 rc = afb_ws_binary_v(wreq->stubws->ws, wb.iovec, wb.count);
409 ERROR("error while sending fail");
413 static int server_req_subscribe_cb(struct afb_xreq *xreq, struct afb_event event)
416 struct writebuf wb = { .count = 0 };
417 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
419 rc = afb_evt_add_watch(wreq->stubws->listener, event);
423 if (writebuf_char(&wb, CHAR_FOR_EVT_SUBSCRIBE)
424 && writebuf_uint32(&wb, wreq->msgid)
425 && writebuf_uint32(&wb, (uint32_t)afb_evt_event_id(event))
426 && writebuf_string(&wb, afb_evt_event_name(event))) {
427 rc2 = afb_ws_binary_v(wreq->stubws->ws, wb.iovec, wb.count);
431 ERROR("error while subscribing event");
436 static int server_req_unsubscribe_cb(struct afb_xreq *xreq, struct afb_event event)
439 struct writebuf wb = { .count = 0 };
440 struct server_req *wreq = CONTAINER_OF_XREQ(struct server_req, xreq);
442 if (writebuf_char(&wb, CHAR_FOR_EVT_UNSUBSCRIBE)
443 && writebuf_uint32(&wb, wreq->msgid)
444 && writebuf_uint32(&wb, (uint32_t)afb_evt_event_id(event))
445 && writebuf_string(&wb, afb_evt_event_name(event))) {
446 rc2 = afb_ws_binary_v(wreq->stubws->ws, wb.iovec, wb.count);
450 ERROR("error while subscribing event");
452 rc = afb_evt_remove_watch(wreq->stubws->listener, event);
456 static const struct afb_xreq_query_itf server_req_xreq_itf = {
457 .success = server_req_success_cb,
458 .fail = server_req_fail_cb,
459 .unref = server_req_destroy_cb,
460 .subcall = server_req_subcall_cb,
461 .subscribe = server_req_subscribe_cb,
462 .unsubscribe = server_req_unsubscribe_cb
465 /******************* client part **********************************/
467 /* search a memorized call */
468 static struct client_call *client_call_search(struct afb_stub_ws *stubws, uint32_t msgid)
470 struct client_call *call;
472 call = stubws->calls;
473 while (call != NULL && call->msgid != msgid)
479 /* search the event */
480 static struct client_event *client_event_search(struct afb_stub_ws *stubws, uint32_t eventid, const char *name)
482 struct client_event *ev;
485 while (ev != NULL && (ev->eventid != eventid || 0 != strcmp(afb_evt_event_name(ev->event), name)))
492 /* allocates and init the memorizing call */
493 static struct client_call *client_call_make(struct afb_stub_ws *stubws, struct afb_xreq *xreq)
495 struct client_call *call;
497 call = malloc(sizeof *call);
499 afb_xreq_addref(xreq);
501 call->msgid = ptr2id(call);
502 while(client_call_search(stubws, call->msgid) != NULL)
504 call->stubws = stubws;
505 call->next = stubws->calls;
506 stubws->calls = call;
511 /* free and release the memorizing call */
512 static void client_call_destroy(struct client_call *call)
514 struct client_call **prv;
516 prv = &call->stubws->calls;
517 while (*prv != NULL) {
525 afb_xreq_unref(call->xreq);
529 /* get event data from the message */
530 static int client_msg_event_read(struct readbuf *rb, uint32_t *eventid, const char **name)
532 return readbuf_uint32(rb, eventid) && readbuf_string(rb, name, NULL);
535 /* get event from the message */
536 static int client_msg_event_get(struct afb_stub_ws *stubws, struct readbuf *rb, struct client_event **ev)
541 /* get event data from the message */
542 if (!client_msg_event_read(rb, &eventid, &name)) {
543 ERROR("Invalid message");
547 /* check conflicts */
548 *ev = client_event_search(stubws, eventid, name);
550 ERROR("event %s not found", name);
557 /* get event from the message */
558 static int client_msg_call_get(struct afb_stub_ws *stubws, struct readbuf *rb, struct client_call **call)
562 /* get event data from the message */
563 if (!readbuf_uint32(rb, &msgid)) {
564 ERROR("Invalid message");
569 *call = client_call_search(stubws, msgid);
571 ERROR("message not found");
578 /* read a subscrition message */
579 static int client_msg_subscription_get(struct afb_stub_ws *stubws, struct readbuf *rb, struct client_call **call, struct client_event **ev)
581 return client_msg_call_get(stubws, rb, call) && client_msg_event_get(stubws, rb, ev);
585 static void client_event_create(struct afb_stub_ws *stubws, struct readbuf *rb)
590 struct client_event *ev;
592 /* get event data from the message */
593 offset = client_msg_event_read(rb, &eventid, &name);
595 ERROR("Invalid message");
599 /* check conflicts */
600 ev = client_event_search(stubws, eventid, name);
606 /* no conflict, try to add it */
607 ev = malloc(sizeof *ev);
609 ev->event = afb_evt_create_event(name);
610 if (ev->event.closure == NULL)
614 ev->eventid = eventid;
615 ev->next = stubws->events;
620 ERROR("can't create event %s, out of memory", name);
623 /* removes an event */
624 static void client_event_drop(struct afb_stub_ws *stubws, struct readbuf *rb)
626 struct client_event *ev, **prv;
628 /* retrieves the event */
629 if (!client_msg_event_get(stubws, rb, &ev))
632 /* decrease the reference count */
636 /* unlinks the event */
637 prv = &stubws->events;
642 /* destroys the event */
643 afb_event_drop(ev->event);
647 /* subscribes an event */
648 static void client_event_subscribe(struct afb_stub_ws *stubws, struct readbuf *rb)
650 struct client_event *ev;
651 struct client_call *call;
653 if (client_msg_subscription_get(stubws, rb, &call, &ev)) {
654 /* subscribe the request from the event */
655 if (afb_xreq_subscribe(call->xreq, ev->event) < 0)
656 ERROR("can't subscribe: %m");
660 /* unsubscribes an event */
661 static void client_event_unsubscribe(struct afb_stub_ws *stubws, struct readbuf *rb)
663 struct client_event *ev;
664 struct client_call *call;
666 if (client_msg_subscription_get(stubws, rb, &call, &ev)) {
667 /* unsubscribe the request from the event */
668 if (afb_xreq_unsubscribe(call->xreq, ev->event) < 0)
669 ERROR("can't unsubscribe: %m");
673 /* receives broadcasted events */
674 static void client_event_broadcast(struct afb_stub_ws *stubws, struct readbuf *rb)
676 struct json_object *object;
679 if (readbuf_string(rb, &event, NULL) && readbuf_object(rb, &object))
680 afb_evt_broadcast(event, object);
682 ERROR("unreadable broadcasted event");
686 static void client_event_push(struct afb_stub_ws *stubws, struct readbuf *rb)
688 struct client_event *ev;
689 struct json_object *object;
691 if (client_msg_event_get(stubws, rb, &ev) && readbuf_object(rb, &object))
692 afb_event_push(ev->event, object);
694 ERROR("unreadable push event");
697 static void client_reply_success(struct afb_stub_ws *stubws, struct readbuf *rb)
699 struct client_call *call;
700 struct json_object *object;
704 /* retrieve the message data */
705 if (!client_msg_call_get(stubws, rb, &call))
708 if (readbuf_uint32(rb, &flags)
709 && readbuf_string(rb, &info, NULL)
710 && readbuf_object(rb, &object)) {
711 call->xreq->context.flags = (unsigned)flags;
712 afb_xreq_success(call->xreq, object, *info ? info : NULL);
714 /* failing to have the answer */
715 afb_xreq_fail(call->xreq, "error", "ws error");
717 client_call_destroy(call);
720 static void client_reply_fail(struct afb_stub_ws *stubws, struct readbuf *rb)
722 struct client_call *call;
723 const char *info, *status;
726 /* retrieve the message data */
727 if (!client_msg_call_get(stubws, rb, &call))
730 if (readbuf_uint32(rb, &flags)
731 && readbuf_string(rb, &status, NULL)
732 && readbuf_string(rb, &info, NULL)) {
733 call->xreq->context.flags = (unsigned)flags;
734 afb_xreq_fail(call->xreq, status, *info ? info : NULL);
736 /* failing to have the answer */
737 afb_xreq_fail(call->xreq, "error", "ws error");
739 client_call_destroy(call);
742 /* send a subcall reply */
743 static void client_send_subcall_reply(struct client_subcall *subcall, int status, json_object *object)
746 struct writebuf wb = { .count = 0 };
747 char ie = status < 0;
749 if (!writebuf_char(&wb, CHAR_FOR_SUBCALL_REPLY)
750 || !writebuf_uint32(&wb, subcall->subcallid)
751 || !writebuf_char(&wb, ie)
752 || !writebuf_object(&wb, object)) {
757 rc = afb_ws_binary_v(subcall->stubws->ws, wb.iovec, wb.count);
760 ERROR("error while sending subcall reply");
763 /* callback for subcall reply */
764 static void client_subcall_reply_cb(void *closure, int status, json_object *object)
766 client_send_subcall_reply(closure, status, object);
770 /* received a subcall request */
771 static void client_subcall(struct afb_stub_ws *stubws, struct readbuf *rb)
773 struct client_subcall *subcall;
774 struct client_call *call;
775 const char *api, *verb;
777 struct json_object *object;
779 subcall = malloc(sizeof *subcall);
783 /* retrieve the message data */
784 if (!client_msg_call_get(stubws, rb, &call))
787 if (readbuf_uint32(rb, &subcallid)
788 && readbuf_string(rb, &api, NULL)
789 && readbuf_string(rb, &verb, NULL)
790 && readbuf_object(rb, &object)) {
791 subcall->stubws = stubws;
792 subcall->subcallid = subcallid;
793 afb_xreq_subcall(call->xreq, api, verb, object, client_subcall_reply_cb, subcall);
798 static void client_on_description(struct afb_stub_ws *stubws, struct readbuf *rb)
801 struct client_describe *desc;
802 struct json_object *object;
804 if (!readbuf_uint32(rb, &descid))
805 ERROR("unreadable description");
807 desc = stubws->describes;
808 while (desc && desc->descid != descid)
811 ERROR("unexpected description");
813 if (readbuf_object(rb, &object))
814 desc->result = object;
816 ERROR("bad description");
817 jobs_leave(desc->jobloop);
822 /* callback when receiving binary data */
823 static void client_on_binary(void *closure, char *data, size_t size)
826 struct afb_stub_ws *stubws = closure;
827 struct readbuf rb = { .head = data, .end = data + size };
829 pthread_mutex_lock(&stubws->mutex);
830 switch (*rb.head++) {
831 case CHAR_FOR_ANSWER_SUCCESS: /* success */
832 client_reply_success(stubws, &rb);
834 case CHAR_FOR_ANSWER_FAIL: /* fail */
835 client_reply_fail(stubws, &rb);
837 case CHAR_FOR_EVT_BROADCAST: /* broadcast */
838 client_event_broadcast(stubws, &rb);
840 case CHAR_FOR_EVT_ADD: /* creates the event */
841 client_event_create(stubws, &rb);
843 case CHAR_FOR_EVT_DEL: /* drops the event */
844 client_event_drop(stubws, &rb);
846 case CHAR_FOR_EVT_PUSH: /* pushs the event */
847 client_event_push(stubws, &rb);
849 case CHAR_FOR_EVT_SUBSCRIBE: /* subscribe event for a request */
850 client_event_subscribe(stubws, &rb);
852 case CHAR_FOR_EVT_UNSUBSCRIBE: /* unsubscribe event for a request */
853 client_event_unsubscribe(stubws, &rb);
855 case CHAR_FOR_SUBCALL_CALL: /* subcall */
856 client_subcall(stubws, &rb);
858 case CHAR_FOR_DESCRIPTION: /* description */
859 client_on_description(stubws, &rb);
861 default: /* unexpected message */
862 /* TODO: close the connection */
865 pthread_mutex_unlock(&stubws->mutex);
870 /* on call, propagate it to the ws service */
871 static void client_call_cb(void * closure, struct afb_xreq *xreq)
874 struct client_call *call;
875 struct writebuf wb = { .count = 0 };
878 struct afb_stub_ws *stubws = closure;
880 pthread_mutex_lock(&stubws->mutex);
882 /* create the recording data */
883 call = client_call_make(stubws, xreq);
885 afb_xreq_fail_f(xreq, "error", "out of memory");
889 /* creates the call message */
890 raw = afb_xreq_raw(xreq, &szraw);
893 if (!writebuf_char(&wb, CHAR_FOR_CALL)
894 || !writebuf_uint32(&wb, call->msgid)
895 || !writebuf_uint32(&wb, (uint32_t)xreq->context.flags)
896 || !writebuf_string(&wb, xreq->verb)
897 || !writebuf_string(&wb, afb_session_uuid(xreq->context.session))
898 || !writebuf_string_length(&wb, raw, szraw))
902 rc = afb_ws_binary_v(stubws->ws, wb.iovec, wb.count);
906 afb_xreq_fail(xreq, "error", "websocket sending error");
910 afb_xreq_fail(xreq, "error", "internal: raw is NULL!");
914 afb_xreq_fail(xreq, "error", "overflow: size doesn't match 32 bits!");
917 client_call_destroy(call);
919 pthread_mutex_unlock(&stubws->mutex);
922 static void client_send_describe_cb(int signum, void *closure, struct jobloop *jobloop)
924 struct client_describe *desc = closure;
925 struct writebuf wb = { .count = 0 };
928 /* record the jobloop */
929 desc->jobloop = jobloop;
932 if (writebuf_char(&wb, CHAR_FOR_DESCRIBE)
933 && writebuf_uint32(&wb, desc->descid)
934 && afb_ws_binary_v(desc->stubws->ws, wb.iovec, wb.count) >= 0)
940 /* get the description */
941 static struct json_object *client_describe_cb(void * closure)
943 struct client_describe desc, *d;
944 struct afb_stub_ws *stubws = closure;
946 /* fill in stack the description of the task */
947 pthread_mutex_lock(&stubws->mutex);
949 desc.descid = ptr2id(&desc);
950 d = stubws->describes;
952 if (d->descid != desc.descid)
956 d = stubws->describes;
959 desc.stubws = stubws;
960 desc.next = stubws->describes;
961 stubws->describes = &desc;
962 pthread_mutex_unlock(&stubws->mutex);
964 /* synchronous job: send the request and wait its result */
965 jobs_enter(NULL, 0, client_send_describe_cb, &desc);
967 /* unlink and send the result */
968 pthread_mutex_lock(&stubws->mutex);
969 d = stubws->describes;
971 stubws->describes = desc.next;
974 if (d->next != &desc)
982 pthread_mutex_unlock(&stubws->mutex);
986 /******************* client description part for server *****************************/
988 /* on call, propagate it to the ws service */
989 static void server_on_call(struct afb_stub_ws *stubws, struct readbuf *rb)
991 struct server_req *wreq;
993 const char *uuid, *verb;
994 uint32_t flags, msgid;
996 struct json_object *object;
998 afb_stub_ws_addref(stubws);
1000 /* reads the call message data */
1001 if (!readbuf_uint32(rb, &msgid)
1002 || !readbuf_uint32(rb, &flags)
1003 || !readbuf_string(rb, &verb, &lenverb)
1004 || !readbuf_string(rb, &uuid, NULL)
1005 || !readbuf_object(rb, &object))
1008 /* create the request */
1009 wreq = malloc(++lenverb + sizeof *wreq);
1013 afb_xreq_init(&wreq->xreq, &server_req_xreq_itf);
1014 wreq->stubws = stubws;
1015 wreq->msgid = msgid;
1016 cverb = (char*)&wreq[1];
1017 memcpy(cverb, verb, lenverb);
1019 /* init the context */
1020 if (afb_context_connect(&wreq->xreq.context, uuid, NULL) < 0)
1022 wreq->xreq.context.flags = flags;
1024 /* makes the call */
1025 wreq->xreq.cred = afb_cred_addref(stubws->cred);
1026 wreq->xreq.api = stubws->apiname;
1027 wreq->xreq.verb = cverb;
1028 wreq->xreq.json = object;
1029 afb_xreq_process(&wreq->xreq, stubws->apiset);
1035 json_object_put(object);
1037 afb_stub_ws_unref(stubws);
1040 /* on subcall reply */
1041 static void server_on_subcall_reply(struct afb_stub_ws *stubws, struct readbuf *rb)
1045 struct json_object *object;
1046 struct server_subcall *sc, **psc;
1048 /* reads the call message data */
1049 if (!readbuf_uint32(rb, &subcallid)
1050 || !readbuf_char(rb, &ie)
1051 || !readbuf_object(rb, &object)) {
1052 /* TODO bad protocol */
1056 /* search the subcall and unlink it */
1057 pthread_mutex_lock(&stubws->mutex);
1058 psc = &stubws->subcalls;
1059 while ((sc = *psc) && sc->subcallid != subcallid)
1062 pthread_mutex_unlock(&stubws->mutex);
1063 /* TODO subcall not found */
1066 pthread_mutex_unlock(&stubws->mutex);
1067 sc->callback(sc->closure, -(int)ie, object);
1070 json_object_put(object);
1073 static void server_send_description(struct afb_stub_ws *stubws, uint32_t descid, struct json_object *descobj)
1075 struct writebuf wb = { .count = 0 };
1077 if (!writebuf_char(&wb, CHAR_FOR_DESCRIPTION)
1078 || !writebuf_uint32(&wb, descid)
1079 || !writebuf_object(&wb, descobj)
1080 || afb_ws_binary_v(stubws->ws, wb.iovec, wb.count) < 0)
1081 ERROR("can't send description");
1084 static void server_describe_job(int signum, void *closure)
1087 struct json_object *obj;
1088 struct server_describe *desc = closure;
1090 /* get the description if possible */
1093 && !afb_apiset_get(desc->stubws->apiset, desc->stubws->apiname, &api)
1094 && api.itf->describe) {
1095 obj = api.itf->describe(api.closure);
1099 server_send_description(desc->stubws, desc->descid, obj);
1100 json_object_put(obj);
1101 afb_stub_ws_unref(desc->stubws);
1105 /* on describe, propagate it to the ws service */
1106 static void server_on_describe(struct afb_stub_ws *stubws, struct readbuf *rb)
1110 struct server_describe *desc;
1112 /* reads the descid */
1113 if (readbuf_uint32(rb, &descid)) {
1114 /* create asynchronous job */
1115 desc = malloc(sizeof *desc);
1117 desc->descid = descid;
1118 desc->stubws = stubws;
1119 afb_stub_ws_addref(stubws);
1120 if (jobs_queue(NULL, 0, server_describe_job, desc) < 0)
1121 server_describe_job(0, desc);
1124 server_send_description(stubws, descid, NULL);
1126 ERROR("can't provide description");
1129 /* callback when receiving binary data */
1130 static void server_on_binary(void *closure, char *data, size_t size)
1133 struct readbuf rb = { .head = data, .end = data + size };
1134 switch (*rb.head++) {
1136 server_on_call(closure, &rb);
1138 case CHAR_FOR_SUBCALL_REPLY:
1139 server_on_subcall_reply(closure, &rb);
1141 case CHAR_FOR_DESCRIBE:
1142 server_on_describe(closure, &rb);
1144 default: /* unexpected message */
1145 /* TODO: close the connection */
1152 /******************* server part: manage events **********************************/
1154 static void server_event_send(struct afb_stub_ws *stubws, char order, const char *event, int eventid, const char *data)
1157 struct writebuf wb = { .count = 0 };
1159 if (writebuf_char(&wb, order)
1160 && writebuf_uint32(&wb, eventid)
1161 && writebuf_string(&wb, event)
1162 && (data == NULL || writebuf_string(&wb, data))) {
1163 rc = afb_ws_binary_v(stubws->ws, wb.iovec, wb.count);
1167 ERROR("error while sending %c for event %s", order, event);
1170 static void server_event_add(void *closure, const char *event, int eventid)
1172 server_event_send(closure, CHAR_FOR_EVT_ADD, event, eventid, NULL);
1175 static void server_event_remove(void *closure, const char *event, int eventid)
1177 server_event_send(closure, CHAR_FOR_EVT_DEL, event, eventid, NULL);
1180 static void server_event_push(void *closure, const char *event, int eventid, struct json_object *object)
1182 const char *data = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
1183 server_event_send(closure, CHAR_FOR_EVT_PUSH, event, eventid, data ? : "null");
1184 json_object_put(object);
1187 static void server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object)
1190 struct afb_stub_ws *stubws = closure;
1192 struct writebuf wb = { .count = 0 };
1194 if (writebuf_char(&wb, CHAR_FOR_EVT_BROADCAST) && writebuf_string(&wb, event) && writebuf_object(&wb, object)) {
1195 rc = afb_ws_binary_v(stubws->ws, wb.iovec, wb.count);
1197 ERROR("error while broadcasting event %s", event);
1199 ERROR("error while broadcasting event %s", event);
1200 json_object_put(object);
1203 /*****************************************************/
1205 /* callback when receiving a hangup */
1206 static void server_on_hangup(void *closure)
1208 struct afb_stub_ws *stubws = closure;
1210 /* close the socket */
1211 if (stubws->fd >= 0) {
1216 /* release the client */
1217 afb_stub_ws_unref(stubws);
1220 /*****************************************************/
1222 /* the interface for events pushing */
1223 static const struct afb_evt_itf server_evt_itf = {
1224 .broadcast = server_event_broadcast,
1225 .push = server_event_push,
1226 .add = server_event_add,
1227 .remove = server_event_remove
1230 static const struct afb_ws_itf stub_ws_client_ws_itf =
1234 .on_binary = client_on_binary,
1239 static const struct afb_ws_itf server_ws_itf =
1243 .on_binary = server_on_binary,
1245 .on_hangup = server_on_hangup
1248 static struct afb_api_itf ws_api_itf = {
1249 .call = client_call_cb,
1250 .describe = client_describe_cb
1253 /*****************************************************/
1255 static struct afb_stub_ws *afb_stub_ws_create(int fd, const char *apiname, struct afb_apiset *apiset, const struct afb_ws_itf *itf)
1257 struct afb_stub_ws *stubws;
1259 stubws = calloc(1, sizeof *stubws + strlen(apiname));
1263 fcntl(fd, F_SETFD, FD_CLOEXEC);
1264 fcntl(fd, F_SETFL, O_NONBLOCK);
1265 stubws->ws = afb_ws_create(afb_common_get_event_loop(), fd, itf, stubws);
1266 if (stubws->ws != NULL) {
1268 strcpy(stubws->apiname, apiname);
1269 stubws->apiset = afb_apiset_addref(apiset);
1270 stubws->refcount = 1;
1271 stubws->subcalls = NULL;
1279 struct afb_stub_ws *afb_stub_ws_create_client(int fd, const char *apiname, struct afb_apiset *apiset)
1281 struct afb_api afb_api;
1282 struct afb_stub_ws *stubws;
1284 stubws = afb_stub_ws_create(fd, apiname, apiset, &stub_ws_client_ws_itf);
1286 afb_api.closure = stubws;
1287 afb_api.itf = &ws_api_itf;
1288 if (afb_apiset_add(apiset, stubws->apiname, afb_api) >= 0)
1290 afb_stub_ws_unref(stubws);
1296 struct afb_stub_ws *afb_stub_ws_create_server(int fd, const char *apiname, struct afb_apiset *apiset)
1298 struct afb_stub_ws *stubws;
1300 stubws = afb_stub_ws_create(fd, apiname, apiset, &server_ws_itf);
1302 stubws->cred = afb_cred_create_for_socket(fd);
1303 stubws->listener = afb_evt_listener_create(&server_evt_itf, stubws);
1304 if (stubws->listener != NULL)
1306 afb_stub_ws_unref(stubws);
1311 void afb_stub_ws_unref(struct afb_stub_ws *stubws)
1313 struct server_subcall *sc, *nsc;
1315 if (!__atomic_sub_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED)) {
1316 afb_evt_listener_unref(stubws->listener);
1317 afb_ws_destroy(stubws->ws);
1318 nsc = stubws->subcalls;
1322 sc->callback(sc->closure, 1, NULL);
1325 afb_cred_unref(stubws->cred);
1326 afb_apiset_unref(stubws->apiset);
1331 void afb_stub_ws_addref(struct afb_stub_ws *stubws)
1333 __atomic_add_fetch(&stubws->refcount, 1, __ATOMIC_RELAXED);