| Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
| Yang Gao | b946b5e | 2015-03-27 13:20:59 -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 | |
| 34 | #include "test/cpp/util/cli_call.h" |
| 35 | |
| 36 | #include <iostream> |
| 37 | |
| yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 38 | #include <grpc++/channel.h> |
| 39 | #include <grpc++/client_context.h> |
| Sree Kuchibhotla | b0d0c8e | 2016-01-13 22:52:17 -0800 | [diff] [blame] | 40 | #include <grpc++/support/byte_buffer.h> |
| 41 | #include <grpc/grpc.h> |
| Craig Tiller | b37d53e | 2016-10-26 16:16:35 -0700 | [diff] [blame] | 42 | #include <grpc/slice.h> |
| Craig Tiller | 28b7242 | 2016-10-26 21:15:29 -0700 | [diff] [blame] | 43 | #include <grpc/support/log.h> |
| Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 44 | |
| 45 | namespace grpc { |
| 46 | namespace testing { |
| 47 | namespace { |
| Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 48 | void* tag(int i) { return (void*)(intptr_t)i; } |
| Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 49 | } // namespace |
| 50 | |
| Yuchen Zeng | f932921 | 2016-09-09 14:27:12 -0700 | [diff] [blame^] | 51 | enum CliCall::CallStatus : intptr_t { CREATE, PROCESS, FINISH }; |
| 52 | |
| yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 53 | Status CliCall::Call(std::shared_ptr<grpc::Channel> channel, |
| Yang Gao | 102eccb6 | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 54 | const grpc::string& method, const grpc::string& request, |
| yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 55 | grpc::string* response, |
| 56 | const OutgoingMetadataContainer& metadata, |
| 57 | IncomingMetadataContainer* server_initial_metadata, |
| 58 | IncomingMetadataContainer* server_trailing_metadata) { |
| Yuchen Zeng | f932921 | 2016-09-09 14:27:12 -0700 | [diff] [blame^] | 59 | 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 | |
| 66 | CliCall::CliCall(std::shared_ptr<grpc::Channel> channel, |
| 67 | const grpc::string& method, |
| 68 | const OutgoingMetadataContainer& metadata) |
| 69 | : stub_(new grpc::GenericStub(channel)) { |
| Yang Gao | 102eccb6 | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 70 | if (!metadata.empty()) { |
| yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 71 | for (OutgoingMetadataContainer::const_iterator iter = metadata.begin(); |
| Yang Gao | 102eccb6 | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 72 | iter != metadata.end(); ++iter) { |
| Yuchen Zeng | f932921 | 2016-09-09 14:27:12 -0700 | [diff] [blame^] | 73 | ctx_.AddMetadata(iter->first, iter->second); |
| Yang Gao | 102eccb6 | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
| Yuchen Zeng | f932921 | 2016-09-09 14:27:12 -0700 | [diff] [blame^] | 76 | call_ = stub_->Call(&ctx_, method, &cq_, tag(1)); |
| Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 77 | void* got_tag; |
| 78 | bool ok; |
| Yuchen Zeng | f932921 | 2016-09-09 14:27:12 -0700 | [diff] [blame^] | 79 | cq_.Next(&got_tag, &ok); |
| Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 80 | GPR_ASSERT(ok); |
| Yuchen Zeng | f932921 | 2016-09-09 14:27:12 -0700 | [diff] [blame^] | 81 | } |
| 82 | |
| 83 | void CliCall::Write(const grpc::string& request) { |
| 84 | void* got_tag; |
| 85 | bool ok; |
| Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 86 | |
| Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 87 | grpc_slice s = grpc_slice_from_copied_string(request.c_str()); |
| Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 88 | grpc::Slice req_slice(s, grpc::Slice::STEAL_REF); |
| 89 | grpc::ByteBuffer send_buffer(&req_slice, 1); |
| Yuchen Zeng | f932921 | 2016-09-09 14:27:12 -0700 | [diff] [blame^] | 90 | call_->Write(send_buffer, tag(2)); |
| 91 | cq_.Next(&got_tag, &ok); |
| Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 92 | GPR_ASSERT(ok); |
| Yuchen Zeng | f932921 | 2016-09-09 14:27:12 -0700 | [diff] [blame^] | 93 | } |
| Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 94 | |
| Yuchen Zeng | f932921 | 2016-09-09 14:27:12 -0700 | [diff] [blame^] | 95 | void 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 Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 106 | std::vector<grpc::Slice> slices; |
| yang-g | 4335e11 | 2016-08-30 10:09:08 -0700 | [diff] [blame] | 107 | (void)recv_buffer.Dump(&slices); |
| Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 108 | |
| 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 Zeng | f932921 | 2016-09-09 14:27:12 -0700 | [diff] [blame^] | 114 | if (server_initial_metadata) { |
| 115 | *server_initial_metadata = ctx_.GetServerInitialMetadata(); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void 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 | |
| 129 | Status 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 Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 139 | } |
| yang-g | df012d0 | 2016-05-18 15:44:06 -0700 | [diff] [blame] | 140 | |
| Yang Gao | 102eccb6 | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 141 | return status; |
| Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | } // namespace testing |
| David Garcia Quintas | 2bf574f | 2016-01-14 15:27:08 -0800 | [diff] [blame] | 145 | } // namespace grpc |