Menu

[r2]: / trunk / test / udp-server.cpp  Maximize  Restore  History

Download this file

70 lines (54 with data), 1.4 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
/**
* \file udp-server.cpp
* \brief Simple JSON-RPC UDP server.
* \author Sebastien Vincent
*/
#include <stdio.h>
#include <stdlib.h>
#include <jsonrpc/jsonrpc.h>
class Plop
{
public:
bool Print(const Json::Value& root, Json::Value& response)
{
std::cout << "Plop" << std::endl;
response["jsonrpc"] = 2.0;
response["id"] = root["id"];
response["result"] = "success";
return true;
}
Json::Value GetDescription()
{
Json::FastWriter writer;
Json::Value root;
Json::Value parameters;
Json::Value param1;
root["description"] = "Print Plop";
/* type of parameter named arg1 */
param1["type"] = "integer";
param1["description"] = "argument 1";
/* push it into the parameters list */
parameters["arg1"] = param1;
root["parameters"] = parameters;
/* no value returned */
root["returns"] = Json::Value::null;
return root;
}
};
int main(int argc, char** argv)
{
Plop a;
Json::Rpc::UdpServer server(std::string("127.0.0.1"), 8086);
server.AddMethod(new Json::Rpc::RpcMethod<Plop>(a, &Plop::Print, std::string("system.print"), a.GetDescription()));
/* server.SetEncapsulatedFormat(Json::Rpc::NETSTRING); */
if(!server.Bind())
{
std::cout << "Bind failed" << std::endl;
return -1;
}
while(1)
{
server.WaitMessage(1000);
}
return EXIT_SUCCESS;
}