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
31 DEFINE_string(address, "", "Address to bind to");
32
33 using grpc::testing::EchoRequest;
34 using grpc::testing::EchoResponse;
35
36 // In some distros, gflags is in the namespace google, and in some others,
37 // in gflags. This hack is enabling us to find both.
38 namespace google {}
39 namespace gflags {}
40 using namespace google;
41 using namespace gflags;
42
43 namespace grpc {
44 namespace testing {
45
46 class ServiceImpl final : public ::grpc::testing::EchoTestService::Service {
BidiStream(ServerContext * context,ServerReaderWriter<EchoResponse,EchoRequest> * stream)47 Status BidiStream(
48 ServerContext* context,
49 ServerReaderWriter<EchoResponse, EchoRequest>* stream) override {
50 EchoRequest request;
51 EchoResponse response;
52 while (stream->Read(&request)) {
53 gpr_log(GPR_INFO, "recv msg %s", request.message().c_str());
54 response.set_message(request.message());
55 stream->Write(response);
56 }
57 return Status::OK;
58 }
59 };
60
RunServer()61 void RunServer() {
62 ServiceImpl service;
63
64 ServerBuilder builder;
65 builder.AddListeningPort(FLAGS_address, grpc::InsecureServerCredentials());
66 builder.RegisterService(&service);
67 std::unique_ptr<Server> server(builder.BuildAndStart());
68 std::cout << "Server listening on " << FLAGS_address << std::endl;
69 server->Wait();
70 }
71 } // namespace testing
72 } // namespace grpc
73
main(int argc,char ** argv)74 int main(int argc, char** argv) {
75 ParseCommandLineFlags(&argc, &argv, true);
76 grpc::testing::RunServer();
77
78 return 0;
79 }
80