Split up RPCService

This commit is contained in:
Vinnie Falco
2013-09-22 01:25:11 -07:00
parent b123b1a849
commit 5bd6fb27e6
18 changed files with 310 additions and 195 deletions

View File

@@ -84,7 +84,19 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\ripple\json\ripple_json.cpp" /> <ClCompile Include="..\..\src\ripple\json\ripple_json.cpp" />
<ClCompile Include="..\..\src\ripple\rpc\api\RPCService.cpp"> <ClCompile Include="..\..\src\ripple\rpc\impl\Handler.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\src\ripple\rpc\impl\Manager.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\src\ripple\rpc\impl\Service.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
@@ -1562,7 +1574,10 @@
<ClInclude Include="..\..\src\ripple\json\impl\json_autolink.h" /> <ClInclude Include="..\..\src\ripple\json\impl\json_autolink.h" />
<ClInclude Include="..\..\src\ripple\json\impl\json_batchallocator.h" /> <ClInclude Include="..\..\src\ripple\json\impl\json_batchallocator.h" />
<ClInclude Include="..\..\src\ripple\json\ripple_json.h" /> <ClInclude Include="..\..\src\ripple\json\ripple_json.h" />
<ClInclude Include="..\..\src\ripple\rpc\api\RPCService.h" /> <ClInclude Include="..\..\src\ripple\rpc\api\Handler.h" />
<ClInclude Include="..\..\src\ripple\rpc\api\Manager.h" />
<ClInclude Include="..\..\src\ripple\rpc\api\Service.h" />
<ClInclude Include="..\..\src\ripple\rpc\impl\ManagerImpl.h" />
<ClInclude Include="..\..\src\ripple\rpc\ripple_rpc.h" /> <ClInclude Include="..\..\src\ripple\rpc\ripple_rpc.h" />
<ClInclude Include="..\..\src\ripple\sophia\ripple_sophia.h" /> <ClInclude Include="..\..\src\ripple\sophia\ripple_sophia.h" />
<ClInclude Include="..\..\src\ripple\testoverlay\api\ConfigType.h" /> <ClInclude Include="..\..\src\ripple\testoverlay\api\ConfigType.h" />

View File

@@ -211,6 +211,9 @@
<Filter Include="[1] Ripple\http\impl"> <Filter Include="[1] Ripple\http\impl">
<UniqueIdentifier>{386ebc1c-0cbe-43a6-b48e-ac3c503da0ee}</UniqueIdentifier> <UniqueIdentifier>{386ebc1c-0cbe-43a6-b48e-ac3c503da0ee}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="[1] Ripple\rpc\impl">
<UniqueIdentifier>{95c93134-7e8d-47ec-9649-4a8d5d97ce12}</UniqueIdentifier>
</Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\src\ripple_basics\containers\RangeSet.cpp"> <ClCompile Include="..\..\src\ripple_basics\containers\RangeSet.cpp">
@@ -993,9 +996,6 @@
<ClCompile Include="..\..\src\ripple\rpc\ripple_rpc.cpp"> <ClCompile Include="..\..\src\ripple\rpc\ripple_rpc.cpp">
<Filter>[1] Ripple\rpc</Filter> <Filter>[1] Ripple\rpc</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\ripple\rpc\api\RPCService.cpp">
<Filter>[1] Ripple\rpc\api</Filter>
</ClCompile>
<ClCompile Include="..\..\src\ripple\http\ripple_http.cpp"> <ClCompile Include="..\..\src\ripple\http\ripple_http.cpp">
<Filter>[1] Ripple\http</Filter> <Filter>[1] Ripple\http</Filter>
</ClCompile> </ClCompile>
@@ -1020,6 +1020,15 @@
<ClCompile Include="..\..\src\ripple\http\impl\Peer.cpp"> <ClCompile Include="..\..\src\ripple\http\impl\Peer.cpp">
<Filter>[1] Ripple\http\impl</Filter> <Filter>[1] Ripple\http\impl</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\ripple\rpc\impl\Handler.cpp">
<Filter>[1] Ripple\rpc\impl</Filter>
</ClCompile>
<ClCompile Include="..\..\src\ripple\rpc\impl\Service.cpp">
<Filter>[1] Ripple\rpc\impl</Filter>
</ClCompile>
<ClCompile Include="..\..\src\ripple\rpc\impl\Manager.cpp">
<Filter>[1] Ripple\rpc\impl</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\src\ripple_basics\containers\KeyCache.h"> <ClInclude Include="..\..\src\ripple_basics\containers\KeyCache.h">
@@ -1980,9 +1989,6 @@
<ClInclude Include="..\..\src\ripple\rpc\ripple_rpc.h"> <ClInclude Include="..\..\src\ripple\rpc\ripple_rpc.h">
<Filter>[1] Ripple\rpc</Filter> <Filter>[1] Ripple\rpc</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\ripple\rpc\api\RPCService.h">
<Filter>[1] Ripple\rpc\api</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ripple\http\ripple_http.h"> <ClInclude Include="..\..\src\ripple\http\ripple_http.h">
<Filter>[1] Ripple\http</Filter> <Filter>[1] Ripple\http</Filter>
</ClInclude> </ClInclude>
@@ -2016,6 +2022,18 @@
<ClInclude Include="..\..\src\ripple\http\impl\Door.h"> <ClInclude Include="..\..\src\ripple\http\impl\Door.h">
<Filter>[1] Ripple\http\impl</Filter> <Filter>[1] Ripple\http\impl</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\ripple\rpc\api\Handler.h">
<Filter>[1] Ripple\rpc\api</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ripple\rpc\api\Manager.h">
<Filter>[1] Ripple\rpc\api</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ripple\rpc\api\Service.h">
<Filter>[1] Ripple\rpc\api</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ripple\rpc\impl\ManagerImpl.h">
<Filter>[1] Ripple\rpc\impl</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<CustomBuild Include="..\..\src\ripple_data\protocol\ripple.proto"> <CustomBuild Include="..\..\src\ripple_data\protocol\ripple.proto">

