1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  * Copyright (C) 2023 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19 
20 #include <iostream>
21 #include <memory>
22 #include <string>
23 
24 #include <gflags/gflags.h>
25 #include <grpcpp/ext/proto_server_reflection_plugin.h>
26 #include <grpcpp/grpcpp.h>
27 #include <grpcpp/health_check_service_interface.h>
28 
29 #include "echo.grpc.pb.h"
30 
31 using echoserver::EchoReply;
32 using echoserver::EchoRequest;
33 using echoserver::EchoService;
34 using grpc::Server;
35 using grpc::ServerBuilder;
36 using grpc::ServerContext;
37 using grpc::Status;
38 
39 DEFINE_string(grpc_uds_path, "", "grpc_uds_path");
40 
41 class EchoServiceImpl final : public EchoService::Service {
Echo(ServerContext * context,const EchoRequest * request,EchoReply * reply)42   Status Echo(ServerContext* context, const EchoRequest* request,
43               EchoReply* reply) override {
44     reply->set_message(request->message());
45     return Status::OK;
46   }
47 };
48 
RunServer()49 void RunServer() {
50   std::string server_address("unix:" + FLAGS_grpc_uds_path);
51   EchoServiceImpl service;
52 
53   grpc::EnableDefaultHealthCheckService(true);
54   grpc::reflection::InitProtoReflectionServerBuilderPlugin();
55   ServerBuilder builder;
56   // Listen on the given address without any authentication mechanism.
57   builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
58   // Register "service" as the instance through which we'll communicate with
59   // clients. In this case it corresponds to an *synchronous* service.
60   builder.RegisterService(&service);
61   // Finally assemble the server.
62   std::unique_ptr<Server> server(builder.BuildAndStart());
63   std::cout << "Server listening on " << server_address << std::endl;
64 
65   // Wait for the server to shutdown. Note that some other thread must be
66   // responsible for shutting down the server for this call to ever return.
67   server->Wait();
68 }
69 
main(int argc,char ** argv)70 int main(int argc, char** argv) {
71   ::gflags::ParseCommandLineFlags(&argc, &argv, true);
72   RunServer();
73 
74   return 0;
75 }