-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsl_server.cc
78 lines (71 loc) · 3.14 KB
/
sl_server.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
#include "sl_server.h"
ServiceLayerServer::ServiceLayerServer() : sl_func_(false) {}
Status ServiceLayerServer::registeruser(ServerContext* context, const RegisterRequest* request,
RegisterReply* reply){
// if registeruser fails, the username has already been taken,
// so return an invalid status
if (!sl_func_.registeruser(request->username())) {
return Status(StatusCode::INVALID_ARGUMENT, "Username is taken.");
}
return Status::OK;
}
Status ServiceLayerServer::chirp(ServerContext* context, const ChirpRequest* request,
ChirpReply* reply){
// determine if the user and parent_id are valid before creating a chirp
if (!sl_func_.user_exists(request->username())) {
LOG(ERROR) << "User " << request->username() << " does not exist in database." << std::endl;
return Status(StatusCode::INVALID_ARGUMENT, "User does not exist.");
} else if (!sl_func_.valid_parent_id(request->parent_id())) {
return Status(StatusCode::INVALID_ARGUMENT, "Parent ID not valid");
}
// create mutable_chirp with reply object
Chirp* chirp = reply->mutable_chirp();
// call function to assign values to chirp
sl_func_.chirp(chirp, request->username(), request->text(), request->parent_id());
return Status::OK;
}
Status ServiceLayerServer::follow(ServerContext* context, const FollowRequest* request,
FollowReply* reply){
// determine if the `username` and `to_follow` are valid before attempting to follow
if (!sl_func_.user_exists(request->username())) {
return Status(StatusCode::INVALID_ARGUMENT, "User does not exist.");
} else if (!sl_func_.user_exists(request->to_follow())) {
return Status(StatusCode::INVALID_ARGUMENT, "User to follow does not exist.");
}
// follow if both parties exist in database
sl_func_.follow(request->username(), request->to_follow());
return Status::OK;
}
Status ServiceLayerServer::read(ServerContext* context, const ReadRequest* request,
ReadReply* reply){
// retrieve thread starting from given id
std::vector<Chirp> chirps = sl_func_.read(request->chirp_id());
// empty vector indicates that the original ID is invalid
if (!chirps.size()) {
return Status(StatusCode::INVALID_ARGUMENT, "Invalid chirp ID");
}
// Loop through all chirps and make them the reply
for(int i = 0; i < chirps.size(); i++) {
Chirp *chirp = reply->mutable_chirps(i);
*chirp = chirps[i];
}
return Status::OK;
}
Status ServiceLayerServer::monitor(ServerContext* context, const MonitorRequest* request, ServerWriter<MonitorReply>* writer){
// keep polling the database while monitor is true
while(!context->IsCancelled()) {
// request the new chirps
Chirps chirps = sl_func_.monitor(request->username());
// loop through the chirps and add them to the reply
for (int i = 0; i < chirps.chirps_size(); i++) {
// add each chirp to stream
MonitorReply monitor_reply;
Chirp *chirp = monitor_reply.mutable_chirp();
*chirp = chirps.chirps(i);
writer->Write(monitor_reply);
}
}
// clear the monitor to false that that new requests aren't storec
sl_func_.clear_monitor(request->username());
return Status::OK;
}