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