From 5cfeb192a3ff47c5cacc21abe20db2f61a66dd2b Mon Sep 17 00:00:00 2001 From: Yaroslav Brustinov Date: Sun, 18 Dec 2016 20:11:31 +0200 Subject: changes from code review Change-Id: I628608643d902bd6310b04b8036fc5f1fcc42309 Signed-off-by: Yaroslav Brustinov --- src/rpc-server/commands/trex_rpc_cmd_general.cpp | 7 ++--- src/rpc-server/commands/trex_rpc_cmd_stream.cpp | 29 ++++++++---------- src/rpc-server/trex_rpc_cmd.cpp | 38 ++++++++++++++++++------ src/rpc-server/trex_rpc_cmd_api.h | 20 +++++++++++++ 4 files changed, 63 insertions(+), 31 deletions(-) (limited to 'src/rpc-server') diff --git a/src/rpc-server/commands/trex_rpc_cmd_general.cpp b/src/rpc-server/commands/trex_rpc_cmd_general.cpp index b3b1b3b4..d4854a79 100644 --- a/src/rpc-server/commands/trex_rpc_cmd_general.cpp +++ b/src/rpc-server/commands/trex_rpc_cmd_general.cpp @@ -631,11 +631,8 @@ TrexRpcCmdPushRemote::_run(const Json::Value ¶ms, Json::Value &result) { uint8_t port_id = parse_port(params, result); std::string pcap_filename = parse_string(params, "pcap_filename", result); double ipg_usec = parse_double(params, "ipg_usec", result); - double min_ipg_sec = 0; - if (params.isMember("min_ipg_usec")) { - min_ipg_sec = usec_to_sec(parse_double(params, "min_ipg_usec", result)); - } - double speedup = parse_double(params, "speedup", result); + double min_ipg_sec = usec_to_sec(parse_udouble(params, "min_ipg_usec", result, 0)); + double speedup = parse_udouble(params, "speedup", result); uint32_t count = parse_uint32(params, "count", result); double duration = parse_double(params, "duration", result); bool is_dual = parse_bool(params, "is_dual", result, false); diff --git a/src/rpc-server/commands/trex_rpc_cmd_stream.cpp b/src/rpc-server/commands/trex_rpc_cmd_stream.cpp index 9a57c5f9..3f73a5d7 100644 --- a/src/rpc-server/commands/trex_rpc_cmd_stream.cpp +++ b/src/rpc-server/commands/trex_rpc_cmd_stream.cpp @@ -40,7 +40,7 @@ TrexRpcCmdAddStream::_run(const Json::Value ¶ms, Json::Value &result) { uint8_t port_id = parse_port(params, result); - uint32_t stream_id = parse_int(params, "stream_id", result); + uint32_t stream_id = parse_uint32(params, "stream_id", result); const Json::Value §ion = parse_object(params, "stream", result); @@ -62,7 +62,7 @@ TrexRpcCmdAddStream::_run(const Json::Value ¶ms, Json::Value &result) { stream->m_random_seed = parse_uint32(section, "random_seed", result,0); /* default is zero */ /* inter stream gap */ - stream->m_isg_usec = parse_double(section, "isg", result); + stream->m_isg_usec = parse_udouble(section, "isg", result); stream->m_next_stream_id = parse_int(section, "next_stream_id", result); @@ -114,7 +114,7 @@ TrexRpcCmdAddStream::_run(const Json::Value ¶ms, Json::Value &result) { generate_parse_err(result, "RX stats is not supported on this interface"); } - stream->m_rx_check.m_pg_id = parse_int(rx, "stream_id", result); + stream->m_rx_check.m_pg_id = parse_uint32(rx, "stream_id", result); std::string type = parse_string(rx, "rule_type", result); if (type == "latency") { stream->m_rx_check.m_rule_type = TrexPlatformApi::IF_STAT_PAYLOAD; @@ -155,7 +155,7 @@ TrexRpcCmdAddStream::allocate_new_stream(const Json::Value §ion, uint8_t por } else if (type == "single_burst") { - uint32_t total_pkts = parse_int(mode, "total_pkts", result); + uint32_t total_pkts = parse_uint32(mode, "total_pkts", result); stream.reset(new TrexStream(TrexStream::stSINGLE_BURST, port_id, stream_id)); stream->set_single_burst(total_pkts); @@ -163,9 +163,9 @@ TrexRpcCmdAddStream::allocate_new_stream(const Json::Value §ion, uint8_t por } else if (type == "multi_burst") { - double ibg_usec = parse_double(mode, "ibg", result); - uint32_t num_bursts = parse_int(mode, "count", result); - uint32_t pkts_per_burst = parse_int(mode, "pkts_per_burst", result); + double ibg_usec = parse_udouble(mode, "ibg", result); + uint32_t num_bursts = parse_uint32(mode, "count", result); + uint32_t pkts_per_burst = parse_uint32(mode, "pkts_per_burst", result); stream.reset(new TrexStream(TrexStream::stMULTI_BURST,port_id, stream_id )); stream->set_multi_burst(pkts_per_burst,num_bursts,ibg_usec); @@ -186,12 +186,7 @@ TrexRpcCmdAddStream::allocate_new_stream(const Json::Value §ion, uint8_t por void TrexRpcCmdAddStream::parse_rate(const Json::Value &rate, std::unique_ptr &stream, Json::Value &result) { - double value = parse_double(rate, "value", result); - if (value <= 0) { - std::stringstream ss; - ss << "rate value must be a positive number - got: '" << value << "'"; - generate_parse_err(result, ss.str()); - } + double value = parse_udouble(rate, "value", result); auto rate_types = {"pps", "bps_L1", "bps_L2", "percentage"}; std::string rate_type = parse_choice(rate, "type", rate_types, result); @@ -533,7 +528,7 @@ TrexRpcCmdRemoveStream::_run(const Json::Value ¶ms, Json::Value &result) { uint8_t port_id = parse_port(params, result); TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); - uint32_t stream_id = parse_int(params, "stream_id", result); + uint32_t stream_id = parse_uint32(params, "stream_id", result); TrexStream *stream = port->get_stream_by_id(stream_id); if (!stream) { @@ -615,7 +610,7 @@ TrexRpcCmdGetStream::_run(const Json::Value ¶ms, Json::Value &result) { TrexStatelessPort *port = get_stateless_obj()->get_port_by_id(port_id); bool get_pkt = parse_bool(params, "get_pkt", result); - uint32_t stream_id = parse_int(params, "stream_id", result); + uint32_t stream_id = parse_uint32(params, "stream_id", result); TrexStream *stream = port->get_stream_by_id(stream_id); @@ -660,9 +655,9 @@ TrexRpcCmdStartTraffic::_run(const Json::Value ¶ms, Json::Value &result) { std::string type = parse_choice(mul_obj, "type", TrexPortMultiplier::g_types, result); std::string op = parse_string(mul_obj, "op", result); - double value = parse_double(mul_obj, "value", result); + double value = parse_udouble(mul_obj, "value", result); - if ( value <=0 ){ + if ( value == 0 ){ generate_parse_err(result, "multiplier can't be zero"); } diff --git a/src/rpc-server/trex_rpc_cmd.cpp b/src/rpc-server/trex_rpc_cmd.cpp index 265d426b..6c56a59f 100644 --- a/src/rpc-server/trex_rpc_cmd.cpp +++ b/src/rpc-server/trex_rpc_cmd.cpp @@ -153,6 +153,8 @@ TrexRpcCommand::type_to_str(field_type_e type) { return "int"; case FIELD_TYPE_DOUBLE: return "double"; + case FIELD_TYPE_UDOUBLE: + return "unsigned double"; case FIELD_TYPE_OBJ: return "object"; case FIELD_TYPE_STR: @@ -176,7 +178,7 @@ TrexRpcCommand::json_type_to_name(const Json::Value &value) { case Json::uintValue: return "uint"; case Json::realValue: - return "real"; + return "double"; case Json::stringValue: return "string"; case Json::booleanValue: @@ -223,31 +225,41 @@ TrexRpcCommand::check_field_type(const Json::Value &parent, const std::string &n void TrexRpcCommand::check_field_type_common(const Json::Value &field, const std::string &name, field_type_e type, Json::Value &result) { - std::stringstream ss; + std::string specific_err; /* first check if field exists */ if (field == Json::Value::null) { - ss << "field '" << name << "' is missing"; - generate_parse_err(result, ss.str()); + specific_err = "field '" + name + "' is missing"; + generate_parse_err(result, specific_err); } bool rc = true; + specific_err = "is '" + std::string(json_type_to_name(field)) + "', expecting '" + std::string(type_to_str(type)) + "'"; switch (type) { case FIELD_TYPE_BYTE: - if ( (!field.isUInt()) || (field.asInt() > 0xFF)) { + if (!field.isUInt64()) { + rc = false; + } else if (field.asUInt64() > 0xFF) { + specific_err = "has size bigger than uint8."; rc = false; } break; case FIELD_TYPE_UINT16: - if ( (!field.isUInt()) || (field.asInt() > 0xFFFF)) { + if (!field.isUInt64()) { + rc = false; + } else if (field.asUInt64() > 0xFFFF) { + specific_err = "has size bigger than uint16."; rc = false; } break; case FIELD_TYPE_UINT32: - if ( (!field.isUInt()) || (field.asUInt() > 0xFFFFFFFF)) { + if (!field.isUInt64()) { + rc = false; + } else if (field.asUInt64() > 0xFFFFFFFF) { + specific_err = "has size bigger than uint32."; rc = false; } break; @@ -276,6 +288,15 @@ TrexRpcCommand::check_field_type_common(const Json::Value &field, const std::str } break; + case FIELD_TYPE_UDOUBLE: + if (!field.isDouble()) { + rc = false; + } else if (field.asDouble() < 0) { + specific_err = "has negative value."; + rc = false; + } + break; + case FIELD_TYPE_OBJ: if (!field.isObject()) { rc = false; @@ -300,8 +321,7 @@ TrexRpcCommand::check_field_type_common(const Json::Value &field, const std::str } if (!rc) { - ss << "error at offset: " << field.getOffsetStart() << " - '" << name << "' is '" << json_type_to_name(field) << "', expecting '" << type_to_str(type) << "'"; - generate_parse_err(result, ss.str()); + generate_parse_err(result, "error at offset: " + std::to_string(field.getOffsetStart()) + " - '" + name + "' " + specific_err); } } diff --git a/src/rpc-server/trex_rpc_cmd_api.h b/src/rpc-server/trex_rpc_cmd_api.h index de0f5b58..2536f69c 100644 --- a/src/rpc-server/trex_rpc_cmd_api.h +++ b/src/rpc-server/trex_rpc_cmd_api.h @@ -114,6 +114,7 @@ protected: FIELD_TYPE_UINT64, FIELD_TYPE_INT, FIELD_TYPE_DOUBLE, + FIELD_TYPE_UDOUBLE, FIELD_TYPE_BOOL, FIELD_TYPE_STR, FIELD_TYPE_OBJ, @@ -184,6 +185,11 @@ protected: return parent[param].asDouble(); } + template double parse_udouble(const Json::Value &parent, const T ¶m, Json::Value &result) { + check_field_type(parent, param, FIELD_TYPE_UDOUBLE, result); + return parent[param].asDouble(); + } + template bool parse_bool(const Json::Value &parent, const T ¶m, Json::Value &result) { check_field_type(parent, param, FIELD_TYPE_BOOL, result); return parent[param].asBool(); @@ -256,6 +262,20 @@ protected: return parse_double(parent, param, result); } + template double parse_udouble(const Json::Value &parent, const T ¶m, Json::Value &result, double def) { + /* if not exists - default */ + if (parent[param] == Json::Value::null) { + if (def < 0) { + std::stringstream ss; + ss << "default value of '" << param << "' is negative (please report)"; + generate_parse_err(result, ss.str()); + } else { + return def; + } + } + return parse_udouble(parent, param, result); + } + template bool parse_bool(const Json::Value &parent, const T ¶m, Json::Value &result, bool def) { /* if not exists - default */ if (parent[param] == Json::Value::null) { -- cgit 1.2.3-korg