ModularServer.h 5.8 KB

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