Fabcoin Core  0.16.2
P2P Digital Currency
ModularServer.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
22 #pragma once
23 
24 #include <map>
25 #include <memory>
26 #include <string>
27 #include <tuple>
28 #include <vector>
29 
30 #include <jsonrpccpp/common/procedure.h>
31 #include <jsonrpccpp/server/iprocedureinvokationhandler.h>
32 #include <jsonrpccpp/server/abstractserverconnector.h>
33 #include <jsonrpccpp/server/requesthandlerfactory.h>
34 
35 template <class I> using AbstractMethodPointer = void(I::*)(Json::Value const& _parameter, Json::Value& _result);
36 template <class I> using AbstractNotificationPointer = void(I::*)(Json::Value const& _parameter);
37 
38 template <class I>
40 {
41 public:
44 
45  using MethodBinding = std::tuple<jsonrpc::Procedure, AbstractMethodPointer<I>>;
46  using NotificationBinding = std::tuple<jsonrpc::Procedure, AbstractNotificationPointer<I>>;
47  using Methods = std::vector<MethodBinding>;
48  using Notifications = std::vector<NotificationBinding>;
49  struct RPCModule { std::string name; std::string version; };
50  using RPCModules = std::vector<RPCModule>;
51 
52  virtual ~ServerInterface() {}
53  Methods const& methods() const { return m_methods; }
54  Notifications const& notifications() const { return m_notifications; }
56  virtual RPCModules implementedModules() const = 0;
57 
58 protected:
59  void bindAndAddMethod(jsonrpc::Procedure const& _proc, MethodPointer _pointer) { m_methods.emplace_back(_proc, _pointer); }
60  void bindAndAddNotification(jsonrpc::Procedure const& _proc, NotificationPointer _pointer) { m_notifications.emplace_back(_proc, _pointer); }
61 
62 private:
65 };
66 
67 template <class... Is>
68 class ModularServer: public jsonrpc::IProcedureInvokationHandler
69 {
70 public:
72  : m_handler(jsonrpc::RequestHandlerFactory::createProtocolHandler(jsonrpc::JSONRPC_SERVER_V2, *this))
73  {
74  m_handler->AddProcedure(jsonrpc::Procedure("rpc_modules", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, NULL));
75  m_implementedModules = Json::objectValue;
76  }
77  inline virtual void modules(const Json::Value &request, Json::Value &response)
78  {
79  (void)request;
80  response = m_implementedModules;
81  }
82 
83  virtual ~ModularServer() { StopListening(); }
84 
85  virtual void StartListening()
86  {
87  for (auto const& connector: m_connectors)
88  connector->StartListening();
89  }
90 
91  virtual void StopListening()
92  {
93  for (auto const& connector: m_connectors)
94  connector->StopListening();
95  }
96 
97  virtual void HandleMethodCall(jsonrpc::Procedure& _proc, Json::Value const& _input, Json::Value& _output) override
98  {
99  if (_proc.GetProcedureName() == "rpc_modules")
100  modules(_input, _output);
101  }
102 
103  virtual void HandleNotificationCall(jsonrpc::Procedure& _proc, Json::Value const& _input) override
104  {
105  (void)_proc;
106  (void)_input;
107  }
108 
110  unsigned addConnector(jsonrpc::AbstractServerConnector* _connector)
111  {
112  m_connectors.emplace_back(_connector);
113  _connector->SetHandler(m_handler.get());
114  return m_connectors.size() - 1;
115  }
116 
117  jsonrpc::AbstractServerConnector* connector(unsigned _i) const
118  {
119  return m_connectors.at(_i).get();
120  }
121 
122 protected:
123  std::vector<std::unique_ptr<jsonrpc::AbstractServerConnector>> m_connectors;
124  std::unique_ptr<jsonrpc::IProtocolHandler> m_handler;
127 };
128 
129 template <class I, class... Is>
130 class ModularServer<I, Is...> : public ModularServer<Is...>
131 {
132 public:
135 
136  ModularServer<I, Is...>(I* _i, Is*... _is): ModularServer<Is...>(_is...), m_interface(_i)
137  {
138  if (!m_interface)
139  return;
140  for (auto const& method: m_interface->methods())
141  {
142  m_methods[std::get<0>(method).GetProcedureName()] = std::get<1>(method);
143  this->m_handler->AddProcedure(std::get<0>(method));
144  }
145 
146  for (auto const& notification: m_interface->notifications())
147  {
148  m_notifications[std::get<0>(notification).GetProcedureName()] = std::get<1>(notification);
149  this->m_handler->AddProcedure(std::get<0>(notification));
150  }
151  // Store module with version.
152  for (auto const& module: m_interface->implementedModules())
153  this->m_implementedModules[module.name] = module.version;
154  }
155 
156  virtual void HandleMethodCall(jsonrpc::Procedure& _proc, Json::Value const& _input, Json::Value& _output) override
157  {
158  auto pointer = m_methods.find(_proc.GetProcedureName());
159  if (pointer != m_methods.end())
160  (m_interface.get()->*(pointer->second))(_input, _output);
161  else
162  ModularServer<Is...>::HandleMethodCall(_proc, _input, _output);
163  }
164 
165  virtual void HandleNotificationCall(jsonrpc::Procedure& _proc, Json::Value const& _input) override
166  {
167  auto pointer = m_notifications.find(_proc.GetProcedureName());
168  if (pointer != m_notifications.end())
169  (m_interface.get()->*(pointer->second))(_input);
170  else
172  }
173 
174 private:
175  std::unique_ptr<I> m_interface;
176  std::map<std::string, MethodPointer> m_methods;
177  std::map<std::string, NotificationPointer> m_notifications;
178 };
std::tuple< jsonrpc::Procedure, AbstractNotificationPointer< PersonalFace >> NotificationBinding
Definition: ModularServer.h:46
std::vector< RPCModule > RPCModules
Definition: ModularServer.h:50
jsonrpc::AbstractServerConnector * connector(unsigned _i) const
virtual void HandleMethodCall(jsonrpc::Procedure &_proc, Json::Value const &_input, Json::Value &_output) override
AbstractNotificationPointer< PersonalFace > NotificationPointer
Definition: ModularServer.h:43
AbstractNotificationPointer< I > NotificationPointer
virtual void StopListening()
Definition: ModularServer.h:91
std::vector< NotificationBinding > Notifications
Definition: ModularServer.h:48
std::unique_ptr< jsonrpc::IProtocolHandler > m_handler
virtual void HandleNotificationCall(jsonrpc::Procedure &_proc, Json::Value const &_input) override
AbstractMethodPointer< PersonalFace > MethodPointer
Definition: ModularServer.h:42
void bindAndAddMethod(jsonrpc::Procedure const &_proc, MethodPointer _pointer)
Definition: ModularServer.h:59
void bindAndAddNotification(jsonrpc::Procedure const &_proc, NotificationPointer _pointer)
Definition: ModularServer.h:60
Methods const & methods() const
Definition: ModularServer.h:53
Json::Value m_implementedModules
Mapping for implemented modules, to be filled by subclasses during construction.
Notifications m_notifications
Definition: ModularServer.h:64
virtual void StartListening()
Definition: ModularServer.h:85
Config::Value_type Value
virtual void modules(const Json::Value &request, Json::Value &response)
Definition: ModularServer.h:77
void(I::*)(Json::Value const &_parameter) AbstractNotificationPointer
Definition: ModularServer.h:36
std::tuple< jsonrpc::Procedure, AbstractMethodPointer< PersonalFace >> MethodBinding
Definition: ModularServer.h:45
virtual void HandleMethodCall(jsonrpc::Procedure &_proc, Json::Value const &_input, Json::Value &_output) override
Definition: ModularServer.h:97
std::vector< MethodBinding > Methods
Definition: ModularServer.h:47
void(I::*)(Json::Value const &_parameter, Json::Value &_result) AbstractMethodPointer
Definition: ModularServer.h:35
virtual ~ServerInterface()
Definition: ModularServer.h:52
virtual ~ModularServer()
Definition: ModularServer.h:83
Notifications const & notifications() const
Definition: ModularServer.h:54
std::vector< std::unique_ptr< jsonrpc::AbstractServerConnector > > m_connectors
std::map< std::string, NotificationPointer > m_notifications
#define I(x, y, z)
Definition: Hash.cpp:82
unsigned addConnector(jsonrpc::AbstractServerConnector *_connector)
server takes ownership of the connector
std::map< std::string, MethodPointer > m_methods
virtual RPCModules implementedModules() const =0
virtual void HandleNotificationCall(jsonrpc::Procedure &_proc, Json::Value const &_input) override
std::unique_ptr< I > m_interface
AbstractMethodPointer< I > MethodPointer