/*
- * Copyright 2016 IoT.bzh
+ * Copyright (C) 2016 "IoT.bzh"
* Author: José Bollo <jose.bollo@iot.bzh>
*
* Licensed under the Apache License, Version 2.0 (the "License");
#define RETERR 4
#define EVENT 5
-static void wsj1_on_hangup(struct afb_wsj1 *ws);
-static void wsj1_on_text(struct afb_wsj1 *ws, char *text, size_t size);
+static void wsj1_on_hangup(struct afb_wsj1 *wsj1);
+static void wsj1_on_text(struct afb_wsj1 *wsj1, char *text, size_t size);
static struct afb_ws_itf wsj1_itf = {
.on_hangup = (void*)wsj1_on_hangup,
}
}
-static void wsj1_on_hangup(struct afb_wsj1 *ws)
+static void wsj1_on_hangup(struct afb_wsj1 *wsj1)
{
- if (ws->itf->on_hangup != NULL)
- ws->itf->on_hangup(ws->closure);
+ if (wsj1->itf->on_hangup != NULL)
+ wsj1->itf->on_hangup(wsj1->closure, wsj1);
}
size_t items[10][2];
int n;
struct afb_wsj1_msg *msg;
- struct wsj1_call *call;
+ struct wsj1_call *call = NULL;
/* allocate */
msg = calloc(1, sizeof *msg);
afb_wsj1_addref(wsj1);
msg->wsj1 = wsj1;
msg->next = wsj1->messages;
- msg->next->previous = msg;
+ if (msg->next != NULL)
+ msg->next->previous = msg;
wsj1->messages = msg;
/* incoke the handler */
return msg->token;
}
-
-
-
-
-
-
-
-
-
-#if 0
-
-
-
-
-
-static void wsj1_emit(struct afb_wsj1 *wsj1, int code, const char *id, size_t idlen, struct json_object *data, const char *token)
-{
- json_object *msg;
- 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);
- if (token)
- json_object_array_add(msg, json_object_new_string(token));
-
- /* emits the reply */
- txt = json_object_to_json_string(msg);
- afb_ws_text(wsj1->ws, txt, strlen(txt));
- json_object_put(msg);
-}
-
-static void wsj1_msg_reply(struct afb_wsj1_msg *msg, int retcode, const char *status, const char *info, json_object *resp)
-{
- const char *token = afb_context_sent_token(&msg->context);
- wsj1_emit(msg->wsj1, retcode, msg->id, msg->idlen, afb_msg_json_reply(status, info, resp, token, NULL), token);
-}
-
-static void wsj1_msg_fail(struct afb_wsj1_msg *msg, const char *status, const char *info)
+struct afb_wsj1 *afb_wsj1_msg_wsj1(struct afb_wsj1_msg *msg)
{
- wsj1_msg_reply(msg, RETERR, status, info, NULL);
+ return msg->wsj1;
}
-static void wsj1_msg_success(struct afb_wsj1_msg *msg, json_object *obj, const char *info)
+int afb_wsj1_close(struct afb_wsj1 *wsj1, uint16_t code, const char *text)
{
- wsj1_msg_reply(msg, RETOK, "success", info, obj);
+ return afb_ws_close(wsj1->ws, code, text);
}
-static const char *wsj1_msg_raw(struct afb_wsj1_msg *msg, size_t *size)
-{
- *size = msg->objlen;
- return msg->obj;
-}
-
-static void wsj1_msg_send(struct afb_wsj1_msg *msg, const char *buffer, size_t size)
-{
- afb_ws_text(msg->wsj1->ws, buffer, size);
-}
-
-static void wsj1_send_event(struct afb_wsj1 *wsj1, const char *event, struct json_object *object)
-{
- wsj1_emit(wsj1, EVENT, event, strlen(event), afb_msg_json_event(event, object), NULL);
-}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-#endif
-
-
static int wsj1_send_isot(struct afb_wsj1 *wsj1, int i1, const char *s1, const char *o1, const char *t1)
{
char code[2] = { (char)('0' + i1), 0 };
- return afb_ws_texts(wsj1->ws, "[", code, ",\"", s1, "\",", o1, t1 != NULL ? ",\"" : "]", t1, "\"]", NULL);
+ return afb_ws_texts(wsj1->ws, "[", code, ",\"", s1, "\",", o1 == NULL ? "null" : o1, t1 != NULL ? ",\"" : "]", t1, "\"]", NULL);
}
static int wsj1_send_issot(struct afb_wsj1 *wsj1, int i1, const char *s1, const char *s2, const char *o1, const char *t1)
{
char code[2] = { (char)('0' + i1), 0 };
- return afb_ws_texts(wsj1->ws, "[", code, ",\"", s1, "\",\"", s2, "\",", o1, t1 != NULL ? ",\"" : "]", t1, "\"]", NULL);
+ return afb_ws_texts(wsj1->ws, "[", code, ",\"", s1, "\",\"", s2, "\",", o1 == NULL ? "null" : o1, t1 != NULL ? ",\"" : "]", t1, "\"]", NULL);
}
int afb_wsj1_send_event_j(struct afb_wsj1 *wsj1, const char *event, struct json_object *object)
{
- return afb_wsj1_send_event_s(wsj1, event, json_object_to_json_string(object));
+ const char *objstr = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
+ int rc = afb_wsj1_send_event_s(wsj1, event, objstr);
+ json_object_put(object);
+ return rc;
}
int afb_wsj1_send_event_s(struct afb_wsj1 *wsj1, const char *event, const char *object)
int afb_wsj1_call_j(struct afb_wsj1 *wsj1, const char *api, const char *verb, struct json_object *object, void (*on_reply)(void *closure, struct afb_wsj1_msg *msg), void *closure)
{
- return afb_wsj1_call_s(wsj1, api, verb, json_object_to_json_string(object), on_reply, closure);
+ const char *objstr = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
+ int rc = afb_wsj1_call_s(wsj1, api, verb, objstr, on_reply, closure);
+ json_object_put(object);
+ return rc;
}
int afb_wsj1_call_s(struct afb_wsj1 *wsj1, const char *api, const char *verb, const char *object, void (*on_reply)(void *closure, struct afb_wsj1_msg *msg), void *closure)
return rc;
}
-
-int afb_wsj1_reply_ok_j(struct afb_wsj1_msg *msg, struct json_object *object, const char *token)
-{
- return afb_wsj1_reply_ok_s(msg, json_object_to_json_string(object), token);
-}
-
-int afb_wsj1_reply_ok_s(struct afb_wsj1_msg *msg, const char *object, const char *token)
+int afb_wsj1_reply_j(struct afb_wsj1_msg *msg, struct json_object *object, const char *token, int iserror)
{
- return wsj1_send_isot(msg->wsj1, RETOK, msg->id, object, token);
-}
-
-int afb_wsj1_reply_error_j(struct afb_wsj1_msg *msg, struct json_object *object, const char *token)
-{
- return afb_wsj1_reply_error_s(msg, json_object_to_json_string(object), token);
+ const char *objstr = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
+ int rc = afb_wsj1_reply_s(msg, objstr, token, iserror);
+ json_object_put(object);
+ return rc;
}
-int afb_wsj1_reply_error_s(struct afb_wsj1_msg *msg, const char *object, const char *token)
+int afb_wsj1_reply_s(struct afb_wsj1_msg *msg, const char *object, const char *token, int iserror)
{
- return wsj1_send_isot(msg->wsj1, RETERR, msg->id, object, token);
+ return wsj1_send_isot(msg->wsj1, iserror ? RETERR : RETOK, msg->id, object, token);
}