blob: 28f5e47b46710fcf64aed93d870086b0aa487c26 [file] [log] [blame]
Yang Gaob946b5e2015-03-27 13:20:59 -07001/*
2 *
murgatroid99ace28d32016-01-14 10:12:10 -08003 * Copyright 2015-2016, 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>
40#include <grpc++/generic/generic_stub.h>
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080041#include <grpc++/support/byte_buffer.h>
42#include <grpc/grpc.h>
43#include <grpc/support/log.h>
44#include <grpc/support/slice.h>
Yang Gaob946b5e2015-03-27 13:20:59 -070045
46namespace grpc {
47namespace testing {
48namespace {
Craig Tiller7536af02015-12-22 13:49:30 -080049void* tag(int i) { return (void*)(intptr_t)i; }
Yang Gaob946b5e2015-03-27 13:20:59 -070050} // namespace
51
yang-g8c2be9f2015-08-19 16:28:09 -070052Status CliCall::Call(std::shared_ptr<grpc::Channel> channel,
Yang Gao102eccb2015-06-16 00:43:25 -070053 const grpc::string& method, const grpc::string& request,
yang-ge21908f2015-08-25 13:47:51 -070054 grpc::string* response,
55 const OutgoingMetadataContainer& metadata,
56 IncomingMetadataContainer* server_initial_metadata,
57 IncomingMetadataContainer* server_trailing_metadata) {
Yang Gaob946b5e2015-03-27 13:20:59 -070058 std::unique_ptr<grpc::GenericStub> stub(new grpc::GenericStub(channel));
59 grpc::ClientContext ctx;
Yang Gao102eccb2015-06-16 00:43:25 -070060 if (!metadata.empty()) {
yang-ge21908f2015-08-25 13:47:51 -070061 for (OutgoingMetadataContainer::const_iterator iter = metadata.begin();
Yang Gao102eccb2015-06-16 00:43:25 -070062 iter != metadata.end(); ++iter) {
63 ctx.AddMetadata(iter->first, iter->second);
64 }
65 }
Yang Gaob946b5e2015-03-27 13:20:59 -070066 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 Gao102eccb2015-06-16 00:43:25 -070088 return Status(StatusCode::INTERNAL, "Failed to read response");
Yang Gaob946b5e2015-03-27 13:20:59 -070089 }
90 grpc::Status status;
91 call->Finish(&status, tag(5));
92 cq.Next(&got_tag, &ok);
93 GPR_ASSERT(ok);
94
Yang Gaoc1a2c312015-06-16 10:59:46 -070095 if (status.ok()) {
Yang Gaob946b5e2015-03-27 13:20:59 -070096 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 Gaob946b5e2015-03-27 13:20:59 -0700104 }
Yang Gao102eccb2015-06-16 00:43:25 -0700105 *server_initial_metadata = ctx.GetServerInitialMetadata();
106 *server_trailing_metadata = ctx.GetServerTrailingMetadata();
107 return status;
Yang Gaob946b5e2015-03-27 13:20:59 -0700108}
109
110} // namespace testing
murgatroid99ace28d32016-01-14 10:12:10 -0800111} // namespace grpc