blob: 0e1eab16773a8e71ba4c35064707ee76e2433fef [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 Gaoced2b892015-03-26 22:59:54 -070065#include <grpc++/byte_buffer.h>
Yang Gaoa5e20d32015-03-25 09:55:20 -070066#include <grpc++/channel_arguments.h>
67#include <grpc++/channel_interface.h>
68#include <grpc++/client_context.h>
69#include <grpc++/create_channel.h>
70#include <grpc++/credentials.h>
Yang Gaoced2b892015-03-26 22:59:54 -070071#include <grpc++/generic_stub.h>
Yang Gaoa5e20d32015-03-25 09:55:20 -070072#include <grpc++/status.h>
73#include <grpc++/stream.h>
Yang Gaoa5e20d32015-03-25 09:55:20 -070074
75#include <grpc/grpc.h>
Yang Gaoced2b892015-03-26 22:59:54 -070076#include <grpc/support/log.h>
77#include <grpc/support/slice.h>
Yang Gaoa5e20d32015-03-25 09:55:20 -070078
79// In some distros, gflags is in the namespace google, and in some others,
80// in gflags. This hack is enabling us to find both.
Yang Gaoced2b892015-03-26 22:59:54 -070081namespace google {}
82namespace gflags {}
Yang Gaoa5e20d32015-03-25 09:55:20 -070083using namespace google;
84using namespace gflags;
85
Yang Gaoced2b892015-03-26 22:59:54 -070086DEFINE_bool(enable_ssl, true, "Whether to use ssl/tls.");
87DEFINE_bool(use_auth, false, "Whether to create default google credentials.");
88DEFINE_string(input_binary_file, "",
89 "Path to input file containing serialized request.");
90DEFINE_string(output_binary_file, "output.bin",
91 "Path to output file to write serialized response.");
Yang Gaoa5e20d32015-03-25 09:55:20 -070092
Yang Gaoced2b892015-03-26 22:59:54 -070093void* tag(int i) { return (void*)(gpr_intptr) i; }
Yang Gaoa5e20d32015-03-25 09:55:20 -070094
Yang Gaoced2b892015-03-26 22:59:54 -070095void Call(std::shared_ptr<grpc::ChannelInterface> channel,
96 const grpc::string& method) {
97 std::unique_ptr<grpc::GenericStub> stub(new grpc::GenericStub(channel));
98 grpc::ClientContext ctx;
99 grpc::CompletionQueue cq;
100 std::unique_ptr<grpc::GenericClientAsyncReaderWriter> call(
101 stub->Call(&ctx, method, &cq, tag(1)));
102 void* got_tag;
103 bool ok;
104 cq.Next(&got_tag, &ok);
105 GPR_ASSERT(ok);
106
107 std::ifstream input_file(FLAGS_input_binary_file,
108 std::ios::in | std::ios::binary);
109 std::stringstream input_stream;
110 input_stream << input_file.rdbuf();
111
112 gpr_slice s = gpr_slice_from_copied_string(input_stream.str().c_str());
113 grpc::Slice req_slice(s, grpc::Slice::STEAL_REF);
114 grpc::ByteBuffer request(&req_slice, 1);
115 call->Write(request, tag(2));
116 cq.Next(&got_tag, &ok);
117 GPR_ASSERT(ok);
118 call->WritesDone(tag(3));
119 cq.Next(&got_tag, &ok);
120 GPR_ASSERT(ok);
121 grpc::ByteBuffer recv_buffer;
122 call->Read(&recv_buffer, tag(4));
123 cq.Next(&got_tag, &ok);
124 if (!ok) {
125 std::cout << "Failed to read response." << std::endl;
126 return;
127 }
128 grpc::Status status;
129 call->Finish(&status, tag(5));
130 cq.Next(&got_tag, &ok);
131 GPR_ASSERT(ok);
132
133 if (status.IsOk()) {
134 std::cout << "RPC finished with OK status." << std::endl;
135 std::vector<grpc::Slice> slices;
136 recv_buffer.Dump(&slices);
137
138 std::ofstream output_file(FLAGS_output_binary_file,
139 std::ios::trunc | std::ios::binary);
140 for (size_t i = 0; i < slices.size(); i++) {
141 output_file.write(reinterpret_cast<const char*>(slices[i].begin()),
142 slices[i].size());
143 }
144 } else {
145 std::cout << "RPC finished with status code " << status.code()
146 << " details: " << status.details() << std::endl;
147 }
148}
Yang Gaoa5e20d32015-03-25 09:55:20 -0700149
150int main(int argc, char** argv) {
151 grpc_init();
152
153 ParseCommandLineFlags(&argc, &argv, true);
154
Yang Gaoced2b892015-03-26 22:59:54 -0700155 if (argc < 4 || grpc::string(argv[1]) != "call") {
Yang Gaoa5e20d32015-03-25 09:55:20 -0700156 std::cout << "Usage: grpc_cli call server_host:port full_method_string\n"
157 << "Example: grpc_cli call service.googleapis.com "
Yang Gaoced2b892015-03-26 22:59:54 -0700158 << "/grpc.testing.TestService/UnaryCall "
159 << "--input_binary_file=input.bin --output_binary_file=output.bin"
160 << std::endl;
Yang Gaoa5e20d32015-03-25 09:55:20 -0700161 }
162 grpc::string server_address(argv[2]);
Yang Gaoa5e20d32015-03-25 09:55:20 -0700163
Yang Gaoced2b892015-03-26 22:59:54 -0700164 if (FLAGS_input_binary_file.empty()) {
165 std::cout << "Missing --input_binary_file for serialized request."
166 << std::endl;
167 return 1;
168 }
169 std::cout << "connecting to " << server_address << std::endl;
170
171 // TODO(yangg) basic check of method string
172
173 std::unique_ptr<grpc::Credentials> creds;
174 if (!FLAGS_enable_ssl) {
175 creds = grpc::InsecureCredentials();
176 } else {
177 if (FLAGS_use_auth) {
178 creds = grpc::GoogleDefaultCredentials();
179 } else {
180 creds = grpc::SslCredentials(grpc::SslCredentialsOptions());
181 }
182 }
Yang Gaoa5e20d32015-03-25 09:55:20 -0700183 std::shared_ptr<grpc::ChannelInterface> channel =
184 grpc::CreateChannel(server_address, creds, grpc::ChannelArguments());
185
Yang Gaoced2b892015-03-26 22:59:54 -0700186 Call(channel, method);
Yang Gaoa5e20d32015-03-25 09:55:20 -0700187
188 channel.reset();
189 grpc_shutdown();
190 return 0;
191}