diff options
author | Lee Clagett <code@leeclagett.com> | 2017-01-25 00:16:05 -0500 |
---|---|---|
committer | Lee Clagett <code@leeclagett.com> | 2017-01-25 15:39:32 -0500 |
commit | c02e1cb943903d34becfaf1c6d9cd71e4424d748 (patch) | |
tree | a363ce155ba8f6b14a924c84380ec2886894f22d /src/common | |
parent | Merge pull request #1622 (diff) | |
download | monero-c02e1cb943903d34becfaf1c6d9cd71e4424d748.tar.xz |
Updates to epee HTTP client code
- http_simple_client now uses std::chrono for timeouts
- http_simple_client accepts timeouts per connect / invoke call
- shortened names of epee http invoke functions
- invoke command functions only take relative path, connection
is not automatically performed
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/http_connection.h | 17 | ||||
-rw-r--r-- | src/common/rpc_client.h | 35 |
2 files changed, 17 insertions, 35 deletions
diff --git a/src/common/http_connection.h b/src/common/http_connection.h index 156474cdd..8a786361a 100644 --- a/src/common/http_connection.h +++ b/src/common/http_connection.h @@ -28,6 +28,7 @@ #pragma once +#include <chrono> #include "string_tools.h" #include "net/http_client.h" @@ -38,20 +39,16 @@ private: epee::net_utils::http::http_simple_client * mp_http_client; bool m_ok; public: - static unsigned int const TIMEOUT = 200000; + static constexpr std::chrono::seconds TIMEOUT() + { + return std::chrono::minutes(3) + std::chrono::seconds(30); + } - t_http_connection( - epee::net_utils::http::http_simple_client * p_http_client - , uint32_t ip - , uint16_t port - ) + t_http_connection(epee::net_utils::http::http_simple_client* p_http_client) : mp_http_client(p_http_client) , m_ok(false) { - // TODO fix http client so that it accepts properly typed arguments - std::string ip_str = epee::string_tools::get_ip_string_from_int32(ip); - std::string port_str = boost::lexical_cast<std::string>(port); - m_ok = mp_http_client->connect(ip_str, port_str, TIMEOUT); + m_ok = mp_http_client->connect(TIMEOUT()); } ~t_http_connection() diff --git a/src/common/rpc_client.h b/src/common/rpc_client.h index 9c0036198..f5ecc8b50 100644 --- a/src/common/rpc_client.h +++ b/src/common/rpc_client.h @@ -34,7 +34,6 @@ #include "storages/http_abstract_invoke.h" #include "net/http_client.h" #include "string_tools.h" -#include <boost/lexical_cast.hpp> namespace tools { @@ -42,27 +41,16 @@ namespace tools { private: epee::net_utils::http::http_simple_client m_http_client; - uint32_t m_ip; - uint16_t m_port; public: t_rpc_client( uint32_t ip , uint16_t port ) : m_http_client{} - , m_ip{ip} - , m_port{port} - {} - - std::string build_url(std::string const & relative_url) const { - std::string result = - "http://" - + epee::string_tools::get_ip_string_from_int32(m_ip) - + ":" - + boost::lexical_cast<std::string>(m_port) - + relative_url; - return result; + m_http_client.set_server( + epee::string_tools::get_ip_string_from_int32(ip), std::to_string(port) + ); } template <typename T_req, typename T_res> @@ -72,8 +60,7 @@ namespace tools , std::string const & method_name ) { - std::string rpc_url = build_url("/json_rpc"); - t_http_connection connection(&m_http_client, m_ip, m_port); + t_http_connection connection(&m_http_client); bool ok = connection.is_open(); if (!ok) @@ -81,7 +68,7 @@ namespace tools fail_msg_writer() << "Couldn't connect to daemon"; return false; } - ok = ok && epee::net_utils::invoke_http_json_rpc(rpc_url, method_name, req, res, m_http_client); + ok = ok && epee::net_utils::invoke_http_json_rpc("/json_rpc", method_name, req, res, m_http_client, t_http_connection::TIMEOUT()); if (!ok) { fail_msg_writer() << "Daemon request failed"; @@ -101,11 +88,10 @@ namespace tools , std::string const & fail_msg ) { - std::string rpc_url = build_url("/json_rpc"); - t_http_connection connection(&m_http_client, m_ip, m_port); + t_http_connection connection(&m_http_client); bool ok = connection.is_open(); - ok = ok && epee::net_utils::invoke_http_json_rpc(rpc_url, method_name, req, res, m_http_client); + ok = ok && epee::net_utils::invoke_http_json_rpc("/json_rpc", method_name, req, res, m_http_client, t_http_connection::TIMEOUT()); if (!ok) { fail_msg_writer() << "Couldn't connect to daemon"; @@ -130,11 +116,10 @@ namespace tools , std::string const & fail_msg ) { - std::string rpc_url = build_url(relative_url); - t_http_connection connection(&m_http_client, m_ip, m_port); + t_http_connection connection(&m_http_client); bool ok = connection.is_open(); - ok = ok && epee::net_utils::invoke_http_json_remote_command2(rpc_url, req, res, m_http_client); + ok = ok && epee::net_utils::invoke_http_json(relative_url, req, res, m_http_client, t_http_connection::TIMEOUT()); if (!ok) { fail_msg_writer() << "Couldn't connect to daemon"; @@ -153,7 +138,7 @@ namespace tools bool check_connection() { - t_http_connection connection(&m_http_client, m_ip, m_port); + t_http_connection connection(&m_http_client); return connection.is_open(); } }; |