• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2023 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_cat.h"
26 
27 #include <grpcpp/ext/admin_services.h>
28 #include <grpcpp/ext/proto_server_reflection_plugin.h>
29 #include <grpcpp/grpcpp.h>
30 #include <grpcpp/health_check_service_interface.h>
31 #include <grpcpp/xds_server_builder.h>
32 
33 #ifdef BAZEL_BUILD
34 #include "examples/protos/helloworld.grpc.pb.h"
35 #else
36 #include "helloworld.grpc.pb.h"
37 #endif
38 
39 ABSL_FLAG(int32_t, port, 50051, "Server port for service.");
40 ABSL_FLAG(int32_t, maintenance_port, 50052,
41           "Server port for maintenance if --secure is used.");
42 ABSL_FLAG(bool, secure, true, "Secure mode");
43 
44 using grpc::CallbackServerContext;
45 using grpc::Server;
46 using grpc::ServerBuilder;
47 using grpc::ServerUnaryReactor;
48 using grpc::Status;
49 using helloworld::Greeter;
50 using helloworld::HelloReply;
51 using helloworld::HelloRequest;
52 
53 // Logic and data behind the server's behavior.
54 class GreeterServiceImpl final : public Greeter::CallbackService {
SayHello(CallbackServerContext * context,const HelloRequest * request,HelloReply * reply)55   ServerUnaryReactor* SayHello(CallbackServerContext* context,
56                                const HelloRequest* request,
57                                HelloReply* reply) override {
58     std::string prefix("Hello ");
59     reply->set_message(prefix + request->name());
60 
61     ServerUnaryReactor* reactor = context->DefaultReactor();
62     reactor->Finish(Status::OK);
63     return reactor;
64   }
65 };
66 
RunServer()67 void RunServer() {
68   grpc::EnableDefaultHealthCheckService(true);
69   grpc::reflection::InitProtoReflectionServerBuilderPlugin();
70   int port = absl::GetFlag(FLAGS_port);
71   int maintenance_port = absl::GetFlag(FLAGS_maintenance_port);
72   grpc::XdsServerBuilder xds_builder;
73   ServerBuilder builder;
74   std::unique_ptr<Server> xds_enabled_server;
75   std::unique_ptr<Server> server;
76   GreeterServiceImpl service;
77   // Register "service" as the instance through which we'll communicate with
78   // clients. In this case it corresponds to an *synchronous* service.
79   xds_builder.RegisterService(&service);
80   if (absl::GetFlag(FLAGS_secure)) {
81     // Listen on the given address with XdsServerCredentials and a fallback of
82     // InsecureServerCredentials
83     xds_builder.AddListeningPort(
84         absl::StrCat("0.0.0.0:", port),
85         grpc::XdsServerCredentials(grpc::InsecureServerCredentials()));
86     xds_enabled_server = xds_builder.BuildAndStart();
87     gpr_log(GPR_INFO, "Server starting on 0.0.0.0:%d", port);
88     grpc::AddAdminServices(&builder);
89     // For the maintenance server, do not use any authentication mechanism.
90     builder.AddListeningPort(absl::StrCat("0.0.0.0:", maintenance_port),
91                              grpc::InsecureServerCredentials());
92     server = builder.BuildAndStart();
93     gpr_log(GPR_INFO, "Maintenance server listening on 0.0.0.0:%d",
94             maintenance_port);
95   } else {
96     grpc::AddAdminServices(&xds_builder);
97     // Listen on the given address without any authentication mechanism.
98     builder.AddListeningPort(absl::StrCat("0.0.0.0:", port),
99                              grpc::InsecureServerCredentials());
100     server = xds_builder.BuildAndStart();
101     gpr_log(GPR_INFO, "Server listening on 0.0.0.0:%d", port);
102   }
103 
104   // Wait for the server to shutdown. Note that some other thread must be
105   // responsible for shutting down the server for this call to ever return.
106   server->Wait();
107 }
108 
main(int argc,char ** argv)109 int main(int argc, char** argv) {
110   absl::ParseCommandLine(argc, argv);
111   RunServer();
112   return 0;
113 }
114