• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gflags/gflags.h>
20 #include <grpc/grpc.h>
21 #include <grpc/support/log.h>
22 #include <grpc/support/time.h>
23 #include <grpcpp/server.h>
24 #include <grpcpp/server_builder.h>
25 #include <grpcpp/server_context.h>
26 
27 #include <sstream>
28 
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 DEFINE_int32(port, 50051, "Server port.");
39 DEFINE_string(server_id, "cpp_server", "Server ID to include in responses.");
40 
41 using grpc::Server;
42 using grpc::ServerBuilder;
43 using grpc::ServerContext;
44 using grpc::ServerCredentials;
45 using grpc::ServerReader;
46 using grpc::ServerReaderWriter;
47 using grpc::ServerWriter;
48 using grpc::Status;
49 using grpc::testing::Empty;
50 using grpc::testing::SimpleRequest;
51 using grpc::testing::SimpleResponse;
52 using grpc::testing::TestService;
53 
54 class TestServiceImpl : public TestService::Service {
55  public:
TestServiceImpl(const std::string & i)56   TestServiceImpl(const std::string& i) : hostname_(i) {}
57 
UnaryCall(ServerContext * context,const SimpleRequest * request,SimpleResponse * response)58   Status UnaryCall(ServerContext* context, const SimpleRequest* request,
59                    SimpleResponse* response) {
60     response->set_server_id(FLAGS_server_id);
61     response->set_hostname(hostname_);
62     context->AddInitialMetadata("hostname", hostname_);
63     return Status::OK;
64   }
65 
EmptyCall(ServerContext * context,const Empty * request,Empty * response)66   Status EmptyCall(ServerContext* context, const Empty* request,
67                    Empty* response) {
68     context->AddInitialMetadata("hostname", hostname_);
69     return Status::OK;
70   }
71 
72  private:
73   std::string hostname_;
74 };
75 
RunServer(const int port,const std::string & hostname)76 void RunServer(const int port, const std::string& hostname) {
77   std::ostringstream server_address;
78   server_address << "0.0.0.0:" << port;
79 
80   TestServiceImpl service(hostname);
81   ServerBuilder builder;
82   builder.RegisterService(&service);
83   builder.AddListeningPort(server_address.str(),
84                            grpc::InsecureServerCredentials());
85   std::unique_ptr<Server> server(builder.BuildAndStart());
86   gpr_log(GPR_INFO, "Server listening on %s", server_address.str().c_str());
87 
88   server->Wait();
89 }
90 
main(int argc,char ** argv)91 int main(int argc, char** argv) {
92   grpc::testing::TestEnvironment env(argc, argv);
93   grpc::testing::InitTest(&argc, &argv, true);
94 
95   char* hostname = grpc_gethostname();
96   if (hostname == nullptr) {
97     std::cout << "Failed to get hostname, terminating" << std::endl;
98     return 1;
99   }
100   if (FLAGS_port == 0) {
101     std::cout << "Invalid port, terminating" << std::endl;
102     return 1;
103   }
104 
105   RunServer(FLAGS_port, hostname);
106 
107   return 0;
108 }
109