X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-ws-json1.c;h=88f9b358fe6e2cdea3e971bea9ae7978b95c23de;hb=26bca5f8a8c6f9403a84945a5cd914b6da948efd;hp=67ce2d8e8b9e5c5cbc89cd146cf8a2aa540a690c;hpb=dde7d32db7ac1f784e84e79d33442558a7c21d41;p=src%2Fapp-framework-binder.git diff --git a/src/afb-ws-json1.c b/src/afb-ws-json1.c index 67ce2d8e..88f9b358 100644 --- a/src/afb-ws-json1.c +++ b/src/afb-ws-json1.c @@ -1,5 +1,5 @@ /* - * Copyright 2016 IoT.bzh + * Copyright (C) 2016 "IoT.bzh" * Author: José Bollo * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -29,7 +29,7 @@ #include "afb-ws-json1.h" #include "afb-msg-json.h" #include "session.h" -#include "afb-req-itf.h" +#include #include "afb-apis.h" #include "afb-context.h" @@ -51,6 +51,7 @@ struct afb_ws_json1 struct AFB_clientCtx *session; struct json_tokener *tokener; struct afb_ws *ws; + int new_session; }; static void aws_send_event(struct afb_ws_json1 *ws, const char *event, struct json_object *object); @@ -65,12 +66,12 @@ static inline struct afb_event_listener listener_for(struct afb_ws_json1 *aws) return (struct afb_event_listener){ .itf = &event_listener_itf, .closure = aws }; } -struct afb_ws_json1 *afb_ws_json1_create(int fd, struct AFB_clientCtx *session, void (*cleanup)(void*), void *cleanup_closure) +struct afb_ws_json1 *afb_ws_json1_create(int fd, struct afb_context *context, void (*cleanup)(void*), void *cleanup_closure) { struct afb_ws_json1 *result; assert(fd >= 0); - assert(session != NULL); + assert(context != NULL); result = malloc(sizeof * result); if (result == NULL) @@ -79,7 +80,8 @@ struct afb_ws_json1 *afb_ws_json1_create(int fd, struct AFB_clientCtx *session, result->cleanup = cleanup; result->cleanup_closure = cleanup_closure; result->requests = NULL; - result->session = ctxClientAddRef(session); + result->session = ctxClientAddRef(context->session); + result->new_session = context->created != 0; if (result->session == NULL) goto error2; @@ -127,6 +129,10 @@ static void aws_on_hangup(struct afb_ws_json1 *ws) struct afb_wsreq { + /* + * CAUTION: 'context' field should be the first because there + * is an implicit convertion to struct afb_context + */ struct afb_context context; int refcount; struct afb_ws_json1 *aws; @@ -167,7 +173,9 @@ static const struct afb_req_itf wsreq_itf = { .context_get = (void*)afb_context_get, .context_set = (void*)afb_context_set, .addref = (void*)wsreq_addref, - .unref = (void*)wsreq_unref + .unref = (void*)wsreq_unref, + .session_close = (void*)afb_context_close, + .session_set_LOA = (void*)afb_context_change_loa }; static int aws_wsreq_parse(struct afb_wsreq *r, char *text, size_t size) @@ -316,6 +324,10 @@ static void aws_on_text(struct afb_ws_json1 *ws, char *text, size_t size) afb_context_init(&wsreq->context, ws->session, wsreq->tok); if (!wsreq->context.invalidated) wsreq->context.validated = 1; + if (ws->new_session != 0) { + wsreq->context.created = 1; + ws->new_session = 0; + } wsreq->refcount = 1; wsreq->aws = ws; wsreq->next = ws->requests; @@ -343,7 +355,7 @@ static void wsreq_addref(struct afb_wsreq *wsreq) static void wsreq_unref(struct afb_wsreq *wsreq) { if (--wsreq->refcount == 0) { - struct afb_wsreq **prv = &wsreq->ws->requests; + struct afb_wsreq **prv = &wsreq->aws->requests; while(*prv != NULL) { if (*prv == wsreq) { *prv = wsreq->next; @@ -352,6 +364,7 @@ static void wsreq_unref(struct afb_wsreq *wsreq) prv = &(*prv)->next; } afb_context_disconnect(&wsreq->context); + json_object_put(wsreq->root); free(wsreq->text); free(wsreq); } @@ -403,15 +416,17 @@ static void aws_emit(struct afb_ws_json1 *aws, int code, const char *id, size_t json_object_array_add(msg, json_object_new_string(token)); /* emits the reply */ - txt = json_object_to_json_string(msg); + txt = json_object_to_json_string_ext(msg, JSON_C_TO_STRING_PLAIN); 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) { + const char *uuid = afb_context_sent_uuid(&wsreq->context); const char *token = afb_context_sent_token(&wsreq->context); - aws_emit(wsreq->aws, retcode, wsreq->id, wsreq->idlen, afb_msg_json_reply(status, info, resp, token, NULL), token); + struct json_object *reply = afb_msg_json_reply(status, info, resp, token, uuid); + aws_emit(wsreq->aws, retcode, wsreq->id, wsreq->idlen, reply, token); } static void wsreq_fail(struct afb_wsreq *wsreq, const char *status, const char *info)