From eed24b37d98317ed4652040533a7d948777ee0fd Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Sat, 16 Apr 2016 05:48:37 +0800 Subject: [PATCH] checking if connected to deamon fixed --- src/page.h | 18 +++++++----------- src/rpccalls.h | 37 +++++++++++++++++++------------------ 2 files changed, 26 insertions(+), 29 deletions(-) diff --git a/src/page.h b/src/page.h index 205ad14..9a3f90b 100644 --- a/src/page.h +++ b/src/page.h @@ -61,15 +61,14 @@ namespace xmreg { string index(uint64_t page_no = 0, bool refresh_page = false) { + // connect to the deamon if not yet connected + bool is_connected = rpc.connect_to_monero_deamon(); -// bool is_connected = rpc.check_connection(); -// -// cout << "check connection: " << is_connected << endl; -// -// if (!is_connected) -// { -// return "Connection to the Monero demon does not exist or was lost!"; -// } + if (!is_connected) + { + cerr << "Connection to the Monero demon does not exist or was lost!" << endl; + return "Connection to the Monero demon does not exist or was lost!"; + } //get current server timestamp server_timestamp = std::time(nullptr); @@ -112,9 +111,6 @@ namespace xmreg { end_height = end_height - start_height > no_of_last_blocks ? no_of_last_blocks : end_height; - cout << start_height << ", " << end_height << endl; - - // iterate over last no_of_last_blocks of blocks for (uint64_t i = start_height; i <= end_height; ++i) { diff --git a/src/rpccalls.h b/src/rpccalls.h index 0f7e0be..f2368b7 100644 --- a/src/rpccalls.h +++ b/src/rpccalls.h @@ -2,11 +2,14 @@ // Created by mwo on 13/04/16. // + #ifndef CROWXMR_RPCCALLS_H #define CROWXMR_RPCCALLS_H #include "monero_headers.h" +#include + namespace xmreg { @@ -15,26 +18,23 @@ namespace xmreg using namespace std; - using request = cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL::request; - using response = cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL::response; - using http_simple_client = epee::net_utils::http::http_simple_client; - - class rpccalls { - string deamon_url {"http:://127.0.0.1:18081"}; - uint64_t timeout_time {200000}; + string deamon_url ; + uint64_t timeout_time; epee::net_utils::http::url_content url; - http_simple_client m_http_client; - boost::mutex m_daemon_rpc_mutex; + epee::net_utils::http::http_simple_client m_http_client; + std::mutex m_daemon_rpc_mutex; string port; public: - rpccalls() + rpccalls(string _deamon_url = "http:://127.0.0.1:18081", + uint64_t _timeout = 200000) + : deamon_url {_deamon_url}, timeout_time {_timeout} { epee::net_utils::parse_url(deamon_url, url); @@ -42,9 +42,14 @@ namespace xmreg } bool - check_connection() + connect_to_monero_deamon() { - m_daemon_rpc_mutex.lock(); + std::lock_guard guard(m_daemon_rpc_mutex); + + if(m_http_client.is_connected()) + { + return true; + } return m_http_client.connect(url.host, port, @@ -57,14 +62,12 @@ namespace xmreg COMMAND_RPC_GET_HEIGHT::request req; COMMAND_RPC_GET_HEIGHT::response res; - m_daemon_rpc_mutex.lock(); + std::lock_guard guard(m_daemon_rpc_mutex); bool r = epee::net_utils::invoke_http_json_remote_command2( deamon_url + "/getheight", req, res, m_http_client, timeout_time); - m_daemon_rpc_mutex.unlock(); - if (!r) { cerr << "Error connecting to Monero deamon at " @@ -85,14 +88,12 @@ namespace xmreg COMMAND_RPC_GET_TRANSACTION_POOL::request req; COMMAND_RPC_GET_TRANSACTION_POOL::response res; - m_daemon_rpc_mutex.lock(); + std::lock_guard guard(m_daemon_rpc_mutex); bool r = epee::net_utils::invoke_http_json_remote_command2( deamon_url + "/get_transaction_pool", req, res, m_http_client, timeout_time); - m_daemon_rpc_mutex.unlock(); - if (!r) { cerr << "Error connecting to Monero deamon at "