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 <grpcpp/ext/proto_server_reflection_plugin.h>
20 #include <grpcpp/grpcpp.h>
21 #include <grpcpp/health_check_service_interface.h>
22
23 #include <iostream>
24 #include <memory>
25 #include <string>
26
27 #include "absl/flags/flag.h"
28 #include "absl/flags/parse.h"
29 #include "absl/strings/str_format.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 // Finally assemble the server.
75 std::unique_ptr<Server> server(builder.BuildAndStart());
76 std::cout << "Server listening on " << server_address << std::endl;
77
78 // Wait for the server to shutdown. Note that some other thread must be
79 // responsible for shutting down the server for this call to ever return.
80 server->Wait();
81 }
82
main(int argc,char ** argv)83 int main(int argc, char** argv) {
84 absl::ParseCommandLine(argc, argv);
85 RunServer(absl::GetFlag(FLAGS_port));
86 return 0;
87 }
88