blob: 555fd8d2cd57cdbbad1e093c77dadcb72e256cf0 [file] [log] [blame]
Yang Gaod9569372015-02-19 14:22:52 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Yang Gaod9569372015-02-19 14:22:52 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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 Gaod9569372015-02-19 14:22:52 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Yang Gaod9569372015-02-19 14:22:52 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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 Gaod9569372015-02-19 14:22:52 -080016 *
17 */
18
19#include <iostream>
20#include <memory>
21#include <string>
22
Craig Tillerd6599a32015-09-03 09:37:02 -070023#include <grpc++/grpc++.h>
24
Nicolas "Pixel" Noble24263c32017-01-11 01:03:09 +010025#ifdef BAZEL_BUILD
26#include "examples/protos/helloworld.grpc.pb.h"
27#else
Nicolas "Pixel" Noble9efc0832015-04-10 00:15:08 +020028#include "helloworld.grpc.pb.h"
Nicolas "Pixel" Noble24263c32017-01-11 01:03:09 +010029#endif
Yang Gaod9569372015-02-19 14:22:52 -080030
yang-gd36da042015-08-25 15:15:35 -070031using grpc::Channel;
Yang Gaod9569372015-02-19 14:22:52 -080032using grpc::ClientContext;
33using grpc::Status;
34using helloworld::HelloRequest;
35using helloworld::HelloReply;
36using helloworld::Greeter;
37
38class GreeterClient {
39 public:
yang-gd36da042015-08-25 15:15:35 -070040 GreeterClient(std::shared_ptr<Channel> channel)
Yang Gaod9569372015-02-19 14:22:52 -080041 : stub_(Greeter::NewStub(channel)) {}
42
Ryan3fec5252016-11-21 21:37:39 -050043 // Assembles the client's payload, sends it and presents the response back
Craig Tillerd6599a32015-09-03 09:37:02 -070044 // from the server.
Yang Gaod9569372015-02-19 14:22:52 -080045 std::string SayHello(const std::string& user) {
Craig Tillerd6599a32015-09-03 09:37:02 -070046 // Data we are sending to the server.
Yang Gaod9569372015-02-19 14:22:52 -080047 HelloRequest request;
48 request.set_name(user);
Craig Tillerd6599a32015-09-03 09:37:02 -070049
50 // Container for the data we expect from the server.
Yang Gaod9569372015-02-19 14:22:52 -080051 HelloReply reply;
Craig Tillerd6599a32015-09-03 09:37:02 -070052
53 // Context for the client. It could be used to convey extra information to
54 // the server and/or tweak certain RPC behaviors.
Yang Gaod9569372015-02-19 14:22:52 -080055 ClientContext context;
56
Craig Tillerd6599a32015-09-03 09:37:02 -070057 // The actual RPC.
Yang Gao600d70c2015-02-20 10:50:46 -080058 Status status = stub_->SayHello(&context, request, &reply);
Craig Tillerd6599a32015-09-03 09:37:02 -070059
60 // Act upon its status.
Yang Gaofd762762015-06-17 16:31:39 -070061 if (status.ok()) {
Yang Gaod9569372015-02-19 14:22:52 -080062 return reply.message();
63 } else {
Yuchen Zeng955a3642016-06-17 13:48:03 -070064 std::cout << status.error_code() << ": " << status.error_message()
65 << std::endl;
Craig Tillerd6599a32015-09-03 09:37:02 -070066 return "RPC failed";
Yang Gaod9569372015-02-19 14:22:52 -080067 }
68 }
69
Yang Gaod9569372015-02-19 14:22:52 -080070 private:
71 std::unique_ptr<Greeter::Stub> stub_;
72};
73
74int main(int argc, char** argv) {
Craig Tillerd6599a32015-09-03 09:37:02 -070075 // 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 Boeuf8c48a2a2015-10-17 22:23:02 -070078 // (use of InsecureChannelCredentials()).
79 GreeterClient greeter(grpc::CreateChannel(
80 "localhost:50051", grpc::InsecureChannelCredentials()));
Yang Gaod9569372015-02-19 14:22:52 -080081 std::string user("world");
82 std::string reply = greeter.SayHello(user);
83 std::cout << "Greeter received: " << reply << std::endl;
84
Yang Gao27bd6962015-05-08 11:42:08 -070085 return 0;
Yang Gaod9569372015-02-19 14:22:52 -080086}