X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-proto-ws.c;h=edf63634e21dd0d1d68bdea68ba08f9c51c7ec77;hb=ad66a4685f5d50ef05338157c84a572088693ed3;hp=f9313545b03bf5f871fa763447fadb1f68a87586;hpb=4521c1e7ae5371ab9d639adc617d17fb4e8ded0c;p=src%2Fapp-framework-binder.git diff --git a/src/afb-proto-ws.c b/src/afb-proto-ws.c index f9313545..edf63634 100644 --- a/src/afb-proto-ws.c +++ b/src/afb-proto-ws.c @@ -17,8 +17,6 @@ #define _GNU_SOURCE -#define NO_PLUGIN_VERBOSE_MACRO - #include #include #include @@ -37,8 +35,8 @@ #include "afb-ws.h" #include "afb-msg-json.h" #include "afb-proto-ws.h" -#include "jobs.h" #include "fdev.h" +#include "verbose.h" struct afb_proto_ws; @@ -91,7 +89,7 @@ For the purpose of handling events the server can: struct client_call { struct client_call *next; /* the next call */ struct afb_proto_ws *protows; /* the proto_ws */ - void *request; + void *request; /* the request closure */ uint32_t callid; /* the message identifier */ }; @@ -134,9 +132,6 @@ struct afb_proto_ws /* count of references */ int refcount; - /* file descriptor */ - struct fdev *fdev; - /* resource control */ pthread_mutex_t mutex; @@ -160,6 +155,9 @@ struct afb_proto_ws /* on hangup callback */ void (*on_hangup)(void *closure); + + /* queuing facility for processing messages */ + int (*queuing)(struct afb_proto_ws *proto, void (*process)(int s, void *c), void *closure); }; /******************* streaming objects **********************************/ @@ -260,10 +258,11 @@ static int readbuf_object(struct readbuf *rb, struct json_object **object) { const char *string; struct json_object *o; + enum json_tokener_error jerr; int rc = readbuf_string(rb, &string, NULL); if (rc) { - o = json_tokener_parse(string); - if (o == NULL && strcmp(string, "null")) + o = json_tokener_parse_verbose(string, &jerr); + if (jerr != json_tokener_success) o = json_object_new_string(string); *object = o; } @@ -327,6 +326,32 @@ static int writebuf_object(struct writebuf *wb, struct json_object *object) return string != NULL && writebuf_string(wb, string); } +/******************* queuing of messages *****************/ + +/* queue the processing of the received message (except if size=0 cause it's not a valid message) */ +static void queue_message_processing(struct afb_proto_ws *protows, char *data, size_t size, void (*processing)(int,void*)) +{ + struct binary *binary; + + if (size) { + binary = malloc(sizeof *binary); + if (!binary) { + /* TODO process the problem */ + errno = ENOMEM; + } else { + binary->protows = protows; + binary->rb.base = data; + binary->rb.head = data; + binary->rb.end = data + size; + if (!protows->queuing + || protows->queuing(protows, processing, binary) < 0) + processing(0, binary); + return; + } + } + free(data); +} + /******************* ws request part for server *****************/ void afb_proto_ws_call_addref(struct afb_proto_ws_call *call) @@ -487,6 +512,8 @@ static void client_on_event_create(struct afb_proto_ws *protows, struct readbuf if (protows->client_itf->on_event_create && client_msg_event_read(rb, &event_id, &event_name)) protows->client_itf->on_event_create(protows->closure, event_name, (int)event_id); + else + ERROR("Ignoring creation of event"); } /* removes an event */ @@ -497,6 +524,8 @@ static void client_on_event_remove(struct afb_proto_ws *protows, struct readbuf if (protows->client_itf->on_event_remove && client_msg_event_read(rb, &event_id, &event_name)) protows->client_itf->on_event_remove(protows->closure, event_name, (int)event_id); + else + ERROR("Ignoring deletion of event"); } /* subscribes an event */ @@ -508,6 +537,8 @@ static void client_on_event_subscribe(struct afb_proto_ws *protows, struct readb if (protows->client_itf->on_event_subscribe && client_msg_call_get(protows, rb, &call) && client_msg_event_read(rb, &event_id, &event_name)) protows->client_itf->on_event_subscribe(protows->closure, call->request, event_name, (int)event_id); + else + ERROR("Ignoring subscription to event"); } /* unsubscribes an event */ @@ -519,16 +550,21 @@ static void client_on_event_unsubscribe(struct afb_proto_ws *protows, struct rea if (protows->client_itf->on_event_unsubscribe && client_msg_call_get(protows, rb, &call) && client_msg_event_read(rb, &event_id, &event_name)) protows->client_itf->on_event_unsubscribe(protows->closure, call->request, event_name, (int)event_id); + else + ERROR("Ignoring unsubscription to event"); } /* receives broadcasted events */ static void client_on_event_broadcast(struct afb_proto_ws *protows, struct readbuf *rb) { - const char *event_name; + const char *event_name, *uuid; + char hop; struct json_object *object; - if (protows->client_itf->on_event_broadcast && readbuf_string(rb, &event_name, NULL) && readbuf_object(rb, &object)) - protows->client_itf->on_event_broadcast(protows->closure, event_name, object); + if (protows->client_itf->on_event_broadcast && readbuf_string(rb, &event_name, NULL) && readbuf_object(rb, &object) && (uuid = readbuf_get(rb, 16)) && readbuf_char(rb, &hop)) + protows->client_itf->on_event_broadcast(protows->closure, event_name, object, (unsigned char*)uuid, (uint8_t)hop); + else + ERROR("Ignoring broadcast of event"); } /* pushs an event */ @@ -540,6 +576,8 @@ static void client_on_event_push(struct afb_proto_ws *protows, struct readbuf *r if (protows->client_itf->on_event_push && client_msg_event_read(rb, &event_id, &event_name) && readbuf_object(rb, &object)) protows->client_itf->on_event_push(protows->closure, event_name, (int)event_id, object); + else + ERROR("Ignoring push of event"); } static void client_on_reply(struct afb_proto_ws *protows, struct readbuf *rb) @@ -626,25 +664,9 @@ static void client_on_binary_job(int sig, void *closure) /* callback when receiving binary data */ static void client_on_binary(void *closure, char *data, size_t size) { - int rc; - struct binary *binary; + struct afb_proto_ws *protows = closure; - if (size) { - binary = malloc(sizeof *binary); - if (!binary) { - errno = ENOMEM; - } else { - binary->protows = closure; - binary->rb.base = data; - binary->rb.head = data; - binary->rb.end = data + size; - rc = jobs_queue(NULL, 0, client_on_binary_job, binary); - if (rc >= 0) - return; - free(binary); - } - } - free(data); + queue_message_processing(protows, data, size, client_on_binary_job); } int afb_proto_ws_client_call( @@ -872,25 +894,9 @@ static void server_on_binary_job(int sig, void *closure) static void server_on_binary(void *closure, char *data, size_t size) { - int rc; - struct binary *binary; + struct afb_proto_ws *protows = closure; - if (size) { - binary = malloc(sizeof *binary); - if (!binary) { - errno = ENOMEM; - } else { - binary->protows = closure; - binary->rb.base = data; - binary->rb.head = data; - binary->rb.end = data + size; - rc = jobs_queue(NULL, 0, server_on_binary_job, binary); - if (rc >= 0) - return; - free(binary); - } - } - free(data); + queue_message_processing(protows, data, size, server_on_binary_job); } /******************* server part: manage events **********************************/ @@ -901,9 +907,9 @@ static int server_event_send(struct afb_proto_ws *protows, char order, const cha int rc; if (writebuf_char(&wb, order) - && (order == CHAR_FOR_EVT_BROADCAST || writebuf_uint32(&wb, event_id)) + && writebuf_uint32(&wb, event_id) && writebuf_string(&wb, event_name) - && (order == CHAR_FOR_EVT_ADD || order == CHAR_FOR_EVT_DEL || writebuf_object(&wb, data))) { + && (order != CHAR_FOR_EVT_PUSH || writebuf_object(&wb, data))) { pthread_mutex_lock(&protows->mutex); rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count); pthread_mutex_unlock(&protows->mutex); @@ -928,9 +934,26 @@ int afb_proto_ws_server_event_push(struct afb_proto_ws *protows, const char *eve return server_event_send(protows, CHAR_FOR_EVT_PUSH, event_name, event_id, data); } -int afb_proto_ws_server_event_broadcast(struct afb_proto_ws *protows, const char *event_name, struct json_object *data) +int afb_proto_ws_server_event_broadcast(struct afb_proto_ws *protows, const char *event_name, struct json_object *data, const unsigned char uuid[16], uint8_t hop) { - return server_event_send(protows, CHAR_FOR_EVT_BROADCAST, event_name, 0, data); + struct writebuf wb = { .count = 0 }; + int rc; + + if (!hop--) + return 0; + + if (writebuf_char(&wb, CHAR_FOR_EVT_BROADCAST) + && writebuf_string(&wb, event_name) + && writebuf_object(&wb, data) + && writebuf_put(&wb, uuid, 16) + && writebuf_char(&wb, (char)hop)) { + pthread_mutex_lock(&protows->mutex); + rc = afb_ws_binary_v(protows->ws, wb.iovec, wb.count); + pthread_mutex_unlock(&protows->mutex); + if (rc >= 0) + return 0; + } + return -1; } /*****************************************************/ @@ -940,8 +963,18 @@ static void on_hangup(void *closure) { struct afb_proto_ws *protows = closure; struct client_describe *cd, *ncd; + struct client_call *call, *ncall; + + ncd = __atomic_exchange_n(&protows->describes, NULL, __ATOMIC_RELAXED); + ncall = __atomic_exchange_n(&protows->calls, NULL, __ATOMIC_RELAXED); + + while (ncall) { + call= ncall; + ncall = call->next; + protows->client_itf->on_reply(protows->closure, call->request, NULL, "disconnected", "server hung up"); + free(call); + } - ncd = protows->describes; while (ncd) { cd= ncd; ncd = cd->next; @@ -949,9 +982,9 @@ static void on_hangup(void *closure) free(cd); } - if (protows->fdev) { - fdev_unref(protows->fdev); - protows->fdev = 0; + if (protows->ws) { + afb_ws_destroy(protows->ws); + protows->ws = 0; if (protows->on_hangup) protows->on_hangup(protows->closure); } @@ -991,7 +1024,6 @@ static struct afb_proto_ws *afb_proto_ws_create(struct fdev *fdev, const struct fcntl(fdev_fd(fdev), F_SETFL, O_NONBLOCK); protows->ws = afb_ws_create(fdev, itf, protows); if (protows->ws != NULL) { - protows->fdev = fdev; protows->refcount = 1; protows->closure = closure; protows->server_itf = itfs; @@ -1016,9 +1048,8 @@ struct afb_proto_ws *afb_proto_ws_create_server(struct fdev *fdev, const struct void afb_proto_ws_unref(struct afb_proto_ws *protows) { - if (!__atomic_sub_fetch(&protows->refcount, 1, __ATOMIC_RELAXED)) { + if (protows && !__atomic_sub_fetch(&protows->refcount, 1, __ATOMIC_RELAXED)) { afb_proto_ws_hangup(protows); - afb_ws_destroy(protows->ws); pthread_mutex_destroy(&protows->mutex); free(protows); } @@ -1041,7 +1072,8 @@ int afb_proto_ws_is_server(struct afb_proto_ws *protows) void afb_proto_ws_hangup(struct afb_proto_ws *protows) { - afb_ws_hangup(protows->ws); + if (protows->ws) + afb_ws_hangup(protows->ws); } void afb_proto_ws_on_hangup(struct afb_proto_ws *protows, void (*on_hangup)(void *closure)) @@ -1049,3 +1081,7 @@ void afb_proto_ws_on_hangup(struct afb_proto_ws *protows, void (*on_hangup)(void protows->on_hangup = on_hangup; } +void afb_proto_ws_set_queuing(struct afb_proto_ws *protows, int (*queuing)(struct afb_proto_ws*, void (*)(int,void*), void*)) +{ + protows->queuing = queuing; +}