3 * Copyright 2020 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
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"
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.");
43 using grpc::ServerBuilder;
44 using grpc::ServerContext;
46 using grpc::testing::Empty;
47 using grpc::testing::SimpleRequest;
48 using grpc::testing::SimpleResponse;
49 using grpc::testing::TestService;
51 class TestServiceImpl : public TestService::Service {
53 explicit TestServiceImpl(const std::string& i) : hostname_(i) {}
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_);
63 Status EmptyCall(ServerContext* context, const Empty* /*request*/,
64 Empty* /*response*/) override {
65 context->AddInitialMetadata("hostname", hostname_);
70 std::string hostname_;
73 void RunServer(const int port, const std::string& hostname) {
74 std::ostringstream server_address;
75 server_address << "0.0.0.0:" << port;
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());
88 int main(int argc, char** argv) {
89 grpc::testing::TestEnvironment env(argc, argv);
90 grpc::testing::InitTest(&argc, &argv, true);
92 char* hostname = grpc_gethostname();
93 if (hostname == nullptr) {
94 std::cout << "Failed to get hostname, terminating" << std::endl;
97 if (absl::GetFlag(FLAGS_port) == 0) {
98 std::cout << "Invalid port, terminating" << std::endl;
102 RunServer(absl::GetFlag(FLAGS_port), hostname);