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