-
Notifications
You must be signed in to change notification settings - Fork 2
/
phxrpc_kvraft_dispatcher.cpp
executable file
·191 lines (139 loc) · 4.17 KB
/
phxrpc_kvraft_dispatcher.cpp
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
/* phxrpc_kvraft_dispatcher.h
Generated by phxrpc_pb2service from kvraft.proto
Please DO NOT edit unless you know exactly what you are doing.
*/
#include "phxrpc_kvraft_dispatcher.h"
#include <errno.h>
#include "phxrpc/http.h"
#include "phxrpc/file.h"
#include "kvraft.pb.h"
#include "phxrpc_kvraft_service.h"
KVRaftDispatcher::KVRaftDispatcher(KVRaftService &service, phxrpc::DispatcherArgs_t *dispatcher_args)
: service_(service), dispatcher_args_(dispatcher_args) {
}
KVRaftDispatcher::~KVRaftDispatcher() {
}
const phxrpc::BaseDispatcher<KVRaftDispatcher>::URIFuncMap &KVRaftDispatcher::GetURIFuncMap() {
static phxrpc::BaseDispatcher<KVRaftDispatcher>::URIFuncMap uri_func_map = {
{"/kvraft/PHXEcho", &KVRaftDispatcher::PHXEcho},
{"/kvraft/RequestVote", &KVRaftDispatcher::RequestVote},
{"/kvraft/AppendEntries", &KVRaftDispatcher::AppendEntries},
{"/kvraft/Command", &KVRaftDispatcher::Command}};
return uri_func_map;
}
int KVRaftDispatcher::PHXEcho(const phxrpc::BaseRequest &req, phxrpc::BaseResponse *const resp) {
dispatcher_args_->server_monitor->SvrCall(-1, "PHXEcho", 1);
int ret{-1};
google::protobuf::StringValue req_pb;
google::protobuf::StringValue resp_pb;
// unpack request
{
ret = req.ToPb(&req_pb);
if (0 != ret) {
phxrpc::log(LOG_ERR, "ToPb err %d", ret);
return -EINVAL;
}
}
// logic process
{
if (0 == ret) {
ret = service_.PHXEcho(req_pb, &resp_pb);
}
}
// pack response
{
if (0 != resp->FromPb(resp_pb)) {
phxrpc::log(LOG_ERR, "FromPb err %d", ret);
return -ENOMEM;
}
}
phxrpc::log(LOG_DEBUG, "RETN: PHXEcho = %d", ret);
return ret;
}
int KVRaftDispatcher::RequestVote(const phxrpc::BaseRequest &req, phxrpc::BaseResponse *const resp) {
dispatcher_args_->server_monitor->SvrCall(-1, "RequestVote", 1);
int ret{-1};
kvraft::RequestVoteArgs req_pb;
kvraft::RequestVoteReply resp_pb;
// unpack request
{
ret = req.ToPb(&req_pb);
if (0 != ret) {
phxrpc::log(LOG_ERR, "ToPb err %d", ret);
return -EINVAL;
}
}
// logic process
{
if (0 == ret) {
ret = service_.RequestVote(req_pb, &resp_pb);
}
}
// pack response
{
if (0 != resp->FromPb(resp_pb)) {
phxrpc::log(LOG_ERR, "FromPb err %d", ret);
return -ENOMEM;
}
}
phxrpc::log(LOG_DEBUG, "RETN: RequestVote = %d", ret);
return ret;
}
int KVRaftDispatcher::AppendEntries(const phxrpc::BaseRequest &req, phxrpc::BaseResponse *const resp) {
dispatcher_args_->server_monitor->SvrCall(-1, "AppendEntries", 1);
int ret{-1};
kvraft::AppendEntriesArgs req_pb;
kvraft::AppendEntriesReply resp_pb;
// unpack request
{
ret = req.ToPb(&req_pb);
if (0 != ret) {
phxrpc::log(LOG_ERR, "ToPb err %d", ret);
return -EINVAL;
}
}
// logic process
{
if (0 == ret) {
ret = service_.AppendEntries(req_pb, &resp_pb);
}
}
// pack response
{
if (0 != resp->FromPb(resp_pb)) {
phxrpc::log(LOG_ERR, "FromPb err %d", ret);
return -ENOMEM;
}
}
phxrpc::log(LOG_DEBUG, "RETN: AppendEntries = %d", ret);
return ret;
}
int KVRaftDispatcher::Command(const phxrpc::BaseRequest &req, phxrpc::BaseResponse *const resp) {
dispatcher_args_->server_monitor->SvrCall(-1, "Command", 1);
int ret{-1};
kvraft::KVArgs req_pb;
kvraft::KVReply resp_pb;
// unpack request
{
ret = req.ToPb(&req_pb);
if (0 != ret) {
phxrpc::log(LOG_ERR, "ToPb err %d", ret);
return -EINVAL;
}
}
// logic process
{
if (0 == ret) {
ret = service_.Command(req_pb, &resp_pb);
}
}
// pack response
{
if (0 != resp->FromPb(resp_pb)) {
phxrpc::log(LOG_ERR, "FromPb err %d", ret);
return -ENOMEM;
}
}
phxrpc::log(LOG_DEBUG, "RETN: Command = %d", ret);
return ret;
}