Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/server.h
// Copyright (c) 2010 Satoshi Nakamoto | // Copyright (c) 2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Copyright (c) 2017-2018 The Bitcoin developers | // Copyright (c) 2017-2018 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifndef BITCOIN_RPC_SERVER_H | #ifndef BITCOIN_RPC_SERVER_H | ||||
#define BITCOIN_RPC_SERVER_H | #define BITCOIN_RPC_SERVER_H | ||||
#include "amount.h" | #include "amount.h" | ||||
#include "rpc/command.h" | |||||
#include "rpc/jsonrpcrequest.h" | #include "rpc/jsonrpcrequest.h" | ||||
#include "rpc/protocol.h" | #include "rpc/protocol.h" | ||||
#include "uint256.h" | #include "uint256.h" | ||||
#include <cstdint> | #include <cstdint> | ||||
#include <functional> | #include <functional> | ||||
#include <list> | #include <list> | ||||
#include <map> | #include <map> | ||||
Show All 26 Lines | struct UniValueType { | ||||
bool typeAny; | bool typeAny; | ||||
UniValue::VType type; | UniValue::VType type; | ||||
}; | }; | ||||
/** | /** | ||||
* Class for registering and managing all RPC calls. | * Class for registering and managing all RPC calls. | ||||
*/ | */ | ||||
class RPCServer : public boost::noncopyable { | class RPCServer : public boost::noncopyable { | ||||
private: | |||||
std::map<std::string, RPCCommand *> commands; | |||||
schancel: This should be std::map<RPCCommandName, RPCCommand *>? | |||||
jasonbcoxAuthorUnsubmitted Done Inline ActionsRemoved RPCCommandName since it's an abstraction that's unnecessary at this stage. jasonbcox: Removed RPCCommandName since it's an abstraction that's unnecessary at this stage. | |||||
public: | public: | ||||
RPCServer() {} | RPCServer() {} | ||||
/** | /** | ||||
* Attempts to execute an RPC command from the given request. | * Attempts to execute an RPC command from the given request. | ||||
* If no RPC command exists that matches the request, an error is returned. | * If no RPC command exists that matches the request, an error is returned. | ||||
*/ | */ | ||||
UniValue ExecuteCommand(Config &config, | UniValue ExecuteCommand(Config &config, | ||||
const JSONRPCRequest &request) const; | const JSONRPCRequest &request) const; | ||||
/** | |||||
* Register an RPC command. | |||||
*/ | |||||
void RegisterCommand(RPCCommand *command) { | |||||
schancelUnsubmitted Done Inline ActionsI strongly believe this should accept a factory class that creates a strongly typed RPCCommandClass. The factory should look something like this: // Could use a pair or optional instead class RPCCommandErrorHandler { private: RPCCommand and Error/help value public: RPCCommandErrorHandler( std::string error); UniValue Execute(Config* config) { return UniValue(error); } } class FooCommand : RPCCommand { protected: const std::string Name = "foocommand"; ... request specific type safe parameters... public: FooComand(TypeSafeConstructor); virtual UniValue Execute(Config &config) { ... } static RPCCommand CreateFromJSONRequest(JSONRPCRequest &request) { ... parse strongly typed values from request... ... or return a RPCCommandErrorHandler ... return FooCommand(...parsed request...). } } static std::string GetName(); } void Register(RPCServer &server) { server.RegisterCommand(FooCommand::GetName(), &FooCommand::CreateFromJSONRequest) } schancel: I strongly believe this should accept a factory class that creates a strongly typed… | |||||
jasonbcoxAuthorUnsubmitted Done Inline ActionsI thought this over a lot and it makes sense. Thanks for the example. jasonbcox: I thought this over a lot and it makes sense. Thanks for the example. | |||||
commands[command->GetName().GetNameStr()] = command; | |||||
} | |||||
/** | |||||
* Return a command if one matches the given command name. | |||||
* Returns nullptr if no command matches. | |||||
* | |||||
* TODO: Add support for short names. | |||||
*/ | |||||
RPCCommand *MatchCommand(std::string name) const; | |||||
deadalnixUnsubmitted Done Inline ActionsWhy is this part of the public API ? deadalnix: Why is this part of the public API ? | |||||
jasonbcoxAuthorUnsubmitted Done Inline ActionsFor testing. jasonbcox: For testing. | |||||
}; | }; | ||||
/** | /** | ||||
* Query whether RPC is running | * Query whether RPC is running | ||||
*/ | */ | ||||
bool IsRPCRunning(); | bool IsRPCRunning(); | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 211 Lines • Show Last 20 Lines |
This should be std::map<RPCCommandName, RPCCommand *>?