checking if connected to deamon fixed

master
moneroexamples 9 years ago
parent c5c3fed647
commit eed24b37d9

@ -61,15 +61,14 @@ namespace xmreg {
string string
index(uint64_t page_no = 0, bool refresh_page = false) 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(); if (!is_connected)
// {
// cout << "check connection: " << is_connected << endl; 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!";
// if (!is_connected) }
// {
// return "Connection to the Monero demon does not exist or was lost!";
// }
//get current server timestamp //get current server timestamp
server_timestamp = std::time(nullptr); server_timestamp = std::time(nullptr);
@ -112,9 +111,6 @@ namespace xmreg {
end_height = end_height - start_height > no_of_last_blocks end_height = end_height - start_height > no_of_last_blocks
? no_of_last_blocks : end_height; ? no_of_last_blocks : end_height;
cout << start_height << ", " << end_height << endl;
// iterate over last no_of_last_blocks of blocks // iterate over last no_of_last_blocks of blocks
for (uint64_t i = start_height; i <= end_height; ++i) for (uint64_t i = start_height; i <= end_height; ++i)
{ {

@ -2,11 +2,14 @@
// Created by mwo on 13/04/16. // Created by mwo on 13/04/16.
// //
#ifndef CROWXMR_RPCCALLS_H #ifndef CROWXMR_RPCCALLS_H
#define CROWXMR_RPCCALLS_H #define CROWXMR_RPCCALLS_H
#include "monero_headers.h" #include "monero_headers.h"
#include <mutex>
namespace xmreg namespace xmreg
{ {
@ -15,26 +18,23 @@ namespace xmreg
using namespace std; 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 class rpccalls
{ {
string deamon_url {"http:://127.0.0.1:18081"}; string deamon_url ;
uint64_t timeout_time {200000}; uint64_t timeout_time;
epee::net_utils::http::url_content url; epee::net_utils::http::url_content url;
http_simple_client m_http_client; epee::net_utils::http::http_simple_client m_http_client;
boost::mutex m_daemon_rpc_mutex; std::mutex m_daemon_rpc_mutex;
string port; string port;
public: 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); epee::net_utils::parse_url(deamon_url, url);
@ -42,9 +42,14 @@ namespace xmreg
} }
bool bool
check_connection() connect_to_monero_deamon()
{
std::lock_guard<std::mutex> guard(m_daemon_rpc_mutex);
if(m_http_client.is_connected())
{ {
m_daemon_rpc_mutex.lock(); return true;
}
return m_http_client.connect(url.host, return m_http_client.connect(url.host,
port, port,
@ -57,14 +62,12 @@ namespace xmreg
COMMAND_RPC_GET_HEIGHT::request req; COMMAND_RPC_GET_HEIGHT::request req;
COMMAND_RPC_GET_HEIGHT::response res; COMMAND_RPC_GET_HEIGHT::response res;
m_daemon_rpc_mutex.lock(); std::lock_guard<std::mutex> guard(m_daemon_rpc_mutex);
bool r = epee::net_utils::invoke_http_json_remote_command2( bool r = epee::net_utils::invoke_http_json_remote_command2(
deamon_url + "/getheight", deamon_url + "/getheight",
req, res, m_http_client, timeout_time); req, res, m_http_client, timeout_time);
m_daemon_rpc_mutex.unlock();
if (!r) if (!r)
{ {
cerr << "Error connecting to Monero deamon at " 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::request req;
COMMAND_RPC_GET_TRANSACTION_POOL::response res; COMMAND_RPC_GET_TRANSACTION_POOL::response res;
m_daemon_rpc_mutex.lock(); std::lock_guard<std::mutex> guard(m_daemon_rpc_mutex);
bool r = epee::net_utils::invoke_http_json_remote_command2( bool r = epee::net_utils::invoke_http_json_remote_command2(
deamon_url + "/get_transaction_pool", deamon_url + "/get_transaction_pool",
req, res, m_http_client, timeout_time); req, res, m_http_client, timeout_time);
m_daemon_rpc_mutex.unlock();
if (!r) if (!r)
{ {
cerr << "Error connecting to Monero deamon at " cerr << "Error connecting to Monero deamon at "

Loading…
Cancel
Save