| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 gRPC authors. |
| 4 |
* |
| 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 |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 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. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <iostream> |
| 20 |
#include <memory> |
| 21 |
#include <string> |
| 22 |
|
| 23 |
#include <grpcpp/grpcpp.h> |
| 24 |
|
| 25 |
#ifdef BAZEL_BUILD |
| 26 |
#include "examples/protos/helloworld.grpc.pb.h" |
| 27 |
#else |
| 28 |
#include "helloworld.grpc.pb.h" |
| 29 |
#endif |
| 30 |
|
| 31 |
using grpc::Channel; |
| 32 |
using grpc::ChannelArguments; |
| 33 |
using grpc::ClientContext; |
| 34 |
using grpc::Status; |
| 35 |
using helloworld::Greeter; |
| 36 |
using helloworld::HelloReply; |
| 37 |
using helloworld::HelloRequest; |
| 38 |
|
| 39 |
class GreeterClient { |
| 40 |
public: |
| 41 |
GreeterClient(std::shared_ptr<Channel> channel) |
| 42 |
: stub_(Greeter::NewStub(channel)) {} |
| 43 |
|
| 44 |
// Assembles the client's payload, sends it and presents the response back |
| 45 |
// from the server. |
| 46 |
std::string SayHello(const std::string& user) { |
| 47 |
// Data we are sending to the server. |
| 48 |
HelloRequest request; |
| 49 |
request.set_name(user); |
| 50 |
|
| 51 |
// Container for the data we expect from the server. |
| 52 |
HelloReply reply; |
| 53 |
|
| 54 |
// Context for the client. It could be used to convey extra information to |
| 55 |
// the server and/or tweak certain RPC behaviors. |
| 56 |
ClientContext context; |
| 57 |
|
| 58 |
// The actual RPC. |
| 59 |
Status status = stub_->SayHello(&context, request, &reply); |
| 60 |
|
| 61 |
// Act upon its status. |
| 62 |
if (status.ok()) { |
| 63 |
return reply.message(); |
| 64 |
} else { |
| 65 |
std::cout << status.error_code() << ": " << status.error_message() |
| 66 |
<< std::endl; |
| 67 |
return "RPC failed"; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
private: |
| 72 |
std::unique_ptr<Greeter::Stub> stub_; |
| 73 |
}; |
| 74 |
|
| 75 |
int main(int argc, char** argv) { |
| 76 |
// Instantiate the client. It requires a channel, out of which the actual RPCs |
| 77 |
// are created. This channel models a connection to an endpoint (in this case, |
| 78 |
// localhost at port 50051). We indicate that the channel isn't authenticated |
| 79 |
// (use of InsecureChannelCredentials()). |
| 80 |
ChannelArguments args; |
| 81 |
// Set the load balancing policy for the channel. |
| 82 |
args.SetLoadBalancingPolicyName("round_robin"); |
| 83 |
GreeterClient greeter(grpc::CreateCustomChannel( |
| 84 |
"localhost:50051", grpc::InsecureChannelCredentials(), args)); |
| 85 |
std::string user("world"); |
| 86 |
std::string reply = greeter.SayHello(user); |
| 87 |
std::cout << "Greeter received: " << reply << std::endl; |
| 88 |
|
| 89 |
return 0; |
| 90 |
} |