Store ip/port as uint32/uint16 instead of string

This commit is contained in:
Zachary Michaels 2014-07-02 09:29:49 -04:00
parent 8b4dc37f83
commit 424accef25
7 changed files with 53 additions and 53 deletions

View File

@ -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<std::string>& args);

View File

@ -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(

View File

@ -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);

View File

@ -8,7 +8,6 @@
#include "include_base_utils.h"
#include "version.h"
#include "daemon/command_server.h"
#include "daemon/rpc_command_executor.h"
#include <boost/program_options.hpp>
#include <initializer_list>
@ -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<t_rpc_command_executor>
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;

View File

@ -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<std::string>(ip);
std::string port_str = boost::lexical_cast<std::string>(port);
m_ok = mp_http_client->connect(ip_str, port_str, TIMEOUT);
}
~t_http_connection()

View File

@ -39,28 +39,28 @@ namespace {
<< "difficulty: " << boost::lexical_cast<std::string>(header.difficulty) << std::endl
<< "reward: " << boost::lexical_cast<std::string>(header.reward);
}
}
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;
std::string build_url(uint32_t host, uint16_t port, std::string const & relative_url)
{
std::string result =
"http://"
+ boost::lexical_cast<std::string>(host)
+ ":"
+ boost::lexical_cast<std::string>(port)
+ relative_url;
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);

View File

@ -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();