View File

@@ -174,7 +174,7 @@
#endif #endif
// Here temporarily // 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. // used to invoke RPC commands before they pass to the original code.
#ifndef RIPPLE_USE_RPC_SERVICE_MANAGER #ifndef RIPPLE_USE_RPC_SERVICE_MANAGER
#define RIPPLE_USE_RPC_SERVICE_MANAGER 0 #define RIPPLE_USE_RPC_SERVICE_MANAGER 0

View 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

View 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

View File

@@ -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

View 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

View 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);
}
}
}

View File

@@ -0,0 +1,16 @@
//------------------------------------------------------------------------------
/*
Copyright (c) 2011-2013, OpenCoin, Inc.
*/
//==============================================================================
namespace ripple {
namespace RPC {
Manager* Manager::New (Journal journal)
{
return new ManagerImpl (journal);
}
}
}

View File

@@ -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: public:
// The type of map we use to look up by function name. // 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) : 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); SharedState::Access state (m_state);
@@ -76,21 +79,7 @@ private:
SharedState m_state; SharedState m_state;
}; };
//------------------------------------------------------------------------------ }
RPCService::Manager* RPCService::Manager::New (Journal journal)
{
return new RPCService::ManagerImp (journal);
} }
//------------------------------------------------------------------------------ #endif
RPCService::RPCService ()
{
}
RPCService::~RPCService ()
{
}
}

View 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;
}
}
}

View File

@@ -11,4 +11,7 @@
#include "beast/modules/beast_core/system/BeforeBoost.h" // must come first #include "beast/modules/beast_core/system/BeforeBoost.h" // must come first
#include <boost/unordered_map.hpp> #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"

View File

@@ -11,6 +11,8 @@
#include "../json/ripple_json.h" #include "../json/ripple_json.h"
#include "api/RPCService.h" # include "api/Handler.h"
# include "api/Service.h"
#include "api/Manager.h"
#endif #endif

View File

@@ -17,7 +17,7 @@ namespace Validators
All operations are performed asynchronously on an internal thread. All operations are performed asynchronously on an internal thread.
*/ */
class Manager : public RPCService class Manager : public RPC::Service
{ {
public: public:
/** Create a new Manager object. /** Create a new Manager object.

View File

@@ -125,7 +125,7 @@ public:
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
// //
// RPCService // RPC::Service
// //
Json::Value rpcPrint (Json::Value const& args) Json::Value rpcPrint (Json::Value const& args)

View File

@@ -56,7 +56,7 @@ public:
, m_tempNodeCache ("NodeCache", 16384, 90) , m_tempNodeCache ("NodeCache", 16384, 90)
, m_sleCache ("LedgerEntryCache", 4096, 120) , m_sleCache ("LedgerEntryCache", 4096, 120)
, m_rpcServiceManager (RPCService::Manager::New ( , m_rpcServiceManager (RPC::Manager::New (
LogJournal::get <RPCServiceManagerLog> ())) LogJournal::get <RPCServiceManagerLog> ()))
// The JobQueue has to come pretty early since // The JobQueue has to come pretty early since
@@ -173,7 +173,7 @@ public:
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
RPCService::Manager& getRPCServiceManager() RPC::Manager& getRPCServiceManager()
{ {
return *m_rpcServiceManager; return *m_rpcServiceManager;
} }
@@ -800,7 +800,7 @@ private:
LocalCredentials m_localCredentials; LocalCredentials m_localCredentials;
TransactionMaster m_txMaster; TransactionMaster m_txMaster;
ScopedPointer <RPCService::Manager> m_rpcServiceManager; ScopedPointer <RPC::Manager> m_rpcServiceManager;
// These are Stoppable-related // These are Stoppable-related
ScopedPointer <JobQueue> m_jobQueue; ScopedPointer <JobQueue> m_jobQueue;

View File

@@ -76,7 +76,7 @@ public:
virtual boost::asio::io_service& getIOService () = 0; virtual boost::asio::io_service& getIOService () = 0;
virtual RPCService::Manager& getRPCServiceManager() = 0; virtual RPC::Manager& getRPCServiceManager() = 0;
virtual NodeCache& getTempNodeCache () = 0; virtual NodeCache& getTempNodeCache () = 0;
virtual SLECache& getSLECache () = 0; virtual SLECache& getSLECache () = 0;
virtual Validators::Manager& getValidators () = 0; virtual Validators::Manager& getValidators () = 0;

View File

@@ -5,9 +5,9 @@
//============================================================================== //==============================================================================
// Used for logging // Used for logging
struct RPC; struct RPCLog;
SETUP_LOG (RPC) SETUP_LOGN (RPCLog, "RPC")
unsigned int const gMaxHTTPHeaderSize = 0x02000000; unsigned int const gMaxHTTPHeaderSize = 0x02000000;
@@ -75,7 +75,7 @@ std::string rfc1123Time ()
std::string HTTPReply (int nStatus, const std::string& strMsg) 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) if (nStatus == 401)
return strprintf ("HTTP/1.0 401 Authorization Required\r\n" return strprintf ("HTTP/1.0 401 Authorization Required\r\n"