blob: d452be360d18b1f080d6a364f5d0ae78b92235bd [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>
David Garcia Quintas2425bbb2016-01-25 17:32:48 -080040#include <grpc++/completion_queue.h>
yang-g9e2f90c2015-08-21 15:35:03 -070041#include <grpc++/generic/generic_stub.h>
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080042#include <grpc++/support/byte_buffer.h>
43#include <grpc/grpc.h>
44#include <grpc/support/log.h>
45#include <grpc/support/slice.h>
Yang Gaob946b5e2015-03-27 13:20:59 -070046
47namespace grpc {
48namespace testing {
49namespace {
Craig Tiller7536af02015-12-22 13:49:30 -080050void* tag(int i) { return (void*)(intptr_t)i; }
Yang Gaob946b5e2015-03-27 13:20:59 -070051} // namespace
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) {
Yang Gaob946b5e2015-03-27 13:20:59 -070059 std::unique_ptr<grpc::GenericStub> stub(new grpc::GenericStub(channel));
60 grpc::ClientContext ctx;
Yang Gao102eccb62015-06-16 00:43:25 -070061 if (!metadata.empty()) {
yang-ge21908f2015-08-25 13:47:51 -070062 for (OutgoingMetadataContainer::const_iterator iter = metadata.begin();
Yang Gao102eccb62015-06-16 00:43:25 -070063 iter != metadata.end(); ++iter) {
64 ctx.AddMetadata(iter->first, iter->second);
65 }
66 }
Yang Gaob946b5e2015-03-27 13:20:59 -070067 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;
Yang Gao102eccb62015-06-16 00:43:25 -070089 return Status(StatusCode::INTERNAL, "Failed to read response");
Yang Gaob946b5e2015-03-27 13:20:59 -070090 }
91 grpc::Status status;
92 call->Finish(&status, tag(5));
93 cq.Next(&got_tag, &ok);
94 GPR_ASSERT(ok);
95
Yang Gaoc1a2c312015-06-16 10:59:46 -070096 if (status.ok()) {
Yang Gaob946b5e2015-03-27 13:20:59 -070097 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 }
Yang Gaob946b5e2015-03-27 13:20:59 -0700105 }
Yang Gao102eccb62015-06-16 00:43:25 -0700106 *server_initial_metadata = ctx.GetServerInitialMetadata();
107 *server_trailing_metadata = ctx.GetServerTrailingMetadata();
108 return status;
Yang Gaob946b5e2015-03-27 13:20:59 -0700109}
110
111} // namespace testing
David Garcia Quintas2bf574f2016-01-14 15:27:08 -0800112} // namespace grpc