Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
murgatroid99 | ace28d3 | 2016-01-14 10:12:10 -0800 | [diff] [blame^] | 3 | * Copyright 2015-2016, 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> |
| 40 | #include <grpc++/generic/generic_stub.h> |
Sree Kuchibhotla | b0d0c8e | 2016-01-13 22:52:17 -0800 | [diff] [blame] | 41 | #include <grpc++/support/byte_buffer.h> |
| 42 | #include <grpc/grpc.h> |
| 43 | #include <grpc/support/log.h> |
| 44 | #include <grpc/support/slice.h> |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 45 | |
| 46 | namespace grpc { |
| 47 | namespace testing { |
| 48 | namespace { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 49 | void* tag(int i) { return (void*)(intptr_t)i; } |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 50 | } // namespace |
| 51 | |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 52 | Status CliCall::Call(std::shared_ptr<grpc::Channel> channel, |
Yang Gao | 102eccb | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 53 | const grpc::string& method, const grpc::string& request, |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 54 | grpc::string* response, |
| 55 | const OutgoingMetadataContainer& metadata, |
| 56 | IncomingMetadataContainer* server_initial_metadata, |
| 57 | IncomingMetadataContainer* server_trailing_metadata) { |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 58 | std::unique_ptr<grpc::GenericStub> stub(new grpc::GenericStub(channel)); |
| 59 | grpc::ClientContext ctx; |
Yang Gao | 102eccb | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 60 | if (!metadata.empty()) { |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 61 | for (OutgoingMetadataContainer::const_iterator iter = metadata.begin(); |
Yang Gao | 102eccb | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 62 | iter != metadata.end(); ++iter) { |
| 63 | ctx.AddMetadata(iter->first, iter->second); |
| 64 | } |
| 65 | } |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 66 | grpc::CompletionQueue cq; |
| 67 | std::unique_ptr<grpc::GenericClientAsyncReaderWriter> call( |
| 68 | stub->Call(&ctx, method, &cq, tag(1))); |
| 69 | void* got_tag; |
| 70 | bool ok; |
| 71 | cq.Next(&got_tag, &ok); |
| 72 | GPR_ASSERT(ok); |
| 73 | |
| 74 | gpr_slice s = gpr_slice_from_copied_string(request.c_str()); |
| 75 | grpc::Slice req_slice(s, grpc::Slice::STEAL_REF); |
| 76 | grpc::ByteBuffer send_buffer(&req_slice, 1); |
| 77 | call->Write(send_buffer, tag(2)); |
| 78 | cq.Next(&got_tag, &ok); |
| 79 | GPR_ASSERT(ok); |
| 80 | call->WritesDone(tag(3)); |
| 81 | cq.Next(&got_tag, &ok); |
| 82 | GPR_ASSERT(ok); |
| 83 | grpc::ByteBuffer recv_buffer; |
| 84 | call->Read(&recv_buffer, tag(4)); |
| 85 | cq.Next(&got_tag, &ok); |
| 86 | if (!ok) { |
| 87 | std::cout << "Failed to read response." << std::endl; |
Yang Gao | 102eccb | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 88 | return Status(StatusCode::INTERNAL, "Failed to read response"); |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 89 | } |
| 90 | grpc::Status status; |
| 91 | call->Finish(&status, tag(5)); |
| 92 | cq.Next(&got_tag, &ok); |
| 93 | GPR_ASSERT(ok); |
| 94 | |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 95 | if (status.ok()) { |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 96 | std::vector<grpc::Slice> slices; |
| 97 | recv_buffer.Dump(&slices); |
| 98 | |
| 99 | response->clear(); |
| 100 | for (size_t i = 0; i < slices.size(); i++) { |
| 101 | response->append(reinterpret_cast<const char*>(slices[i].begin()), |
| 102 | slices[i].size()); |
| 103 | } |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 104 | } |
Yang Gao | 102eccb | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 105 | *server_initial_metadata = ctx.GetServerInitialMetadata(); |
| 106 | *server_trailing_metadata = ctx.GetServerTrailingMetadata(); |
| 107 | return status; |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | } // namespace testing |
murgatroid99 | ace28d3 | 2016-01-14 10:12:10 -0800 | [diff] [blame^] | 111 | } // namespace grpc |