-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMyRpcApi.h
104 lines (98 loc) · 2.1 KB
/
MyRpcApi.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
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
#pragma once
#include "blib/ErrorHandler.h"
#include "src/rpc/client/RpcApi.h"
#include "src/auth/DefaultAuthChecker.h"
#include "src/serializer/json/JsonSerializer.h"
using namespace brpc;
class MySubRpcApi : public RpcApi
{
public:
MySubRpcApi(RpcApi* parent) : RpcApi("sub", parent){}
public:
int print(cstring str)
{
ObjectList args;
args.addValue(str);
return call<int>("print", args);
}
};
class CallbackRpcService : public RpcService
{
public:
CallbackRpcService(cstring serviceName) : RpcService(serviceName)
{
as(CallbackRpcService::onclick);
}
void onclick(cstring event, int count)
{
printf(">>>>on event click(%s): %d\n", event, count);
}
};
class MyRpcApi : public RpcApi
{
public:
MyRpcApi(cstring url, cstring name, cstring password) :
checker("", ""),
dispatcher("callback"),
client(url, &dispatcher, &checker, "text/json", 1000*10),
//RpcApi(&client, loginArgs)
//_init1(loginArgs.addValue(name)),
//_init2(loginArgs.addValue(password)),
//RpcApi(client=new P2pRpcClient(url, &dispatcher, &serializer, &checker), loginArgs)
RpcApi("nova")
{
ObjectList loginArgs;
loginArgs.addValue(name);
loginArgs.addValue(password);
init(&client, loginArgs);
subRpcApi = new MySubRpcApi(this);
}
virtual ~MyRpcApi()
{
delete subRpcApi;
try{
logout();
}catch (Exception& e) {
ErrorHandler::handle(e);
}
}
public:
int print(cstring str)
{
ObjectList args;
args.addValue(str);
return call<int>("print", args);
}
int print(TestObject* obj)
{
ObjectList args;
args.addValue(objectToMap(obj));
return call<int>("print", args);
}
double sum(int a, double b)
{
ObjectList args;
args.addValue(a);
args.addValue(b);
return call<double>("sum", args);
}
bool playMusic(cstring name)
{
ObjectList args;
args.addValue(name);
return call<bool>("playMusic", args);
}
String execute(cstring cmd)
{
ObjectList args;
args.addValue(cmd);
return call<String>("execute", args);
}
private:
CallbackRpcService dispatcher;
DefaultAuthChecker checker;
RpcClient client;
//ObjectList loginArgs;
//unsigned int _init1, _init2;
MySubRpcApi* subRpcApi;
};