checking if connected to deamon fixed

This commit is contained in:
moneroexamples 2016-04-16 05:48:37 +08:00
parent c5c3fed647
commit eed24b37d9
2 changed files with 26 additions and 29 deletions

View file

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

View file

@ -2,11 +2,14 @@
// Created by mwo on 13/04/16.
//
#ifndef CROWXMR_RPCCALLS_H
#define CROWXMR_RPCCALLS_H
#include "monero_headers.h"
#include <mutex>
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<std::mutex> 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<std::mutex> 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<std::mutex> 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 "