summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorimarom <imarom@cisco.com>2015-08-20 10:36:53 +0300
committerimarom <imarom@cisco.com>2015-08-20 10:36:53 +0300
commitda56afe25e71f4dc65ae4669889eec5b8fc43afc (patch)
tree6f4a1782297cf43c5123ea5b0424348af3c58dca /src
parent2a4d1ac17610d15c65d6337306ffeda04ab29bef (diff)
draft
Diffstat (limited to 'src')
-rwxr-xr-xsrc/console/trex_console.py19
-rw-r--r--src/console/trex_rpc_client.py27
-rw-r--r--src/rpc-server/include/trex_rpc_server_api.h44
-rw-r--r--src/rpc-server/src/commands/trex_rpc_cmd_test.cpp8
-rw-r--r--src/rpc-server/src/trex_rpc_req_resp_server.cpp15
-rw-r--r--src/rpc-server/src/trex_rpc_server.cpp33
-rw-r--r--src/rpc-server/src/trex_rpc_server_mock.cpp3
7 files changed, 119 insertions, 30 deletions
diff --git a/src/console/trex_console.py b/src/console/trex_console.py
index 584089ae..be84640c 100755
--- a/src/console/trex_console.py
+++ b/src/console/trex_console.py
@@ -23,8 +23,7 @@ class TrexConsole(cmd.Cmd):
rc, msg = self.rpc_client.query_rpc_server()
if rc:
- lst = msg.split('\n')
- self.supported_rpc = [str(x) for x in lst if x]
+ self.supported_rpc = [str(x) for x in msg if x]
# a cool hack - i stole this function and added space
def completenames(self, text, *ignored):
@@ -57,7 +56,11 @@ class TrexConsole(cmd.Cmd):
if not rc:
print "\n*** Failed to query RPC server: " + str(msg)
- print "\nRPC server supports the following commands: \n\n" + msg
+ print "\nRPC server supports the following commands: \n\n"
+ for func in msg:
+ if func:
+ print func
+ print "\n"
def do_ping (self, line):
'''\npings the RPC server\n'''
@@ -81,7 +84,6 @@ class TrexConsole(cmd.Cmd):
else:
print "[FAILED]\n"
- print "Server Response:\n\n{0}\n".format(json.dumps(msg))
def complete_rpc (self, text, line, begidx, endidx):
return [x for x in self.supported_rpc if x.startswith(text)]
@@ -104,11 +106,10 @@ class TrexConsole(cmd.Cmd):
def main ():
# RPC client
- try:
- rpc_client = RpcClient("localhost", 5050)
- rpc_client.connect()
- except Exception as e:
- print "\n*** " + str(e) + "\n"
+ rpc_client = RpcClient("localhost", 5050)
+ rc, msg = rpc_client.connect()
+ if not rc:
+ print "\n*** " + msg + "\n"
exit(-1)
# console
diff --git a/src/console/trex_rpc_client.py b/src/console/trex_rpc_client.py
index fe8f69a2..f3edd252 100644
--- a/src/console/trex_rpc_client.py
+++ b/src/console/trex_rpc_client.py
@@ -2,6 +2,7 @@
import zmq
import json
from time import sleep
+import random
class RpcClient():
@@ -22,6 +23,16 @@ class RpcClient():
return rc
+ def pretty_json (self, json_str):
+ return json.dumps(json.loads(json_str), indent = 4, separators=(',', ': '))
+
+ def verbose_msg (self, msg):
+ if not self.verbose:
+ return
+
+ print "[verbose] " + msg
+
+
def create_jsonrpc_v2 (self, method_name, params = {}, id = None):
msg = {}
msg["jsonrpc"] = "2.0"
@@ -36,10 +47,10 @@ class RpcClient():
return json.dumps(msg)
def invoke_rpc_method (self, method_name, params = {}, block = True):
- msg = self.create_jsonrpc_v2(method_name, params, id = 1)
+ id = random.randint(1, 1000)
+ msg = self.create_jsonrpc_v2(method_name, params, id = id)
- if self.verbose:
- print "\n[verbose] Sending Request To Server: " + str(msg) + "\n"
+ self.verbose_msg("Sending Request To Server:\n\n" + self.pretty_json(msg) + "\n")
if block:
self.socket.send(msg)
@@ -66,8 +77,7 @@ class RpcClient():
if not got_response:
return False, "Failed To Get Server Response"
- if self.verbose:
- print "[verbose] Server Response: " + str(response)
+ self.verbose_msg("Server Response:\n\n" + self.pretty_json(response) + "\n")
# decode
response_json = json.loads(response)
@@ -75,6 +85,9 @@ class RpcClient():
if (response_json.get("jsonrpc") != "2.0"):
return False, "Malfromed Response ({0})".format(str(response))
+ if (response_json.get("id") != id):
+ return False, "Server Replied With Bad ID ({0})".format(str(response))
+
# error reported by server
if ("error" in response_json):
return False, response_json["error"]["message"]
@@ -111,11 +124,11 @@ class RpcClient():
rc, err = self.ping_rpc_server()
if not rc:
self.context.destroy(linger = 0)
- raise Exception(err)
+ return False, err
#print "Connection Established !\n"
print "[SUCCESS]\n"
-
+ return True, ""
def __del__ (self):
print "Shutting down RPC client\n"
diff --git a/src/rpc-server/include/trex_rpc_server_api.h b/src/rpc-server/include/trex_rpc_server_api.h
index bb455be2..6bb81c73 100644
--- a/src/rpc-server/include/trex_rpc_server_api.h
+++ b/src/rpc-server/include/trex_rpc_server_api.h
@@ -68,7 +68,7 @@ private:
class TrexRpcServerInterface {
public:
- TrexRpcServerInterface(const TrexRpcServerConfig &cfg);
+ TrexRpcServerInterface(const TrexRpcServerConfig &cfg, const std::string &name);
virtual ~TrexRpcServerInterface();
/**
@@ -84,22 +84,42 @@ public:
void stop();
/**
+ * set verbose on or off
+ *
+ */
+ void set_verbose(bool verbose);
+
+ /**
* return TRUE if server is active
*
*/
bool is_running();
+ /**
+ * is the server verbose or not
+ *
+ */
+ bool is_verbose();
+
protected:
/**
* instances implement this
*
*/
- virtual void _rpc_thread_cb() = 0;
- virtual void _stop_rpc_thread() = 0;
+ virtual void _rpc_thread_cb() = 0;
+ virtual void _stop_rpc_thread() = 0;
+
+ /**
+ * prints a verbosed message (if enabled)
+ *
+ */
+ void verbose_msg(const std::string &msg);
TrexRpcServerConfig m_cfg;
bool m_is_running;
+ bool m_is_verbose;
std::thread *m_thread;
+ std::string m_name;
};
/**
@@ -116,16 +136,30 @@ public:
TrexRpcServer(const TrexRpcServerConfig &req_resp_cfg);
~TrexRpcServer();
+ /**
+ * starts the RPC server
+ *
+ * @author imarom (19-Aug-15)
+ */
void start();
+
+ /**
+ * stops the RPC server
+ *
+ * @author imarom (19-Aug-15)
+ */
void stop();
+ void set_verbose(bool verbose);
+
static const std::string &get_server_uptime() {
return s_server_uptime;
}
private:
- std::vector<TrexRpcServerInterface *> m_servers;
- static const std::string s_server_uptime;
+ std::vector<TrexRpcServerInterface *> m_servers;
+ bool m_verbose;
+ static const std::string s_server_uptime;
};
#endif /* __TREX_RPC_SERVER_API_H__ */
diff --git a/src/rpc-server/src/commands/trex_rpc_cmd_test.cpp b/src/rpc-server/src/commands/trex_rpc_cmd_test.cpp
index f2d4121e..e9cc4665 100644
--- a/src/rpc-server/src/commands/trex_rpc_cmd_test.cpp
+++ b/src/rpc-server/src/commands/trex_rpc_cmd_test.cpp
@@ -95,7 +95,6 @@ TrexRpcCmdPing::_run(const Json::Value &params, Json::Value &result) {
TrexRpcCommand::rpc_cmd_rc_e
TrexRpcCmdGetReg::_run(const Json::Value &params, Json::Value &result) {
vector<string> cmds;
- stringstream ss;
/* validate count */
if (params.size() != 0) {
@@ -104,11 +103,14 @@ TrexRpcCmdGetReg::_run(const Json::Value &params, Json::Value &result) {
TrexRpcCommandsTable::get_instance().query(cmds);
+
+ Json::Value test = Json::arrayValue;
for (auto cmd : cmds) {
- ss << cmd << "\n";
+ test.append(cmd);
}
- result["result"] = ss.str();
+ result["result"] = test;
+
return (RPC_CMD_OK);
}
diff --git a/src/rpc-server/src/trex_rpc_req_resp_server.cpp b/src/rpc-server/src/trex_rpc_req_resp_server.cpp
index e40f5554..7484758d 100644
--- a/src/rpc-server/src/trex_rpc_req_resp_server.cpp
+++ b/src/rpc-server/src/trex_rpc_req_resp_server.cpp
@@ -34,7 +34,7 @@ limitations under the License.
* ZMQ based request-response server
*
*/
-TrexRpcServerReqRes::TrexRpcServerReqRes(const TrexRpcServerConfig &cfg) : TrexRpcServerInterface(cfg) {
+TrexRpcServerReqRes::TrexRpcServerReqRes(const TrexRpcServerConfig &cfg) : TrexRpcServerInterface(cfg, "req resp") {
/* ZMQ is not thread safe - this should be outside */
m_context = zmq_ctx_new();
}
@@ -84,6 +84,9 @@ 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);
+
handle_request(request);
}
@@ -128,14 +131,16 @@ void TrexRpcServerReqRes::handle_request(const std::string &request) {
}
/* write the JSON to string and sever on ZMQ */
- std::string reponse_str;
+ std::string response_str;
if (response.size() == 1) {
- reponse_str = writer.write(response[0]);
+ response_str = writer.write(response[0]);
} else {
- reponse_str = writer.write(response);
+ response_str = writer.write(response);
}
- zmq_send(m_socket, reponse_str.c_str(), reponse_str.size(), 0);
+ verbose_msg("Server Replied: " + response_str);
+
+ zmq_send(m_socket, response_str.c_str(), response_str.size(), 0);
}
diff --git a/src/rpc-server/src/trex_rpc_server.cpp b/src/rpc-server/src/trex_rpc_server.cpp
index 139614e5..366bfc5b 100644
--- a/src/rpc-server/src/trex_rpc_server.cpp
+++ b/src/rpc-server/src/trex_rpc_server.cpp
@@ -24,11 +24,13 @@ limitations under the License.
#include <unistd.h>
#include <zmq.h>
#include <sstream>
+#include <iostream>
/************** RPC server interface ***************/
-TrexRpcServerInterface::TrexRpcServerInterface(const TrexRpcServerConfig &cfg) : m_cfg(cfg) {
+TrexRpcServerInterface::TrexRpcServerInterface(const TrexRpcServerConfig &cfg, const std::string &name) : m_cfg(cfg), m_name(name) {
m_is_running = false;
+ m_is_verbose = false;
}
TrexRpcServerInterface::~TrexRpcServerInterface() {
@@ -37,6 +39,14 @@ TrexRpcServerInterface::~TrexRpcServerInterface() {
}
}
+void TrexRpcServerInterface::verbose_msg(const std::string &msg) {
+ if (!m_is_verbose) {
+ return;
+ }
+
+ std::cout << "[verbose][" << m_name << "] " << msg << "\n";
+}
+
/**
* starts a RPC specific server
*
@@ -45,6 +55,8 @@ TrexRpcServerInterface::~TrexRpcServerInterface() {
void TrexRpcServerInterface::start() {
m_is_running = true;
+ verbose_msg("Starting RPC Server");
+
m_thread = new std::thread(&TrexRpcServerInterface::_rpc_thread_cb, this);
if (!m_thread) {
throw TrexRpcException("unable to create RPC thread");
@@ -54,14 +66,27 @@ void TrexRpcServerInterface::start() {
void TrexRpcServerInterface::stop() {
m_is_running = false;
+ verbose_msg("Attempting To Stop RPC Server");
+
/* call the dynamic type class stop */
_stop_rpc_thread();
/* hold until thread has joined */
m_thread->join();
+
+ verbose_msg("Server Stopped");
+
delete m_thread;
}
+void TrexRpcServerInterface::set_verbose(bool verbose) {
+ m_is_verbose = verbose;
+}
+
+bool TrexRpcServerInterface::is_verbose() {
+ return m_is_verbose;
+}
+
bool TrexRpcServerInterface::is_running() {
return m_is_running;
}
@@ -120,3 +145,9 @@ void TrexRpcServer::stop() {
}
}
+void TrexRpcServer::set_verbose(bool verbose) {
+ for (auto server : m_servers) {
+ server->set_verbose(verbose);
+ }
+}
+
diff --git a/src/rpc-server/src/trex_rpc_server_mock.cpp b/src/rpc-server/src/trex_rpc_server_mock.cpp
index b01fff90..fd4f051c 100644
--- a/src/rpc-server/src/trex_rpc_server_mock.cpp
+++ b/src/rpc-server/src/trex_rpc_server_mock.cpp
@@ -60,6 +60,9 @@ int main(int argc, char *argv[]) {
/* init the RPC server */
rpc.start();
+ cout << "Setting Server To Full Verbose\n\n";
+ rpc.set_verbose(true);
+
cout << "Server Started\n\n";
while (true) {