Menu

[r1]: / trunk / src / jsonrpc_handler.h  Maximize  Restore  History

Download this file

282 lines (245 with data), 8.3 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* JsonRpc-Cpp - JSON-RPC implementation.
* Copyright (C) 2008 Sebastien Vincent <sebastien.vincent@cppextrem.com>
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file jsonrpc_handler.h
* \brief JSON-RPC processor engine.
* \author Sebastien Vincent
*/
#ifndef JSONRPC_HANDLER_H
#define JSONRPC_HANDLER_H
#include <string>
#include <list>
#include <json/json.h>
/**
* \namespace Json
* \brief JSON (JavaScript Object Notation).
*/
namespace Json
{
/**
* \namespace Json::Rpc
* \brief JSON-RPC (remote procedure call using JSON as encoding format).
*/
namespace Rpc
{
/**
* \enum ErrorCode
* \brief JSON-RPC error codes.
* \note Value from -32099 to -32000 are reserved for implementation-defined server-errors.
*/
enum ErrorCode
{
PARSING_ERROR = -32700, /**< Invalid JSON. An error occurred on the server while parsing the JSON text. */
INVALID_REQUEST = -32600, /**< The received JSON not a valid JSON-RPC Request. */
METHOD_NOT_FOUND = -32601, /**< The requested remote-procedure does not exist / is not available. */
INVALID_PARAMS = -32602, /**< Invalid method parameters. */
INTERNAL_ERROR = -32603, /**< Internal JSON-RPC error. */
};
/**
* \class CallbackMethod
* \brief Callback-style method.
*/
class CallbackMethod
{
public:
/**
* \brief Destructor.
*/
virtual ~CallbackMethod();
/**
* \brief Call the method.
* \param msg JSON-RPC request or notification
* \param response response produced (may be Json::Value::null)
* \return true if message has been correctly processed, false otherwise
* \note Have to be implemented by subclasses
*/
virtual bool Call(const Json::Value& msg, Json::Value& response) = 0;
/**
* \brief Get the name of the methods (optional).
* \return name of the method as std::string
* \note Have to be implemented by subclasses
*/
virtual std::string GetName() const = 0;
/**
* \brief Get the description of the methods (optional).
* \return description
*/
virtual Json::Value GetDescription() const = 0;
};
/**
* \brief Template class that represent the RPC method.
*/
template<class T> class RpcMethod : public CallbackMethod
{
public:
/**
* \brief the T method signature.
*/
typedef bool (T::*Method)(const Json::Value& msg, Json::Value& response);
/**
* \brief Constructor.
* \param obj Object
* \param method the class
* \param name symbolic name (i.e. system.describe)
* \param description method description (in JSON format)
*/
RpcMethod(T& obj, Method method, const std::string& name, const Json::Value description = Json::Value::null)
{
m_obj = &obj;
m_name = name;
m_method = method;
m_description = description;
}
/**
* \brief Call the method.
* \param msg JSON-RPC request or notification
* \param response response produced (may be Json::Value::null)
* \return true if message has been correctly processed, false otherwise
*/
virtual bool Call(const Json::Value& msg, Json::Value& response)
{
(m_obj->*m_method)(msg, response);
}
/**
* \brief Get the name of the methods (optional).
* \return name of the method as std::string
* \note Have to be implemented by subclasses
*/
virtual std::string GetName() const
{
return m_name;
}
/**
* \brief Get the description of the methods (optional).
* \return description
*/
virtual Json::Value GetDescription() const
{
return m_description;
}
private:
/**
* \brief Object pointer.
*/
T* m_obj;
/**
* \brief Method of T class.
*/
Method m_method;
/**
* \brief Symbolic name.
*/
std::string m_name;
/**
* \brief JSON-formated description of the RPC method.
*/
Json::Value m_description;
};
/**
* \class Handler
* \brief Container of methods which can be called remotly.
*/
class Handler
{
public:
/**
* \brief Constructor.
*/
Handler();
/**
* \brief Copy constructor.
* \param handler Handler object to copy
*/
Handler(const Handler& handler);
/**
* \brief Destructor.
*/
~Handler();
/**
* \brief Add a new RPC method.
* \param method the method
*/
void AddMethod(CallbackMethod* method);
/**
* \brief Remote a RPC method.
* \param name name of the RPC method
*/
void DeleteMethod(const std::string& name);
/**
* \brief Process a JSON-RPC message.
* \param msg JSON-RPC message as std::string
* \param response JSON-RPC response (could be Json::Value::null)
* \return true if the request has been correctly processed, false otherwise (may be
* caused by parsed error, ...)
* \note in case msg is a notification, response is equal to Json::Value::null
* and the return value is true.
*/
bool Process(const std::string& msg, Json::Value& response);
/**
* \brief Process a JSON-RPC message.
* \param msg JSON-RPC message as C-String
* \param response JSON-RPC response (could be Json::Value::null)
* \return true if the request has been correctly processed, false otherwise (may be
* caused by parsed error, ...)
* \note in case msg is a notification, response is equal to Json::Value::null
* and the return value is true.
*/
bool Process(const char* msg, Json::Value& response);
/**
* \brief RPC method that get all the RPC methods and their description.
* \param msg request
* \param response response
* \return true if processed correctly, false otherwise
*/
bool SystemDescribe(const Json::Value& msg, Json::Value& response);
/**
* \brief Get a std::string representation of Json::Value.
* \param value JSON message
* \return string representation
*/
std::string GetString(Json::Value value);
private:
/**
* \brief JSON reader.
*/
Json::Reader m_reader;
/**
* \brief JSON write.
*/
Json::FastWriter m_writer;
/**
* \brief List of RPC methods.
*/
std::list<CallbackMethod*> m_methods;
/**
* \brief Find CallbackMethod by name.
* \param name name of the CallbackMethod
* \return a CallbackMethod pointer if found, 0 otherwise
*/
CallbackMethod* Lookup(const std::string& name) const;
/**
* \brief Check if the message is a valid JSON one.
* \param msg the message to check
* \param root message parsed (only valid if method returns true)
* \return true if the message is a JSON one, false otherwise
*/
bool Check(const std::string& msg, Json::Value& root);
};
} /* namespace Rpc */
} /* namespace Json */
#endif /* JSONRPC_HANDLER_H */