Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <iostream> |
| 20 | #include <memory> |
| 21 | #include <string> |
| 22 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 23 | #include <grpc++/grpc++.h> |
| 24 | |
Nicolas "Pixel" Noble | 24263c3 | 2017-01-11 01:03:09 +0100 | [diff] [blame] | 25 | #ifdef BAZEL_BUILD |
| 26 | #include "examples/protos/helloworld.grpc.pb.h" |
| 27 | #else |
Nicolas "Pixel" Noble | 9efc083 | 2015-04-10 00:15:08 +0200 | [diff] [blame] | 28 | #include "helloworld.grpc.pb.h" |
Nicolas "Pixel" Noble | 24263c3 | 2017-01-11 01:03:09 +0100 | [diff] [blame] | 29 | #endif |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 30 | |
yang-g | d36da04 | 2015-08-25 15:15:35 -0700 | [diff] [blame] | 31 | using grpc::Channel; |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 32 | using grpc::ClientContext; |
| 33 | using grpc::Status; |
| 34 | using helloworld::HelloRequest; |
| 35 | using helloworld::HelloReply; |
| 36 | using helloworld::Greeter; |
| 37 | |
| 38 | class GreeterClient { |
| 39 | public: |
yang-g | d36da04 | 2015-08-25 15:15:35 -0700 | [diff] [blame] | 40 | GreeterClient(std::shared_ptr<Channel> channel) |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 41 | : stub_(Greeter::NewStub(channel)) {} |
| 42 | |
Ryan | 3fec525 | 2016-11-21 21:37:39 -0500 | [diff] [blame] | 43 | // Assembles the client's payload, sends it and presents the response back |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 44 | // from the server. |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 45 | std::string SayHello(const std::string& user) { |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 46 | // Data we are sending to the server. |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 47 | HelloRequest request; |
| 48 | request.set_name(user); |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 49 | |
| 50 | // Container for the data we expect from the server. |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 51 | HelloReply reply; |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 52 | |
| 53 | // Context for the client. It could be used to convey extra information to |
| 54 | // the server and/or tweak certain RPC behaviors. |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 55 | ClientContext context; |
| 56 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 57 | // The actual RPC. |
Yang Gao | 600d70c | 2015-02-20 10:50:46 -0800 | [diff] [blame] | 58 | Status status = stub_->SayHello(&context, request, &reply); |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 59 | |
| 60 | // Act upon its status. |
Yang Gao | fd76276 | 2015-06-17 16:31:39 -0700 | [diff] [blame] | 61 | if (status.ok()) { |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 62 | return reply.message(); |
| 63 | } else { |
Yuchen Zeng | 955a364 | 2016-06-17 13:48:03 -0700 | [diff] [blame] | 64 | std::cout << status.error_code() << ": " << status.error_message() |
| 65 | << std::endl; |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 66 | return "RPC failed"; |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 70 | private: |
| 71 | std::unique_ptr<Greeter::Stub> stub_; |
| 72 | }; |
| 73 | |
| 74 | int main(int argc, char** argv) { |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 75 | // Instantiate the client. It requires a channel, out of which the actual RPCs |
| 76 | // are created. This channel models a connection to an endpoint (in this case, |
| 77 | // localhost at port 50051). We indicate that the channel isn't authenticated |
Julien Boeuf | 8c48a2a | 2015-10-17 22:23:02 -0700 | [diff] [blame] | 78 | // (use of InsecureChannelCredentials()). |
| 79 | GreeterClient greeter(grpc::CreateChannel( |
| 80 | "localhost:50051", grpc::InsecureChannelCredentials())); |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 81 | std::string user("world"); |
| 82 | std::string reply = greeter.SayHello(user); |
| 83 | std::cout << "Greeter received: " << reply << std::endl; |
| 84 | |
Yang Gao | 27bd696 | 2015-05-08 11:42:08 -0700 | [diff] [blame] | 85 | return 0; |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 86 | } |