OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015, 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 <iostream> |
| 35 #include <memory> |
| 36 #include <string> |
| 37 |
| 38 #include <grpc++/grpc++.h> |
| 39 |
| 40 #include "helloworld.grpc.pb.h" |
| 41 |
| 42 using grpc::Channel; |
| 43 using grpc::ClientAsyncResponseReader; |
| 44 using grpc::ClientContext; |
| 45 using grpc::CompletionQueue; |
| 46 using grpc::Status; |
| 47 using helloworld::HelloRequest; |
| 48 using helloworld::HelloReply; |
| 49 using helloworld::Greeter; |
| 50 |
| 51 class GreeterClient { |
| 52 public: |
| 53 explicit GreeterClient(std::shared_ptr<Channel> channel) |
| 54 : stub_(Greeter::NewStub(channel)) {} |
| 55 |
| 56 // Assambles the client's payload, sends it and presents the response back |
| 57 // from the server. |
| 58 std::string SayHello(const std::string& user) { |
| 59 // Data we are sending to the server. |
| 60 HelloRequest request; |
| 61 request.set_name(user); |
| 62 |
| 63 // Container for the data we expect from the server. |
| 64 HelloReply reply; |
| 65 |
| 66 // Context for the client. It could be used to convey extra information to |
| 67 // the server and/or tweak certain RPC behaviors. |
| 68 ClientContext context; |
| 69 |
| 70 // The producer-consumer queue we use to communicate asynchronously with the |
| 71 // gRPC runtime. |
| 72 CompletionQueue cq; |
| 73 |
| 74 // Storage for the status of the RPC upon completion. |
| 75 Status status; |
| 76 |
| 77 // stub_->AsyncSayHello() perform the RPC call, returning an instance we |
| 78 // store in "rpc". Because we are using the asynchronous API, we need the |
| 79 // hold on to the "rpc" instance in order to get updates on the ongoig RPC. |
| 80 std::unique_ptr<ClientAsyncResponseReader<HelloReply> > rpc( |
| 81 stub_->AsyncSayHello(&context, request, &cq)); |
| 82 |
| 83 // Request that, upon completion of the RPC, "reply" be updated with the |
| 84 // server's response; "status" with the indication of whether the operation |
| 85 // was successful. Tag the request with the integer 1. |
| 86 rpc->Finish(&reply, &status, (void*)1); |
| 87 void* got_tag; |
| 88 bool ok = false; |
| 89 // Block until the next result is available in the completion queue "cq". |
| 90 cq.Next(&got_tag, &ok); |
| 91 |
| 92 // Verify that the result from "cq" corresponds, by its tag, our previous |
| 93 // request. |
| 94 GPR_ASSERT(got_tag == (void*)1); |
| 95 // ... and that the request was completed successfully. Note that "ok" |
| 96 // corresponds solely to the request for updates introduced by Finish(). |
| 97 GPR_ASSERT(ok); |
| 98 |
| 99 // Act upon the status of the actual RPC. |
| 100 if (status.ok()) { |
| 101 return reply.message(); |
| 102 } else { |
| 103 return "RPC failed"; |
| 104 } |
| 105 } |
| 106 |
| 107 private: |
| 108 // Out of the passed in Channel comes the stub, stored here, our view of the |
| 109 // server's exposed services. |
| 110 std::unique_ptr<Greeter::Stub> stub_; |
| 111 }; |
| 112 |
| 113 int main(int argc, char** argv) { |
| 114 // Instantiate the client. It requires a channel, out of which the actual RPCs |
| 115 // are created. This channel models a connection to an endpoint (in this case, |
| 116 // localhost at port 50051). We indicate that the channel isn't authenticated |
| 117 // (use of InsecureChannelCredentials()). |
| 118 GreeterClient greeter(grpc::CreateChannel( |
| 119 "localhost:50051", grpc::InsecureChannelCredentials())); |
| 120 std::string user("world"); |
| 121 std::string reply = greeter.SayHello(user); // The actual RPC call! |
| 122 std::cout << "Greeter received: " << reply << std::endl; |
| 123 |
| 124 return 0; |
| 125 } |
OLD | NEW |