blob: ee9f2752737bcdba92fcaa02cce7509ddd652f57 [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
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 Gaoa5e20d32015-03-25 09:55:20 -070059
Yang Gaoced2b892015-03-26 22:59:54 -070060#include <fstream>
61#include <iostream>
62#include <sstream>
63
Yang Gaoa5e20d32015-03-25 09:55:20 -070064#include <gflags/gflags.h>
Yang Gaob946b5e2015-03-27 13:20:59 -070065#include "test/cpp/util/cli_call.h"
Yang Gao103837e2015-04-15 15:23:54 -070066#include "test/cpp/util/test_config.h"
Yang Gaoa5e20d32015-03-25 09:55:20 -070067#include <grpc++/channel_arguments.h>
68#include <grpc++/channel_interface.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
72#include <grpc/grpc.h>
Yang Gaoa5e20d32015-03-25 09:55:20 -070073
Yang Gaoced2b892015-03-26 22:59:54 -070074DEFINE_bool(enable_ssl, true, "Whether to use ssl/tls.");
75DEFINE_bool(use_auth, false, "Whether to create default google credentials.");
76DEFINE_string(input_binary_file, "",
77 "Path to input file containing serialized request.");
78DEFINE_string(output_binary_file, "output.bin",
79 "Path to output file to write serialized response.");
Yang Gaoa5e20d32015-03-25 09:55:20 -070080
Yang Gaoa5e20d32015-03-25 09:55:20 -070081int main(int argc, char** argv) {
82 grpc_init();
83
Yang Gao103837e2015-04-15 15:23:54 -070084 grpc::testing::InitTest(&argc, &argv, true);
Yang Gaoa5e20d32015-03-25 09:55:20 -070085
Yang Gaoced2b892015-03-26 22:59:54 -070086 if (argc < 4 || grpc::string(argv[1]) != "call") {
Yang Gaoa5e20d32015-03-25 09:55:20 -070087 std::cout << "Usage: grpc_cli call server_host:port full_method_string\n"
88 << "Example: grpc_cli call service.googleapis.com "
Yang Gaoced2b892015-03-26 22:59:54 -070089 << "/grpc.testing.TestService/UnaryCall "
90 << "--input_binary_file=input.bin --output_binary_file=output.bin"
91 << std::endl;
Yang Gaoa5e20d32015-03-25 09:55:20 -070092 }
93 grpc::string server_address(argv[2]);
Yang Gaob946b5e2015-03-27 13:20:59 -070094 // TODO(yangg) basic check of method string
Yang Gao166f9d02015-03-26 23:06:45 -070095 grpc::string method(argv[3]);
Yang Gaoa5e20d32015-03-25 09:55:20 -070096
Yang Gaoced2b892015-03-26 22:59:54 -070097 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 Gaob946b5e2015-03-27 13:20:59 -0700104 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 Gaoced2b892015-03-26 22:59:54 -0700108
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 Gaoa5e20d32015-03-25 09:55:20 -0700119 std::shared_ptr<grpc::ChannelInterface> channel =
120 grpc::CreateChannel(server_address, creds, grpc::ChannelArguments());
121
Yang Gaob946b5e2015-03-27 13:20:59 -0700122 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 Gaoa5e20d32015-03-25 09:55:20 -0700129
130 channel.reset();
131 grpc_shutdown();
132 return 0;
133}