added pretty printer for the mock server on C++
authorimarom <[email protected]>
Thu, 3 Sep 2015 02:49:10 +0000 (05:49 +0300)
committerimarom <[email protected]>
Thu, 3 Sep 2015 02:49:10 +0000 (05:49 +0300)
src/rpc-server/trex_rpc_jsonrpc_v2_parser.cpp
src/rpc-server/trex_rpc_jsonrpc_v2_parser.h
src/rpc-server/trex_rpc_req_resp_server.cpp
src/rpc-server/trex_rpc_server.cpp
src/rpc-server/trex_rpc_server_api.h

index 3831bb3..928baca 100644 (file)
@@ -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);
+}
+
index 3367ad6..ebffaeb 100644 (file)
@@ -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:
 
     /**
index 7484758..c4d9dfd 100644 (file)
@@ -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);
     
index 366bfc5..6b8c200 100644 (file)
@@ -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
  * 
index 6bb81c7..ab1bc45 100644 (file)
@@ -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;