1 /*
2 * Copyright 2024 gRPC authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <grpcpp/ext/proto_server_reflection_plugin.h>
18 #include <grpcpp/grpcpp.h>
19 #include <grpcpp/health_check_service_interface.h>
20
21 #include <iostream>
22 #include <memory>
23 #include <string>
24
25 #ifdef BAZEL_BUILD
26 #include "examples/protos/helloworld.grpc.pb.h"
27 #else
28 #include "helloworld.grpc.pb.h"
29 #endif
30
31 using grpc::Server;
32 using grpc::ServerBuilder;
33 using grpc::ServerContext;
34 using grpc::Status;
35 using grpc::StatusCode;
36 using helloworld::Greeter;
37 using helloworld::HelloReply;
38 using helloworld::HelloRequest;
39
40 // Logic and data behind the server's behavior.
41 class GreeterServiceImpl final : public Greeter::Service {
42 public:
SayHello(ServerContext * context,const HelloRequest * request,HelloReply * reply)43 Status SayHello(ServerContext* context, const HelloRequest* request,
44 HelloReply* reply) override {
45 if (++request_counter_ % request_modulo_ != 0) {
46 // Return an OK status for every request_modulo_ number of requests,
47 // return UNAVAILABLE otherwise.
48 std::cout << "return UNAVAILABLE" << std::endl;
49 return Status(StatusCode::UNAVAILABLE, "");
50 }
51 std::string prefix("Hello ");
52 reply->set_message(prefix + request->name());
53 std::cout << "return OK" << std::endl;
54 return Status::OK;
55 }
56
57 private:
58 static constexpr int request_modulo_ = 4;
59 int request_counter_ = 0;
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 RunServer(/*port=*/50052);
85 return 0;
86 }
87