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 | /* |
| 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 |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 44 | protoc --proto_path=src/proto/grpc/testing/ \ |
| 45 | --encode=grpc.testing.SimpleRequest |
| 46 | src/proto/grpc/testing/messages.proto \ |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 47 | < input.txt > input.bin |
| 48 | 2. Start a server |
| 49 | make interop_server && bins/opt/interop_server --port=50051 |
| 50 | 3. Run the tool |
| 51 | make grpc_cli && bins/opt/grpc_cli call localhost:50051 \ |
| 52 | /grpc.testing.TestService/UnaryCall --enable_ssl=false \ |
| 53 | --input_binary_file=input.bin --output_binary_file=output.bin |
| 54 | 4. Decode response |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 55 | protoc --proto_path=src/proto/grpc/testing/ \ |
| 56 | --decode=grpc.testing.SimpleResponse src/proto/grpc/testing/messages.proto \ |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 57 | < output.bin > output.txt |
| 58 | 5. Now the text form of response should be in output.txt |
Yang Gao | 102eccb6 | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 59 | Optionally, metadata can be passed to server via flag --metadata, e.g. |
| 60 | --metadata="MyHeaderKey1:Value1:MyHeaderKey2:Value2" |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 61 | */ |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 62 | |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 63 | #include <fstream> |
| 64 | #include <iostream> |
| 65 | #include <sstream> |
| 66 | |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 67 | #include <gflags/gflags.h> |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 68 | #include <grpc++/channel.h> |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 69 | #include <grpc++/create_channel.h> |
Julien Boeuf | 5be92a3 | 2015-08-28 16:28:18 -0700 | [diff] [blame] | 70 | #include <grpc++/security/credentials.h> |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 71 | #include <grpc++/support/string_ref.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 72 | #include <grpc/grpc.h> |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 73 | |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 74 | #include "test/cpp/util/cli_call.h" |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 75 | #include "test/cpp/util/string_ref_helper.h" |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 76 | #include "test/cpp/util/test_config.h" |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 77 | |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 78 | DEFINE_bool(enable_ssl, true, "Whether to use ssl/tls."); |
| 79 | DEFINE_bool(use_auth, false, "Whether to create default google credentials."); |
| 80 | DEFINE_string(input_binary_file, "", |
| 81 | "Path to input file containing serialized request."); |
| 82 | DEFINE_string(output_binary_file, "output.bin", |
| 83 | "Path to output file to write serialized response."); |
Yang Gao | 102eccb6 | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 84 | DEFINE_string(metadata, "", |
| 85 | "Metadata to send to server, in the form of key1:val1:key2:val2"); |
| 86 | |
| 87 | void ParseMetadataFlag( |
| 88 | std::multimap<grpc::string, grpc::string>* client_metadata) { |
| 89 | if (FLAGS_metadata.empty()) { |
| 90 | return; |
| 91 | } |
| 92 | std::vector<grpc::string> fields; |
Craig Tiller | aeedff1 | 2015-06-22 12:31:53 -0700 | [diff] [blame] | 93 | const char* delim = ":"; |
Yang Gao | 102eccb6 | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 94 | size_t cur, next = -1; |
| 95 | do { |
| 96 | cur = next + 1; |
| 97 | next = FLAGS_metadata.find_first_of(delim, cur); |
| 98 | fields.push_back(FLAGS_metadata.substr(cur, next - cur)); |
| 99 | } while (next != grpc::string::npos); |
| 100 | if (fields.size() % 2) { |
| 101 | std::cout << "Failed to parse metadata flag" << std::endl; |
| 102 | exit(1); |
| 103 | } |
| 104 | for (size_t i = 0; i < fields.size(); i += 2) { |
| 105 | client_metadata->insert( |
| 106 | std::pair<grpc::string, grpc::string>(fields[i], fields[i + 1])); |
| 107 | } |
| 108 | } |
| 109 | |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 110 | template <typename T> |
| 111 | void PrintMetadata(const T& m, const grpc::string& message) { |
Yang Gao | 102eccb6 | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 112 | if (m.empty()) { |
| 113 | return; |
| 114 | } |
| 115 | std::cout << message << std::endl; |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 116 | grpc::string pair; |
| 117 | for (typename T::const_iterator iter = m.begin(); iter != m.end(); ++iter) { |
| 118 | pair.clear(); |
| 119 | pair.append(iter->first.data(), iter->first.size()); |
| 120 | pair.append(" : "); |
| 121 | pair.append(iter->second.data(), iter->second.size()); |
| 122 | std::cout << pair << std::endl; |
Yang Gao | 102eccb6 | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 125 | |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 126 | int main(int argc, char** argv) { |
Yang Gao | 103837e | 2015-04-15 15:23:54 -0700 | [diff] [blame] | 127 | grpc::testing::InitTest(&argc, &argv, true); |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 128 | |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 129 | if (argc < 4 || grpc::string(argv[1]) != "call") { |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 130 | std::cout << "Usage: grpc_cli call server_host:port full_method_string\n" |
| 131 | << "Example: grpc_cli call service.googleapis.com " |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 132 | << "/grpc.testing.TestService/UnaryCall " |
| 133 | << "--input_binary_file=input.bin --output_binary_file=output.bin" |
| 134 | << std::endl; |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 135 | } |
| 136 | grpc::string server_address(argv[2]); |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 137 | // TODO(yangg) basic check of method string |
Yang Gao | 166f9d0 | 2015-03-26 23:06:45 -0700 | [diff] [blame] | 138 | grpc::string method(argv[3]); |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 139 | |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 140 | if (FLAGS_input_binary_file.empty()) { |
| 141 | std::cout << "Missing --input_binary_file for serialized request." |
| 142 | << std::endl; |
| 143 | return 1; |
| 144 | } |
| 145 | std::cout << "connecting to " << server_address << std::endl; |
| 146 | |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 147 | std::ifstream input_file(FLAGS_input_binary_file, |
| 148 | std::ios::in | std::ios::binary); |
| 149 | std::stringstream input_stream; |
| 150 | input_stream << input_file.rdbuf(); |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 151 | |
Julien Boeuf | e5adc0e | 2015-10-12 14:08:10 -0700 | [diff] [blame] | 152 | std::shared_ptr<grpc::ChannelCredentials> creds; |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 153 | if (!FLAGS_enable_ssl) { |
Julien Boeuf | e5adc0e | 2015-10-12 14:08:10 -0700 | [diff] [blame] | 154 | creds = grpc::InsecureChannelCredentials(); |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 155 | } else { |
| 156 | if (FLAGS_use_auth) { |
| 157 | creds = grpc::GoogleDefaultCredentials(); |
| 158 | } else { |
| 159 | creds = grpc::SslCredentials(grpc::SslCredentialsOptions()); |
| 160 | } |
| 161 | } |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 162 | std::shared_ptr<grpc::Channel> channel = |
yang-g | 730055d | 2015-08-27 12:29:45 -0700 | [diff] [blame] | 163 | grpc::CreateChannel(server_address, creds); |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 164 | |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 165 | grpc::string response; |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 166 | std::multimap<grpc::string, grpc::string> client_metadata; |
| 167 | std::multimap<grpc::string_ref, grpc::string_ref> server_initial_metadata, |
| 168 | server_trailing_metadata; |
Yang Gao | 102eccb6 | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 169 | ParseMetadataFlag(&client_metadata); |
| 170 | PrintMetadata(client_metadata, "Sending client initial metadata:"); |
| 171 | grpc::Status s = grpc::testing::CliCall::Call( |
| 172 | channel, method, input_stream.str(), &response, client_metadata, |
| 173 | &server_initial_metadata, &server_trailing_metadata); |
| 174 | PrintMetadata(server_initial_metadata, |
| 175 | "Received initial metadata from server:"); |
| 176 | PrintMetadata(server_trailing_metadata, |
| 177 | "Received trailing metadata from server:"); |
Yang Gao | 763afe1 | 2015-06-16 13:19:07 -0700 | [diff] [blame] | 178 | if (s.ok()) { |
Yang Gao | 102eccb6 | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 179 | std::cout << "Rpc succeeded with OK status" << std::endl; |
| 180 | if (!response.empty()) { |
| 181 | std::ofstream output_file(FLAGS_output_binary_file, |
| 182 | std::ios::trunc | std::ios::binary); |
| 183 | output_file << response; |
| 184 | } |
| 185 | } else { |
Yang Gao | 763afe1 | 2015-06-16 13:19:07 -0700 | [diff] [blame] | 186 | std::cout << "Rpc failed with status code " << s.error_code() |
| 187 | << " error message " << s.error_message() << std::endl; |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 188 | } |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 189 | |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 190 | return 0; |
| 191 | } |