From 424accef25372400d79018e8eacb68b3c9b0cfd3 Mon Sep 17 00:00:00 2001 From: Zachary Michaels Date: Wed, 2 Jul 2014 09:29:49 -0400 Subject: [PATCH] Store ip/port as uint32/uint16 instead of string --- src/daemon/command_parser_executor.h | 7 +++-- src/daemon/command_server.cpp | 8 +++--- src/daemon/command_server.h | 4 +-- src/daemon/daemon.cpp | 16 +++++++---- src/daemon/http_connection.h | 9 ++++-- src/daemon/rpc_command_executor.cpp | 42 ++++++++++++++-------------- src/daemon/rpc_command_executor.h | 20 ++++--------- 7 files changed, 53 insertions(+), 53 deletions(-) diff --git a/src/daemon/command_parser_executor.h b/src/daemon/command_parser_executor.h index a0b950cf3..ae0c46090 100644 --- a/src/daemon/command_parser_executor.h +++ b/src/daemon/command_parser_executor.h @@ -9,9 +9,10 @@ class t_command_parser_executor final private: t_rpc_command_executor m_executor; public: - t_command_parser_executor(t_rpc_command_executor && executor) : - m_executor(std::move(executor)) - {} + t_command_parser_executor( + uint32_t ip + , uint16_t port + ); bool print_peer_list(const std::vector& args); diff --git a/src/daemon/command_server.cpp b/src/daemon/command_server.cpp index fdae8f857..0c8203d19 100644 --- a/src/daemon/command_server.cpp +++ b/src/daemon/command_server.cpp @@ -1,16 +1,16 @@ #include "cryptonote_config.h" #include "version.h" #include "daemon/command_server.h" -#include "daemon/rpc_command_executor.h" namespace daemonize { namespace p = std::placeholders; t_command_server::t_command_server( - t_rpc_command_executor && executor - ) : - m_parser(std::move(executor)) + uint32_t ip + , uint16_t port + ) + : m_parser(ip, port) , m_command_lookup() { m_command_lookup.set_handler( diff --git a/src/daemon/command_server.h b/src/daemon/command_server.h index 318273b77..69b149ccd 100644 --- a/src/daemon/command_server.h +++ b/src/daemon/command_server.h @@ -2,7 +2,6 @@ #include "console_handler.h" #include "daemon/command_parser_executor.h" -#include "daemon/rpc_command_executor.h" namespace daemonize { @@ -12,7 +11,8 @@ private: epee::command_handler m_command_lookup; public: t_command_server( - t_rpc_command_executor && executor + uint32_t ip + , uint16_t port ); bool process_command_str(const std::string& cmd); diff --git a/src/daemon/daemon.cpp b/src/daemon/daemon.cpp index 48f3e85f9..39fa57557 100644 --- a/src/daemon/daemon.cpp +++ b/src/daemon/daemon.cpp @@ -8,7 +8,6 @@ #include "include_base_utils.h" #include "version.h" #include "daemon/command_server.h" -#include "daemon/rpc_command_executor.h" #include #include @@ -156,15 +155,20 @@ int main(int argc, char* argv[]) auto rpc_ip_str = command_line::get_arg(vm, cryptonote::core_rpc_server::arg_rpc_bind_ip); auto rpc_port_str = command_line::get_arg(vm, cryptonote::core_rpc_server::arg_rpc_bind_port); - auto host = t_rpc_command_executor::parse_host(rpc_ip_str, rpc_port_str); - if (!host.ok) + uint32_t rpc_ip; + uint16_t rpc_port; + if (!epee::string_tools::get_xtype_from_string(rpc_ip, rpc_ip_str)) { - std::cerr << "Invalid RPC host" << std::endl; + std::cerr << "Invalid IP" << std::endl; + return 1; + } + if (!epee::string_tools::get_xtype_from_string(rpc_port, rpc_port_str)) + { + std::cerr << "Invalid port" << std::endl; return 1; } - t_command_server - rpc_commands{t_rpc_command_executor{std::move(rpc_ip_str), std::move(rpc_port_str)}}; + t_command_server rpc_commands{rpc_ip, rpc_port}; if (rpc_commands.process_command_vec(command)) { return 0; diff --git a/src/daemon/http_connection.h b/src/daemon/http_connection.h index 5d817606f..1f4e837a6 100644 --- a/src/daemon/http_connection.h +++ b/src/daemon/http_connection.h @@ -13,12 +13,15 @@ public: t_http_connection( epee::net_utils::http::http_simple_client * p_http_client - , std::string const & daemon_ip - , std::string const & daemon_port + , uint32_t ip + , uint16_t port ) : mp_http_client(p_http_client) { - m_ok = mp_http_client->connect(daemon_ip, daemon_port, TIMEOUT); + // TODO fix http client so that it accepts properly typed arguments + std::string ip_str = boost::lexical_cast(ip); + std::string port_str = boost::lexical_cast(port); + m_ok = mp_http_client->connect(ip_str, port_str, TIMEOUT); } ~t_http_connection() diff --git a/src/daemon/rpc_command_executor.cpp b/src/daemon/rpc_command_executor.cpp index 5c53f8a15..98f6a3a76 100644 --- a/src/daemon/rpc_command_executor.cpp +++ b/src/daemon/rpc_command_executor.cpp @@ -39,28 +39,28 @@ namespace { << "difficulty: " << boost::lexical_cast(header.difficulty) << std::endl << "reward: " << boost::lexical_cast(header.reward); } + + std::string build_url(uint32_t host, uint16_t port, std::string const & relative_url) + { + std::string result = + "http://" + + boost::lexical_cast(host) + + ":" + + boost::lexical_cast(port) + + relative_url; + return result; + } } -t_rpc_command_executor::t_host_result t_rpc_command_executor::parse_host( - std::string const & rpc_host_ip_str, std::string const & rpc_host_port_str) -{ - t_host_result result; - - bool ip_ok = epee::string_tools::get_ip_int32_from_string(result.rpc_host_ip, rpc_host_ip_str); - bool port_ok = epee::string_tools::get_xtype_from_string(result.rpc_host_port, rpc_host_port_str); - result.ok = ip_ok && port_ok; - - return result; -} - -t_rpc_command_executor::t_rpc_command_executor(std::string && rpc_host_ip_str, std::string && rpc_host_port_str) +t_rpc_command_executor::t_rpc_command_executor( + uint32_t ip + , uint16_t port + ) : m_http_client() - , m_rpc_host_ip_str(std::move(rpc_host_ip_str)) - , m_rpc_host_port_str(std::move(rpc_host_port_str)) + , m_ip(ip) + , m_port(port) {} -t_rpc_command_executor::t_rpc_command_executor(t_rpc_command_executor && other) = default; - bool t_rpc_command_executor::print_peer_list() { cryptonote::COMMAND_RPC_GET_PEER_LIST::request req; cryptonote::COMMAND_RPC_GET_PEER_LIST::response res; @@ -333,8 +333,8 @@ bool t_rpc_command_executor::json_rpc_request( , std::string const & fail_msg ) { - std::string rpc_url = "http://" + m_rpc_host_ip_str + ":" + m_rpc_host_port_str + "/json_rpc"; - t_http_connection connection(&m_http_client, m_rpc_host_ip_str, m_rpc_host_port_str); + std::string rpc_url = build_url(m_ip, m_port, "/json_rpc"); + t_http_connection connection(&m_http_client, m_ip, m_port); bool ok = connection.is_open(); ok = ok && epee::net_utils::invoke_http_json_rpc(rpc_url, method_name, req, res, m_http_client); @@ -362,8 +362,8 @@ bool t_rpc_command_executor::rpc_request( , std::string const & fail_msg ) { - std::string rpc_url = "http://" + m_rpc_host_ip_str + ":" + m_rpc_host_port_str + relative_url; - t_http_connection connection(&m_http_client, m_rpc_host_ip_str, m_rpc_host_port_str); + std::string rpc_url = build_url(m_ip, m_port, relative_url); + t_http_connection connection(&m_http_client, m_ip, m_port); bool ok = connection.is_open(); ok = ok && epee::net_utils::invoke_http_json_remote_command2(rpc_url, req, res, m_http_client); diff --git a/src/daemon/rpc_command_executor.h b/src/daemon/rpc_command_executor.h index a13db94b8..730df12b3 100644 --- a/src/daemon/rpc_command_executor.h +++ b/src/daemon/rpc_command_executor.h @@ -11,21 +11,13 @@ namespace daemonize { class t_rpc_command_executor final { private: epee::net_utils::http::http_simple_client m_http_client; - std::string m_rpc_host_ip_str; - std::string m_rpc_host_port_str; + uint32_t m_ip; + uint16_t m_port; public: - struct t_host_result { - bool ok; - uint32_t rpc_host_ip; - uint16_t rpc_host_port; - }; - - static t_host_result parse_host( - std::string const & rpc_host_ip_str, std::string const & rpc_host_port_str); - - t_rpc_command_executor(std::string && rpc_host_ip_str, std::string && rpc_host_port_str); - - t_rpc_command_executor(t_rpc_command_executor && other); + t_rpc_command_executor( + uint32_t ip + , uint16_t port + ); bool print_peer_list();