X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-ws-json.c;h=9387f8d781b5301fda53b7aadb1d3eda8232d9da;hb=f262b0f726ac0577f40525038b779185f144873f;hp=471831e73a8bff590b4e484e76b6a79ebf8aa859;hpb=9e3afb8aa598f3e69e2c3723335507c12b4cd1f1;p=src%2Fapp-framework-binder.git diff --git a/src/afb-ws-json.c b/src/afb-ws-json.c index 471831e7..9387f8d7 100644 --- a/src/afb-ws-json.c +++ b/src/afb-ws-json.c @@ -27,17 +27,17 @@ #include "afb-ws.h" #include "afb-ws-json.h" +#include "afb-msg-json.h" #include "session.h" #include "afb-req-itf.h" #include "afb-apis.h" -static void aws_on_close(struct afb_ws_json *ws, uint16_t code, char *text, size_t size); +static void aws_on_hangup(struct afb_ws_json *ws); static void aws_on_text(struct afb_ws_json *ws, char *text, size_t size); static struct afb_ws_itf aws_itf = { - .on_close = (void*)aws_on_close, - .on_text = (void*)aws_on_text, - .on_binary = NULL, + .on_hangup = (void*)aws_on_hangup, + .on_text = (void*)aws_on_text }; struct afb_wsreq; @@ -52,7 +52,6 @@ struct afb_ws_json struct afb_ws *ws; }; - static void aws_send_event(struct afb_ws_json *ws, const char *event, struct json_object *object); static const struct afb_event_sender_itf event_sender_itf = { @@ -103,10 +102,14 @@ error: return NULL; } -static void aws_on_close(struct afb_ws_json *ws, uint16_t code, char *text, size_t size) +static void aws_on_hangup(struct afb_ws_json *ws) { - /* do nothing but free the text */ - free(text); + ctxClientEventSenderRemove(ws->context, (struct afb_event_sender){ .itf = &event_sender_itf, .closure = ws }); + afb_ws_destroy(ws->ws); + json_tokener_free(ws->tokener); + if (ws->cleanup != NULL) + ws->cleanup(ws->cleanup_closure); + free(ws); } #define CALL 2 @@ -280,14 +283,6 @@ static int aws_wsreq_parse(struct afb_wsreq *r, char *text, size_t size) /* done */ r->text = text; r->size = size; -fprintf(stderr, "\n\nONTEXT([%d, %.*s, %.*s/%.*s, %.*s, %.*s])\n\n", - r->code, - (int)r->idlen, r->id, - (int)r->apilen, r->api, - (int)r->verblen, r->verb, - (int)r->objlen, r->obj, - (int)r->toklen, r->tok -); return 1; bad_header: @@ -322,7 +317,7 @@ bad_header: free(wsreq); alloc_error: free(text); - afb_ws_close(ws->ws, 1008); + afb_ws_close(ws->ws, 1008, NULL); return; } @@ -389,35 +384,30 @@ static void wsreq_session_close(struct afb_wsreq *wsreq) ctxClientClose(context); } - -static void wsreq_reply(struct afb_wsreq *wsreq, int retcode, const char *status, const char *info, json_object *resp) +static void aws_emit(struct afb_ws_json *aws, int code, const char *id, size_t idlen, struct json_object *data) { - json_object *root, *request, *reply; - const char *message; - - /* builds the answering structure */ - root = json_object_new_object(); - json_object_object_add(root, "jtype", json_object_new_string("afb-reply")); - request = json_object_new_object(); - json_object_object_add(root, "request", request); - json_object_object_add(request, "status", json_object_new_string(status)); - if (info) - json_object_object_add(request, "info", json_object_new_string(info)); - if (resp) - json_object_object_add(root, "response", resp); - - /* make the reply */ - reply = json_object_new_array(); - json_object_array_add(reply, json_object_new_int(retcode)); - json_object_array_add(reply, json_object_new_string_len(wsreq->id, (int)wsreq->idlen)); - json_object_array_add(reply, root); - json_object_array_add(reply, json_object_new_string(wsreq->aws->context->token)); + json_object *msg; + const char *token; + const char *txt; + + /* pack the message */ + msg = json_object_new_array(); + json_object_array_add(msg, json_object_new_int(code)); + json_object_array_add(msg, json_object_new_string_len(id, (int)idlen)); + json_object_array_add(msg, data); + token = aws->context->token; + if (token) + json_object_array_add(msg, json_object_new_string(token)); /* emits the reply */ - message = json_object_to_json_string(reply); - afb_ws_text(wsreq->aws->ws, message, strlen(message)); - json_object_put(reply); + txt = json_object_to_json_string(msg); + afb_ws_text(aws->ws, txt, strlen(txt)); + json_object_put(msg); +} +static void wsreq_reply(struct afb_wsreq *wsreq, int retcode, const char *status, const char *info, json_object *resp) +{ + aws_emit(wsreq->aws, retcode, wsreq->id, wsreq->idlen, afb_msg_json_reply(status, info, resp, NULL, NULL)); /* TODO eliminates the wsreq */ } @@ -444,26 +434,6 @@ static void wsreq_send(struct afb_wsreq *wsreq, char *buffer, size_t size) static void aws_send_event(struct afb_ws_json *aws, const char *event, struct json_object *object) { - json_object *root, *reply; - const char *message; - - /* builds the answering structure */ - root = json_object_new_object(); - json_object_object_add(root, "jtype", json_object_new_string("afb-event")); - json_object_object_add(root, "event", json_object_new_string(event)); - if (object) - json_object_object_add(root, "data", object); - - /* make the reply */ - reply = json_object_new_array(); - json_object_array_add(reply, json_object_new_int(EVENT)); - json_object_array_add(reply, json_object_new_string(event)); - json_object_array_add(reply, root); - json_object_array_add(reply, json_object_new_string(aws->context->token)); - - /* emits the reply */ - message = json_object_to_json_string(reply); - afb_ws_text(aws->ws, message, strlen(message)); - json_object_put(reply); + aws_emit(aws, EVENT, event, strlen(event), afb_msg_json_event(event, object)); }