blob: d9232ec4b6c1bcd99e6da6d533f98d05b5b8896d [file] [log] [blame]
Yang Gaob946b5e2015-03-27 13:20:59 -07001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Yang Gaob946b5e2015-03-27 13:20:59 -07004 * 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
34#include "test/cpp/util/cli_call.h"
35
36#include <iostream>
37
yang-g9e2f90c2015-08-21 15:35:03 -070038#include <grpc++/channel.h>
39#include <grpc++/client_context.h>
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080040#include <grpc++/support/byte_buffer.h>
41#include <grpc/grpc.h>
Craig Tillerb37d53e2016-10-26 16:16:35 -070042#include <grpc/slice.h>
Craig Tiller28b72422016-10-26 21:15:29 -070043#include <grpc/support/log.h>
Yang Gaob946b5e2015-03-27 13:20:59 -070044
45namespace grpc {
46namespace testing {
47namespace {
Craig Tiller7536af02015-12-22 13:49:30 -080048void* tag(int i) { return (void*)(intptr_t)i; }
Yang Gaob946b5e2015-03-27 13:20:59 -070049} // namespace
50
Yuchen Zengf9329212016-09-09 14:27:12 -070051enum CliCall::CallStatus : intptr_t { CREATE, PROCESS, FINISH };
52
yang-g8c2be9f2015-08-19 16:28:09 -070053Status CliCall::Call(std::shared_ptr<grpc::Channel> channel,
Yang Gao102eccb62015-06-16 00:43:25 -070054 const grpc::string& method, const grpc::string& request,
yang-ge21908f2015-08-25 13:47:51 -070055 grpc::string* response,
56 const OutgoingMetadataContainer& metadata,
57 IncomingMetadataContainer* server_initial_metadata,
58 IncomingMetadataContainer* server_trailing_metadata) {
Yuchen Zengf9329212016-09-09 14:27:12 -070059 CliCall call(channel, method, metadata);
60 call.Write(request);
61 call.WritesDone();
62 call.Read(response, server_initial_metadata);
63 return call.Finish(server_trailing_metadata);
64}
65
66CliCall::CliCall(std::shared_ptr<grpc::Channel> channel,
67 const grpc::string& method,
68 const OutgoingMetadataContainer& metadata)
69 : stub_(new grpc::GenericStub(channel)) {
Yang Gao102eccb62015-06-16 00:43:25 -070070 if (!metadata.empty()) {
yang-ge21908f2015-08-25 13:47:51 -070071 for (OutgoingMetadataContainer::const_iterator iter = metadata.begin();
Yang Gao102eccb62015-06-16 00:43:25 -070072 iter != metadata.end(); ++iter) {
Yuchen Zengf9329212016-09-09 14:27:12 -070073 ctx_.AddMetadata(iter->first, iter->second);
Yang Gao102eccb62015-06-16 00:43:25 -070074 }
75 }
Yuchen Zengf9329212016-09-09 14:27:12 -070076 call_ = stub_->Call(&ctx_, method, &cq_, tag(1));
Yang Gaob946b5e2015-03-27 13:20:59 -070077 void* got_tag;
78 bool ok;
Yuchen Zengf9329212016-09-09 14:27:12 -070079 cq_.Next(&got_tag, &ok);
Yang Gaob946b5e2015-03-27 13:20:59 -070080 GPR_ASSERT(ok);
Yuchen Zengf9329212016-09-09 14:27:12 -070081}
82
83void CliCall::Write(const grpc::string& request) {
84 void* got_tag;
85 bool ok;
Yang Gaob946b5e2015-03-27 13:20:59 -070086
Craig Tillerd41a4a72016-10-26 16:16:06 -070087 grpc_slice s = grpc_slice_from_copied_string(request.c_str());
Yang Gaob946b5e2015-03-27 13:20:59 -070088 grpc::Slice req_slice(s, grpc::Slice::STEAL_REF);
89 grpc::ByteBuffer send_buffer(&req_slice, 1);
Yuchen Zengf9329212016-09-09 14:27:12 -070090 call_->Write(send_buffer, tag(2));
91 cq_.Next(&got_tag, &ok);
Yang Gaob946b5e2015-03-27 13:20:59 -070092 GPR_ASSERT(ok);
Yuchen Zengf9329212016-09-09 14:27:12 -070093}
Yang Gaob946b5e2015-03-27 13:20:59 -070094
Yuchen Zengf9329212016-09-09 14:27:12 -070095void CliCall::Read(grpc::string* response,
96 IncomingMetadataContainer* server_initial_metadata) {
97 void* got_tag;
98 bool ok;
99
100 grpc::ByteBuffer recv_buffer;
101 call_->Read(&recv_buffer, tag(4));
102 cq_.Next(&got_tag, &ok);
103 if (!ok) {
104 fprintf(stderr, "Failed to read response.");
105 } else {
Yang Gaob946b5e2015-03-27 13:20:59 -0700106 std::vector<grpc::Slice> slices;
yang-g4335e112016-08-30 10:09:08 -0700107 (void)recv_buffer.Dump(&slices);
Yang Gaob946b5e2015-03-27 13:20:59 -0700108
109 response->clear();
110 for (size_t i = 0; i < slices.size(); i++) {
111 response->append(reinterpret_cast<const char*>(slices[i].begin()),
112 slices[i].size());
113 }
Yuchen Zengf9329212016-09-09 14:27:12 -0700114 if (server_initial_metadata) {
115 *server_initial_metadata = ctx_.GetServerInitialMetadata();
116 }
117 }
118}
119
120void CliCall::WritesDone() {
121 void* got_tag;
122 bool ok;
123
124 call_->WritesDone(tag(3));
125 cq_.Next(&got_tag, &ok);
126 GPR_ASSERT(ok);
127}
128
129Status CliCall::Finish(IncomingMetadataContainer* server_trailing_metadata) {
130 void* got_tag;
131 bool ok;
132 grpc::Status status;
133
134 call_->Finish(&status, tag(5));
135 cq_.Next(&got_tag, &ok);
136 GPR_ASSERT(ok);
137 if (server_trailing_metadata) {
138 *server_trailing_metadata = ctx_.GetServerTrailingMetadata();
Yang Gaob946b5e2015-03-27 13:20:59 -0700139 }
yang-gdf012d02016-05-18 15:44:06 -0700140
Yang Gao102eccb62015-06-16 00:43:25 -0700141 return status;
Yang Gaob946b5e2015-03-27 13:20:59 -0700142}
143
144} // namespace testing
David Garcia Quintas2bf574f2016-01-14 15:27:08 -0800145} // namespace grpc