1 /*
2 *
3 * Copyright 2021 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <iostream>
20 #include <memory>
21 #include <string>
22
23 #include "absl/flags/flag.h"
24 #include "absl/flags/parse.h"
25 #include "absl/strings/str_format.h"
26
27 #include <grpcpp/ext/proto_server_reflection_plugin.h>
28 #include <grpcpp/grpcpp.h>
29 #include <grpcpp/health_check_service_interface.h>
30
31 #ifdef BAZEL_BUILD
32 #include "examples/protos/helloworld.grpc.pb.h"
33 #else
34 #include "helloworld.grpc.pb.h"
35 #endif
36
37 ABSL_FLAG(uint16_t, port, 50051, "Server port for the service");
38
39 using grpc::CallbackServerContext;
40 using grpc::Server;
41 using grpc::ServerBuilder;
42 using grpc::ServerUnaryReactor;
43 using grpc::Status;
44 using helloworld::Greeter;
45 using helloworld::HelloReply;
46 using helloworld::HelloRequest;
47
48 // Logic and data behind the server's behavior.
49 class GreeterServiceImpl final : public Greeter::CallbackService {
SayHello(CallbackServerContext * context,const HelloRequest * request,HelloReply * reply)50 ServerUnaryReactor* SayHello(CallbackServerContext* context,
51 const HelloRequest* request,
52 HelloReply* reply) override {
53 std::string prefix("Hello ");
54 reply->set_message(prefix + request->name());
55
56 ServerUnaryReactor* reactor = context->DefaultReactor();
57 reactor->Finish(Status::OK);
58 return reactor;
59 }
60 };
61
RunServer(uint16_t port)62 void RunServer(uint16_t port) {
63 std::string server_address = absl::StrFormat("0.0.0.0:%d", port);
64 GreeterServiceImpl service;
65
66 grpc::EnableDefaultHealthCheckService(true);
67 grpc::reflection::InitProtoReflectionServerBuilderPlugin();
68 ServerBuilder builder;
69 // Listen on the given address without any authentication mechanism.
70 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
71 // Register "service" as the instance through which we'll communicate with
72 // clients. In this case it corresponds to an *synchronous* service.
73 builder.RegisterService(&service);
74 // Sample way of setting keepalive arguments on the server. Here, we are
75 // configuring the server to send keepalive pings at a period of 10 minutes
76 // with a timeout of 20 seconds. Additionally, pings will be sent even if
77 // there are no calls in flight on an active HTTP2 connection. When receiving
78 // pings, the server will permit pings at an interval of 10 seconds.
79 builder.AddChannelArgument(GRPC_ARG_KEEPALIVE_TIME_MS,
80 10 * 60 * 1000 /*10 min*/);
81 builder.AddChannelArgument(GRPC_ARG_KEEPALIVE_TIMEOUT_MS,
82 20 * 1000 /*20 sec*/);
83 builder.AddChannelArgument(GRPC_ARG_KEEPALIVE_PERMIT_WITHOUT_CALLS, 1);
84 builder.AddChannelArgument(
85 GRPC_ARG_HTTP2_MIN_RECV_PING_INTERVAL_WITHOUT_DATA_MS,
86 10 * 1000 /*10 sec*/);
87 // Finally assemble the server.
88 std::unique_ptr<Server> server(builder.BuildAndStart());
89 std::cout << "Server listening on " << server_address << std::endl;
90
91 // Wait for the server to shutdown. Note that some other thread must be
92 // responsible for shutting down the server for this call to ever return.
93 server->Wait();
94 }
95
main(int argc,char ** argv)96 int main(int argc, char** argv) {
97 absl::ParseCommandLine(argc, argv);
98 RunServer(absl::GetFlag(FLAGS_port));
99 return 0;
100 }
101