danicoin/src/node_rpc_proxy/NodeRpcProxy.h
2014-08-13 15:07:19 +01:00

90 lines
3.5 KiB
C++

// Copyright (c) 2012-2014, The CryptoNote developers, The Bytecoin developers
//
// This file is part of Bytecoin.
//
// Bytecoin is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Bytecoin is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Bytecoin. If not, see <http://www.gnu.org/licenses/>.
#pragma once
#include <string>
#include <thread>
#include <boost/asio/io_service.hpp>
#include "common/ObserverManager.h"
#include "include_base_utils.h"
#include "net/http_client.h"
#include "InitState.h"
#include "INode.h"
namespace cryptonote {
class NodeRpcProxy : public CryptoNote::INode {
public:
NodeRpcProxy(const std::string& nodeHost, unsigned short nodePort);
virtual ~NodeRpcProxy();
virtual bool addObserver(CryptoNote::INodeObserver* observer);
virtual bool removeObserver(CryptoNote::INodeObserver* observer);
virtual void init(const Callback& callback);
virtual bool shutdown();
virtual size_t getPeerCount() const;
virtual uint64_t getLastLocalBlockHeight() const;
virtual uint64_t getLastKnownBlockHeight() const;
virtual void relayTransaction(const cryptonote::Transaction& transaction, const Callback& callback);
virtual void getRandomOutsByAmounts(std::vector<uint64_t>&& amounts, uint64_t outsCount, std::vector<COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount>& result, const Callback& callback);
virtual void getNewBlocks(std::list<crypto::hash>&& knownBlockIds, std::list<cryptonote::block_complete_entry>& newBlocks, uint64_t& startHeight, const Callback& callback);
virtual void getTransactionOutsGlobalIndices(const crypto::hash& transactionHash, std::vector<uint64_t>& outsGlobalIndices, const Callback& callback);
unsigned int rpcTimeout() const { return m_rpcTimeout; }
void rpcTimeout(unsigned int val) { m_rpcTimeout = val; }
private:
void resetInternalState();
void workerThread(const Callback& initialized_callback);
void pullNodeStatusAndScheduleTheNext();
void updateNodeStatus();
void updatePeerCount();
void doRelayTransaction(const cryptonote::Transaction& transaction, const Callback& callback);
void doGetRandomOutsByAmounts(std::vector<uint64_t>& amounts, uint64_t outsCount, std::vector<COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount>& result, const Callback& callback);
void doGetNewBlocks(std::list<crypto::hash>& knownBlockIds, std::list<cryptonote::block_complete_entry>& newBlocks, uint64_t& startHeight, const Callback& callback);
void doGetTransactionOutsGlobalIndices(const crypto::hash& transactionHash, std::vector<uint64_t>& outsGlobalIndices, const Callback& callback);
private:
tools::InitState m_initState;
std::thread m_workerThread;
boost::asio::io_service m_ioService;
tools::ObserverManager<CryptoNote::INodeObserver> m_observerManager;
std::string m_nodeAddress;
unsigned int m_rpcTimeout;
epee::net_utils::http::http_simple_client m_httpClient;
boost::asio::deadline_timer m_pullTimer;
uint64_t m_pullInterval;
// Internal state
size_t m_peerCount;
uint64_t m_nodeHeight;
uint64_t m_networkHeight;
crypto::hash m_lastKnowHash;
};
}