summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorimarom <imarom@cisco.com>2015-09-03 05:49:10 +0300
committerimarom <imarom@cisco.com>2015-09-03 05:49:10 +0300
commitea30ab5d1165707ffb1297dabf2b24ecbd5d39a5 (patch)
tree67aaaad706231797254e67853548a68a4df24711 /src
parent1912c95b5480cd601581a00645bb2e75e9c6e7a3 (diff)
added pretty printer for the mock server on C++
Diffstat (limited to 'src')
-rw-r--r--src/rpc-server/trex_rpc_jsonrpc_v2_parser.cpp25
-rw-r--r--src/rpc-server/trex_rpc_jsonrpc_v2_parser.h8
-rw-r--r--src/rpc-server/trex_rpc_req_resp_server.cpp5
-rw-r--r--src/rpc-server/trex_rpc_server.cpp5
-rw-r--r--src/rpc-server/trex_rpc_server_api.h7
5 files changed, 48 insertions, 2 deletions
diff --git a/src/rpc-server/trex_rpc_jsonrpc_v2_parser.cpp b/src/rpc-server/trex_rpc_jsonrpc_v2_parser.cpp
index 3831bb37..928baca6 100644
--- a/src/rpc-server/trex_rpc_jsonrpc_v2_parser.cpp
+++ b/src/rpc-server/trex_rpc_jsonrpc_v2_parser.cpp
@@ -200,3 +200,28 @@ void TrexJsonRpcV2Parser::parse_single_request(Json::Value &request,
commands.push_back(new JsonRpcMethod(msg_id, rpc_cmd, request["params"]));
}
+/**
+ * tries to pretty a JSON str
+ *
+ * @author imarom (03-Sep-15)
+ *
+ * @param json_str
+ *
+ * @return std::string
+ */
+std::string TrexJsonRpcV2Parser::pretty_json_str(const std::string &json_str) {
+ Json::Reader reader;
+ Json::Value value;
+
+ /* basic JSON parsing */
+ bool rc = reader.parse(json_str, value, false);
+ if (!rc) {
+ /* duplicate the soruce */
+ return json_str;
+ }
+
+ /* successfully parsed */
+ Json::StyledWriter writer;
+ return writer.write(value);
+}
+
diff --git a/src/rpc-server/trex_rpc_jsonrpc_v2_parser.h b/src/rpc-server/trex_rpc_jsonrpc_v2_parser.h
index 3367ad6a..ebffaeb7 100644
--- a/src/rpc-server/trex_rpc_jsonrpc_v2_parser.h
+++ b/src/rpc-server/trex_rpc_jsonrpc_v2_parser.h
@@ -79,6 +79,14 @@ public:
*/
void parse(std::vector<TrexJsonRpcV2ParsedObject *> &commands);
+ /**
+ * *tries* to generate a pretty string from JSON
+ * if json_str is not a valid JSON string
+ * it will duplicate the source
+ *
+ */
+ static std::string pretty_json_str(const std::string &json_str);
+
private:
/**
diff --git a/src/rpc-server/trex_rpc_req_resp_server.cpp b/src/rpc-server/trex_rpc_req_resp_server.cpp
index 7484758d..c4d9dfdb 100644
--- a/src/rpc-server/trex_rpc_req_resp_server.cpp
+++ b/src/rpc-server/trex_rpc_req_resp_server.cpp
@@ -85,7 +85,7 @@ void TrexRpcServerReqRes::_rpc_thread_cb() {
/* transform it to a string */
std::string request((const char *)m_msg_buffer, msg_size);
- verbose_msg("Server Received: " + request);
+ verbose_json("Server Received: ", TrexJsonRpcV2Parser::pretty_json_str(request));
handle_request(request);
}
@@ -110,6 +110,7 @@ void TrexRpcServerReqRes::_stop_rpc_thread() {
*/
void TrexRpcServerReqRes::handle_request(const std::string &request) {
std::vector<TrexJsonRpcV2ParsedObject *> commands;
+
Json::FastWriter writer;
Json::Value response;
@@ -139,7 +140,7 @@ void TrexRpcServerReqRes::handle_request(const std::string &request) {
response_str = writer.write(response);
}
- verbose_msg("Server Replied: " + response_str);
+ verbose_json("Server Replied: ", response_str);
zmq_send(m_socket, response_str.c_str(), response_str.size(), 0);
diff --git a/src/rpc-server/trex_rpc_server.cpp b/src/rpc-server/trex_rpc_server.cpp
index 366bfc5b..6b8c200d 100644
--- a/src/rpc-server/trex_rpc_server.cpp
+++ b/src/rpc-server/trex_rpc_server.cpp
@@ -21,6 +21,7 @@ limitations under the License.
#include <trex_rpc_server_api.h>
#include <trex_rpc_req_resp_server.h>
+#include <trex_rpc_jsonrpc_v2_parser.h>
#include <unistd.h>
#include <zmq.h>
#include <sstream>
@@ -47,6 +48,10 @@ void TrexRpcServerInterface::verbose_msg(const std::string &msg) {
std::cout << "[verbose][" << m_name << "] " << msg << "\n";
}
+void TrexRpcServerInterface::verbose_json(const std::string &msg, const std::string &json_str) {
+ verbose_msg(msg + "\n\n" + TrexJsonRpcV2Parser::pretty_json_str(json_str));
+}
+
/**
* starts a RPC specific server
*
diff --git a/src/rpc-server/trex_rpc_server_api.h b/src/rpc-server/trex_rpc_server_api.h
index 6bb81c73..ab1bc454 100644
--- a/src/rpc-server/trex_rpc_server_api.h
+++ b/src/rpc-server/trex_rpc_server_api.h
@@ -115,6 +115,13 @@ protected:
*/
void verbose_msg(const std::string &msg);
+ /**
+ * prints a verbose message with a JSON to be converted to
+ * string
+ *
+ */
+ void verbose_json(const std::string &msg, const std::string &json_str);
+
TrexRpcServerConfig m_cfg;
bool m_is_running;
bool m_is_verbose;