OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, 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 |
| 34 #include "test/cpp/util/cli_call.h" |
| 35 |
| 36 #include <iostream> |
| 37 |
| 38 #include <grpc++/channel.h> |
| 39 #include <grpc++/client_context.h> |
| 40 #include <grpc++/completion_queue.h> |
| 41 #include <grpc++/generic/generic_stub.h> |
| 42 #include <grpc++/support/byte_buffer.h> |
| 43 #include <grpc/grpc.h> |
| 44 #include <grpc/support/log.h> |
| 45 #include <grpc/support/slice.h> |
| 46 |
| 47 namespace grpc { |
| 48 namespace testing { |
| 49 namespace { |
| 50 void* tag(int i) { return (void*)(intptr_t)i; } |
| 51 } // namespace |
| 52 |
| 53 Status CliCall::Call(std::shared_ptr<grpc::Channel> channel, |
| 54 const grpc::string& method, const grpc::string& request, |
| 55 grpc::string* response, |
| 56 const OutgoingMetadataContainer& metadata, |
| 57 IncomingMetadataContainer* server_initial_metadata, |
| 58 IncomingMetadataContainer* server_trailing_metadata) { |
| 59 std::unique_ptr<grpc::GenericStub> stub(new grpc::GenericStub(channel)); |
| 60 grpc::ClientContext ctx; |
| 61 if (!metadata.empty()) { |
| 62 for (OutgoingMetadataContainer::const_iterator iter = metadata.begin(); |
| 63 iter != metadata.end(); ++iter) { |
| 64 ctx.AddMetadata(iter->first, iter->second); |
| 65 } |
| 66 } |
| 67 grpc::CompletionQueue cq; |
| 68 std::unique_ptr<grpc::GenericClientAsyncReaderWriter> call( |
| 69 stub->Call(&ctx, method, &cq, tag(1))); |
| 70 void* got_tag; |
| 71 bool ok; |
| 72 cq.Next(&got_tag, &ok); |
| 73 GPR_ASSERT(ok); |
| 74 |
| 75 gpr_slice s = gpr_slice_from_copied_string(request.c_str()); |
| 76 grpc::Slice req_slice(s, grpc::Slice::STEAL_REF); |
| 77 grpc::ByteBuffer send_buffer(&req_slice, 1); |
| 78 call->Write(send_buffer, tag(2)); |
| 79 cq.Next(&got_tag, &ok); |
| 80 GPR_ASSERT(ok); |
| 81 call->WritesDone(tag(3)); |
| 82 cq.Next(&got_tag, &ok); |
| 83 GPR_ASSERT(ok); |
| 84 grpc::ByteBuffer recv_buffer; |
| 85 call->Read(&recv_buffer, tag(4)); |
| 86 cq.Next(&got_tag, &ok); |
| 87 if (!ok) { |
| 88 std::cout << "Failed to read response." << std::endl; |
| 89 return Status(StatusCode::INTERNAL, "Failed to read response"); |
| 90 } |
| 91 grpc::Status status; |
| 92 call->Finish(&status, tag(5)); |
| 93 cq.Next(&got_tag, &ok); |
| 94 GPR_ASSERT(ok); |
| 95 |
| 96 if (status.ok()) { |
| 97 std::vector<grpc::Slice> slices; |
| 98 recv_buffer.Dump(&slices); |
| 99 |
| 100 response->clear(); |
| 101 for (size_t i = 0; i < slices.size(); i++) { |
| 102 response->append(reinterpret_cast<const char*>(slices[i].begin()), |
| 103 slices[i].size()); |
| 104 } |
| 105 } |
| 106 *server_initial_metadata = ctx.GetServerInitialMetadata(); |
| 107 *server_trailing_metadata = ctx.GetServerTrailingMetadata(); |
| 108 return status; |
| 109 } |
| 110 |
| 111 } // namespace testing |
| 112 } // namespace grpc |
OLD | NEW |