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 <grpcpp/server.h>
20 #include <grpcpp/server_builder.h>
21 #include <grpcpp/server_context.h>
22
23 #include <iostream>
24 #include <memory>
25 #include <string>
26
27 #include "absl/flags/flag.h"
28 #include "absl/log/log.h"
29 #include "src/core/util/crash.h"
30 #include "src/proto/grpc/testing/echo.grpc.pb.h"
31 #include "test/cpp/util/test_config.h"
32
33 ABSL_FLAG(std::string, address, "", "Address to bind to");
34
35 using grpc::testing::EchoRequest;
36 using grpc::testing::EchoResponse;
37
38 namespace grpc {
39 namespace testing {
40
41 class ServiceImpl final : public grpc::testing::EchoTestService::Service {
BidiStream(ServerContext *,ServerReaderWriter<EchoResponse,EchoRequest> * stream)42 Status BidiStream(
43 ServerContext* /*context*/,
44 ServerReaderWriter<EchoResponse, EchoRequest>* stream) override {
45 EchoRequest request;
46 EchoResponse response;
47 while (stream->Read(&request)) {
48 LOG(INFO) << "recv msg " << request.message();
49 response.set_message(request.message());
50 stream->Write(response);
51 }
52 return Status::OK;
53 }
54 };
55
RunServer()56 void RunServer() {
57 ServiceImpl service;
58
59 ServerBuilder builder;
60 builder.AddListeningPort(absl::GetFlag(FLAGS_address),
61 grpc::InsecureServerCredentials());
62 builder.RegisterService(&service);
63 std::unique_ptr<Server> server(builder.BuildAndStart());
64 std::cout << "Server listening on " << absl::GetFlag(FLAGS_address)
65 << std::endl;
66 server->Wait();
67 }
68 } // namespace testing
69 } // namespace grpc
70
main(int argc,char ** argv)71 int main(int argc, char** argv) {
72 grpc::testing::InitTest(&argc, &argv, true);
73 grpc::testing::RunServer();
74
75 return 0;
76 }
77