Imported Upstream version 1.21.0
[platform/upstream/grpc.git] / test / cpp / end2end / client_crash_test_server.cc
1 /*
2  *
3  * Copyright 2015 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 <iostream>
21 #include <memory>
22 #include <string>
23
24 #include <grpc/support/log.h>
25 #include <grpcpp/server.h>
26 #include <grpcpp/server_builder.h>
27 #include <grpcpp/server_context.h>
28
29 #include "src/proto/grpc/testing/echo.grpc.pb.h"
30 #include "test/cpp/util/test_config.h"
31
32 DEFINE_string(address, "", "Address to bind to");
33
34 using grpc::testing::EchoRequest;
35 using grpc::testing::EchoResponse;
36
37 // In some distros, gflags is in the namespace google, and in some others,
38 // in gflags. This hack is enabling us to find both.
39 namespace google {}
40 namespace gflags {}
41 using namespace google;
42 using namespace gflags;
43
44 namespace grpc {
45 namespace testing {
46
47 class ServiceImpl final : public ::grpc::testing::EchoTestService::Service {
48   Status BidiStream(
49       ServerContext* context,
50       ServerReaderWriter<EchoResponse, EchoRequest>* stream) override {
51     EchoRequest request;
52     EchoResponse response;
53     while (stream->Read(&request)) {
54       gpr_log(GPR_INFO, "recv msg %s", request.message().c_str());
55       response.set_message(request.message());
56       stream->Write(response);
57     }
58     return Status::OK;
59   }
60 };
61
62 void RunServer() {
63   ServiceImpl service;
64
65   ServerBuilder builder;
66   builder.AddListeningPort(FLAGS_address, grpc::InsecureServerCredentials());
67   builder.RegisterService(&service);
68   std::unique_ptr<Server> server(builder.BuildAndStart());
69   std::cout << "Server listening on " << FLAGS_address << std::endl;
70   server->Wait();
71 }
72 }  // namespace testing
73 }  // namespace grpc
74
75 int main(int argc, char** argv) {
76   grpc::testing::InitTest(&argc, &argv, true);
77   grpc::testing::RunServer();
78
79   return 0;
80 }