-
Notifications
You must be signed in to change notification settings - Fork 20
/
calculator_server.cc
54 lines (44 loc) · 1.69 KB
/
calculator_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
#include <calculator.grpc.pb.h>
#include <grpc++/grpc++.h>
#include <memory>
#include <iostream>
using namespace grpc;
using namespace calculator;
class CalculatorServiceImpl final : public Calculator::Service {
Status Add(ServerContext* context, const AddRequest* request, AddResponse* response) {
response->set_sum(request->addend_1() + request->addend_2());
return Status::OK;
}
Status Sub(ServerContext* context, const SubRequest* request, SubResponse* response) {
response->set_difference(request->minuend() - request->subtrahend());
return Status::OK;
}
Status Mul(ServerContext* context, const MulRequest* request, MulResponse* response) {
response->set_product(request->multiplicand() * request->multiplier());
return Status::OK;
}
Status Div(ServerContext* context, const DivRequest* request, DivResponse* response) {
response->set_quotient(request->dividend() / request->divisor());
return Status::OK;
}
Status Mod(ServerContext* context, const ModRequest* request, ModResponse* response) {
response->set_result(request->dividend() % request->divisor());
return Status::OK;
}
};
void RunServer() {
std::string server_address{"localhost:2510"};
CalculatorServiceImpl service;
// Build server
ServerBuilder builder;
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
builder.RegisterService(&service);
std::unique_ptr<Server> server{builder.BuildAndStart()};
// Run server
std::cout << "Server listening on " << server_address << std::endl;
server->Wait();
}
int main(int argc, char** argv) {
RunServer();
return 0;
}