greeter_client.cc 2.6 KB

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