Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 1 | /* |
Julien Boeuf | 5be92a3 | 2015-08-28 16:28:18 -0700 | [diff] [blame] | 2 | |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 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 | /* |
yang-g | df012d0 | 2016-05-18 15:44:06 -0700 | [diff] [blame] | 35 | A command line tool to talk to a grpc server. |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 36 | Example of talking to grpc interop server: |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame^] | 37 | grpc_cli call localhost:50051 UnaryCall src/proto/grpc/testing/test.proto \ |
| 38 | "response_size:10" --enable_ssl=false |
yang-g | df012d0 | 2016-05-18 15:44:06 -0700 | [diff] [blame] | 39 | |
| 40 | Options: |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame^] | 41 | 1. --proto_path, if your proto file is not under current working directory, |
yang-g | df012d0 | 2016-05-18 15:44:06 -0700 | [diff] [blame] | 42 | use this flag to provide a search root. It should work similar to the |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame^] | 43 | counterpart in protoc. |
| 44 | 2. --metadata specifies metadata to be sent to the server, such as: |
yang-g | df012d0 | 2016-05-18 15:44:06 -0700 | [diff] [blame] | 45 | --metadata="MyHeaderKey1:Value1:MyHeaderKey2:Value2" |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame^] | 46 | 3. --enable_ssl, whether to use tls. |
| 47 | 4. --use_auth, if set to true, attach a GoogleDefaultCredentials to the call |
| 48 | 3. --input_binary_file, a file containing the serialized request. The file |
yang-g | df012d0 | 2016-05-18 15:44:06 -0700 | [diff] [blame] | 49 | can be generated by calling something like: |
| 50 | protoc --proto_path=src/proto/grpc/testing/ \ |
| 51 | --encode=grpc.testing.SimpleRequest \ |
| 52 | src/proto/grpc/testing/messages.proto \ |
| 53 | < input.txt > input.bin |
| 54 | If this is used and no proto file is provided in the argument list, the |
| 55 | method string has to be exact in the form of /package.service/method. |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame^] | 56 | 4. --output_binary_file, a file to write binary format response into, it can |
yang-g | df012d0 | 2016-05-18 15:44:06 -0700 | [diff] [blame] | 57 | be later decoded using protoc: |
| 58 | protoc --proto_path=src/proto/grpc/testing/ \ |
| 59 | --decode=grpc.testing.SimpleResponse \ |
| 60 | src/proto/grpc/testing/messages.proto \ |
| 61 | < output.bin > output.txt |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 62 | */ |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 63 | |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 64 | #include <fstream> |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame^] | 65 | #include <functional> |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 66 | #include <iostream> |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 67 | |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 68 | #include <gflags/gflags.h> |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame^] | 69 | #include <grpc++/support/config.h> |
| 70 | #include "test/cpp/util/grpc_tool.h" |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 71 | #include "test/cpp/util/test_config.h" |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 72 | |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame^] | 73 | DEFINE_string(outfile, "", "Output file (default is stdout)"); |
Yang Gao | 102eccb | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 74 | |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame^] | 75 | static bool SimplePrint(const grpc::string& outfile, |
| 76 | const grpc::string& output) { |
| 77 | if (outfile.empty()) { |
| 78 | std::cout << output; |
| 79 | } else { |
| 80 | std::ofstream output_file(outfile, std::ios::trunc | std::ios::binary); |
| 81 | output_file << output; |
| 82 | output_file.close(); |
Yang Gao | 102eccb | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 83 | } |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame^] | 84 | return true; |
Yang Gao | 102eccb | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 85 | } |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 86 | |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 87 | int main(int argc, char** argv) { |
Yang Gao | 103837e | 2015-04-15 15:23:54 -0700 | [diff] [blame] | 88 | grpc::testing::InitTest(&argc, &argv, true); |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 89 | |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame^] | 90 | return grpc::testing::GrpcToolMainLib( |
| 91 | argc, (const char**)argv, |
| 92 | std::bind(SimplePrint, FLAGS_outfile, std::placeholders::_1)); |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 93 | } |