Imported Upstream version 1.36.0
[platform/upstream/grpc.git] / test / cpp / interop / xds_interop_server.cc
1 /*
2  *
3  * Copyright 2020 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 <grpc/grpc.h>
20 #include <grpc/support/log.h>
21 #include <grpc/support/time.h>
22 #include <grpcpp/server.h>
23 #include <grpcpp/server_builder.h>
24 #include <grpcpp/server_context.h>
25
26 #include <sstream>
27
28 #include "absl/flags/flag.h"
29 #include "src/core/lib/gpr/string.h"
30 #include "src/core/lib/iomgr/gethostname.h"
31 #include "src/core/lib/transport/byte_stream.h"
32 #include "src/proto/grpc/testing/empty.pb.h"
33 #include "src/proto/grpc/testing/messages.pb.h"
34 #include "src/proto/grpc/testing/test.grpc.pb.h"
35 #include "test/core/util/test_config.h"
36 #include "test/cpp/util/test_config.h"
37
38 ABSL_FLAG(int32_t, port, 50051, "Server port.");
39 ABSL_FLAG(std::string, server_id, "cpp_server",
40           "Server ID to include in responses.");
41
42 using grpc::Server;
43 using grpc::ServerBuilder;
44 using grpc::ServerContext;
45 using grpc::Status;
46 using grpc::testing::Empty;
47 using grpc::testing::SimpleRequest;
48 using grpc::testing::SimpleResponse;
49 using grpc::testing::TestService;
50
51 class TestServiceImpl : public TestService::Service {
52  public:
53   explicit TestServiceImpl(const std::string& i) : hostname_(i) {}
54
55   Status UnaryCall(ServerContext* context, const SimpleRequest* /*request*/,
56                    SimpleResponse* response) override {
57     response->set_server_id(absl::GetFlag(FLAGS_server_id));
58     response->set_hostname(hostname_);
59     context->AddInitialMetadata("hostname", hostname_);
60     return Status::OK;
61   }
62
63   Status EmptyCall(ServerContext* context, const Empty* /*request*/,
64                    Empty* /*response*/) override {
65     context->AddInitialMetadata("hostname", hostname_);
66     return Status::OK;
67   }
68
69  private:
70   std::string hostname_;
71 };
72
73 void RunServer(const int port, const std::string& hostname) {
74   std::ostringstream server_address;
75   server_address << "0.0.0.0:" << port;
76
77   TestServiceImpl service(hostname);
78   ServerBuilder builder;
79   builder.RegisterService(&service);
80   builder.AddListeningPort(server_address.str(),
81                            grpc::InsecureServerCredentials());
82   std::unique_ptr<Server> server(builder.BuildAndStart());
83   gpr_log(GPR_INFO, "Server listening on %s", server_address.str().c_str());
84
85   server->Wait();
86 }
87
88 int main(int argc, char** argv) {
89   grpc::testing::TestEnvironment env(argc, argv);
90   grpc::testing::InitTest(&argc, &argv, true);
91
92   char* hostname = grpc_gethostname();
93   if (hostname == nullptr) {
94     std::cout << "Failed to get hostname, terminating" << std::endl;
95     return 1;
96   }
97   if (absl::GetFlag(FLAGS_port) == 0) {
98     std::cout << "Invalid port, terminating" << std::endl;
99     return 1;
100   }
101
102   RunServer(absl::GetFlag(FLAGS_port), hostname);
103
104   return 0;
105 }