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>
38 #include "afb-msg-json.h"
39 #include "afb-proto-ws.h"
44 /******** implementation of internal binder protocol per api **************/
47 This protocol is asymetric: there is a client and a server
49 The client can require the following actions:
55 The server must reply to the previous actions by
57 - answering success or failure of the call
59 - answering the required description
61 The server can also within the context of a call
65 - subscribe or unsubscribe an event
67 For the purpose of handling events the server can:
69 - create/destroy an event
71 - push or brodcast data as an event
74 /************** constants for protocol definition *************************/
76 #define CHAR_FOR_CALL 'C'
77 #define CHAR_FOR_ANSWER_SUCCESS 'T'
78 #define CHAR_FOR_ANSWER_FAIL 'F'
79 #define CHAR_FOR_EVT_BROADCAST '*'
80 #define CHAR_FOR_EVT_ADD '+'
81 #define CHAR_FOR_EVT_DEL '-'
82 #define CHAR_FOR_EVT_PUSH '!'
83 #define CHAR_FOR_EVT_SUBSCRIBE 'S'
84 #define CHAR_FOR_EVT_UNSUBSCRIBE 'U'
85 #define CHAR_FOR_SUBCALL_CALL 'B'
86 #define CHAR_FOR_SUBCALL_REPLY 'R'
87 #define CHAR_FOR_DESCRIBE 'D'
88 #define CHAR_FOR_DESCRIPTION 'd'
90 /******************* handling subcalls *****************************/
93 * Structure on server side for recording pending
98 struct server_subcall *next; /**< next subcall for the client */
99 uint32_t subcallid; /**< the subcallid */
100 void (*callback)(void*, int, struct json_object*); /**< callback on completion */
101 void *closure; /**< closure of the callback */
105 * Structure for sending back replies on client side
107 struct afb_proto_ws_subcall
109 struct afb_proto_ws *protows; /**< proto descriptor */
111 uint32_t subcallid; /**< subcallid for the reply */
115 * structure for recording calls on client side
118 struct client_call *next; /* the next call */
119 struct afb_proto_ws *protows; /* the proto_ws */
121 uint32_t callid; /* the message identifier */
125 * structure for a ws request
127 struct afb_proto_ws_call {
128 struct client_call *next; /* the next call */
129 struct afb_proto_ws *protows; /* the client of the request */
130 uint32_t refcount; /* reference count */
131 uint32_t callid; /* the incoming request callid */
132 char *buffer; /* the incoming buffer */
136 * structure for recording describe requests
138 struct client_describe
140 struct client_describe *next;
141 struct afb_proto_ws *protows;
142 void (*callback)(void*, struct json_object*);
148 * structure for jobs of describing
150 struct afb_proto_ws_describe
152 struct afb_proto_ws *protows;
156 /******************* proto description for client or servers ******************/
160 /* count of references */
163 /* file descriptor */
166 /* resource control */
167 pthread_mutex_t mutex;
172 /* the client closure */
175 /* the client side interface */
176 const struct afb_proto_ws_client_itf *client_itf;
178 /* the server side interface */
179 const struct afb_proto_ws_server_itf *server_itf;
181 /* emitted calls (client side) */
182 struct client_call *calls;
184 /* pending subcalls (server side) */
185 struct server_subcall *subcalls;
187 /* pending description (client side) */
188 struct client_describe *describes;
190 /* on hangup callback */
191 void (*on_hangup)(void *closure);
194 /******************* streaming objects **********************************/
196 #define WRITEBUF_COUNT_MAX 32
199 struct iovec iovec[WRITEBUF_COUNT_MAX];
200 uint32_t uints[WRITEBUF_COUNT_MAX];
206 char *base, *head, *end;
211 struct afb_proto_ws *protows;
215 /******************* common useful tools **********************************/
218 * translate a pointer to some integer
219 * @param ptr the pointer to translate
222 static inline uint32_t ptr2id(void *ptr)
224 return (uint32_t)(((intptr_t)ptr) >> 6);
227 /******************* serialisation part **********************************/
229 static char *readbuf_get(struct readbuf *rb, uint32_t length)
231 char *before = rb->head;
232 char *after = before + length;
239 static int readbuf_char(struct readbuf *rb, char *value)
241 if (rb->head >= rb->end)
243 *value = *rb->head++;
247 static int readbuf_uint32(struct readbuf *rb, uint32_t *value)
249 char *after = rb->head + sizeof *value;
252 memcpy(value, rb->head, sizeof *value);
254 *value = le32toh(*value);
258 static int readbuf_string(struct readbuf *rb, const char **value, size_t *length)
261 if (!readbuf_uint32(rb, &len) || !len)
264 *length = (size_t)(len - 1);
265 return (*value = readbuf_get(rb, len)) != NULL && rb->head[-1] == 0;
268 static int readbuf_object(struct readbuf *rb, struct json_object **object)
271 struct json_object *o;
272 int rc = readbuf_string(rb, &string, NULL);
274 o = json_tokener_parse(string);
275 if (o == NULL && strcmp(string, "null"))
276 o = json_object_new_string(string);
282 static int writebuf_put(struct writebuf *wb, const void *value, size_t length)
285 if (i == WRITEBUF_COUNT_MAX)
287 wb->iovec[i].iov_base = (void*)value;
288 wb->iovec[i].iov_len = length;
293 static int writebuf_char(struct writebuf *wb, char value)
296 if (i == WRITEBUF_COUNT_MAX)
298 *(char*)&wb->uints[i] = value;
299 wb->iovec[i].iov_base = &wb->uints[i];
300 wb->iovec[i].iov_len = 1;
305 static int writebuf_uint32(struct writebuf *wb, uint32_t value)
308 if (i == WRITEBUF_COUNT_MAX)
310 wb->uints[i] = htole32(value);
311 wb->iovec[i].iov_base = &wb->uints[i];
312 wb->iovec[i].iov_len = sizeof wb->uints[i];
317 static int writebuf_string_length(struct writebuf *wb, const char *value, size_t length)
319 uint32_t len = (uint32_t)++length;
320 return (size_t)len == length && len && writebuf_uint32(wb, len) && writebuf_put(wb, value, length);
323 static int writebuf_string(struct writebuf *wb, const char *value)
325 return writebuf_string_length(wb, value, strlen(value));
328 static int writebuf_object(struct writebuf *wb, struct json_object *object)
330 const char *string = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
331 return string != NULL && writebuf_string(wb, string);
334 /******************* ws request part for server *****************/
336 void afb_proto_ws_call_addref(struct afb_proto_ws_call *call)
338 __atomic_add_fetch(&call->refcount, 1, __ATOMIC_RELAXED);
341 void afb_proto_ws_call_unref(struct afb_proto_ws_call *call)
343 if (__atomic_sub_fetch(&call->refcount, 1, __ATOMIC_RELAXED))
346 afb_proto_ws_unref(call->protows);
351 int afb_proto_ws_call_success(struct afb_proto_ws_call *call, struct json_object *obj, const char *info)
354 struct writebuf wb = { .count = 0 };
355 struct afb_proto_ws *protows = call->protows;
357 if (writebuf_char(&wb, CHAR_FOR_ANSWER_SUCCESS)
358 && writebuf_uint32(&wb, call->callid)
359 && writebuf_string(&wb, info ?: "")
360 && writebuf_object(&wb, obj)) {
361 pthread_mutex_lock(&protows->mutex);
362 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
363 pthread_mutex_unlock(&protows->mutex);
373 int afb_proto_ws_call_fail(struct afb_proto_ws_call *call, const char *status, const char *info)
376 struct writebuf wb = { .count = 0 };
377 struct afb_proto_ws *protows = call->protows;
379 if (writebuf_char(&wb, CHAR_FOR_ANSWER_FAIL)
380 && writebuf_uint32(&wb, call->callid)
381 && writebuf_string(&wb, status)
382 && writebuf_string(&wb, info ? : "")) {
383 pthread_mutex_lock(&protows->mutex);
384 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
385 pthread_mutex_unlock(&protows->mutex);
395 int afb_proto_ws_call_subcall(struct afb_proto_ws_call *call, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure)
398 struct writebuf wb = { .count = 0 };
399 struct server_subcall *sc, *osc;
400 struct afb_proto_ws *protows = call->protows;
402 sc = malloc(sizeof *sc);
406 sc->callback = callback;
407 sc->closure = cb_closure;
409 pthread_mutex_lock(&protows->mutex);
410 sc->subcallid = ptr2id(sc);
413 osc = protows->subcalls;
414 while(osc && osc->subcallid != sc->subcallid)
417 sc->next = protows->subcalls;
418 protows->subcalls = sc;
419 pthread_mutex_unlock(&protows->mutex);
421 if (writebuf_char(&wb, CHAR_FOR_SUBCALL_CALL)
422 && writebuf_uint32(&wb, sc->subcallid)
423 && writebuf_uint32(&wb, call->callid)
424 && writebuf_string(&wb, api)
425 && writebuf_string(&wb, verb)
426 && writebuf_object(&wb, args)) {
427 pthread_mutex_lock(&protows->mutex);
428 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
429 pthread_mutex_unlock(&protows->mutex);
440 int afb_proto_ws_call_subscribe(struct afb_proto_ws_call *call, const char *event_name, int event_id)
443 struct writebuf wb = { .count = 0 };
444 struct afb_proto_ws *protows = call->protows;
446 if (writebuf_char(&wb, CHAR_FOR_EVT_SUBSCRIBE)
447 && writebuf_uint32(&wb, call->callid)
448 && writebuf_uint32(&wb, (uint32_t)event_id)
449 && writebuf_string(&wb, event_name)) {
450 pthread_mutex_lock(&protows->mutex);
451 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
452 pthread_mutex_unlock(&protows->mutex);
462 int afb_proto_ws_call_unsubscribe(struct afb_proto_ws_call *call, const char *event_name, int event_id)
465 struct writebuf wb = { .count = 0 };
466 struct afb_proto_ws *protows = call->protows;
468 if (writebuf_char(&wb, CHAR_FOR_EVT_UNSUBSCRIBE)
469 && writebuf_uint32(&wb, call->callid)
470 && writebuf_uint32(&wb, (uint32_t)event_id)
471 && writebuf_string(&wb, event_name)) {
472 pthread_mutex_lock(&protows->mutex);
473 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
474 pthread_mutex_unlock(&protows->mutex);
484 /******************* client part **********************************/
486 /* search a memorized call */
487 static struct client_call *client_call_search_locked(struct afb_proto_ws *protows, uint32_t callid)
489 struct client_call *call;
491 call = protows->calls;
492 while (call != NULL && call->callid != callid)
498 static struct client_call *client_call_search_unlocked(struct afb_proto_ws *protows, uint32_t callid)
500 struct client_call *result;
502 pthread_mutex_lock(&protows->mutex);
503 result = client_call_search_locked(protows, callid);
504 pthread_mutex_unlock(&protows->mutex);
508 /* free and release the memorizing call */
509 static void client_call_destroy(struct client_call *call)
511 struct client_call **prv;
512 struct afb_proto_ws *protows = call->protows;
514 pthread_mutex_lock(&protows->mutex);
515 prv = &call->protows->calls;
516 while (*prv != NULL) {
523 pthread_mutex_unlock(&protows->mutex);
527 /* get event data from the message */
528 static int client_msg_event_read(struct readbuf *rb, uint32_t *eventid, const char **name)
530 return readbuf_uint32(rb, eventid) && readbuf_string(rb, name, NULL);
533 /* get event from the message */
534 static int client_msg_call_get(struct afb_proto_ws *protows, struct readbuf *rb, struct client_call **call)
538 /* get event data from the message */
539 if (!readbuf_uint32(rb, &callid)) {
544 *call = client_call_search_unlocked(protows, callid);
553 static void client_on_event_create(struct afb_proto_ws *protows, struct readbuf *rb)
555 const char *event_name;
558 if (protows->client_itf->on_event_create && client_msg_event_read(rb, &event_id, &event_name))
559 protows->client_itf->on_event_create(protows->closure, event_name, (int)event_id);
562 /* removes an event */
563 static void client_on_event_remove(struct afb_proto_ws *protows, struct readbuf *rb)
565 const char *event_name;
568 if (protows->client_itf->on_event_remove && client_msg_event_read(rb, &event_id, &event_name))
569 protows->client_itf->on_event_remove(protows->closure, event_name, (int)event_id);
572 /* subscribes an event */
573 static void client_on_event_subscribe(struct afb_proto_ws *protows, struct readbuf *rb)
575 const char *event_name;
577 struct client_call *call;
579 if (protows->client_itf->on_event_subscribe && client_msg_call_get(protows, rb, &call) && client_msg_event_read(rb, &event_id, &event_name))
580 protows->client_itf->on_event_subscribe(protows->closure, call->request, event_name, (int)event_id);
583 /* unsubscribes an event */
584 static void client_on_event_unsubscribe(struct afb_proto_ws *protows, struct readbuf *rb)
586 const char *event_name;
588 struct client_call *call;
590 if (protows->client_itf->on_event_unsubscribe && client_msg_call_get(protows, rb, &call) && client_msg_event_read(rb, &event_id, &event_name))
591 protows->client_itf->on_event_unsubscribe(protows->closure, call->request, event_name, (int)event_id);
594 /* receives broadcasted events */
595 static void client_on_event_broadcast(struct afb_proto_ws *protows, struct readbuf *rb)
597 const char *event_name;
598 struct json_object *object;
600 if (protows->client_itf->on_event_broadcast && readbuf_string(rb, &event_name, NULL) && readbuf_object(rb, &object))
601 protows->client_itf->on_event_broadcast(protows->closure, event_name, object);
605 static void client_on_event_push(struct afb_proto_ws *protows, struct readbuf *rb)
607 const char *event_name;
609 struct json_object *object;
611 if (protows->client_itf->on_event_push && client_msg_event_read(rb, &event_id, &event_name) && readbuf_object(rb, &object))
612 protows->client_itf->on_event_push(protows->closure, event_name, (int)event_id, object);
615 static void client_on_reply_success(struct afb_proto_ws *protows, struct readbuf *rb)
617 struct client_call *call;
618 struct json_object *object;
621 if (!client_msg_call_get(protows, rb, &call))
624 if (readbuf_string(rb, &info, NULL) && readbuf_object(rb, &object)) {
625 protows->client_itf->on_reply_success(protows->closure, call->request, object, info);
627 protows->client_itf->on_reply_fail(protows->closure, call->request, "proto-error", "can't process success");
629 client_call_destroy(call);
632 static void client_on_reply_fail(struct afb_proto_ws *protows, struct readbuf *rb)
634 struct client_call *call;
635 const char *info, *status;
637 if (!client_msg_call_get(protows, rb, &call))
641 if (readbuf_string(rb, &status, NULL) && readbuf_string(rb, &info, NULL)) {
642 protows->client_itf->on_reply_fail(protows->closure, call->request, status, info);
644 protows->client_itf->on_reply_fail(protows->closure, call->request, "proto-error", "can't process fail");
646 client_call_destroy(call);
649 /* send a subcall reply */
650 static int client_send_subcall_reply(struct afb_proto_ws *protows, uint32_t subcallid, int status, json_object *object)
652 struct writebuf wb = { .count = 0 };
653 char ie = status < 0;
656 if (writebuf_char(&wb, CHAR_FOR_SUBCALL_REPLY)
657 && writebuf_uint32(&wb, subcallid)
658 && writebuf_char(&wb, ie)
659 && writebuf_object(&wb, object)) {
660 pthread_mutex_lock(&protows->mutex);
661 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
662 pthread_mutex_unlock(&protows->mutex);
669 /* callback for subcall reply */
670 int afb_proto_ws_subcall_reply(struct afb_proto_ws_subcall *subcall, int status, struct json_object *result)
672 int rc = client_send_subcall_reply(subcall->protows, subcall->subcallid, status, result);
673 afb_proto_ws_unref(subcall->protows);
674 free(subcall->buffer);
679 /* received a subcall request */
680 static void client_on_subcall(struct afb_proto_ws *protows, struct readbuf *rb)
682 struct afb_proto_ws_subcall *subcall;
683 struct client_call *call;
684 const char *api, *verb;
686 struct json_object *object;
688 /* get the subcallid */
689 if (!readbuf_uint32(rb, &subcallid))
692 /* if not expected drop it */
693 if (!protows->client_itf->on_subcall)
696 /* retrieve the message data */
697 if (!client_msg_call_get(protows, rb, &call))
700 /* allocation of the subcall */
701 subcall = malloc(sizeof *subcall);
706 if (readbuf_string(rb, &api, NULL)
707 && readbuf_string(rb, &verb, NULL)
708 && readbuf_object(rb, &object)) {
709 afb_proto_ws_addref(protows);
710 subcall->protows = protows;
711 subcall->subcallid = subcallid;
712 subcall->buffer = rb->base;
714 protows->client_itf->on_subcall(protows->closure, subcall, call->request, api, verb, object);
719 client_send_subcall_reply(protows, subcallid, 1, NULL);
722 static void client_on_description(struct afb_proto_ws *protows, struct readbuf *rb)
725 struct client_describe *desc, **prv;
726 struct json_object *object;
728 if (readbuf_uint32(rb, &descid)) {
729 pthread_mutex_lock(&protows->mutex);
730 prv = &protows->describes;
731 while ((desc = *prv) && desc->descid != descid)
734 pthread_mutex_unlock(&protows->mutex);
737 pthread_mutex_unlock(&protows->mutex);
738 if (!readbuf_object(rb, &object))
740 desc->callback(desc->closure, object);
746 /* callback when receiving binary data */
747 static void client_on_binary_job(int sig, void *closure)
749 struct binary *binary = closure;
752 switch (*binary->rb.head++) {
753 case CHAR_FOR_ANSWER_SUCCESS: /* success */
754 client_on_reply_success(binary->protows, &binary->rb);
756 case CHAR_FOR_ANSWER_FAIL: /* fail */
757 client_on_reply_fail(binary->protows, &binary->rb);
759 case CHAR_FOR_EVT_BROADCAST: /* broadcast */
760 client_on_event_broadcast(binary->protows, &binary->rb);
762 case CHAR_FOR_EVT_ADD: /* creates the event */
763 client_on_event_create(binary->protows, &binary->rb);
765 case CHAR_FOR_EVT_DEL: /* removes the event */
766 client_on_event_remove(binary->protows, &binary->rb);
768 case CHAR_FOR_EVT_PUSH: /* pushs the event */
769 client_on_event_push(binary->protows, &binary->rb);
771 case CHAR_FOR_EVT_SUBSCRIBE: /* subscribe event for a request */
772 client_on_event_subscribe(binary->protows, &binary->rb);
774 case CHAR_FOR_EVT_UNSUBSCRIBE: /* unsubscribe event for a request */
775 client_on_event_unsubscribe(binary->protows, &binary->rb);
777 case CHAR_FOR_SUBCALL_CALL: /* subcall */
778 client_on_subcall(binary->protows, &binary->rb);
780 case CHAR_FOR_DESCRIPTION: /* description */
781 client_on_description(binary->protows, &binary->rb);
783 default: /* unexpected message */
784 /* TODO: close the connection */
788 free(binary->rb.base);
792 /* callback when receiving binary data */
793 static void client_on_binary(void *closure, char *data, size_t size)
796 struct binary *binary;
799 binary = malloc(sizeof *binary);
803 binary->protows = closure;
804 binary->rb.base = data;
805 binary->rb.head = data;
806 binary->rb.end = data + size;
807 rc = jobs_queue(NULL, 0, client_on_binary_job, binary);
816 int afb_proto_ws_client_call(
817 struct afb_proto_ws *protows,
819 struct json_object *args,
820 const char *sessionid,
825 struct client_call *call;
826 struct writebuf wb = { .count = 0 };
828 /* allocate call data */
829 call = malloc(sizeof *call);
834 call->request = request;
837 pthread_mutex_lock(&protows->mutex);
838 call->callid = ptr2id(call);
839 while(client_call_search_locked(protows, call->callid) != NULL)
841 call->protows = protows;
842 call->next = protows->calls;
843 protows->calls = call;
844 pthread_mutex_unlock(&protows->mutex);
846 /* creates the call message */
847 if (!writebuf_char(&wb, CHAR_FOR_CALL)
848 || !writebuf_uint32(&wb, call->callid)
849 || !writebuf_string(&wb, verb)
850 || !writebuf_string(&wb, sessionid)
851 || !writebuf_object(&wb, args)) {
857 pthread_mutex_lock(&protows->mutex);
858 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
859 pthread_mutex_unlock(&protows->mutex);
866 client_call_destroy(call);
871 /* get the description */
872 int afb_proto_ws_client_describe(struct afb_proto_ws *protows, void (*callback)(void*, struct json_object*), void *closure)
874 struct client_describe *desc, *d;
875 struct writebuf wb = { .count = 0 };
877 desc = malloc(sizeof *desc);
883 /* fill in stack the description of the task */
884 pthread_mutex_lock(&protows->mutex);
885 desc->descid = ptr2id(desc);
886 d = protows->describes;
888 if (d->descid != desc->descid)
892 d = protows->describes;
895 desc->callback = callback;
896 desc->closure = closure;
897 desc->protows = protows;
898 desc->next = protows->describes;
899 protows->describes = desc;
902 if (writebuf_char(&wb, CHAR_FOR_DESCRIBE)
903 && writebuf_uint32(&wb, desc->descid)
904 && afb_ws_binary_v(protows->ws, wb.iovec, wb.count) >= 0) {
905 pthread_mutex_unlock(&protows->mutex);
909 d = protows->describes;
911 protows->describes = desc->next;
913 while(d && d->next != desc)
916 d->next = desc->next;
918 pthread_mutex_unlock(&protows->mutex);
921 /* TODO? callback(closure, NULL); */
925 /******************* client description part for server *****************************/
927 /* on call, propagate it to the ws service */
928 static void server_on_call(struct afb_proto_ws *protows, struct readbuf *rb)
930 struct afb_proto_ws_call *call;
931 const char *uuid, *verb;
934 struct json_object *object;
936 afb_proto_ws_addref(protows);
938 /* reads the call message data */
939 if (!readbuf_uint32(rb, &callid)
940 || !readbuf_string(rb, &verb, &lenverb)
941 || !readbuf_string(rb, &uuid, NULL)
942 || !readbuf_object(rb, &object))
945 /* create the request */
946 call = malloc(sizeof *call);
950 call->protows = protows;
951 call->callid = callid;
953 call->buffer = rb->base;
954 rb->base = NULL; /* don't free the buffer */
956 protows->server_itf->on_call(protows->closure, call, verb, object, uuid);
960 json_object_put(object);
963 afb_proto_ws_unref(protows);
966 /* on subcall reply */
967 static void server_on_subcall_reply(struct afb_proto_ws *protows, struct readbuf *rb)
971 struct json_object *object;
972 struct server_subcall *sc, **psc;
974 /* reads the call message data */
975 if (!readbuf_uint32(rb, &subcallid)
976 || !readbuf_char(rb, &ie)
977 || !readbuf_object(rb, &object)) {
978 /* TODO bad protocol */
982 /* search the subcall and unlink it */
983 pthread_mutex_lock(&protows->mutex);
984 psc = &protows->subcalls;
985 while ((sc = *psc) && sc->subcallid != subcallid)
988 pthread_mutex_unlock(&protows->mutex);
989 json_object_put(object);
990 /* TODO subcall not found */
993 pthread_mutex_unlock(&protows->mutex);
994 sc->callback(sc->closure, -(int)ie, object);
999 static int server_send_description(struct afb_proto_ws *protows, uint32_t descid, struct json_object *descobj)
1002 struct writebuf wb = { .count = 0 };
1004 if (writebuf_char(&wb, CHAR_FOR_DESCRIPTION)
1005 && writebuf_uint32(&wb, descid)
1006 && writebuf_object(&wb, descobj)) {
1007 pthread_mutex_lock(&protows->mutex);
1008 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
1009 pthread_mutex_unlock(&protows->mutex);
1016 int afb_proto_ws_describe_put(struct afb_proto_ws_describe *describe, struct json_object *description)
1018 int rc = server_send_description(describe->protows, describe->descid, description);
1019 afb_proto_ws_unref(describe->protows);
1024 /* on describe, propagate it to the ws service */
1025 static void server_on_describe(struct afb_proto_ws *protows, struct readbuf *rb)
1028 struct afb_proto_ws_describe *desc;
1030 /* reads the descid */
1031 if (readbuf_uint32(rb, &descid)) {
1032 if (protows->server_itf->on_describe) {
1033 /* create asynchronous job */
1034 desc = malloc(sizeof *desc);
1036 desc->descid = descid;
1037 desc->protows = protows;
1038 afb_proto_ws_addref(protows);
1039 protows->server_itf->on_describe(protows->closure, desc);
1043 server_send_description(protows, descid, NULL);
1047 /* callback when receiving binary data */
1048 static void server_on_binary_job(int sig, void *closure)
1050 struct binary *binary = closure;
1053 switch (*binary->rb.head++) {
1055 server_on_call(binary->protows, &binary->rb);
1057 case CHAR_FOR_SUBCALL_REPLY:
1058 server_on_subcall_reply(binary->protows, &binary->rb);
1060 case CHAR_FOR_DESCRIBE:
1061 server_on_describe(binary->protows, &binary->rb);
1063 default: /* unexpected message */
1064 /* TODO: close the connection */
1068 free(binary->rb.base);
1072 static void server_on_binary(void *closure, char *data, size_t size)
1075 struct binary *binary;
1078 binary = malloc(sizeof *binary);
1082 binary->protows = closure;
1083 binary->rb.base = data;
1084 binary->rb.head = data;
1085 binary->rb.end = data + size;
1086 rc = jobs_queue(NULL, 0, server_on_binary_job, binary);
1095 /******************* server part: manage events **********************************/
1097 static int server_event_send(struct afb_proto_ws *protows, char order, const char *event_name, int event_id, struct json_object *data)
1099 struct writebuf wb = { .count = 0 };
1102 if (writebuf_char(&wb, order)
1103 && (order == CHAR_FOR_EVT_BROADCAST || writebuf_uint32(&wb, event_id))
1104 && writebuf_string(&wb, event_name)
1105 && (order == CHAR_FOR_EVT_ADD || order == CHAR_FOR_EVT_DEL || writebuf_object(&wb, data))) {
1106 pthread_mutex_lock(&protows->mutex);
1107 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
1108 pthread_mutex_unlock(&protows->mutex);
1115 int afb_proto_ws_server_event_create(struct afb_proto_ws *protows, const char *event_name, int event_id)
1117 return server_event_send(protows, CHAR_FOR_EVT_ADD, event_name, event_id, NULL);
1120 int afb_proto_ws_server_event_remove(struct afb_proto_ws *protows, const char *event_name, int event_id)
1122 return server_event_send(protows, CHAR_FOR_EVT_DEL, event_name, event_id, NULL);
1125 int afb_proto_ws_server_event_push(struct afb_proto_ws *protows, const char *event_name, int event_id, struct json_object *data)
1127 return server_event_send(protows, CHAR_FOR_EVT_PUSH, event_name, event_id, data);
1130 int afb_proto_ws_server_event_broadcast(struct afb_proto_ws *protows, const char *event_name, struct json_object *data)
1132 return server_event_send(protows, CHAR_FOR_EVT_BROADCAST, event_name, 0, data);
1135 /*****************************************************/
1137 /* callback when receiving a hangup */
1138 static void on_hangup(void *closure)
1140 struct afb_proto_ws *protows = closure;
1141 struct server_subcall *sc, *nsc;
1142 struct client_describe *cd, *ncd;
1144 nsc = protows->subcalls;
1148 sc->callback(sc->closure, 1, NULL);
1152 ncd = protows->describes;
1156 cd->callback(cd->closure, NULL);
1160 if (protows->fd >= 0) {
1163 if (protows->on_hangup)
1164 protows->on_hangup(protows->closure);
1168 /*****************************************************/
1170 static const struct afb_ws_itf proto_ws_client_ws_itf =
1174 .on_binary = client_on_binary,
1176 .on_hangup = on_hangup
1179 static const struct afb_ws_itf server_ws_itf =
1183 .on_binary = server_on_binary,
1185 .on_hangup = on_hangup
1188 /*****************************************************/
1190 static struct afb_proto_ws *afb_proto_ws_create(struct sd_event *eloop, int fd, const struct afb_proto_ws_server_itf *itfs, const struct afb_proto_ws_client_itf *itfc, void *closure, const struct afb_ws_itf *itf)
1192 struct afb_proto_ws *protows;
1194 protows = calloc(1, sizeof *protows);
1195 if (protows == NULL)
1198 fcntl(fd, F_SETFD, FD_CLOEXEC);
1199 fcntl(fd, F_SETFL, O_NONBLOCK);
1200 protows->ws = afb_ws_create(eloop, fd, itf, protows);
1201 if (protows->ws != NULL) {
1203 protows->refcount = 1;
1204 protows->subcalls = NULL;
1205 protows->closure = closure;
1206 protows->server_itf = itfs;
1207 protows->client_itf = itfc;
1208 pthread_mutex_init(&protows->mutex, NULL);
1216 struct afb_proto_ws *afb_proto_ws_create_client(struct sd_event *eloop, int fd, const struct afb_proto_ws_client_itf *itf, void *closure)
1218 return afb_proto_ws_create(eloop, fd, NULL, itf, closure, &proto_ws_client_ws_itf);
1221 struct afb_proto_ws *afb_proto_ws_create_server(struct sd_event *eloop, int fd, const struct afb_proto_ws_server_itf *itf, void *closure)
1223 return afb_proto_ws_create(eloop, fd, itf, NULL, closure, &server_ws_itf);
1226 void afb_proto_ws_unref(struct afb_proto_ws *protows)
1228 if (!__atomic_sub_fetch(&protows->refcount, 1, __ATOMIC_RELAXED)) {
1229 afb_proto_ws_hangup(protows);
1230 afb_ws_destroy(protows->ws);
1231 pthread_mutex_destroy(&protows->mutex);
1236 void afb_proto_ws_addref(struct afb_proto_ws *protows)
1238 __atomic_add_fetch(&protows->refcount, 1, __ATOMIC_RELAXED);
1241 int afb_proto_ws_is_client(struct afb_proto_ws *protows)
1243 return !!protows->client_itf;
1246 int afb_proto_ws_is_server(struct afb_proto_ws *protows)
1248 return !!protows->server_itf;
1251 void afb_proto_ws_hangup(struct afb_proto_ws *protows)
1253 afb_ws_hangup(protows->ws);
1256 void afb_proto_ws_on_hangup(struct afb_proto_ws *protows, void (*on_hangup)(void *closure))
1258 protows->on_hangup = on_hangup;