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 <sstream>
23 #include <string>
24
25 #include <grpc/support/log.h>
26 #include <grpcpp/channel.h>
27 #include <grpcpp/client_context.h>
28 #include <grpcpp/create_channel.h>
29
30 #include "src/proto/grpc/testing/echo.grpc.pb.h"
31
32 DEFINE_string(address, "", "Address to connect to");
33 DEFINE_string(mode, "", "Test mode to use");
34
35 using grpc::testing::EchoRequest;
36 using grpc::testing::EchoResponse;
37
38 // In some distros, gflags is in the namespace google, and in some others,
39 // in gflags. This hack is enabling us to find both.
40 namespace google {}
41 namespace gflags {}
42 using namespace google;
43 using namespace gflags;
44
main(int argc,char ** argv)45 int main(int argc, char** argv) {
46 ParseCommandLineFlags(&argc, &argv, true);
47 auto stub = grpc::testing::EchoTestService::NewStub(
48 grpc::CreateChannel(FLAGS_address, grpc::InsecureChannelCredentials()));
49
50 EchoRequest request;
51 EchoResponse response;
52 grpc::ClientContext context;
53 context.set_wait_for_ready(true);
54
55 if (FLAGS_mode == "bidi") {
56 auto stream = stub->BidiStream(&context);
57 for (int i = 0;; i++) {
58 std::ostringstream msg;
59 msg << "Hello " << i;
60 request.set_message(msg.str());
61 GPR_ASSERT(stream->Write(request));
62 GPR_ASSERT(stream->Read(&response));
63 GPR_ASSERT(response.message() == request.message());
64 }
65 } else if (FLAGS_mode == "response") {
66 EchoRequest request;
67 request.set_message("Hello");
68 auto stream = stub->ResponseStream(&context, request);
69 for (;;) {
70 GPR_ASSERT(stream->Read(&response));
71 }
72 } else {
73 gpr_log(GPR_ERROR, "invalid test mode '%s'", FLAGS_mode.c_str());
74 return 1;
75 }
76
77 return 0;
78 }
79