mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
Split up RPCService
This commit is contained in:
@@ -174,7 +174,7 @@
|
||||
#endif
|
||||
|
||||
// Here temporarily
|
||||
// Controls whether or not the new RPCService::Manager logic will be
|
||||
// Controls whether or not the new RPC::Manager logic will be
|
||||
// used to invoke RPC commands before they pass to the original code.
|
||||
#ifndef RIPPLE_USE_RPC_SERVICE_MANAGER
|
||||
#define RIPPLE_USE_RPC_SERVICE_MANAGER 0
|
||||
|
||||
52
src/ripple/rpc/api/Handler.h
Normal file
52
src/ripple/rpc/api/Handler.h
Normal file
@@ -0,0 +1,52 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
Copyright (c) 2011-2013, OpenCoin, Inc.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef RIPPLE_RPC_HANDLER_H_INCLUDED
|
||||
#define RIPPLE_RPC_HANDLER_H_INCLUDED
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace ripple {
|
||||
using namespace beast;
|
||||
|
||||
namespace RPC {
|
||||
|
||||
/** An invokable handler for a particular RPC method. */
|
||||
class Handler
|
||||
{
|
||||
public:
|
||||
/** Create a handler with the specified method and function. */
|
||||
template <typename Function> // allocator
|
||||
Handler (std::string const& method_, Function function)
|
||||
: m_method (method_)
|
||||
, m_function (function)
|
||||
{
|
||||
}
|
||||
|
||||
Handler (Handler const& other);
|
||||
Handler& operator= (Handler const& other);
|
||||
|
||||
/** Returns the method called when this handler is invoked. */
|
||||
std::string const& method() const;
|
||||
|
||||
/** Synchronously invoke the method on the associated service.
|
||||
Thread safety:
|
||||
Determined by the owner.
|
||||
*/
|
||||
Json::Value operator() (Json::Value const& args) const;
|
||||
|
||||
private:
|
||||
std::string m_method;
|
||||
SharedFunction <Json::Value (Json::Value const&)> m_function;
|
||||
};
|
||||
|
||||
/** The type of container that holds a set of Handler objects. */
|
||||
typedef std::vector <Handler> Handlers;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
65
src/ripple/rpc/api/Manager.h
Normal file
65
src/ripple/rpc/api/Manager.h
Normal file
@@ -0,0 +1,65 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
Copyright (c) 2011-2013, OpenCoin, Inc.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef RIPPLE_RPC_MANAGER_H_INCLUDED
|
||||
#define RIPPLE_RPC_MANAGER_H_INCLUDED
|
||||
|
||||
#include "../../../beast/beast/utility/Journal.h"
|
||||
|
||||
#include "Handler.h"
|
||||
#include "Service.h"
|
||||
|
||||
namespace ripple {
|
||||
using namespace beast;
|
||||
|
||||
namespace RPC {
|
||||
|
||||
/** Manages a collection of Service interface objects. */
|
||||
class Manager
|
||||
{
|
||||
public:
|
||||
static Manager* New (Journal journal);
|
||||
|
||||
virtual ~Manager() { }
|
||||
|
||||
/** Add a service.
|
||||
The list of commands that the service handles is enumerated and
|
||||
added to the manager's dispatch table.
|
||||
Thread safety:
|
||||
Safe to call from any thread.
|
||||
May only be called once for a given service.
|
||||
*/
|
||||
virtual void add (Service& service) = 0;
|
||||
|
||||
/** Add a subclass of Service and return the original pointer.
|
||||
This is provided as a convenient so that RPCService objects may
|
||||
be added from ctor-initializer lists.
|
||||
*/
|
||||
template <class Derived>
|
||||
Derived* add (Derived* derived)
|
||||
{
|
||||
add (*(static_cast <Service*>(derived)));
|
||||
return derived;
|
||||
}
|
||||
|
||||
/** Execute an RPC command synchronously.
|
||||
On return, if result.first == `true` then result.second will
|
||||
have the Json return value from the call of the handler.
|
||||
*/
|
||||
virtual std::pair <bool, Json::Value> call (
|
||||
std::string const& method, Json::Value const& args) = 0;
|
||||
|
||||
/** Returns the Handler for the specified method, or nullptr.
|
||||
Thread safety:
|
||||
Safe to call from any threads.
|
||||
*/
|
||||
virtual Handler const* find (std::string const& method) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,152 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
Copyright (c) 2011-2013, OpenCoin, Inc.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef RIPPLE_FRAME_RPCSERVICE_H_INCLUDED
|
||||
#define RIPPLE_FRAME_RPCSERVICE_H_INCLUDED
|
||||
|
||||
#include "../../../beast/beast/utility/Journal.h"
|
||||
|
||||
namespace ripple
|
||||
{
|
||||
|
||||
using namespace beast;
|
||||
|
||||
/** Interface for abstacting RPC commands processing. */
|
||||
class RPCService : public Uncopyable
|
||||
{
|
||||
public:
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
/** An invokable handler for a particular method. */
|
||||
class Handler
|
||||
{
|
||||
public:
|
||||
/** Create a handler with the specified method and function. */
|
||||
template <typename Function> // allocator
|
||||
Handler (std::string const& method_, Function function)
|
||||
: m_method (method_)
|
||||
, m_function (function)
|
||||
{
|
||||
}
|
||||
|
||||
Handler (Handler const& other)
|
||||
: m_method (other.m_method)
|
||||
, m_function (other.m_function)
|
||||
{
|
||||
}
|
||||
|
||||
Handler& operator= (Handler const& other)
|
||||
{
|
||||
m_method = other.m_method;
|
||||
m_function = other.m_function;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** Returns the method called when this handler is invoked. */
|
||||
std::string const& method() const
|
||||
{
|
||||
return m_method;
|
||||
}
|
||||
|
||||
/** Synchronously invoke the method on the associated service.
|
||||
Thread safety:
|
||||
Determined by the owner.
|
||||
*/
|
||||
Json::Value operator() (Json::Value const& args) const
|
||||
{
|
||||
return m_function (args);
|
||||
}
|
||||
|
||||
private:
|
||||
std::string m_method;
|
||||
SharedFunction <Json::Value (Json::Value const&)> m_function;
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
/** Manages a collection of RPCService interface objects. */
|
||||
class Manager
|
||||
{
|
||||
public:
|
||||
static Manager* New (Journal journal);
|
||||
|
||||
virtual ~Manager() { }
|
||||
|
||||
/** Add a service.
|
||||
The list of commands that the service handles is enumerated and
|
||||
added to the manager's dispatch table.
|
||||
Thread safety:
|
||||
Safe to call from any thread.
|
||||
May only be called once for a given service.
|
||||
*/
|
||||
virtual void add (RPCService& service) = 0;
|
||||
|
||||
/** Add a subclass of RPCService and return the original pointer.
|
||||
This is provided as a convenient so that RPCService objects may
|
||||
be added from ctor-initializer lists.
|
||||
*/
|
||||
template <class Derived>
|
||||
Derived* add (Derived* derived)
|
||||
{
|
||||
add (*(static_cast <RPCService*>(derived)));
|
||||
return derived;
|
||||
}
|
||||
|
||||
/** Execute an RPC command synchronously.
|
||||
On return, if result.first == `true` then result.second will
|
||||
have the Json return value from the call of the handler.
|
||||
*/
|
||||
virtual std::pair <bool, Json::Value> call (
|
||||
std::string const& method, Json::Value const& args) = 0;
|
||||
|
||||
/** Returns the Handler for the specified method, or nullptr.
|
||||
Thread safety:
|
||||
Safe to call from any threads.
|
||||
*/
|
||||
virtual Handler const* find (std::string const& method) = 0;
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
public:
|
||||
typedef std::vector <Handler> Handlers;
|
||||
|
||||
/** Create the service.
|
||||
Derived classes will usually call add() repeatedly from their
|
||||
constructor to fill in the list of handlers prior to Manager::add.
|
||||
*/
|
||||
RPCService ();
|
||||
|
||||
virtual ~RPCService ();
|
||||
|
||||
/** Returns the handlers associated with this service. */
|
||||
Handlers const& handlers() const
|
||||
{
|
||||
return m_handlers;
|
||||
}
|
||||
|
||||
/** Add a handler for the specified method.
|
||||
Adding a handler after the service is already associated with a
|
||||
Manager results in undefined behavior.
|
||||
Thread safety:
|
||||
May not be called concurrently.
|
||||
*/
|
||||
template <typename Function>
|
||||
void addRPCHandler (std::string const& method, Function function)
|
||||
{
|
||||
m_handlers.push_back (Handler (method, function));
|
||||
}
|
||||
|
||||
private:
|
||||
class ManagerImp;
|
||||
|
||||
Handlers m_handlers;
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
49
src/ripple/rpc/api/Service.h
Normal file
49
src/ripple/rpc/api/Service.h
Normal file
@@ -0,0 +1,49 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
Copyright (c) 2011-2013, OpenCoin, Inc.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef RIPPLE_RPC_SERVCE_H_INCLUDED
|
||||
#define RIPPLE_RPC_SERVCE_H_INCLUDED
|
||||
|
||||
#include "Handler.h"
|
||||
|
||||
namespace ripple {
|
||||
namespace RPC {
|
||||
|
||||
/** Interface for abstacting RPC commands processing. */
|
||||
class Service : public Uncopyable
|
||||
{
|
||||
public:
|
||||
/** Create the service.
|
||||
Derived classes will usually call add() repeatedly from their
|
||||
constructor to fill in the list of handlers prior to Manager::add.
|
||||
*/
|
||||
Service ();
|
||||
|
||||
virtual ~Service ();
|
||||
|
||||
/** Returns the handlers associated with this service. */
|
||||
Handlers const& handlers() const;
|
||||
|
||||
/** Add a handler for the specified method.
|
||||
Adding a handler after the service is already associated with a
|
||||
Manager results in undefined behavior.
|
||||
Thread safety:
|
||||
May not be called concurrently.
|
||||
*/
|
||||
template <typename Function>
|
||||
void addRPCHandler (std::string const& method, Function function)
|
||||
{
|
||||
m_handlers.push_back (Handler (method, function));
|
||||
}
|
||||
|
||||
private:
|
||||
Handlers m_handlers;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
34
src/ripple/rpc/impl/Handler.cpp
Normal file
34
src/ripple/rpc/impl/Handler.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
Copyright (c) 2011-2013, OpenCoin, Inc.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
namespace ripple {
|
||||
namespace RPC {
|
||||
|
||||
Handler::Handler (Handler const& other)
|
||||
: m_method (other.m_method)
|
||||
, m_function (other.m_function)
|
||||
{
|
||||
}
|
||||
|
||||
Handler& Handler::operator= (Handler const& other)
|
||||
{
|
||||
m_method = other.m_method;
|
||||
m_function = other.m_function;
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::string const& Handler::method() const
|
||||
{
|
||||
return m_method;
|
||||
}
|
||||
|
||||
Json::Value Handler::operator() (Json::Value const& args) const
|
||||
{
|
||||
return m_function (args);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
16
src/ripple/rpc/impl/Manager.cpp
Normal file
16
src/ripple/rpc/impl/Manager.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
Copyright (c) 2011-2013, OpenCoin, Inc.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
namespace ripple {
|
||||
namespace RPC {
|
||||
|
||||
Manager* Manager::New (Journal journal)
|
||||
{
|
||||
return new ManagerImpl (journal);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -4,10 +4,13 @@
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
namespace ripple
|
||||
{
|
||||
#ifndef RIPPLE_RPC_MANAGERIMPL_H_INCLUDED
|
||||
#define RIPPLE_RPC_MANAGERIMPL_H_INCLUDED
|
||||
|
||||
class RPCService::ManagerImp : public RPCService::Manager
|
||||
namespace ripple {
|
||||
namespace RPC {
|
||||
|
||||
class ManagerImpl : public Manager
|
||||
{
|
||||
public:
|
||||
// The type of map we use to look up by function name.
|
||||
@@ -16,18 +19,18 @@ public:
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
explicit ManagerImp (Journal journal)
|
||||
explicit ManagerImpl (Journal journal)
|
||||
: m_journal (journal)
|
||||
{
|
||||
}
|
||||
|
||||
~ManagerImp()
|
||||
~ManagerImpl()
|
||||
{
|
||||
}
|
||||
|
||||
void add (RPCService& service)
|
||||
void add (Service& service)
|
||||
{
|
||||
Handlers const& handlers (service.m_handlers);
|
||||
Handlers const& handlers (service.handlers());
|
||||
|
||||
SharedState::Access state (m_state);
|
||||
|
||||
@@ -76,21 +79,7 @@ private:
|
||||
SharedState m_state;
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
RPCService::Manager* RPCService::Manager::New (Journal journal)
|
||||
{
|
||||
return new RPCService::ManagerImp (journal);
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
RPCService::RPCService ()
|
||||
{
|
||||
}
|
||||
|
||||
RPCService::~RPCService ()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
||||
24
src/ripple/rpc/impl/Service.cpp
Normal file
24
src/ripple/rpc/impl/Service.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
Copyright (c) 2011-2013, OpenCoin, Inc.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
namespace ripple {
|
||||
namespace RPC {
|
||||
|
||||
Service::Service ()
|
||||
{
|
||||
}
|
||||
|
||||
Service::~Service ()
|
||||
{
|
||||
}
|
||||
|
||||
Handlers const& Service::handlers() const
|
||||
{
|
||||
return m_handlers;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -11,4 +11,7 @@
|
||||
#include "beast/modules/beast_core/system/BeforeBoost.h" // must come first
|
||||
#include <boost/unordered_map.hpp>
|
||||
|
||||
#include "api/RPCService.cpp"
|
||||
# include "impl/ManagerImpl.h"
|
||||
#include "impl/Manager.cpp"
|
||||
#include "impl/Handler.cpp"
|
||||
#include "impl/Service.cpp"
|
||||
|
||||
@@ -11,6 +11,8 @@
|
||||
|
||||
#include "../json/ripple_json.h"
|
||||
|
||||
#include "api/RPCService.h"
|
||||
# include "api/Handler.h"
|
||||
# include "api/Service.h"
|
||||
#include "api/Manager.h"
|
||||
|
||||
#endif
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace Validators
|
||||
|
||||
All operations are performed asynchronously on an internal thread.
|
||||
*/
|
||||
class Manager : public RPCService
|
||||
class Manager : public RPC::Service
|
||||
{
|
||||
public:
|
||||
/** Create a new Manager object.
|
||||
|
||||
@@ -125,7 +125,7 @@ public:
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
//
|
||||
// RPCService
|
||||
// RPC::Service
|
||||
//
|
||||
|
||||
Json::Value rpcPrint (Json::Value const& args)
|
||||
|
||||
@@ -56,7 +56,7 @@ public:
|
||||
, m_tempNodeCache ("NodeCache", 16384, 90)
|
||||
, m_sleCache ("LedgerEntryCache", 4096, 120)
|
||||
|
||||
, m_rpcServiceManager (RPCService::Manager::New (
|
||||
, m_rpcServiceManager (RPC::Manager::New (
|
||||
LogJournal::get <RPCServiceManagerLog> ()))
|
||||
|
||||
// The JobQueue has to come pretty early since
|
||||
@@ -173,7 +173,7 @@ public:
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
RPCService::Manager& getRPCServiceManager()
|
||||
RPC::Manager& getRPCServiceManager()
|
||||
{
|
||||
return *m_rpcServiceManager;
|
||||
}
|
||||
@@ -800,7 +800,7 @@ private:
|
||||
LocalCredentials m_localCredentials;
|
||||
TransactionMaster m_txMaster;
|
||||
|
||||
ScopedPointer <RPCService::Manager> m_rpcServiceManager;
|
||||
ScopedPointer <RPC::Manager> m_rpcServiceManager;
|
||||
|
||||
// These are Stoppable-related
|
||||
ScopedPointer <JobQueue> m_jobQueue;
|
||||
|
||||
@@ -76,7 +76,7 @@ public:
|
||||
|
||||
virtual boost::asio::io_service& getIOService () = 0;
|
||||
|
||||
virtual RPCService::Manager& getRPCServiceManager() = 0;
|
||||
virtual RPC::Manager& getRPCServiceManager() = 0;
|
||||
virtual NodeCache& getTempNodeCache () = 0;
|
||||
virtual SLECache& getSLECache () = 0;
|
||||
virtual Validators::Manager& getValidators () = 0;
|
||||
|
||||
@@ -5,9 +5,9 @@
|
||||
//==============================================================================
|
||||
|
||||
// Used for logging
|
||||
struct RPC;
|
||||
struct RPCLog;
|
||||
|
||||
SETUP_LOG (RPC)
|
||||
SETUP_LOGN (RPCLog, "RPC")
|
||||
|
||||
unsigned int const gMaxHTTPHeaderSize = 0x02000000;
|
||||
|
||||
@@ -75,7 +75,7 @@ std::string rfc1123Time ()
|
||||
|
||||
std::string HTTPReply (int nStatus, const std::string& strMsg)
|
||||
{
|
||||
WriteLog (lsTRACE, RPC) << "HTTP Reply " << nStatus << " " << strMsg;
|
||||
WriteLog (lsTRACE, RPCLog) << "HTTP Reply " << nStatus << " " << strMsg;
|
||||
|
||||
if (nStatus == 401)
|
||||
return strprintf ("HTTP/1.0 401 Authorization Required\r\n"
|
||||
|
||||
Reference in New Issue
Block a user