1 /*
2 *
3 * Copyright 2024 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 {
50 public:
set_health_check_service(grpc::HealthCheckServiceInterface * health_check_service)51 void set_health_check_service(
52 grpc::HealthCheckServiceInterface* health_check_service) {
53 health_check_service_ = health_check_service;
54 }
55
56 private:
SayHello(CallbackServerContext * context,const HelloRequest * request,HelloReply * reply)57 ServerUnaryReactor* SayHello(CallbackServerContext* context,
58 const HelloRequest* request,
59 HelloReply* reply) override {
60 std::string prefix("Hello ");
61 reply->set_message(prefix + request->name());
62 ServerUnaryReactor* reactor = context->DefaultReactor();
63 reactor->Finish(Status::OK);
64 // Goes down, then up
65 is_serving_ = !is_serving_;
66 health_check_service_->SetServingStatus(is_serving_);
67 return reactor;
68 }
69
70 grpc::HealthCheckServiceInterface* health_check_service_ = nullptr;
71 bool is_serving_ = true;
72 };
73
RunServer(uint16_t port)74 void RunServer(uint16_t port) {
75 std::string server_address = absl::StrFormat("0.0.0.0:%d", port);
76 GreeterServiceImpl service;
77
78 grpc::EnableDefaultHealthCheckService(true);
79 grpc::reflection::InitProtoReflectionServerBuilderPlugin();
80 ServerBuilder builder;
81 // Listen on the given address without any authentication mechanism.
82 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
83 // Register "service" as the instance through which we'll communicate with
84 // clients. In this case it corresponds to an *synchronous* service.
85 builder.RegisterService(&service);
86 // Finally assemble the server.
87 std::unique_ptr<Server> server(builder.BuildAndStart());
88 service.set_health_check_service(server->GetHealthCheckService());
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