greeter_callback_client.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. *
  3. * Copyright 2021 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 <condition_variable>
  19. #include <iostream>
  20. #include <memory>
  21. #include <mutex>
  22. #include <string>
  23. #include <grpcpp/grpcpp.h>
  24. #ifdef BAZEL_BUILD
  25. #include "examples/protos/helloworld.grpc.pb.h"
  26. #else
  27. #include "helloworld.grpc.pb.h"
  28. #endif
  29. using grpc::Channel;
  30. using grpc::ClientContext;
  31. using grpc::Status;
  32. using helloworld::Greeter;
  33. using helloworld::HelloReply;
  34. using helloworld::HelloRequest;
  35. class GreeterClient {
  36. public:
  37. GreeterClient(std::shared_ptr<Channel> channel)
  38. : stub_(Greeter::NewStub(channel)) {}
  39. // Assembles the client's payload, sends it and presents the response back
  40. // from the server.
  41. std::string SayHello(const std::string& user) {
  42. // Data we are sending to the server.
  43. HelloRequest request;
  44. request.set_name(user);
  45. // Container for the data we expect from the server.
  46. HelloReply reply;
  47. // Context for the client. It could be used to convey extra information to
  48. // the server and/or tweak certain RPC behaviors.
  49. ClientContext context;
  50. // The actual RPC.
  51. std::mutex mu;
  52. std::condition_variable cv;
  53. bool done = false;
  54. Status status;
  55. stub_->async()->SayHello(&context, &request, &reply,
  56. [&mu, &cv, &done, &status](Status s) {
  57. status = std::move(s);
  58. std::lock_guard<std::mutex> lock(mu);
  59. done = true;
  60. cv.notify_one();
  61. });
  62. std::unique_lock<std::mutex> lock(mu);
  63. while (!done) {
  64. cv.wait(lock);
  65. }
  66. // Act upon its status.
  67. if (status.ok()) {
  68. return reply.message();
  69. } else {
  70. std::cout << status.error_code() << ": " << status.error_message()
  71. << std::endl;
  72. return "RPC failed";
  73. }
  74. }
  75. private:
  76. std::unique_ptr<Greeter::Stub> stub_;
  77. };
  78. int main(int argc, char** argv) {
  79. // Instantiate the client. It requires a channel, out of which the actual RPCs
  80. // are created. This channel models a connection to an endpoint specified by
  81. // the argument "--target=" which is the only expected argument.
  82. // We indicate that the channel isn't authenticated (use of
  83. // InsecureChannelCredentials()).
  84. std::string target_str;
  85. std::string arg_str("--target");
  86. if (argc > 1) {
  87. std::string arg_val = argv[1];
  88. size_t start_pos = arg_val.find(arg_str);
  89. if (start_pos != std::string::npos) {
  90. start_pos += arg_str.size();
  91. if (arg_val[start_pos] == '=') {
  92. target_str = arg_val.substr(start_pos + 1);
  93. } else {
  94. std::cout << "The only correct argument syntax is --target="
  95. << std::endl;
  96. return 0;
  97. }
  98. } else {
  99. std::cout << "The only acceptable argument is --target=" << std::endl;
  100. return 0;
  101. }
  102. } else {
  103. target_str = "localhost:50051";
  104. }
  105. GreeterClient greeter(
  106. grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials()));
  107. std::string user("world");
  108. std::string reply = greeter.SayHello(user);
  109. std::cout << "Greeter received: " << reply << std::endl;
  110. return 0;
  111. }