-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cc
117 lines (94 loc) · 3.4 KB
/
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
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
#include <grpcpp/grpcpp.h>
#include <string>
#include <iostream>
#include <map>
#include <grpcpp/ext/proto_server_reflection_plugin.h>
#include "bookstore.grpc.pb.h"
#include <cstdlib>
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::ServerWriter;
using grpc::Status;
using grpc::StatusCode;
using bookstore::BookStore;
using bookstore::Empty;
using bookstore::HellowReply;
using bookstore::Book;
using bookstore::Reply;
using bookstore::SearchRequest;
std::map< int, Book > db;
class BookStoreServiceImpl final : public BookStore::Service {
Status SayHello(ServerContext* context, const Empty* request, HellowReply* reply) override {
std::string ourReply("Hi from BookStore gRPC server");
reply->set_message(ourReply);
return Status::OK;
}
Status GetAllBooks(ServerContext* context, const Empty* request, ServerWriter<Book>* writer) override {
Book book;
for(auto const& entry: db) {
book.set_id(entry.second.id());
book.set_name(entry.second.name());
book.set_author(entry.second.author());
writer->Write(book);
}
return Status::OK;
}
Status GetABookById(ServerContext* context, const SearchRequest* request, Book* reply) override {
int requestedId = request->id();
if(db.find(requestedId) == db.end()) {
return Status(StatusCode::NOT_FOUND, "No book entry found for this id");
}
Book requestedBook = db[requestedId];
reply->CopyFrom(requestedBook);
return Status::OK;
}
Status DeleteABook(ServerContext* context, const SearchRequest* request, Empty* reply) override {
int requestedId = request->id();
if(db.find(requestedId) == db.end()) {
return Status(StatusCode::NOT_FOUND, "No book entry found for this id");
}
db.erase(requestedId);
return Status::OK;
}
Status PostBook(ServerContext* context, const Book* request, Reply* reply) override {
srand((unsigned) time((NULL)));
int id = rand();
Book book;
book.set_id(id);
book.set_name(request->name());
book.set_author(request->author());
db[id] = book;
reply->set_msg("Book created with id: " + std::to_string(id));
return Status::OK;
}
Status UpdateABook(ServerContext* context, const Book* request, Book* reply) override {
int requestedId = request->id();
if(db.find(requestedId) == db.end()) {
return Status(StatusCode::NOT_FOUND, "No book entry found for this id");
}
db[requestedId] = *request;
reply->CopyFrom(*request);
return Status::OK;
}
};
void RunServer() {
std::string server_address("0.0.0.0:50051");
BookStoreServiceImpl service;
// grpc::EnableDefaultHealthCheckService(true);
grpc::reflection::InitProtoReflectionServerBuilderPlugin();
ServerBuilder builder;
// Listen on the given address without any authentication mechanism
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
// Register "service" as the instance through which
// communication with client takes place
builder.RegisterService(&service);
// Assembling the server
std::unique_ptr<Server> server(builder.BuildAndStart());
std::cout << "Server listening on port: " << server_address << std::endl;
server->Wait();
}
int main(int argc, char** argv) {
RunServer();
return 0;
}