diff options
Diffstat (limited to 'src/rpc-server')
-rw-r--r-- | src/rpc-server/commands/trex_rpc_cmd_general.cpp | 29 | ||||
-rw-r--r-- | src/rpc-server/commands/trex_rpc_cmds.h | 6 | ||||
-rw-r--r-- | src/rpc-server/trex_rpc_cmd.cpp | 23 | ||||
-rw-r--r-- | src/rpc-server/trex_rpc_cmd_api.h | 9 | ||||
-rw-r--r-- | src/rpc-server/trex_rpc_server.cpp | 21 | ||||
-rw-r--r-- | src/rpc-server/trex_rpc_server_api.h | 44 |
6 files changed, 58 insertions, 74 deletions
diff --git a/src/rpc-server/commands/trex_rpc_cmd_general.cpp b/src/rpc-server/commands/trex_rpc_cmd_general.cpp index 97ccae06..106a167a 100644 --- a/src/rpc-server/commands/trex_rpc_cmd_general.cpp +++ b/src/rpc-server/commands/trex_rpc_cmd_general.cpp @@ -175,6 +175,8 @@ TrexRpcCmdGetSysInfo::_run(const Json::Value ¶ms, Json::Value &result) { section["ports"][i]["driver"] = driver; section["ports"][i]["speed"] = speed; + section["ports"][i]["owner"] = port->get_owner(); + switch (port->get_state()) { case TrexStatelessPort::PORT_STATE_DOWN: section["ports"][i]["status"] = "down"; @@ -208,7 +210,10 @@ trex_rpc_cmd_rc_e TrexRpcCmdGetOwner::_run(const Json::Value ¶ms, Json::Value &result) { Json::Value §ion = result["result"]; - section["owner"] = TrexRpcServer::get_owner(); + uint8_t port_id = parse_port(params, result); + + TrexStatelessPort *port = TrexStateless::get_instance().get_port_by_id(port_id); + section["owner"] = port->get_owner(); return (TREX_RPC_CMD_OK); } @@ -220,17 +225,21 @@ TrexRpcCmdGetOwner::_run(const Json::Value ¶ms, Json::Value &result) { trex_rpc_cmd_rc_e TrexRpcCmdAcquire::_run(const Json::Value ¶ms, Json::Value &result) { + uint8_t port_id = parse_port(params, result); + const string &new_owner = parse_string(params, "user", result); bool force = parse_bool(params, "force", result); /* if not free and not you and not force - fail */ - if ( (!TrexRpcServer::is_free_to_aquire()) && (TrexRpcServer::get_owner() != new_owner) && (!force)) { - generate_execute_err(result, "device is already taken by '" + TrexRpcServer::get_owner() + "'"); + TrexStatelessPort *port = TrexStateless::get_instance().get_port_by_id(port_id); + + if ( (!port->is_free_to_aquire()) && (port->get_owner() != new_owner) && (!force)) { + generate_execute_err(result, "device is already taken by '" + port->get_owner() + "'"); } - string handle = TrexRpcServer::set_owner(new_owner); + port->set_owner(new_owner); - result["result"] = handle; + result["result"] = port->get_owner_handler(); return (TREX_RPC_CMD_OK); } @@ -242,7 +251,15 @@ TrexRpcCmdAcquire::_run(const Json::Value ¶ms, Json::Value &result) { trex_rpc_cmd_rc_e TrexRpcCmdRelease::_run(const Json::Value ¶ms, Json::Value &result) { - TrexRpcServer::clear_owner(); + uint8_t port_id = parse_port(params, result); + + TrexStatelessPort *port = TrexStateless::get_instance().get_port_by_id(port_id); + + if (port->get_state() == TrexStatelessPort::PORT_STATE_TRANSMITTING) { + generate_execute_err(result, "cannot release a port during transmission"); + } + + port->clear_owner(); result["result"] = "ACK"; diff --git a/src/rpc-server/commands/trex_rpc_cmds.h b/src/rpc-server/commands/trex_rpc_cmds.h index 643aa22d..e261d1c6 100644 --- a/src/rpc-server/commands/trex_rpc_cmds.h +++ b/src/rpc-server/commands/trex_rpc_cmds.h @@ -69,9 +69,9 @@ void get_hostname(std::string &hostname); /** * ownership */ -TREX_RPC_CMD_DEFINE(TrexRpcCmdGetOwner, "get_owner", 0, false); -TREX_RPC_CMD_DEFINE(TrexRpcCmdAcquire, "acquire", 2, false); -TREX_RPC_CMD_DEFINE(TrexRpcCmdRelease, "release", 0, true); +TREX_RPC_CMD_DEFINE(TrexRpcCmdGetOwner, "get_owner", 1, false); +TREX_RPC_CMD_DEFINE(TrexRpcCmdAcquire, "acquire", 3, false); +TREX_RPC_CMD_DEFINE(TrexRpcCmdRelease, "release", 1, true); /** diff --git a/src/rpc-server/trex_rpc_cmd.cpp b/src/rpc-server/trex_rpc_cmd.cpp index 437e8b1a..6c355e70 100644 --- a/src/rpc-server/trex_rpc_cmd.cpp +++ b/src/rpc-server/trex_rpc_cmd.cpp @@ -20,6 +20,7 @@ limitations under the License. */ #include <trex_rpc_cmd_api.h> #include <trex_rpc_server_api.h> +#include <trex_stateless_api.h> trex_rpc_cmd_rc_e TrexRpcCommand::run(const Json::Value ¶ms, Json::Value &result) { @@ -57,12 +58,32 @@ TrexRpcCommand::check_param_count(const Json::Value ¶ms, int expected, Json: void TrexRpcCommand::verify_ownership(const Json::Value ¶ms, Json::Value &result) { std::string handler = parse_string(params, "handler", result); + uint8_t port_id = parse_port(params, result); - if (!TrexRpcServer::verify_owner_handler(handler)) { + TrexStatelessPort *port = TrexStateless::get_instance().get_port_by_id(port_id); + + if (!port->verify_owner_handler(handler)) { generate_execute_err(result, "invalid handler provided. please pass the handler given when calling 'acquire' or take ownership"); } } +uint8_t +TrexRpcCommand::parse_port(const Json::Value ¶ms, Json::Value &result) { + uint8_t port_id = parse_byte(params, "port_id", result); + validate_port_id(port_id, result); + + return (port_id); +} + +void +TrexRpcCommand::validate_port_id(uint8_t port_id, Json::Value &result) { + if (port_id >= TrexStateless::get_instance().get_port_count()) { + std::stringstream ss; + ss << "invalid port id - should be between 0 and " << (int)TrexStateless::get_instance().get_port_count() - 1; + generate_execute_err(result, ss.str()); + } +} + const char * TrexRpcCommand::type_to_str(field_type_e type) { switch (type) { diff --git a/src/rpc-server/trex_rpc_cmd_api.h b/src/rpc-server/trex_rpc_cmd_api.h index cab50cfd..3c718eaa 100644 --- a/src/rpc-server/trex_rpc_cmd_api.h +++ b/src/rpc-server/trex_rpc_cmd_api.h @@ -125,6 +125,12 @@ protected: void verify_ownership(const Json::Value ¶ms, Json::Value &result); /** + * validate port id + * + */ + void validate_port_id(uint8_t port_id, Json::Value &result); + + /** * parse functions * */ @@ -146,6 +152,9 @@ protected: const Json::Value & parse_object(const Json::Value &parent, int index, Json::Value &result); const Json::Value & parse_array(const Json::Value &parent, int index, Json::Value &result); + /* shortcut for parsing port id */ + uint8_t parse_port(const Json::Value ¶ms, Json::Value &result); + /** * parse a field from choices * diff --git a/src/rpc-server/trex_rpc_server.cpp b/src/rpc-server/trex_rpc_server.cpp index a5988cab..6b8c200d 100644 --- a/src/rpc-server/trex_rpc_server.cpp +++ b/src/rpc-server/trex_rpc_server.cpp @@ -111,8 +111,6 @@ get_current_date_time() { } const std::string TrexRpcServer::s_server_uptime = get_current_date_time(); -std::string TrexRpcServer::s_owner = "none"; -std::string TrexRpcServer::s_owner_handler = ""; TrexRpcServer::TrexRpcServer(const TrexRpcServerConfig &req_resp_cfg) { @@ -158,22 +156,3 @@ void TrexRpcServer::set_verbose(bool verbose) { } } -/** - * generate a random connection handler - * - */ -std::string TrexRpcServer::generate_handler() { - std::stringstream ss; - - static const char alphanum[] = - "0123456789" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz"; - - /* generate 8 bytes of random handler */ - for (int i = 0; i < 8; ++i) { - ss << alphanum[rand() % (sizeof(alphanum) - 1)]; - } - - return (ss.str()); -} diff --git a/src/rpc-server/trex_rpc_server_api.h b/src/rpc-server/trex_rpc_server_api.h index c34ac0f8..06bbe10c 100644 --- a/src/rpc-server/trex_rpc_server_api.h +++ b/src/rpc-server/trex_rpc_server_api.h @@ -164,49 +164,7 @@ public: } - /** - * query for ownership - * - */ - static const std::string &get_owner() { - return s_owner; - } - - /** - * owner handler - * for the connection - * - */ - static const std::string &get_owner_handler() { - return s_owner_handler; - } - - static bool is_free_to_aquire() { - return (s_owner == "none"); - } - - /** - * take ownership of the server array - * this is static - * ownership is total - * - */ - static std::string set_owner(const std::string &owner) { - s_owner = owner; - s_owner_handler = generate_handler(); - return (s_owner_handler); - } - - static void clear_owner() { - s_owner = "none"; - s_owner_handler = ""; - } - - static bool verify_owner_handler(const std::string &handler) { - - return ( (s_owner != "none") && (s_owner_handler == handler) ); - - } + private: static std::string generate_handler(); |