Repository navigation
Expand file tree
/
Copy pathkvs_client.cc
More file actions
359 lines (295 loc) · 10.1 KB
/
Copy pathkvs_client.cc
File metadata and controls
359 lines (295 loc) · 10.1 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include <iostream>
#include <memory>
#include <string>
#include <grpc++/grpc++.h>
#include "kvs_master.grpc.pb.h"
#include "kvs_cluster.grpc.pb.h"
using grpc::Channel;
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using grpc::ClientContext;
using kvs_master::GetLeaderValueRequest;
using kvs_master::GetLeaderValueReply;
using kvs_master::SendAddrValueRequest;
using kvs_master::SendAddrValueReply;
using kvs_master::SetLeaderValueRequest;
using kvs_master::SetLeaderValueReply;
using kvs_master::KVS_master_service;
using kvs_cluster::PutValueRequest;
using kvs_cluster::PutValueReply;
using kvs_cluster::CPutValueRequest;
using kvs_cluster::CPutValueReply;
using kvs_cluster::GetValueRequest;
using kvs_cluster::GetValueReply;
using kvs_cluster::DeleteValueRequest;
using kvs_cluster::DeleteValueReply;
using kvs_cluster::SendSelfAddrRequest;
using kvs_cluster::SendSelfAddrReply;
using kvs_cluster::GetMasterInitRequest;
using kvs_cluster::GetMasterInitReply;
using kvs_cluster::GetUpdateRequest;
using kvs_cluster::GetUpdateReply;
using kvs_cluster::KVS_cluster_service;
class KVSClient
{
public:
KVSClient(std::shared_ptr<Channel> channel): stub_(KVS_cluster_service::NewStub(channel)) {}
// Assembles the client's payload, sends it and presents the response back
// from the server.
std::string put(const std::string& row, const std::string& col, std::string& val){
// Data we are sending to the server.
PutValueRequest request;
request.set_row(row);
request.set_col(col);
request.set_value(val);
// Container for the data we expect from the server.
PutValueReply reply;
// Context for the client. It could be used to convey extra information to
// the server and/or tweak certain RPC behaviors.
ClientContext context;
// The actual RPC.
Status status = stub_->put(&context, request, &reply);
// Act upon its status.
if (status.ok()){
return reply.value();
}
else{
std::cout << status.error_code() << ": " << status.error_message() << std::endl;
return "RPC failed";
}
}
std::string cput(const std::string& row, const std::string& col, std::string& val1, std::string& val2)
{
// Data we are sending to the server.
CPutValueRequest request;
request.set_row(row);
request.set_col(col);
request.set_value1(val1);
request.set_value2(val2);
// Container for the data we expect from the server.
CPutValueReply reply;
// Context for the client. It could be used to convey extra information to
// the server and/or tweak certain RPC behaviors.
ClientContext context;
// The actual RPC.
Status status = stub_->cput(&context, request, &reply);
// Act upon its status.
if (status.ok())
{
return reply.value();
}
else
{
std::cout << status.error_code() << ": " << status.error_message()
<< std::endl;
return "RPC failed";
}
}
std::string get(const std::string& row, const std::string& col)
{
// Data we are sending to the server.
GetValueRequest request;
request.set_row(row);
request.set_col(col);
// Container for the data we expect from the server.
GetValueReply reply;
// Context for the client. It could be used to convey extra information to
// the server and/or tweak certain RPC behaviors.
ClientContext context;
// The actual RPC.
Status status = stub_->get(&context, request, &reply);
// Act upon its status.
if (status.ok())
{
return reply.value();
}
else
{
std::cout << status.error_code() << ": " << status.error_message()
<< std::endl;
return "RPC failed";
}
}
std::string del(const std::string& row, const std::string& col)
{
// Data we are sending to the server.
DeleteValueRequest request;
request.set_row(row);
request.set_col(col);
// Container for the data we expect from the server.
DeleteValueReply reply;
// Context for the client. It could be used to convey extra information to
// the server and/or tweak certain RPC behaviors.
ClientContext context;
// The actual RPC.
Status status = stub_->del(&context, request, &reply);
// Act upon its status.
if (status.ok())
{
return reply.value();
}
else
{
std::cout << status.error_code() << ": " << status.error_message()
<< std::endl;
return "RPC failed";
}
}
private:
std::unique_ptr<KVS_cluster_service::Stub> stub_;
};
class KVSCluster1_master
{
public:
KVSCluster1_master(std::shared_ptr<Channel> channel)
: stub_(KVS_master_service::NewStub(channel)) {}
// Assembles the client's payload, sends it and presents the response back
// from the server.
std::string get_leader(const std::string& user)
{
// Data we are sending to the server.
GetLeaderValueRequest request;
request.set_user(user);
// Container for the data we expect from the server.
GetLeaderValueReply reply;
// Context for the client. It could be used to convey extra information to
// the server and/or tweak certain RPC behaviors.
ClientContext context;
gpr_timespec timeOut;
timeOut.tv_sec = 15;
timeOut.tv_nsec = 0;
timeOut.clock_type = GPR_TIMESPAN;
context.set_deadline(timeOut);
// The actual RPC.
Status status = stub_->get_leader(&context, request, &reply);
if (!status.ok() && status.error_code() == grpc::DEADLINE_EXCEEDED)
{
// call timeout handler
}
// Act upon its status.
if (status.ok())
{
//std::cout << reply.url() << std::endl;
return reply.url();
}
else
{
std::cout << status.error_code() << ": " << status.error_message()
<< std::endl;
return "RPC failed";
}
}
std::string send_addr()
{
// Data we are sending to the server.
SendAddrValueRequest request;
// Container for the data we expect from the server.
SendAddrValueReply reply;
// Context for the client. It could be used to convey extra information to
// the server and/or tweak certain RPC behaviors.
ClientContext context;
gpr_timespec timeOut;
timeOut.tv_sec = 15;
timeOut.tv_nsec = 0;
timeOut.clock_type = GPR_TIMESPAN;
context.set_deadline(timeOut);
// The actual RPC.
Status status = stub_->send_addr(&context, request, &reply);
if (!status.ok() && status.error_code() == grpc::DEADLINE_EXCEEDED)
{
// call timeout handler
}
// Act upon its status.
if (status.ok())
{
//std::cout << reply.addr() << std::endl;
return reply.addr();
}
else
{
std::cout << status.error_code() << ": " << status.error_message()
<< std::endl;
return "RPC failed";
}
}
std::string set_leader(const std::string& key, const std::string& url)
{
// Data we are sending to the server.
SetLeaderValueRequest request;
request.set_key(key);
request.set_url(url);
// Container for the data we expect from the server.
SetLeaderValueReply reply;
// Context for the client. It could be used to convey extra information to
// the server and/or tweak certain RPC behaviors.
ClientContext context;
gpr_timespec timeOut;
timeOut.tv_sec = 15;
timeOut.tv_nsec = 0;
timeOut.clock_type = GPR_TIMESPAN;
context.set_deadline(timeOut);
// The actual RPC.
Status status = stub_->set_leader(&context, request, &reply);
if (!status.ok() && status.error_code() == grpc::DEADLINE_EXCEEDED)
{
// call timeout handler
}
// Act upon its status.
if (status.ok())
{
//std::cout << reply.ret() << std::endl;
return reply.ret();
}
else
{
std::cout << status.error_code() << ": " << status.error_message()
<< std::endl;
return "RPC failed";
}
}
private:
std::unique_ptr<KVS_master_service::Stub> stub_;
};
class KVSClientInterface{
public:
std::string backendServer;
KVSClientInterface(){}
KVSClientInterface(std::string server){
backendServer = server;
}
KVSClient getClusterPrimary(std::string user){
// Instantiate the client. It requires a channel, out of which the actual RPCs
// are created. This channel models a connection to an endpoint (in this case,
// localhost at port 50051). We indicate that the channel isn't authenticated
// (use of InsecureChannelCredentials()).
KVSCluster1_master objz(grpc::CreateChannel(backendServer, grpc::InsecureChannelCredentials()));
std::cout << objz.get_leader(user) << std::endl;
std::string cluster_addr = objz.get_leader(user);
KVSClient obj(grpc::CreateChannel(cluster_addr, grpc::InsecureChannelCredentials()));
return obj;
}
std::string put(const std::string& row, const std::string& col, std::string& val){
KVSClient obj = getClusterPrimary(row);
std::string response = obj.put(row, col, val);
std::cout << "\n\n\n" << response << "\n\n\n" << std::endl;
return response;
}
std::string cput(const std::string& row, const std::string& col, std::string& val1, std::string& val2){
KVSClient obj = getClusterPrimary(row);
std::string response = obj.cput(row, col, val1, val2);
return response;
}
std::string get(const std::string& row, const std::string& col){
KVSClient obj = getClusterPrimary(row);
std::string response = obj.get(row, col);
std::cout << "\n\n\n" << response << "\n\n\n" << std::endl;
return response;
}
std::string del(const std::string& row, const std::string& col){
KVSClient obj = getClusterPrimary(row);
std::string response = obj.del(row, col);
return response;
}
};