forked from jgarzik/rpcsrv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpcs.cc
81 lines (60 loc) · 1.85 KB
/
rpcs.cc
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
//
// Copyright 2012 Red Hat, Inc.
//
#include "json/json_spirit_reader.h"
#include "json/json_spirit_writer.h"
#include "json/json_spirit_utils.h"
#include "rpcs.h"
#define ARRAYLEN(array) (sizeof(array)/sizeof((array)[0]))
namespace json_rpc {
static bool ping(json_spirit::Object &query, json_spirit::Object &result)
{
json_spirit::Value tmpval(true);
result.push_back(json_spirit::Pair("result", tmpval));
return true;
}
static bool echo(json_spirit::Object &query, json_spirit::Object &result)
{
json_spirit::Value tmpval;
tmpval = json_spirit::find_value(query, "params");
result.push_back(json_spirit::Pair("result", tmpval)); // may be null
return true;
}
struct handler {
std::string method;
bool (*actor)(json_spirit::Object &query,
json_spirit::Object &result);
};
static const struct handler rpc_handlers[] =
{
{ "ping", ping },
{ "echo", echo },
};
void exec(json_spirit::Object &query, json_spirit::Object &result)
{
json_spirit::Value tmpval;
tmpval = json_spirit::find_value(query, "version");
if (tmpval.type() != json_spirit::null_type)
result.push_back(json_spirit::Pair("version", tmpval));
else
result.push_back(json_spirit::Pair("version", "1.0")); // fb to 1.0
tmpval = json_spirit::find_value(query, "id");
result.push_back(json_spirit::Pair("id", tmpval)); // may be null
tmpval = json_spirit::find_value(query, "method");
if (tmpval.type() != json_spirit::str_type) {
result.push_back(json_spirit::Pair("error", "invalid method"));
return;
}
unsigned int i = 0;
for (; i < ARRAYLEN(rpc_handlers); i++) {
if (tmpval.get_str() == rpc_handlers[i].method) {
bool rc = rpc_handlers[i].actor(query, result);
if (rc)
return;
result.push_back(json_spirit::Pair("error", "RPC failed"));
return;
}
}
result.push_back(json_spirit::Pair("error", "unknown RPC method"));
}
} // namespace json_rpc