summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/afb-hreq.c24
-rw-r--r--src/afb-msg-json.c59
-rw-r--r--src/afb-msg-json.h25
-rw-r--r--src/afb-ws-json.c68
5 files changed, 117 insertions, 60 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index e8205270..5153c5c1 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -11,6 +11,7 @@ ADD_LIBRARY(src OBJECT
afb-websock.c
afb-ws.c
afb-ws-json.c
+ afb-msg-json.c
websock.c
verbose.c
utils-upoll.c
diff --git a/src/afb-hreq.c b/src/afb-hreq.c
index 72b90504..d1ba0c13 100644
--- a/src/afb-hreq.c
+++ b/src/afb-hreq.c
@@ -34,6 +34,7 @@
#include "afb-method.h"
#include "afb-req-itf.h"
+#include "afb-msg-json.h"
#include "afb-hreq.h"
#include "session.h"
#include "verbose.h"
@@ -661,24 +662,19 @@ static ssize_t send_json_cb(json_object *obj, uint64_t pos, char *buf, size_t ma
static void req_reply(struct afb_hreq *hreq, unsigned retcode, const char *status, const char *info, json_object *resp)
{
- json_object *root, *request;
+ struct json_object *reply;
+ const char *token, *uuid;
struct MHD_Response *response;
- 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);
- if (hreq->context) {
- json_object_object_add(request, uuid_arg, json_object_new_string(hreq->context->uuid));
- json_object_object_add(request, token_arg, json_object_new_string(hreq->context->token));
+ if (hreq->context == NULL) {
+ token = uuid = NULL;
+ } else {
+ token = hreq->context->token;
+ uuid = hreq->context->uuid;
}
+ reply = afb_msg_json_reply(status, info, resp, token, uuid);
- response = MHD_create_response_from_callback(MHD_SIZE_UNKNOWN, SIZE_RESPONSE_BUFFER, (void*)send_json_cb, root, (void*)json_object_put);
+ response = MHD_create_response_from_callback(MHD_SIZE_UNKNOWN, SIZE_RESPONSE_BUFFER, (void*)send_json_cb, reply, (void*)json_object_put);
afb_hreq_reply(hreq, retcode, response, NULL);
}
diff --git a/src/afb-msg-json.c b/src/afb-msg-json.c
new file mode 100644
index 00000000..dc096ac5
--- /dev/null
+++ b/src/afb-msg-json.c
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2016 IoT.bzh
+ * Author: José Bollo <jose.bollo@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define _GNU_SOURCE
+
+#include <json.h>
+
+#include "afb-msg-json.h"
+
+
+struct json_object *afb_msg_json_reply(const char *status, const char *info, struct json_object *resp, const char *token, const char *uuid)
+{
+ json_object *msg, *request;
+
+ request = json_object_new_object();
+ json_object_object_add(request, "status", json_object_new_string(status));
+ if (info != NULL)
+ json_object_object_add(request, "info", json_object_new_string(info));
+ if (token != NULL)
+ json_object_object_add(request, "token", json_object_new_string(token));
+ if (uuid != NULL)
+ json_object_object_add(request, "uuid", json_object_new_string(uuid));
+
+ msg = json_object_new_object();
+ json_object_object_add(msg, "jtype", json_object_new_string("afb-reply"));
+ json_object_object_add(msg, "request", request);
+ if (resp != NULL)
+ json_object_object_add(msg, "response", resp);
+
+ return msg;
+}
+
+struct json_object *afb_msg_json_event(const char *event, struct json_object *object)
+{
+ json_object *msg;
+
+ msg = json_object_new_object();
+ json_object_object_add(msg, "jtype", json_object_new_string("afb-event"));
+ json_object_object_add(msg, "event", json_object_new_string(event));
+ if (object != NULL)
+ json_object_object_add(msg, "data", object);
+
+ return msg;
+}
+
diff --git a/src/afb-msg-json.h b/src/afb-msg-json.h
new file mode 100644
index 00000000..f1d20be5
--- /dev/null
+++ b/src/afb-msg-json.h
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2016 IoT.bzh
+ * Author: José Bollo <jose.bollo@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+struct json_object;
+
+extern struct json_object *afb_msg_json_reply(const char *status, const char *info, struct json_object *resp, const char *token, const char *uuid);
+
+extern struct json_object *afb_msg_json_event(const char *event, struct json_object *object);
+
diff --git a/src/afb-ws-json.c b/src/afb-ws-json.c
index 471831e7..6fc6e3b3 100644
--- a/src/afb-ws-json.c
+++ b/src/afb-ws-json.c
@@ -27,6 +27,7 @@
#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"
@@ -389,35 +390,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 +440,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));
}