diff --git a/src/Makefile.am b/src/Makefile.am --- a/src/Makefile.am +++ b/src/Makefile.am @@ -139,10 +139,13 @@ random.h \ reverselock.h \ rpc/blockchain.h \ + rpc/command.h \ + rpc/commandname.h \ rpc/client.h \ rpc/jsonrpcrequest.h \ rpc/mining.h \ rpc/misc.h \ + rpc/parameter.h \ rpc/protocol.h \ rpc/server.h \ rpc/tojson.h \ diff --git a/src/rpc/command.h b/src/rpc/command.h new file mode 100644 --- /dev/null +++ b/src/rpc/command.h @@ -0,0 +1,41 @@ +// Copyright (c) 2018 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_RPC_COMMAND_H +#define BITCOIN_RPC_COMMAND_H + +#include "rpc/commandname.h" +#include "rpc/jsonrpcrequest.h" +#include "rpc/parameter.h" + +#include + +#include + +#include + +/** + * Abstract class to define RPC commands. + */ +class RPCCommand : public boost::noncopyable { +public: + RPCCommandName name; + std::vector params; + + RPCCommand(const RPCCommandName &nameIn, + const std::vector ¶msIn) + : name(nameIn), params(paramsIn) {} + + RPCCommandName GetName() const { return name; } + + std::vector GetParameters() const { return params; } + + /** + * Execute() defines the behavior of the command and must be defined for + * all commands. + */ + virtual UniValue Execute(const JSONRPCRequest &jsonRequest) const = 0; +}; + +#endif // BITCOIN_RPC_COMMAND_H diff --git a/src/rpc/commandname.h b/src/rpc/commandname.h new file mode 100644 --- /dev/null +++ b/src/rpc/commandname.h @@ -0,0 +1,22 @@ +// Copyright (c) 2018 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_RPC_COMMAND_NAME_H +#define BITCOIN_RPC_COMMAND_NAME_H + +#include + +#include + +class RPCCommandName { +private: + std::string name; + +public: + RPCCommandName(std::string nameIn) : name(nameIn){}; + + std::string GetName() const { return name; } +}; + +#endif // BITCOIN_RPC_COMMAND_NAME_H diff --git a/src/rpc/parameter.h b/src/rpc/parameter.h new file mode 100644 --- /dev/null +++ b/src/rpc/parameter.h @@ -0,0 +1,36 @@ +// Copyright (c) 2018 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_RPC_PARAMETER_H +#define BITCOIN_RPC_PARAMETER_H + +#include + +#include + +/** + * Abstract class to define RPC parameters. + * Any class derived from RPCParameter may be used by multiple RPCCommands. + */ +class RPCParameter { +private: + std::string name; + std::string description; + +public: + RPCParameter(std::string name, std::string description); + + std::string GetName() const { return name; } + + std::string GetDescription() const { return description; } + + /** + * Validate()'s input for this parameter. For example, this function + * could be defined to only accept a certain range for integer values. + * Validate() should return true if the input is valid, false otherwise. + */ + virtual bool Validate(UniValue input) const = 0; +}; + +#endif // BITCOIN_RPC_PARAMETER_H