/
echo_server.h
78 lines (59 loc) · 1.55 KB
/
echo_server.h
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
// Copyright (C) 2013 Preferred Infrastructure and Nippon Telegraph and Telephone Corporation.
#ifndef ECHO_SERVER_H_
#define ECHO_SERVER_H_
#include <jubatus/msgpack/rpc/server.h>
namespace rpc {
using namespace msgpack;
using namespace msgpack::rpc;
} // namespace rpc
class myecho : public rpc::dispatcher {
public:
typedef rpc::request request;
void dispatch(request req)
try {
std::string method;
req.method().convert(&method);
if(method == "add") {
msgpack::type::tuple<int, int> params;
req.params().convert(¶ms);
add(req, params.get<0>(), params.get<1>());
} else if(method == "echo") {
msgpack::type::tuple<std::string> params;
req.params().convert(¶ms);
echo(req, params.get<0>());
} else if(method == "echo_huge") {
msgpack::type::tuple<msgpack::type::raw_ref> params;
req.params().convert(¶ms);
echo_huge(req, params.get<0>());
} else if(method == "err") {
msgpack::type::tuple<> params;
req.params().convert(¶ms);
err(req);
} else {
req.error(msgpack::rpc::NO_METHOD_ERROR);
}
} catch (msgpack::type_error& e) {
req.error(msgpack::rpc::ARGUMENT_ERROR);
return;
} catch (std::exception& e) {
req.error(std::string(e.what()));
return;
}
void add(request req, int a1, int a2)
{
req.result(a1 + a2);
}
void echo(request req, const std::string& msg)
{
req.result(msg);
}
void echo_huge(request req, const msgpack::type::raw_ref& msg)
{
req.result(msg);
}
void err(request req)
{
req.error(std::string("always fail"));
}
};
#endif /* echo_server.h */