|
| 1 | +#include <ipc/interfaces.h> |
| 2 | + |
| 3 | +#include <chain.h> |
| 4 | +#include <chainparams.h> |
| 5 | +#include <init.h> |
| 6 | +#include <ipc/util.h> |
| 7 | +#include <net.h> |
| 8 | +#include <net_processing.h> |
| 9 | +#include <scheduler.h> |
| 10 | +#include <ui_interface.h> |
| 11 | +#include <util.h> |
| 12 | +#include <validation.h> |
| 13 | +#include <wallet/wallet.h> |
| 14 | + |
| 15 | +#include <boost/thread.hpp> |
| 16 | + |
| 17 | +namespace ipc { |
| 18 | +namespace { |
| 19 | + |
| 20 | +class HandlerImpl : public Handler |
| 21 | +{ |
| 22 | +public: |
| 23 | + HandlerImpl(boost::signals2::connection connection) : connection(std::move(connection)) {} |
| 24 | + void disconnect() override { connection.disconnect(); } |
| 25 | + boost::signals2::scoped_connection connection; |
| 26 | +}; |
| 27 | + |
| 28 | +class WalletImpl : public Wallet |
| 29 | +{ |
| 30 | +public: |
| 31 | + WalletImpl(CWallet& wallet) : wallet(wallet) {} |
| 32 | + CAmount getBalance() override { return wallet.GetBalance(); } |
| 33 | + CWallet& wallet; |
| 34 | +}; |
| 35 | + |
| 36 | +class NodeImpl : public Node |
| 37 | +{ |
| 38 | +public: |
| 39 | + void parseParameters(int argc, const char* const argv[]) override { ::ParseParameters(argc, argv); } |
| 40 | + void softSetArg(const std::string& arg, const std::string& value) override { ::SoftSetArg(arg, value); } |
| 41 | + void softSetBoolArg(const std::string& arg, bool value) override { ::SoftSetBoolArg(arg, value); } |
| 42 | + void readConfigFile(const std::string& confPath) override { ::ReadConfigFile(confPath); } |
| 43 | + void selectParams(const std::string& network) override { ::SelectParams(network); } |
| 44 | + bool appInit() override |
| 45 | + { |
| 46 | + return ::AppInitBasicSetup() && ::AppInitParameterInteraction() && ::AppInitSanityChecks() && |
| 47 | + ::AppInitMain(threadGroup, scheduler); |
| 48 | + } |
| 49 | + void appShutdown() override |
| 50 | + { |
| 51 | + ::Interrupt(threadGroup); |
| 52 | + threadGroup.join_all(); |
| 53 | + ::Shutdown(); |
| 54 | + } |
| 55 | + bool shutdownRequested() override { return ::ShutdownRequested(); } |
| 56 | + std::string helpMessage(HelpMessageMode mode) override { return ::HelpMessage(mode); } |
| 57 | + std::unique_ptr<Wallet> getWallet() override { return util::MakeUnique<WalletImpl>(*pwalletMain); } |
| 58 | + std::unique_ptr<Handler> handleMessageBox(MessageBoxFn fn) override |
| 59 | + { |
| 60 | + return util::MakeUnique<HandlerImpl>(uiInterface.ThreadSafeMessageBox.connect(fn)); |
| 61 | + } |
| 62 | + std::unique_ptr<Handler> handleQuestion(QuestionFn fn) override |
| 63 | + { |
| 64 | + return util::MakeUnique<HandlerImpl>(uiInterface.ThreadSafeQuestion.connect(fn)); |
| 65 | + } |
| 66 | + std::unique_ptr<Handler> handleInitMessage(InitMessageFn fn) override |
| 67 | + { |
| 68 | + return util::MakeUnique<HandlerImpl>(uiInterface.InitMessage.connect(fn)); |
| 69 | + } |
| 70 | + std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) override |
| 71 | + { |
| 72 | + return util::MakeUnique<HandlerImpl>(uiInterface.NotifyNumConnectionsChanged.connect(fn)); |
| 73 | + } |
| 74 | + std::unique_ptr<Handler> handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn) override |
| 75 | + { |
| 76 | + return util::MakeUnique<HandlerImpl>(uiInterface.NotifyNetworkActiveChanged.connect(fn)); |
| 77 | + } |
| 78 | + std::unique_ptr<Handler> handleNotifyAlertChanged(NotifyAlertChangedFn fn) override |
| 79 | + { |
| 80 | + return util::MakeUnique<HandlerImpl>(uiInterface.NotifyAlertChanged.connect(fn)); |
| 81 | + } |
| 82 | + std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) override |
| 83 | + { |
| 84 | + return util::MakeUnique<HandlerImpl>(uiInterface.ShowProgress.connect(fn)); |
| 85 | + } |
| 86 | + std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) override |
| 87 | + { |
| 88 | + return util::MakeUnique<HandlerImpl>( |
| 89 | + uiInterface.NotifyBlockTip.connect([fn](bool initialDownload, const CBlockIndex* block) { |
| 90 | + fn(initialDownload, block->nHeight, block->GetBlockTime(), |
| 91 | + GuessVerificationProgress(Params().TxData(), block)); |
| 92 | + })); |
| 93 | + } |
| 94 | + std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) override |
| 95 | + { |
| 96 | + return util::MakeUnique<HandlerImpl>( |
| 97 | + uiInterface.NotifyHeaderTip.connect([fn](bool initialDownload, const CBlockIndex* block) { |
| 98 | + fn(initialDownload, block->nHeight, block->GetBlockTime(), |
| 99 | + GuessVerificationProgress(Params().TxData(), block)); |
| 100 | + })); |
| 101 | + } |
| 102 | + std::unique_ptr<Handler> handleBannedListChanged(BannedListChangedFn fn) override |
| 103 | + { |
| 104 | + return util::MakeUnique<HandlerImpl>(uiInterface.BannedListChanged.connect(fn)); |
| 105 | + } |
| 106 | + bool getNodesStats(NodesStats& stats) override |
| 107 | + { |
| 108 | + stats.clear(); |
| 109 | + |
| 110 | + if (g_connman) { |
| 111 | + std::vector<CNodeStats> statsTemp; |
| 112 | + g_connman->GetNodeStats(statsTemp); |
| 113 | + |
| 114 | + stats.reserve(statsTemp.size()); |
| 115 | + for (auto& nodeStatsTemp : statsTemp) { |
| 116 | + stats.emplace_back(std::move(nodeStatsTemp), false, CNodeStateStats()); |
| 117 | + } |
| 118 | + |
| 119 | + // Try to retrieve the CNodeStateStats for each node. |
| 120 | + TRY_LOCK(cs_main, lockMain); |
| 121 | + if (lockMain) { |
| 122 | + for (auto& nodeStats : stats) { |
| 123 | + std::get<1>(nodeStats) = GetNodeStateStats(std::get<0>(nodeStats).nodeid, std::get<2>(nodeStats)); |
| 124 | + } |
| 125 | + } |
| 126 | + return true; |
| 127 | + } |
| 128 | + return false; |
| 129 | + } |
| 130 | + bool getBanned(banmap_t& banMap) |
| 131 | + { |
| 132 | + if (g_connman) { |
| 133 | + g_connman->GetBanned(banMap); |
| 134 | + return true; |
| 135 | + } |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + boost::thread_group threadGroup; |
| 140 | + ::CScheduler scheduler; |
| 141 | +}; |
| 142 | + |
| 143 | +} // namespace |
| 144 | + |
| 145 | +std::unique_ptr<Node> MakeNode() { return util::MakeUnique<NodeImpl>(); } |
| 146 | + |
| 147 | +} // namespace ipc |
0 commit comments