blob: 8ee33b1383b3ec63ca08bfc0476b29be14087d29 [file] [log] [blame]
Yang Gaod9569372015-02-19 14:22:52 -08001/*
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 Tillerd6599a32015-09-03 09:37:02 -070038#include <grpc++/grpc++.h>
39
Nicolas "Pixel" Noble24263c32017-01-11 01:03:09 +010040#ifdef BAZEL_BUILD
41#include "examples/protos/helloworld.grpc.pb.h"
42#else
Nicolas "Pixel" Noble9efc0832015-04-10 00:15:08 +020043#include "helloworld.grpc.pb.h"
Nicolas "Pixel" Noble24263c32017-01-11 01:03:09 +010044#endif
Yang Gaod9569372015-02-19 14:22:52 -080045
yang-gd36da042015-08-25 15:15:35 -070046using grpc::Channel;
Yang Gaod9569372015-02-19 14:22:52 -080047using grpc::ClientContext;
48using grpc::Status;
49using helloworld::HelloRequest;
50using helloworld::HelloReply;
51using helloworld::Greeter;
52
53class GreeterClient {
54 public:
yang-gd36da042015-08-25 15:15:35 -070055 GreeterClient(std::shared_ptr<Channel> channel)
Yang Gaod9569372015-02-19 14:22:52 -080056 : stub_(Greeter::NewStub(channel)) {}
57
Ryan3fec5252016-11-21 21:37:39 -050058 // Assembles the client's payload, sends it and presents the response back
Craig Tillerd6599a32015-09-03 09:37:02 -070059 // from the server.
Yang Gaod9569372015-02-19 14:22:52 -080060 std::string SayHello(const std::string& user) {
Craig Tillerd6599a32015-09-03 09:37:02 -070061 // Data we are sending to the server.
Yang Gaod9569372015-02-19 14:22:52 -080062 HelloRequest request;
63 request.set_name(user);
Craig Tillerd6599a32015-09-03 09:37:02 -070064
65 // Container for the data we expect from the server.
Yang Gaod9569372015-02-19 14:22:52 -080066 HelloReply reply;
Craig Tillerd6599a32015-09-03 09:37:02 -070067
68 // Context for the client. It could be used to convey extra information to
69 // the server and/or tweak certain RPC behaviors.
Yang Gaod9569372015-02-19 14:22:52 -080070 ClientContext context;
71
Craig Tillerd6599a32015-09-03 09:37:02 -070072 // The actual RPC.
Yang Gao600d70c2015-02-20 10:50:46 -080073 Status status = stub_->SayHello(&context, request, &reply);
Craig Tillerd6599a32015-09-03 09:37:02 -070074
75 // Act upon its status.
Yang Gaofd762762015-06-17 16:31:39 -070076 if (status.ok()) {
Yang Gaod9569372015-02-19 14:22:52 -080077 return reply.message();
78 } else {
Yuchen Zeng955a3642016-06-17 13:48:03 -070079 std::cout << status.error_code() << ": " << status.error_message()
80 << std::endl;
Craig Tillerd6599a32015-09-03 09:37:02 -070081 return "RPC failed";
Yang Gaod9569372015-02-19 14:22:52 -080082 }
83 }
84
Yang Gaod9569372015-02-19 14:22:52 -080085 private:
86 std::unique_ptr<Greeter::Stub> stub_;
87};
88
89int main(int argc, char** argv) {
Craig Tillerd6599a32015-09-03 09:37:02 -070090 // 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 Boeuf8c48a2a2015-10-17 22:23:02 -070093 // (use of InsecureChannelCredentials()).
94 GreeterClient greeter(grpc::CreateChannel(
95 "localhost:50051", grpc::InsecureChannelCredentials()));
Yang Gaod9569372015-02-19 14:22:52 -080096 std::string user("world");
97 std::string reply = greeter.SayHello(user);
98 std::cout << "Greeter received: " << reply << std::endl;
99
Yang Gao27bd6962015-05-08 11:42:08 -0700100 return 0;
Yang Gaod9569372015-02-19 14:22:52 -0800101}