Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 34 | /* |
| 35 | A command line tool to talk to any grpc server. |
| 36 | Example of talking to grpc interop server: |
| 37 | 1. Prepare request binary file: |
| 38 | a. create a text file input.txt, containing the following: |
| 39 | response_size: 10 |
| 40 | payload: { |
| 41 | body: "hello world" |
| 42 | } |
| 43 | b. under grpc/ run |
| 44 | protoc --proto_path=test/cpp/interop/ \ |
| 45 | --encode=grpc.testing.SimpleRequest test/cpp/interop/messages.proto \ |
| 46 | < input.txt > input.bin |
| 47 | 2. Start a server |
| 48 | make interop_server && bins/opt/interop_server --port=50051 |
| 49 | 3. Run the tool |
| 50 | make grpc_cli && bins/opt/grpc_cli call localhost:50051 \ |
| 51 | /grpc.testing.TestService/UnaryCall --enable_ssl=false \ |
| 52 | --input_binary_file=input.bin --output_binary_file=output.bin |
| 53 | 4. Decode response |
| 54 | protoc --proto_path=test/cpp/interop/ \ |
| 55 | --decode=grpc.testing.SimpleResponse test/cpp/interop/messages.proto \ |
| 56 | < output.bin > output.txt |
| 57 | 5. Now the text form of response should be in output.txt |
| 58 | */ |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 59 | |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 60 | #include <fstream> |
| 61 | #include <iostream> |
| 62 | #include <sstream> |
| 63 | |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 64 | #include <gflags/gflags.h> |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 65 | #include "test/cpp/util/cli_call.h" |
Yang Gao | 103837e | 2015-04-15 15:23:54 -0700 | [diff] [blame^] | 66 | #include "test/cpp/util/test_config.h" |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 67 | #include <grpc++/channel_arguments.h> |
| 68 | #include <grpc++/channel_interface.h> |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 69 | #include <grpc++/create_channel.h> |
| 70 | #include <grpc++/credentials.h> |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 71 | |
| 72 | #include <grpc/grpc.h> |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 73 | |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 74 | DEFINE_bool(enable_ssl, true, "Whether to use ssl/tls."); |
| 75 | DEFINE_bool(use_auth, false, "Whether to create default google credentials."); |
| 76 | DEFINE_string(input_binary_file, "", |
| 77 | "Path to input file containing serialized request."); |
| 78 | DEFINE_string(output_binary_file, "output.bin", |
| 79 | "Path to output file to write serialized response."); |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 80 | |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 81 | int main(int argc, char** argv) { |
| 82 | grpc_init(); |
| 83 | |
Yang Gao | 103837e | 2015-04-15 15:23:54 -0700 | [diff] [blame^] | 84 | grpc::testing::InitTest(&argc, &argv, true); |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 85 | |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 86 | if (argc < 4 || grpc::string(argv[1]) != "call") { |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 87 | std::cout << "Usage: grpc_cli call server_host:port full_method_string\n" |
| 88 | << "Example: grpc_cli call service.googleapis.com " |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 89 | << "/grpc.testing.TestService/UnaryCall " |
| 90 | << "--input_binary_file=input.bin --output_binary_file=output.bin" |
| 91 | << std::endl; |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 92 | } |
| 93 | grpc::string server_address(argv[2]); |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 94 | // TODO(yangg) basic check of method string |
Yang Gao | 166f9d0 | 2015-03-26 23:06:45 -0700 | [diff] [blame] | 95 | grpc::string method(argv[3]); |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 96 | |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 97 | if (FLAGS_input_binary_file.empty()) { |
| 98 | std::cout << "Missing --input_binary_file for serialized request." |
| 99 | << std::endl; |
| 100 | return 1; |
| 101 | } |
| 102 | std::cout << "connecting to " << server_address << std::endl; |
| 103 | |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 104 | std::ifstream input_file(FLAGS_input_binary_file, |
| 105 | std::ios::in | std::ios::binary); |
| 106 | std::stringstream input_stream; |
| 107 | input_stream << input_file.rdbuf(); |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 108 | |
| 109 | std::unique_ptr<grpc::Credentials> creds; |
| 110 | if (!FLAGS_enable_ssl) { |
| 111 | creds = grpc::InsecureCredentials(); |
| 112 | } else { |
| 113 | if (FLAGS_use_auth) { |
| 114 | creds = grpc::GoogleDefaultCredentials(); |
| 115 | } else { |
| 116 | creds = grpc::SslCredentials(grpc::SslCredentialsOptions()); |
| 117 | } |
| 118 | } |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 119 | std::shared_ptr<grpc::ChannelInterface> channel = |
| 120 | grpc::CreateChannel(server_address, creds, grpc::ChannelArguments()); |
| 121 | |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 122 | grpc::string response; |
| 123 | grpc::testing::CliCall::Call(channel, method, input_stream.str(), &response); |
| 124 | if (!response.empty()) { |
| 125 | std::ofstream output_file(FLAGS_output_binary_file, |
| 126 | std::ios::trunc | std::ios::binary); |
| 127 | output_file << response; |
| 128 | } |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 129 | |
| 130 | channel.reset(); |
| 131 | grpc_shutdown(); |
| 132 | return 0; |
| 133 | } |