xref: /aosp_15_r20/external/grpc-grpc/examples/cpp/unix_abstract_sockets/server.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 // Copyright 2021 the 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 
19 #include "examples/protos/helloworld.grpc.pb.h"
20 
21 #include <grpcpp/ext/proto_server_reflection_plugin.h>
22 #include <grpcpp/grpcpp.h>
23 #include <grpcpp/health_check_service_interface.h>
24 
25 using grpc::Server;
26 using grpc::ServerBuilder;
27 using grpc::ServerContext;
28 using grpc::Status;
29 using helloworld::Greeter;
30 using helloworld::HelloReply;
31 using helloworld::HelloRequest;
32 
33 // Logic and data behind the server's behavior.
34 class GreeterServiceImpl final : public Greeter::Service {
SayHello(ServerContext * context,const HelloRequest * request,HelloReply * reply)35   Status SayHello(ServerContext* context, const HelloRequest* request,
36                   HelloReply* reply) override {
37     reply->set_message(request->name());
38     std::cout << "Echoing: " << request->name() << std::endl;
39     return Status::OK;
40   }
41 };
42 
RunServer()43 void RunServer() {
44   std::string server_address("unix-abstract:grpc%00abstract");
45   GreeterServiceImpl service;
46   grpc::EnableDefaultHealthCheckService(true);
47   grpc::reflection::InitProtoReflectionServerBuilderPlugin();
48   ServerBuilder builder;
49   builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
50   builder.RegisterService(&service);
51   std::unique_ptr<Server> server(builder.BuildAndStart());
52   std::cout << "Server listening on " << server_address << " ... ";
53   server->Wait();
54 }
55 
main(int argc,char ** argv)56 int main(int argc, char** argv) {
57   RunServer();
58 
59   return 0;
60 }
61