greeter_client.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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::ClientContext;
  29. using grpc::Status;
  30. using helloworld::Greeter;
  31. using helloworld::HelloReply;
  32. using helloworld::HelloRequest;
  33. class GreeterClient {
  34. public:
  35. GreeterClient(std::shared_ptr<Channel> channel)
  36. : stub_(Greeter::NewStub(channel)) {}
  37. // Assembles the client's payload, sends it and presents the response back
  38. // from the server.
  39. std::string SayHello(const std::string& user) {
  40. // Data we are sending to the server.
  41. HelloRequest request;
  42. request.set_name(user);
  43. // Container for the data we expect from the server.
  44. HelloReply reply;
  45. // Context for the client. It could be used to convey extra information to
  46. // the server and/or tweak certain RPC behaviors.
  47. ClientContext context;
  48. // The actual RPC.
  49. Status status = stub_->SayHello(&context, request, &reply);
  50. // Act upon its status.
  51. if (status.ok()) {
  52. return reply.message();
  53. } else {
  54. std::cout << status.error_code() << ": " << status.error_message()
  55. << std::endl;
  56. return "RPC failed";
  57. }
  58. }
  59. private:
  60. std::unique_ptr<Greeter::Stub> stub_;
  61. };
  62. int main(int argc, char** argv) {
  63. // Instantiate the client. It requires a channel, out of which the actual RPCs
  64. // are created. This channel models a connection to an endpoint specified by
  65. // the argument "--target=" which is the only expected argument.
  66. // We indicate that the channel isn't authenticated (use of
  67. // InsecureChannelCredentials()).
  68. std::string target_str;
  69. std::string arg_str("--target");
  70. if (argc > 1) {
  71. std::string arg_val = argv[1];
  72. size_t start_pos = arg_val.find(arg_str);
  73. if (start_pos != std::string::npos) {
  74. start_pos += arg_str.size();
  75. if (arg_val[start_pos] == '=') {
  76. target_str = arg_val.substr(start_pos + 1);
  77. } else {
  78. std::cout << "The only correct argument syntax is --target="
  79. << std::endl;
  80. return 0;
  81. }
  82. } else {
  83. std::cout << "The only acceptable argument is --target=" << std::endl;
  84. return 0;
  85. }
  86. } else {
  87. target_str = "localhost:50051";
  88. }
  89. GreeterClient greeter(
  90. grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials()));
  91. std::string user("world");
  92. std::string reply = greeter.SayHello(user);
  93. std::cout << "Greeter received: " << reply << std::endl;
  94. return 0;
  95. }