Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 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 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 38 | #include <grpc++/grpc++.h> |
| 39 | |
Nicolas "Pixel" Noble | 24263c3 | 2017-01-11 01:03:09 +0100 | [diff] [blame] | 40 | #ifdef BAZEL_BUILD |
| 41 | #include "examples/protos/helloworld.grpc.pb.h" |
| 42 | #else |
Nicolas "Pixel" Noble | 9efc083 | 2015-04-10 00:15:08 +0200 | [diff] [blame] | 43 | #include "helloworld.grpc.pb.h" |
Nicolas "Pixel" Noble | 24263c3 | 2017-01-11 01:03:09 +0100 | [diff] [blame] | 44 | #endif |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 45 | |
yang-g | d36da04 | 2015-08-25 15:15:35 -0700 | [diff] [blame] | 46 | using grpc::Channel; |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 47 | using grpc::ClientContext; |
| 48 | using grpc::Status; |
| 49 | using helloworld::HelloRequest; |
| 50 | using helloworld::HelloReply; |
| 51 | using helloworld::Greeter; |
| 52 | |
| 53 | class GreeterClient { |
| 54 | public: |
yang-g | d36da04 | 2015-08-25 15:15:35 -0700 | [diff] [blame] | 55 | GreeterClient(std::shared_ptr<Channel> channel) |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 56 | : stub_(Greeter::NewStub(channel)) {} |
| 57 | |
Ryan | 3fec525 | 2016-11-21 21:37:39 -0500 | [diff] [blame] | 58 | // Assembles the client's payload, sends it and presents the response back |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 59 | // from the server. |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 60 | std::string SayHello(const std::string& user) { |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 61 | // Data we are sending to the server. |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 62 | HelloRequest request; |
| 63 | request.set_name(user); |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 64 | |
| 65 | // Container for the data we expect from the server. |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 66 | HelloReply reply; |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 67 | |
| 68 | // Context for the client. It could be used to convey extra information to |
| 69 | // the server and/or tweak certain RPC behaviors. |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 70 | ClientContext context; |
| 71 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 72 | // The actual RPC. |
Yang Gao | 600d70c | 2015-02-20 10:50:46 -0800 | [diff] [blame] | 73 | Status status = stub_->SayHello(&context, request, &reply); |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 74 | |
| 75 | // Act upon its status. |
Yang Gao | fd76276 | 2015-06-17 16:31:39 -0700 | [diff] [blame] | 76 | if (status.ok()) { |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 77 | return reply.message(); |
| 78 | } else { |
Yuchen Zeng | 955a364 | 2016-06-17 13:48:03 -0700 | [diff] [blame] | 79 | std::cout << status.error_code() << ": " << status.error_message() |
| 80 | << std::endl; |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 81 | return "RPC failed"; |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 85 | private: |
| 86 | std::unique_ptr<Greeter::Stub> stub_; |
| 87 | }; |
| 88 | |
| 89 | int main(int argc, char** argv) { |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 90 | // Instantiate the client. It requires a channel, out of which the actual RPCs |
| 91 | // are created. This channel models a connection to an endpoint (in this case, |
| 92 | // localhost at port 50051). We indicate that the channel isn't authenticated |
Julien Boeuf | 8c48a2a | 2015-10-17 22:23:02 -0700 | [diff] [blame] | 93 | // (use of InsecureChannelCredentials()). |
| 94 | GreeterClient greeter(grpc::CreateChannel( |
| 95 | "localhost:50051", grpc::InsecureChannelCredentials())); |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 96 | std::string user("world"); |
| 97 | std::string reply = greeter.SayHello(user); |
| 98 | std::cout << "Greeter received: " << reply << std::endl; |
| 99 | |
Yang Gao | 27bd696 | 2015-05-08 11:42:08 -0700 | [diff] [blame] | 100 | return 0; |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 101 | } |