Removed main.h dependency from rpcserver.cpp
Rebased by @laanwj:
- update for RPC methods added since 84d13ee: setmocktime,
invalidateblock, reconsiderblock. Only the first, setmocktime, required a change,
the other two are thread safe.
This commit is contained in:
committed by
Wladimir J. van der Laan
parent
6b5f5294bb
commit
4401b2d7c5
@@ -7,9 +7,11 @@
|
||||
|
||||
#include "base58.h"
|
||||
#include "init.h"
|
||||
#include "main.h"
|
||||
#include "random.h"
|
||||
#include "sync.h"
|
||||
#include "ui_interface.h"
|
||||
#include "util.h"
|
||||
#include "utilstrencodings.h"
|
||||
#ifdef ENABLE_WALLET
|
||||
#include "wallet.h"
|
||||
#endif
|
||||
@@ -23,11 +25,13 @@
|
||||
#include <boost/iostreams/concepts.hpp>
|
||||
#include <boost/iostreams/stream.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/signals2/signal.hpp>
|
||||
#include <boost/thread.hpp>
|
||||
#include "json/json_spirit_writer_template.h"
|
||||
|
||||
using namespace boost::asio;
|
||||
using namespace json_spirit;
|
||||
using namespace RPCServer;
|
||||
using namespace std;
|
||||
|
||||
static std::string strRPCUserColonPass;
|
||||
@@ -46,6 +50,34 @@ static boost::asio::io_service::work *rpc_dummy_work = NULL;
|
||||
static std::vector<CSubNet> rpc_allow_subnets; //!< List of subnets to allow RPC connections from
|
||||
static std::vector< boost::shared_ptr<ip::tcp::acceptor> > rpc_acceptors;
|
||||
|
||||
static struct CRPCSignals
|
||||
{
|
||||
boost::signals2::signal<void ()> Started;
|
||||
boost::signals2::signal<void ()> Stopped;
|
||||
boost::signals2::signal<void (const CRPCCommand&)> PreCommand;
|
||||
boost::signals2::signal<void (const CRPCCommand&)> PostCommand;
|
||||
} g_rpcSignals;
|
||||
|
||||
void RPCServer::OnStarted(boost::function<void ()> slot)
|
||||
{
|
||||
g_rpcSignals.Started.connect(slot);
|
||||
}
|
||||
|
||||
void RPCServer::OnStopped(boost::function<void ()> slot)
|
||||
{
|
||||
g_rpcSignals.Stopped.connect(slot);
|
||||
}
|
||||
|
||||
void RPCServer::OnPreCommand(boost::function<void (const CRPCCommand&)> slot)
|
||||
{
|
||||
g_rpcSignals.PreCommand.connect(boost::bind(slot, _1));
|
||||
}
|
||||
|
||||
void RPCServer::OnPostCommand(boost::function<void (const CRPCCommand&)> slot)
|
||||
{
|
||||
g_rpcSignals.PostCommand.connect(boost::bind(slot, _1));
|
||||
}
|
||||
|
||||
void RPCTypeCheck(const Array& params,
|
||||
const list<Value_type>& typesExpected,
|
||||
bool fAllowNull)
|
||||
@@ -693,6 +725,7 @@ void StartRPCThreads()
|
||||
for (int i = 0; i < GetArg("-rpcthreads", 4); i++)
|
||||
rpc_worker_group->create_thread(boost::bind(&boost::asio::io_service::run, rpc_io_service));
|
||||
fRPCRunning = true;
|
||||
g_rpcSignals.Started();
|
||||
}
|
||||
|
||||
void StartDummyRPCThread()
|
||||
@@ -735,7 +768,7 @@ void StopRPCThreads()
|
||||
deadlineTimers.clear();
|
||||
|
||||
rpc_io_service->stop();
|
||||
cvBlockChange.notify_all();
|
||||
g_rpcSignals.Stopped();
|
||||
if (rpc_worker_group != NULL)
|
||||
rpc_worker_group->join_all();
|
||||
delete rpc_dummy_work; rpc_dummy_work = NULL;
|
||||
@@ -978,45 +1011,20 @@ json_spirit::Value CRPCTable::execute(const std::string &strMethod, const json_s
|
||||
const CRPCCommand *pcmd = tableRPC[strMethod];
|
||||
if (!pcmd)
|
||||
throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Method not found");
|
||||
#ifdef ENABLE_WALLET
|
||||
if (pcmd->reqWallet && !pwalletMain)
|
||||
throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Method not found (disabled)");
|
||||
#endif
|
||||
|
||||
// Observe safe mode
|
||||
string strWarning = GetWarnings("rpc");
|
||||
if (strWarning != "" && !GetBoolArg("-disablesafemode", false) &&
|
||||
!pcmd->okSafeMode)
|
||||
throw JSONRPCError(RPC_FORBIDDEN_BY_SAFE_MODE, string("Safe mode: ") + strWarning);
|
||||
g_rpcSignals.PreCommand(*pcmd);
|
||||
|
||||
try
|
||||
{
|
||||
// Execute
|
||||
Value result;
|
||||
{
|
||||
if (pcmd->threadSafe)
|
||||
result = pcmd->actor(params, false);
|
||||
#ifdef ENABLE_WALLET
|
||||
else if (!pwalletMain) {
|
||||
LOCK(cs_main);
|
||||
result = pcmd->actor(params, false);
|
||||
} else {
|
||||
LOCK2(cs_main, pwalletMain->cs_wallet);
|
||||
result = pcmd->actor(params, false);
|
||||
}
|
||||
#else // ENABLE_WALLET
|
||||
else {
|
||||
LOCK(cs_main);
|
||||
result = pcmd->actor(params, false);
|
||||
}
|
||||
#endif // !ENABLE_WALLET
|
||||
}
|
||||
return result;
|
||||
return pcmd->actor(params, false);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
throw JSONRPCError(RPC_MISC_ERROR, e.what());
|
||||
}
|
||||
|
||||
g_rpcSignals.PostCommand(*pcmd);
|
||||
}
|
||||
|
||||
std::string HelpExampleCli(string methodname, string args){
|
||||
|
||||
Reference in New Issue
Block a user