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
This commit is contained in:
Lee Clagett 2017-01-25 00:16:05 -05:00
parent ad91ffe7e5
commit c02e1cb943
15 changed files with 172 additions and 323 deletions

View file

@ -43,6 +43,15 @@ namespace epee {
unsigned int g_test_dbg_lock_sleep = 0;
}
namespace {
template<typename Request, typename Response>
bool connect_and_invoke(const std::string& address, const std::string& path, const Request& request, Response& response)
{
epee::net_utils::http::http_simple_client client{};
return client.set_server(address) && epee::net_utils::invoke_http_json(path, request, response, client);
}
}
namespace Monero {
Wallet *WalletManagerImpl::createWallet(const std::string &path, const std::string &password,
@ -145,9 +154,7 @@ bool WalletManagerImpl::connected(uint32_t *version) const
req_t.jsonrpc = "2.0";
req_t.id = epee::serialization::storage_entry(0);
req_t.method = "get_version";
epee::net_utils::http::http_simple_client http_client;
bool r = epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/json_rpc", req_t, resp_t, http_client);
if (!r)
if (!connect_and_invoke(m_daemonAddress, "/json_rpc", req_t, resp_t))
return false;
if (version)
@ -193,8 +200,7 @@ bool WalletManagerImpl::checkPayment(const std::string &address_text, const std:
cryptonote::COMMAND_RPC_GET_TRANSACTIONS::request req;
cryptonote::COMMAND_RPC_GET_TRANSACTIONS::response res;
req.txs_hashes.push_back(epee::string_tools::pod_to_hex(txid));
epee::net_utils::http::http_simple_client http_client;
if (!epee::net_utils::invoke_http_json_remote_command2(daemon_address + "/gettransactions", req, res, http_client) ||
if (!connect_and_invoke(m_daemonAddress, "/gettransactions", req, res) ||
(res.txs.size() != 1 && res.txs_as_hex.size() != 1))
{
error = tr("failed to get transaction from daemon");
@ -312,8 +318,7 @@ uint64_t WalletManagerImpl::blockchainHeight() const
cryptonote::COMMAND_RPC_GET_INFO::request ireq;
cryptonote::COMMAND_RPC_GET_INFO::response ires;
epee::net_utils::http::http_simple_client http_client;
if (!epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/getinfo", ireq, ires, http_client))
if (!connect_and_invoke(m_daemonAddress, "/getinfo", ireq, ires))
return 0;
return ires.height;
}
@ -323,8 +328,7 @@ uint64_t WalletManagerImpl::blockchainTargetHeight() const
cryptonote::COMMAND_RPC_GET_INFO::request ireq;
cryptonote::COMMAND_RPC_GET_INFO::response ires;
epee::net_utils::http::http_simple_client http_client;
if (!epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/getinfo", ireq, ires, http_client))
if (!connect_and_invoke(m_daemonAddress, "/getinfo", ireq, ires))
return 0;
return ires.target_height >= ires.height ? ires.target_height : ires.height;
}
@ -334,8 +338,7 @@ uint64_t WalletManagerImpl::networkDifficulty() const
cryptonote::COMMAND_RPC_GET_INFO::request ireq;
cryptonote::COMMAND_RPC_GET_INFO::response ires;
epee::net_utils::http::http_simple_client http_client;
if (!epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/getinfo", ireq, ires, http_client))
if (!connect_and_invoke(m_daemonAddress, "/getinfo", ireq, ires))
return 0;
return ires.difficulty;
}
@ -346,7 +349,7 @@ double WalletManagerImpl::miningHashRate() const
cryptonote::COMMAND_RPC_MINING_STATUS::response mres;
epee::net_utils::http::http_simple_client http_client;
if (!epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/mining_status", mreq, mres, http_client))
if (!connect_and_invoke(m_daemonAddress, "/mining_status", mreq, mres))
return 0.0;
if (!mres.active)
return 0.0;
@ -366,7 +369,7 @@ void WalletManagerImpl::hardForkInfo(uint8_t &version, uint64_t &earliest_height
req_t.id = epee::serialization::storage_entry(0);
req_t.method = "hard_fork_info";
req_t.params.version = 0;
bool r = epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/json_rpc", req_t, resp_t, http_client);
bool r = connect_and_invoke(m_daemonAddress, "/json_rpc", req_t, resp_t);
if (!r || resp_t.result.status != CORE_RPC_STATUS_OK)
return;
version = resp_t.result.version;
@ -378,8 +381,7 @@ uint64_t WalletManagerImpl::blockTarget() const
cryptonote::COMMAND_RPC_GET_INFO::request ireq;
cryptonote::COMMAND_RPC_GET_INFO::response ires;
epee::net_utils::http::http_simple_client http_client;
if (!epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/getinfo", ireq, ires, http_client))
if (!connect_and_invoke(m_daemonAddress, "/getinfo", ireq, ires))
return 0;
return ires.target;
}
@ -389,8 +391,7 @@ bool WalletManagerImpl::isMining() const
cryptonote::COMMAND_RPC_MINING_STATUS::request mreq;
cryptonote::COMMAND_RPC_MINING_STATUS::response mres;
epee::net_utils::http::http_simple_client http_client;
if (!epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/mining_status", mreq, mres, http_client))
if (!connect_and_invoke(m_daemonAddress, "/mining_status", mreq, mres))
return false;
return mres.active;
}
@ -403,8 +404,7 @@ bool WalletManagerImpl::startMining(const std::string &address, uint32_t threads
mreq.miner_address = address;
mreq.threads_count = threads;
epee::net_utils::http::http_simple_client http_client;
if (!epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/start_mining", mreq, mres, http_client))
if (!connect_and_invoke(m_daemonAddress, "/start_mining", mreq, mres))
return false;
return mres.status == CORE_RPC_STATUS_OK;
}
@ -414,8 +414,7 @@ bool WalletManagerImpl::stopMining()
cryptonote::COMMAND_RPC_STOP_MINING::request mreq;
cryptonote::COMMAND_RPC_STOP_MINING::response mres;
epee::net_utils::http::http_simple_client http_client;
if (!epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/stop_mining", mreq, mres, http_client))
if (!connect_and_invoke(m_daemonAddress, "/stop_mining", mreq, mres))
return false;
return mres.status == CORE_RPC_STATUS_OK;
}