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"
45 /******** implementation of internal binder protocol per api **************/
48 This protocol is asymetric: there is a client and a server
50 The client can require the following actions:
56 The server must reply to the previous actions by
58 - answering success or failure of the call
60 - answering the required description
62 The server can also within the context of a call
66 - subscribe or unsubscribe an event
68 For the purpose of handling events the server can:
70 - create/destroy an event
72 - push or brodcast data as an event
75 /************** constants for protocol definition *************************/
77 #define CHAR_FOR_CALL 'C'
78 #define CHAR_FOR_ANSWER_SUCCESS 'T'
79 #define CHAR_FOR_ANSWER_FAIL 'F'
80 #define CHAR_FOR_EVT_BROADCAST '*'
81 #define CHAR_FOR_EVT_ADD '+'
82 #define CHAR_FOR_EVT_DEL '-'
83 #define CHAR_FOR_EVT_PUSH '!'
84 #define CHAR_FOR_EVT_SUBSCRIBE 'S'
85 #define CHAR_FOR_EVT_UNSUBSCRIBE 'U'
86 #define CHAR_FOR_SUBCALL_CALL 'B'
87 #define CHAR_FOR_SUBCALL_REPLY 'R'
88 #define CHAR_FOR_DESCRIBE 'D'
89 #define CHAR_FOR_DESCRIPTION 'd'
91 /******************* handling subcalls *****************************/
94 * Structure on server side for recording pending
99 struct server_subcall *next; /**< next subcall for the client */
100 uint32_t subcallid; /**< the subcallid */
101 void (*callback)(void*, int, struct json_object*); /**< callback on completion */
102 void *closure; /**< closure of the callback */
106 * Structure for sending back replies on client side
108 struct afb_proto_ws_subcall
110 struct afb_proto_ws *protows; /**< proto descriptor */
112 uint32_t subcallid; /**< subcallid for the reply */
116 * structure for recording calls on client side
119 struct client_call *next; /* the next call */
120 struct afb_proto_ws *protows; /* the proto_ws */
122 uint32_t callid; /* the message identifier */
126 * structure for a ws request
128 struct afb_proto_ws_call {
129 struct client_call *next; /* the next call */
130 struct afb_proto_ws *protows; /* the client of the request */
131 uint32_t refcount; /* reference count */
132 uint32_t callid; /* the incoming request callid */
133 char *buffer; /* the incoming buffer */
137 * structure for recording describe requests
139 struct client_describe
141 struct client_describe *next;
142 struct afb_proto_ws *protows;
143 void (*callback)(void*, struct json_object*);
149 * structure for jobs of describing
151 struct afb_proto_ws_describe
153 struct afb_proto_ws *protows;
157 /******************* proto description for client or servers ******************/
161 /* count of references */
164 /* file descriptor */
167 /* resource control */
168 pthread_mutex_t mutex;
173 /* the client closure */
176 /* the client side interface */
177 const struct afb_proto_ws_client_itf *client_itf;
179 /* the server side interface */
180 const struct afb_proto_ws_server_itf *server_itf;
182 /* emitted calls (client side) */
183 struct client_call *calls;
185 /* pending subcalls (server side) */
186 struct server_subcall *subcalls;
188 /* pending description (client side) */
189 struct client_describe *describes;
191 /* on hangup callback */
192 void (*on_hangup)(void *closure);
195 /******************* streaming objects **********************************/
197 #define WRITEBUF_COUNT_MAX 32
200 struct iovec iovec[WRITEBUF_COUNT_MAX];
201 uint32_t uints[WRITEBUF_COUNT_MAX];
207 char *base, *head, *end;
212 struct afb_proto_ws *protows;
216 /******************* common useful tools **********************************/
219 * translate a pointer to some integer
220 * @param ptr the pointer to translate
223 static inline uint32_t ptr2id(void *ptr)
225 return (uint32_t)(((intptr_t)ptr) >> 6);
228 /******************* serialisation part **********************************/
230 static char *readbuf_get(struct readbuf *rb, uint32_t length)
232 char *before = rb->head;
233 char *after = before + length;
240 static int readbuf_char(struct readbuf *rb, char *value)
242 if (rb->head >= rb->end)
244 *value = *rb->head++;
248 static int readbuf_uint32(struct readbuf *rb, uint32_t *value)
250 char *after = rb->head + sizeof *value;
253 memcpy(value, rb->head, sizeof *value);
255 *value = le32toh(*value);
259 static int readbuf_string(struct readbuf *rb, const char **value, size_t *length)
262 if (!readbuf_uint32(rb, &len) || !len)
265 *length = (size_t)(len - 1);
266 return (*value = readbuf_get(rb, len)) != NULL && rb->head[-1] == 0;
269 static int readbuf_object(struct readbuf *rb, struct json_object **object)
272 struct json_object *o;
273 int rc = readbuf_string(rb, &string, NULL);
275 o = json_tokener_parse(string);
276 if (o == NULL && strcmp(string, "null"))
277 o = json_object_new_string(string);
283 static int writebuf_put(struct writebuf *wb, const void *value, size_t length)
286 if (i == WRITEBUF_COUNT_MAX)
288 wb->iovec[i].iov_base = (void*)value;
289 wb->iovec[i].iov_len = length;
294 static int writebuf_char(struct writebuf *wb, char value)
297 if (i == WRITEBUF_COUNT_MAX)
299 *(char*)&wb->uints[i] = value;
300 wb->iovec[i].iov_base = &wb->uints[i];
301 wb->iovec[i].iov_len = 1;
306 static int writebuf_uint32(struct writebuf *wb, uint32_t value)
309 if (i == WRITEBUF_COUNT_MAX)
311 wb->uints[i] = htole32(value);
312 wb->iovec[i].iov_base = &wb->uints[i];
313 wb->iovec[i].iov_len = sizeof wb->uints[i];
318 static int writebuf_string_length(struct writebuf *wb, const char *value, size_t length)
320 uint32_t len = (uint32_t)++length;
321 return (size_t)len == length && len && writebuf_uint32(wb, len) && writebuf_put(wb, value, length);
324 static int writebuf_string(struct writebuf *wb, const char *value)
326 return writebuf_string_length(wb, value, strlen(value));
329 static int writebuf_object(struct writebuf *wb, struct json_object *object)
331 const char *string = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
332 return string != NULL && writebuf_string(wb, string);
335 /******************* ws request part for server *****************/
337 void afb_proto_ws_call_addref(struct afb_proto_ws_call *call)
339 __atomic_add_fetch(&call->refcount, 1, __ATOMIC_RELAXED);
342 void afb_proto_ws_call_unref(struct afb_proto_ws_call *call)
344 if (__atomic_sub_fetch(&call->refcount, 1, __ATOMIC_RELAXED))
347 afb_proto_ws_unref(call->protows);
352 int afb_proto_ws_call_success(struct afb_proto_ws_call *call, struct json_object *obj, const char *info)
355 struct writebuf wb = { .count = 0 };
356 struct afb_proto_ws *protows = call->protows;
358 if (writebuf_char(&wb, CHAR_FOR_ANSWER_SUCCESS)
359 && writebuf_uint32(&wb, call->callid)
360 && writebuf_string(&wb, info ?: "")
361 && writebuf_object(&wb, obj)) {
362 pthread_mutex_lock(&protows->mutex);
363 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
364 pthread_mutex_unlock(&protows->mutex);
374 int afb_proto_ws_call_fail(struct afb_proto_ws_call *call, const char *status, const char *info)
377 struct writebuf wb = { .count = 0 };
378 struct afb_proto_ws *protows = call->protows;
380 if (writebuf_char(&wb, CHAR_FOR_ANSWER_FAIL)
381 && writebuf_uint32(&wb, call->callid)
382 && writebuf_string(&wb, status)
383 && writebuf_string(&wb, info ? : "")) {
384 pthread_mutex_lock(&protows->mutex);
385 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
386 pthread_mutex_unlock(&protows->mutex);
396 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)
399 struct writebuf wb = { .count = 0 };
400 struct server_subcall *sc, *osc;
401 struct afb_proto_ws *protows = call->protows;
403 sc = malloc(sizeof *sc);
407 sc->callback = callback;
408 sc->closure = cb_closure;
410 pthread_mutex_lock(&protows->mutex);
411 sc->subcallid = ptr2id(sc);
414 osc = protows->subcalls;
415 while(osc && osc->subcallid != sc->subcallid)
418 sc->next = protows->subcalls;
419 protows->subcalls = sc;
420 pthread_mutex_unlock(&protows->mutex);
422 if (writebuf_char(&wb, CHAR_FOR_SUBCALL_CALL)
423 && writebuf_uint32(&wb, sc->subcallid)
424 && writebuf_uint32(&wb, call->callid)
425 && writebuf_string(&wb, api)
426 && writebuf_string(&wb, verb)
427 && writebuf_object(&wb, args)) {
428 pthread_mutex_lock(&protows->mutex);
429 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
430 pthread_mutex_unlock(&protows->mutex);
441 int afb_proto_ws_call_subscribe(struct afb_proto_ws_call *call, const char *event_name, int event_id)
444 struct writebuf wb = { .count = 0 };
445 struct afb_proto_ws *protows = call->protows;
447 if (writebuf_char(&wb, CHAR_FOR_EVT_SUBSCRIBE)
448 && writebuf_uint32(&wb, call->callid)
449 && writebuf_uint32(&wb, (uint32_t)event_id)
450 && writebuf_string(&wb, event_name)) {
451 pthread_mutex_lock(&protows->mutex);
452 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
453 pthread_mutex_unlock(&protows->mutex);
463 int afb_proto_ws_call_unsubscribe(struct afb_proto_ws_call *call, const char *event_name, int event_id)
466 struct writebuf wb = { .count = 0 };
467 struct afb_proto_ws *protows = call->protows;
469 if (writebuf_char(&wb, CHAR_FOR_EVT_UNSUBSCRIBE)
470 && writebuf_uint32(&wb, call->callid)
471 && writebuf_uint32(&wb, (uint32_t)event_id)
472 && writebuf_string(&wb, event_name)) {
473 pthread_mutex_lock(&protows->mutex);
474 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
475 pthread_mutex_unlock(&protows->mutex);
485 /******************* client part **********************************/
487 /* search a memorized call */
488 static struct client_call *client_call_search_locked(struct afb_proto_ws *protows, uint32_t callid)
490 struct client_call *call;
492 call = protows->calls;
493 while (call != NULL && call->callid != callid)
499 static struct client_call *client_call_search_unlocked(struct afb_proto_ws *protows, uint32_t callid)
501 struct client_call *result;
503 pthread_mutex_lock(&protows->mutex);
504 result = client_call_search_locked(protows, callid);
505 pthread_mutex_unlock(&protows->mutex);
509 /* free and release the memorizing call */
510 static void client_call_destroy(struct client_call *call)
512 struct client_call **prv;
513 struct afb_proto_ws *protows = call->protows;
515 pthread_mutex_lock(&protows->mutex);
516 prv = &call->protows->calls;
517 while (*prv != NULL) {
524 pthread_mutex_unlock(&protows->mutex);
528 /* get event data from the message */
529 static int client_msg_event_read(struct readbuf *rb, uint32_t *eventid, const char **name)
531 return readbuf_uint32(rb, eventid) && readbuf_string(rb, name, NULL);
534 /* get event from the message */
535 static int client_msg_call_get(struct afb_proto_ws *protows, struct readbuf *rb, struct client_call **call)
539 /* get event data from the message */
540 if (!readbuf_uint32(rb, &callid)) {
545 *call = client_call_search_unlocked(protows, callid);
554 static void client_on_event_create(struct afb_proto_ws *protows, struct readbuf *rb)
556 const char *event_name;
559 if (protows->client_itf->on_event_create && client_msg_event_read(rb, &event_id, &event_name))
560 protows->client_itf->on_event_create(protows->closure, event_name, (int)event_id);
563 /* removes an event */
564 static void client_on_event_remove(struct afb_proto_ws *protows, struct readbuf *rb)
566 const char *event_name;
569 if (protows->client_itf->on_event_remove && client_msg_event_read(rb, &event_id, &event_name))
570 protows->client_itf->on_event_remove(protows->closure, event_name, (int)event_id);
573 /* subscribes an event */
574 static void client_on_event_subscribe(struct afb_proto_ws *protows, struct readbuf *rb)
576 const char *event_name;
578 struct client_call *call;
580 if (protows->client_itf->on_event_subscribe && client_msg_call_get(protows, rb, &call) && client_msg_event_read(rb, &event_id, &event_name))
581 protows->client_itf->on_event_subscribe(protows->closure, call->request, event_name, (int)event_id);
584 /* unsubscribes an event */
585 static void client_on_event_unsubscribe(struct afb_proto_ws *protows, struct readbuf *rb)
587 const char *event_name;
589 struct client_call *call;
591 if (protows->client_itf->on_event_unsubscribe && client_msg_call_get(protows, rb, &call) && client_msg_event_read(rb, &event_id, &event_name))
592 protows->client_itf->on_event_unsubscribe(protows->closure, call->request, event_name, (int)event_id);
595 /* receives broadcasted events */
596 static void client_on_event_broadcast(struct afb_proto_ws *protows, struct readbuf *rb)
598 const char *event_name;
599 struct json_object *object;
601 if (protows->client_itf->on_event_broadcast && readbuf_string(rb, &event_name, NULL) && readbuf_object(rb, &object))
602 protows->client_itf->on_event_broadcast(protows->closure, event_name, object);
606 static void client_on_event_push(struct afb_proto_ws *protows, struct readbuf *rb)
608 const char *event_name;
610 struct json_object *object;
612 if (protows->client_itf->on_event_push && client_msg_event_read(rb, &event_id, &event_name) && readbuf_object(rb, &object))
613 protows->client_itf->on_event_push(protows->closure, event_name, (int)event_id, object);
616 static void client_on_reply_success(struct afb_proto_ws *protows, struct readbuf *rb)
618 struct client_call *call;
619 struct json_object *object;
622 if (!client_msg_call_get(protows, rb, &call))
625 if (readbuf_string(rb, &info, NULL) && readbuf_object(rb, &object)) {
626 protows->client_itf->on_reply_success(protows->closure, call->request, object, info);
628 protows->client_itf->on_reply_fail(protows->closure, call->request, "proto-error", "can't process success");
630 client_call_destroy(call);
633 static void client_on_reply_fail(struct afb_proto_ws *protows, struct readbuf *rb)
635 struct client_call *call;
636 const char *info, *status;
638 if (!client_msg_call_get(protows, rb, &call))
642 if (readbuf_string(rb, &status, NULL) && readbuf_string(rb, &info, NULL)) {
643 protows->client_itf->on_reply_fail(protows->closure, call->request, status, info);
645 protows->client_itf->on_reply_fail(protows->closure, call->request, "proto-error", "can't process fail");
647 client_call_destroy(call);
650 /* send a subcall reply */
651 static int client_send_subcall_reply(struct afb_proto_ws *protows, uint32_t subcallid, int status, json_object *object)
653 struct writebuf wb = { .count = 0 };
654 char ie = status < 0;
657 if (writebuf_char(&wb, CHAR_FOR_SUBCALL_REPLY)
658 && writebuf_uint32(&wb, subcallid)
659 && writebuf_char(&wb, ie)
660 && writebuf_object(&wb, object)) {
661 pthread_mutex_lock(&protows->mutex);
662 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
663 pthread_mutex_unlock(&protows->mutex);
670 /* callback for subcall reply */
671 int afb_proto_ws_subcall_reply(struct afb_proto_ws_subcall *subcall, int status, struct json_object *result)
673 int rc = client_send_subcall_reply(subcall->protows, subcall->subcallid, status, result);
674 afb_proto_ws_unref(subcall->protows);
675 free(subcall->buffer);
680 /* received a subcall request */
681 static void client_on_subcall(struct afb_proto_ws *protows, struct readbuf *rb)
683 struct afb_proto_ws_subcall *subcall;
684 struct client_call *call;
685 const char *api, *verb;
687 struct json_object *object;
689 /* get the subcallid */
690 if (!readbuf_uint32(rb, &subcallid))
693 /* if not expected drop it */
694 if (!protows->client_itf->on_subcall)
697 /* retrieve the message data */
698 if (!client_msg_call_get(protows, rb, &call))
701 /* allocation of the subcall */
702 subcall = malloc(sizeof *subcall);
707 if (readbuf_string(rb, &api, NULL)
708 && readbuf_string(rb, &verb, NULL)
709 && readbuf_object(rb, &object)) {
710 afb_proto_ws_addref(protows);
711 subcall->protows = protows;
712 subcall->subcallid = subcallid;
713 subcall->buffer = rb->base;
715 protows->client_itf->on_subcall(protows->closure, subcall, call->request, api, verb, object);
720 client_send_subcall_reply(protows, subcallid, 1, NULL);
723 static void client_on_description(struct afb_proto_ws *protows, struct readbuf *rb)
726 struct client_describe *desc, **prv;
727 struct json_object *object;
729 if (readbuf_uint32(rb, &descid)) {
730 pthread_mutex_lock(&protows->mutex);
731 prv = &protows->describes;
732 while ((desc = *prv) && desc->descid != descid)
735 pthread_mutex_unlock(&protows->mutex);
738 pthread_mutex_unlock(&protows->mutex);
739 if (!readbuf_object(rb, &object))
741 desc->callback(desc->closure, object);
747 /* callback when receiving binary data */
748 static void client_on_binary_job(int sig, void *closure)
750 struct binary *binary = closure;
753 switch (*binary->rb.head++) {
754 case CHAR_FOR_ANSWER_SUCCESS: /* success */
755 client_on_reply_success(binary->protows, &binary->rb);
757 case CHAR_FOR_ANSWER_FAIL: /* fail */
758 client_on_reply_fail(binary->protows, &binary->rb);
760 case CHAR_FOR_EVT_BROADCAST: /* broadcast */
761 client_on_event_broadcast(binary->protows, &binary->rb);
763 case CHAR_FOR_EVT_ADD: /* creates the event */
764 client_on_event_create(binary->protows, &binary->rb);
766 case CHAR_FOR_EVT_DEL: /* removes the event */
767 client_on_event_remove(binary->protows, &binary->rb);
769 case CHAR_FOR_EVT_PUSH: /* pushs the event */
770 client_on_event_push(binary->protows, &binary->rb);
772 case CHAR_FOR_EVT_SUBSCRIBE: /* subscribe event for a request */
773 client_on_event_subscribe(binary->protows, &binary->rb);
775 case CHAR_FOR_EVT_UNSUBSCRIBE: /* unsubscribe event for a request */
776 client_on_event_unsubscribe(binary->protows, &binary->rb);
778 case CHAR_FOR_SUBCALL_CALL: /* subcall */
779 client_on_subcall(binary->protows, &binary->rb);
781 case CHAR_FOR_DESCRIPTION: /* description */
782 client_on_description(binary->protows, &binary->rb);
784 default: /* unexpected message */
785 /* TODO: close the connection */
789 free(binary->rb.base);
793 /* callback when receiving binary data */
794 static void client_on_binary(void *closure, char *data, size_t size)
797 struct binary *binary;
800 binary = malloc(sizeof *binary);
804 binary->protows = closure;
805 binary->rb.base = data;
806 binary->rb.head = data;
807 binary->rb.end = data + size;
808 rc = jobs_queue(NULL, 0, client_on_binary_job, binary);
817 int afb_proto_ws_client_call(
818 struct afb_proto_ws *protows,
820 struct json_object *args,
821 const char *sessionid,
826 struct client_call *call;
827 struct writebuf wb = { .count = 0 };
829 /* allocate call data */
830 call = malloc(sizeof *call);
835 call->request = request;
838 pthread_mutex_lock(&protows->mutex);
839 call->callid = ptr2id(call);
840 while(client_call_search_locked(protows, call->callid) != NULL)
842 call->protows = protows;
843 call->next = protows->calls;
844 protows->calls = call;
845 pthread_mutex_unlock(&protows->mutex);
847 /* creates the call message */
848 if (!writebuf_char(&wb, CHAR_FOR_CALL)
849 || !writebuf_uint32(&wb, call->callid)
850 || !writebuf_string(&wb, verb)
851 || !writebuf_string(&wb, sessionid)
852 || !writebuf_object(&wb, args)) {
858 pthread_mutex_lock(&protows->mutex);
859 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
860 pthread_mutex_unlock(&protows->mutex);
867 client_call_destroy(call);
872 /* get the description */
873 int afb_proto_ws_client_describe(struct afb_proto_ws *protows, void (*callback)(void*, struct json_object*), void *closure)
875 struct client_describe *desc, *d;
876 struct writebuf wb = { .count = 0 };
878 desc = malloc(sizeof *desc);
884 /* fill in stack the description of the task */
885 pthread_mutex_lock(&protows->mutex);
886 desc->descid = ptr2id(desc);
887 d = protows->describes;
889 if (d->descid != desc->descid)
893 d = protows->describes;
896 desc->callback = callback;
897 desc->closure = closure;
898 desc->protows = protows;
899 desc->next = protows->describes;
900 protows->describes = desc;
903 if (writebuf_char(&wb, CHAR_FOR_DESCRIBE)
904 && writebuf_uint32(&wb, desc->descid)
905 && afb_ws_binary_v(protows->ws, wb.iovec, wb.count) >= 0) {
906 pthread_mutex_unlock(&protows->mutex);
910 d = protows->describes;
912 protows->describes = desc->next;
914 while(d && d->next != desc)
917 d->next = desc->next;
919 pthread_mutex_unlock(&protows->mutex);
922 /* TODO? callback(closure, NULL); */
926 /******************* client description part for server *****************************/
928 /* on call, propagate it to the ws service */
929 static void server_on_call(struct afb_proto_ws *protows, struct readbuf *rb)
931 struct afb_proto_ws_call *call;
932 const char *uuid, *verb;
935 struct json_object *object;
937 afb_proto_ws_addref(protows);
939 /* reads the call message data */
940 if (!readbuf_uint32(rb, &callid)
941 || !readbuf_string(rb, &verb, &lenverb)
942 || !readbuf_string(rb, &uuid, NULL)
943 || !readbuf_object(rb, &object))
946 /* create the request */
947 call = malloc(sizeof *call);
951 call->protows = protows;
952 call->callid = callid;
954 call->buffer = rb->base;
955 rb->base = NULL; /* don't free the buffer */
957 protows->server_itf->on_call(protows->closure, call, verb, object, uuid);
961 json_object_put(object);
964 afb_proto_ws_unref(protows);
967 /* on subcall reply */
968 static void server_on_subcall_reply(struct afb_proto_ws *protows, struct readbuf *rb)
972 struct json_object *object;
973 struct server_subcall *sc, **psc;
975 /* reads the call message data */
976 if (!readbuf_uint32(rb, &subcallid)
977 || !readbuf_char(rb, &ie)
978 || !readbuf_object(rb, &object)) {
979 /* TODO bad protocol */
983 /* search the subcall and unlink it */
984 pthread_mutex_lock(&protows->mutex);
985 psc = &protows->subcalls;
986 while ((sc = *psc) && sc->subcallid != subcallid)
989 pthread_mutex_unlock(&protows->mutex);
990 json_object_put(object);
991 /* TODO subcall not found */
994 pthread_mutex_unlock(&protows->mutex);
995 sc->callback(sc->closure, -(int)ie, object);
1000 static int server_send_description(struct afb_proto_ws *protows, uint32_t descid, struct json_object *descobj)
1003 struct writebuf wb = { .count = 0 };
1005 if (writebuf_char(&wb, CHAR_FOR_DESCRIPTION)
1006 && writebuf_uint32(&wb, descid)
1007 && writebuf_object(&wb, descobj)) {
1008 pthread_mutex_lock(&protows->mutex);
1009 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
1010 pthread_mutex_unlock(&protows->mutex);
1017 int afb_proto_ws_describe_put(struct afb_proto_ws_describe *describe, struct json_object *description)
1019 int rc = server_send_description(describe->protows, describe->descid, description);
1020 afb_proto_ws_unref(describe->protows);
1025 /* on describe, propagate it to the ws service */
1026 static void server_on_describe(struct afb_proto_ws *protows, struct readbuf *rb)
1029 struct afb_proto_ws_describe *desc;
1031 /* reads the descid */
1032 if (readbuf_uint32(rb, &descid)) {
1033 if (protows->server_itf->on_describe) {
1034 /* create asynchronous job */
1035 desc = malloc(sizeof *desc);
1037 desc->descid = descid;
1038 desc->protows = protows;
1039 afb_proto_ws_addref(protows);
1040 protows->server_itf->on_describe(protows->closure, desc);
1044 server_send_description(protows, descid, NULL);
1048 /* callback when receiving binary data */
1049 static void server_on_binary_job(int sig, void *closure)
1051 struct binary *binary = closure;
1054 switch (*binary->rb.head++) {
1056 server_on_call(binary->protows, &binary->rb);
1058 case CHAR_FOR_SUBCALL_REPLY:
1059 server_on_subcall_reply(binary->protows, &binary->rb);
1061 case CHAR_FOR_DESCRIBE:
1062 server_on_describe(binary->protows, &binary->rb);
1064 default: /* unexpected message */
1065 /* TODO: close the connection */
1069 free(binary->rb.base);
1073 static void server_on_binary(void *closure, char *data, size_t size)
1076 struct binary *binary;
1079 binary = malloc(sizeof *binary);
1083 binary->protows = closure;
1084 binary->rb.base = data;
1085 binary->rb.head = data;
1086 binary->rb.end = data + size;
1087 rc = jobs_queue(NULL, 0, server_on_binary_job, binary);
1096 /******************* server part: manage events **********************************/
1098 static int server_event_send(struct afb_proto_ws *protows, char order, const char *event_name, int event_id, struct json_object *data)
1100 struct writebuf wb = { .count = 0 };
1103 if (writebuf_char(&wb, order)
1104 && (order == CHAR_FOR_EVT_BROADCAST || writebuf_uint32(&wb, event_id))
1105 && writebuf_string(&wb, event_name)
1106 && (order == CHAR_FOR_EVT_ADD || order == CHAR_FOR_EVT_DEL || writebuf_object(&wb, data))) {
1107 pthread_mutex_lock(&protows->mutex);
1108 rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count);
1109 pthread_mutex_unlock(&protows->mutex);
1116 int afb_proto_ws_server_event_create(struct afb_proto_ws *protows, const char *event_name, int event_id)
1118 return server_event_send(protows, CHAR_FOR_EVT_ADD, event_name, event_id, NULL);
1121 int afb_proto_ws_server_event_remove(struct afb_proto_ws *protows, const char *event_name, int event_id)
1123 return server_event_send(protows, CHAR_FOR_EVT_DEL, event_name, event_id, NULL);
1126 int afb_proto_ws_server_event_push(struct afb_proto_ws *protows, const char *event_name, int event_id, struct json_object *data)
1128 return server_event_send(protows, CHAR_FOR_EVT_PUSH, event_name, event_id, data);
1131 int afb_proto_ws_server_event_broadcast(struct afb_proto_ws *protows, const char *event_name, struct json_object *data)
1133 return server_event_send(protows, CHAR_FOR_EVT_BROADCAST, event_name, 0, data);
1136 /*****************************************************/
1138 /* callback when receiving a hangup */
1139 static void on_hangup(void *closure)
1141 struct afb_proto_ws *protows = closure;
1142 struct server_subcall *sc, *nsc;
1143 struct client_describe *cd, *ncd;
1145 nsc = protows->subcalls;
1149 sc->callback(sc->closure, 1, NULL);
1153 ncd = protows->describes;
1157 cd->callback(cd->closure, NULL);
1161 if (protows->fdev) {
1162 fdev_unref(protows->fdev);
1164 if (protows->on_hangup)
1165 protows->on_hangup(protows->closure);
1169 /*****************************************************/
1171 static const struct afb_ws_itf proto_ws_client_ws_itf =
1175 .on_binary = client_on_binary,
1177 .on_hangup = on_hangup
1180 static const struct afb_ws_itf server_ws_itf =
1184 .on_binary = server_on_binary,
1186 .on_hangup = on_hangup
1189 /*****************************************************/
1191 static struct afb_proto_ws *afb_proto_ws_create(struct fdev *fdev, const struct afb_proto_ws_server_itf *itfs, const struct afb_proto_ws_client_itf *itfc, void *closure, const struct afb_ws_itf *itf)
1193 struct afb_proto_ws *protows;
1195 protows = calloc(1, sizeof *protows);
1196 if (protows == NULL)
1199 fcntl(fdev_fd(fdev), F_SETFD, FD_CLOEXEC);
1200 fcntl(fdev_fd(fdev), F_SETFL, O_NONBLOCK);
1201 protows->ws = afb_ws_create(fdev, itf, protows);
1202 if (protows->ws != NULL) {
1203 protows->fdev = fdev;
1204 protows->refcount = 1;
1205 protows->subcalls = NULL;
1206 protows->closure = closure;
1207 protows->server_itf = itfs;
1208 protows->client_itf = itfc;
1209 pthread_mutex_init(&protows->mutex, NULL);
1217 struct afb_proto_ws *afb_proto_ws_create_client(struct fdev *fdev, const struct afb_proto_ws_client_itf *itf, void *closure)
1219 return afb_proto_ws_create(fdev, NULL, itf, closure, &proto_ws_client_ws_itf);
1222 struct afb_proto_ws *afb_proto_ws_create_server(struct fdev *fdev, const struct afb_proto_ws_server_itf *itf, void *closure)
1224 return afb_proto_ws_create(fdev, itf, NULL, closure, &server_ws_itf);
1227 void afb_proto_ws_unref(struct afb_proto_ws *protows)
1229 if (!__atomic_sub_fetch(&protows->refcount, 1, __ATOMIC_RELAXED)) {
1230 afb_proto_ws_hangup(protows);
1231 afb_ws_destroy(protows->ws);
1232 pthread_mutex_destroy(&protows->mutex);
1237 void afb_proto_ws_addref(struct afb_proto_ws *protows)
1239 __atomic_add_fetch(&protows->refcount, 1, __ATOMIC_RELAXED);
1242 int afb_proto_ws_is_client(struct afb_proto_ws *protows)
1244 return !!protows->client_itf;
1247 int afb_proto_ws_is_server(struct afb_proto_ws *protows)
1249 return !!protows->server_itf;
1252 void afb_proto_ws_hangup(struct afb_proto_ws *protows)
1254 afb_ws_hangup(protows->ws);
1257 void afb_proto_ws_on_hangup(struct afb_proto_ws *protows, void (*on_hangup)(void *closure))
1259 protows->on_hangup = on_hangup;