blob: 746d67deeb9145e728065b1d000db645364922cf [file] [log] [blame]
Yang Gaoa5e20d32015-03-25 09:55:20 -07001/*
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 Gaoced2b892015-03-26 22:59:54 -070034/*
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
Yang Gao102eccb2015-06-16 00:43:25 -070044 protoc --proto_path=test/proto/ \
45 --encode=grpc.testing.SimpleRequest test/proto/messages.proto \
Yang Gaoced2b892015-03-26 22:59:54 -070046 < 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
Yang Gao102eccb2015-06-16 00:43:25 -070054 protoc --proto_path=test/proto/ \
55 --decode=grpc.testing.SimpleResponse test/proto/messages.proto \
Yang Gaoced2b892015-03-26 22:59:54 -070056 < output.bin > output.txt
57 5. Now the text form of response should be in output.txt
Yang Gao102eccb2015-06-16 00:43:25 -070058 Optionally, metadata can be passed to server via flag --metadata, e.g.
59 --metadata="MyHeaderKey1:Value1:MyHeaderKey2:Value2"
Yang Gaoced2b892015-03-26 22:59:54 -070060*/
Yang Gaoa5e20d32015-03-25 09:55:20 -070061
Yang Gaoced2b892015-03-26 22:59:54 -070062#include <fstream>
63#include <iostream>
64#include <sstream>
65
Yang Gaoa5e20d32015-03-25 09:55:20 -070066#include <gflags/gflags.h>
yang-g9e2f90c2015-08-21 15:35:03 -070067#include <grpc/grpc.h>
yang-g8c2be9f2015-08-19 16:28:09 -070068#include <grpc++/channel.h>
Yang Gaoa5e20d32015-03-25 09:55:20 -070069#include <grpc++/create_channel.h>
70#include <grpc++/credentials.h>
Yang Gaoa5e20d32015-03-25 09:55:20 -070071
yang-g9e2f90c2015-08-21 15:35:03 -070072#include "test/cpp/util/cli_call.h"
73#include "test/cpp/util/test_config.h"
Yang Gaoa5e20d32015-03-25 09:55:20 -070074
Yang Gaoced2b892015-03-26 22:59:54 -070075DEFINE_bool(enable_ssl, true, "Whether to use ssl/tls.");
76DEFINE_bool(use_auth, false, "Whether to create default google credentials.");
77DEFINE_string(input_binary_file, "",
78 "Path to input file containing serialized request.");
79DEFINE_string(output_binary_file, "output.bin",
80 "Path to output file to write serialized response.");
Yang Gao102eccb2015-06-16 00:43:25 -070081DEFINE_string(metadata, "",
82 "Metadata to send to server, in the form of key1:val1:key2:val2");
83
84void ParseMetadataFlag(
85 std::multimap<grpc::string, grpc::string>* client_metadata) {
86 if (FLAGS_metadata.empty()) {
87 return;
88 }
89 std::vector<grpc::string> fields;
Craig Tilleraeedff12015-06-22 12:31:53 -070090 const char* delim = ":";
Yang Gao102eccb2015-06-16 00:43:25 -070091 size_t cur, next = -1;
92 do {
93 cur = next + 1;
94 next = FLAGS_metadata.find_first_of(delim, cur);
95 fields.push_back(FLAGS_metadata.substr(cur, next - cur));
96 } while (next != grpc::string::npos);
97 if (fields.size() % 2) {
98 std::cout << "Failed to parse metadata flag" << std::endl;
99 exit(1);
100 }
101 for (size_t i = 0; i < fields.size(); i += 2) {
102 client_metadata->insert(
103 std::pair<grpc::string, grpc::string>(fields[i], fields[i + 1]));
104 }
105}
106
107void PrintMetadata(const std::multimap<grpc::string, grpc::string>& m,
108 const grpc::string& message) {
109 if (m.empty()) {
110 return;
111 }
112 std::cout << message << std::endl;
113 for (std::multimap<grpc::string, grpc::string>::const_iterator iter =
114 m.begin();
115 iter != m.end(); ++iter) {
116 std::cout << iter->first << " : " << iter->second << std::endl;
117 }
118}
Yang Gaoa5e20d32015-03-25 09:55:20 -0700119
Yang Gaoa5e20d32015-03-25 09:55:20 -0700120int main(int argc, char** argv) {
Yang Gao103837e2015-04-15 15:23:54 -0700121 grpc::testing::InitTest(&argc, &argv, true);
Yang Gaoa5e20d32015-03-25 09:55:20 -0700122
Yang Gaoced2b892015-03-26 22:59:54 -0700123 if (argc < 4 || grpc::string(argv[1]) != "call") {
Yang Gaoa5e20d32015-03-25 09:55:20 -0700124 std::cout << "Usage: grpc_cli call server_host:port full_method_string\n"
125 << "Example: grpc_cli call service.googleapis.com "
Yang Gaoced2b892015-03-26 22:59:54 -0700126 << "/grpc.testing.TestService/UnaryCall "
127 << "--input_binary_file=input.bin --output_binary_file=output.bin"
128 << std::endl;
Yang Gaoa5e20d32015-03-25 09:55:20 -0700129 }
130 grpc::string server_address(argv[2]);
Yang Gaob946b5e2015-03-27 13:20:59 -0700131 // TODO(yangg) basic check of method string
Yang Gao166f9d02015-03-26 23:06:45 -0700132 grpc::string method(argv[3]);
Yang Gaoa5e20d32015-03-25 09:55:20 -0700133
Yang Gaoced2b892015-03-26 22:59:54 -0700134 if (FLAGS_input_binary_file.empty()) {
135 std::cout << "Missing --input_binary_file for serialized request."
136 << std::endl;
137 return 1;
138 }
139 std::cout << "connecting to " << server_address << std::endl;
140
Yang Gaob946b5e2015-03-27 13:20:59 -0700141 std::ifstream input_file(FLAGS_input_binary_file,
142 std::ios::in | std::ios::binary);
143 std::stringstream input_stream;
144 input_stream << input_file.rdbuf();
Yang Gaoced2b892015-03-26 22:59:54 -0700145
Yang Gaoa8938922015-05-14 11:51:07 -0700146 std::shared_ptr<grpc::Credentials> creds;
Yang Gaoced2b892015-03-26 22:59:54 -0700147 if (!FLAGS_enable_ssl) {
148 creds = grpc::InsecureCredentials();
149 } else {
150 if (FLAGS_use_auth) {
151 creds = grpc::GoogleDefaultCredentials();
152 } else {
153 creds = grpc::SslCredentials(grpc::SslCredentialsOptions());
154 }
155 }
yang-g8c2be9f2015-08-19 16:28:09 -0700156 std::shared_ptr<grpc::Channel> channel =
Yang Gaoa5e20d32015-03-25 09:55:20 -0700157 grpc::CreateChannel(server_address, creds, grpc::ChannelArguments());
158
Yang Gaob946b5e2015-03-27 13:20:59 -0700159 grpc::string response;
Yang Gao102eccb2015-06-16 00:43:25 -0700160 std::multimap<grpc::string, grpc::string> client_metadata,
161 server_initial_metadata, server_trailing_metadata;
162 ParseMetadataFlag(&client_metadata);
163 PrintMetadata(client_metadata, "Sending client initial metadata:");
164 grpc::Status s = grpc::testing::CliCall::Call(
165 channel, method, input_stream.str(), &response, client_metadata,
166 &server_initial_metadata, &server_trailing_metadata);
167 PrintMetadata(server_initial_metadata,
168 "Received initial metadata from server:");
169 PrintMetadata(server_trailing_metadata,
170 "Received trailing metadata from server:");
Yang Gao763afe12015-06-16 13:19:07 -0700171 if (s.ok()) {
Yang Gao102eccb2015-06-16 00:43:25 -0700172 std::cout << "Rpc succeeded with OK status" << std::endl;
173 if (!response.empty()) {
174 std::ofstream output_file(FLAGS_output_binary_file,
175 std::ios::trunc | std::ios::binary);
176 output_file << response;
177 }
178 } else {
Yang Gao763afe12015-06-16 13:19:07 -0700179 std::cout << "Rpc failed with status code " << s.error_code()
180 << " error message " << s.error_message() << std::endl;
Yang Gaob946b5e2015-03-27 13:20:59 -0700181 }
Yang Gaoa5e20d32015-03-25 09:55:20 -0700182
Yang Gaoa5e20d32015-03-25 09:55:20 -0700183 return 0;
184}