-
Notifications
You must be signed in to change notification settings - Fork 1
/
sl_client.cc
136 lines (111 loc) · 4.3 KB
/
sl_client.cc
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
#include "sl_client.h"
ServiceLayerClient::ServiceLayerClient(std::shared_ptr<Channel> channel)
: stub_(ServiceLayer::NewStub(channel)) {}
bool ServiceLayerClient::registeruser(const std::string& username) {
// Send username to the service layer
RegisterRequest request;
request.set_username(username);
// Store response in reply
RegisterReply reply;
// Client's context for additional information to server, or rpc options
ClientContext context;
// Send the rpc
Status status = stub_->registeruser(&context, request, &reply);
// Determine if the status is ok, then process
if (status.ok()) {
LOG(INFO) << "Status ok from ServiceLayerClient register." << std::endl;
return true;
} else {
LOG(ERROR) << status.error_message() << std::endl;
std::cout << status.error_message() << std::endl;
return false;
}
}
Chirp ServiceLayerClient::chirp(const std::string& username, const std::string& text, const std::string& parent_id) {
// create request with username, text, and parent_id
ChirpRequest request;
request.set_username(username);
request.set_text(text);
request.set_parent_id(parent_id);
// create object to hold response
ChirpReply reply;
// declare context for additional information
ClientContext context;
// send the rpc
Status status = stub_->chirp(&context, request, &reply);
// if status ok, print success message and return chirp
// else, print error message and return empty chirp
if (status.ok()) {
LOG(INFO) << "Status ok from ServiceLayerClient chirp." << std::endl;
return reply.chirp();
} else {
LOG(ERROR) << status.error_code() << ": " << status.error_message()
<< std::endl;
std::cout << status.error_message() << std::endl;
Chirp chirp;
return chirp;
}
}
bool ServiceLayerClient::follow(const std::string& username, const std::string& to_follow) {
// Send username of client and username of chirp user to follow
FollowRequest request;
request.set_username(username);
request.set_to_follow(to_follow);
// create reply object
FollowReply reply;
// create context to hold additional information from server
ClientContext context;
// send rpc
Status status = stub_->follow(&context, request, &reply);
// if status ok, return true
// else, print error message and return false
if (status.ok()) {
LOG(INFO) << "Status ok from ServiceLayerClient follow." << std::endl;
return true;
} else {
LOG(ERROR) << status.error_code() << ": " << status.error_message() << std::endl;
std::cout << status.error_message() << std::endl;
return false;
}
}
const google::protobuf::RepeatedPtrField<chirp::Chirp> ServiceLayerClient::read(const std::string& chirp_id) {
// create request with id of chirp to read
ReadRequest request;
request.set_chirp_id(chirp_id);
// create object to store reply
ReadReply reply;
// create context for additional information from server
ClientContext context;
// send rpc
Status status = stub_->read(&context, request, &reply);
// if status ok, return chirps
// else, print error and return empty chirps
if (status.ok()) {
LOG(INFO) << "Status ok from ServiceLayerClient read." << std::endl;
return reply.chirps();
} else {
LOG(ERROR) << status.error_code() << ": " << status.error_message() << std::endl;
std::cout << status.error_message() << std::endl;
const google::protobuf::RepeatedPtrField<chirp::Chirp> chirp; // TODO: return null/invalid value
return chirp;
}
}
void ServiceLayerClient::monitor(const std::string& username) {
// create request with username of monitoring user
MonitorRequest request;
request.set_username(username);
// create object to store stream of replies
MonitorReply reply;
// create context for additional information from server
ClientContext context;
// create `reader` to read stream of data from server
std::unique_ptr<ClientReader<MonitorReply> > reader(stub_->monitor(&context, request));
while(reader->Read(&reply)) {
std::cout << "\"" << reply.chirp().text() << "\"" << " - " << reply.chirp().username() << " ID: " << reply.chirp().id() << std::endl;
}
Status status = reader->Finish();
if (!status.ok()) {
std::cout << "Monitor rpc failed" << std::endl;
LOG(ERROR) << status.error_code() << ": " << status.error_message() << std::endl;
}
}