#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"
struct afb_ws *ws;
};
-struct afb_ws_json *afb_ws_json_create(int fd, struct AFB_clientCtx *context, void (*cleanup)(void*), void *closure)
+
+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 = {
+ .send = (void*)aws_send_event
+};
+
+struct afb_ws_json *afb_ws_json_create(int fd, struct AFB_clientCtx *context, void (*cleanup)(void*), void *cleanup_closure)
{
struct afb_ws_json *result;
goto error;
result->cleanup = cleanup;
- result->cleanup_closure = closure;
+ result->cleanup_closure = cleanup_closure;
result->requests = NULL;
result->context = ctxClientGet(context);
if (result->context == NULL)
if (result->ws == NULL)
goto error4;
+ if (0 > ctxClientEventSenderAdd(result->context, (struct afb_event_sender){ .itf = &event_sender_itf, .closure = result }))
+ goto error5;
+
return result;
+error5:
+ /* TODO */
error4:
json_tokener_free(result->tokener);
error3:
#define CALL 2
#define RETOK 3
#define RETERR 4
+#define EVENT 5
struct afb_wsreq
{
.send = (void*)wsreq_send,
.session_create = (void*)wsreq_session_create,
.session_check = (void*)wsreq_session_check,
- .session_close = (void*)wsreq_session_close
+ .session_close = (void*)wsreq_session_close,
+ .context_get = (void*)afb_context_get,
+ .context_set = (void*)afb_context_set
+
};
static int aws_wsreq_parse(struct afb_wsreq *r, char *text, size_t size)
wsreq->next = ws->requests;
ws->requests = wsreq;
- r.data = wsreq;
+ r.req_closure = wsreq;
r.itf = &wsreq_itf;
afb_apis_call(r, ws->context, wsreq->api, wsreq->apilen, wsreq->verb, wsreq->verblen);
return;
json_tokener_reset(wsreq->aws->tokener);
root = json_tokener_parse_ex(wsreq->aws->tokener, wsreq->obj, (int)wsreq->objlen);
if (root == NULL) {
- /* lazy discovering !!!! not good TODO improve*/
- root = json_object_new_object();
+ /* lazy error detection of json request. Is it to improve? */
+ root = json_object_new_string_len(wsreq->obj, (int)wsreq->objlen);
}
wsreq->root = root;
}
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 */
}
afb_ws_text(wsreq->aws->ws, buffer, size);
}
+static void aws_send_event(struct afb_ws_json *aws, const char *event, struct json_object *object)
+{
+ aws_emit(aws, EVENT, event, strlen(event), afb_msg_json_event(event, object));
+}
+