greeter_async_client.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <grpc/support/log.h>
  22. #include <grpcpp/grpcpp.h>
  23. #ifdef BAZEL_BUILD
  24. #include "examples/protos/helloworld.grpc.pb.h"
  25. #else
  26. #include "helloworld.grpc.pb.h"
  27. #endif
  28. using grpc::Channel;
  29. using grpc::ClientAsyncResponseReader;
  30. using grpc::ClientContext;
  31. using grpc::CompletionQueue;
  32. using grpc::Status;
  33. using helloworld::Greeter;
  34. using helloworld::HelloReply;
  35. using helloworld::HelloRequest;
  36. class GreeterClient {
  37. public:
  38. explicit GreeterClient(std::shared_ptr<Channel> channel)
  39. : stub_(Greeter::NewStub(channel)) {}
  40. // Assembles the client's payload, sends it and presents the response back
  41. // from the server.
  42. std::string SayHello(const std::string& user) {
  43. // Data we are sending to the server.
  44. HelloRequest request;
  45. request.set_name(user);
  46. // Container for the data we expect from the server.
  47. HelloReply reply;
  48. // Context for the client. It could be used to convey extra information to
  49. // the server and/or tweak certain RPC behaviors.
  50. ClientContext context;
  51. // The producer-consumer queue we use to communicate asynchronously with the
  52. // gRPC runtime.
  53. CompletionQueue cq;
  54. // Storage for the status of the RPC upon completion.
  55. Status status;
  56. // stub_->PrepareAsyncSayHello() creates an RPC object, returning
  57. // an instance to store in "call" but does not actually start the RPC
  58. // Because we are using the asynchronous API, we need to hold on to
  59. // the "call" instance in order to get updates on the ongoing RPC.
  60. std::unique_ptr<ClientAsyncResponseReader<HelloReply> > rpc(
  61. stub_->PrepareAsyncSayHello(&context, request, &cq));
  62. // StartCall initiates the RPC call
  63. rpc->StartCall();
  64. // Request that, upon completion of the RPC, "reply" be updated with the
  65. // server's response; "status" with the indication of whether the operation
  66. // was successful. Tag the request with the integer 1.
  67. rpc->Finish(&reply, &status, (void*)1);
  68. void* got_tag;
  69. bool ok = false;
  70. // Block until the next result is available in the completion queue "cq".
  71. // The return value of Next should always be checked. This return value
  72. // tells us whether there is any kind of event or the cq_ is shutting down.
  73. GPR_ASSERT(cq.Next(&got_tag, &ok));
  74. // Verify that the result from "cq" corresponds, by its tag, our previous
  75. // request.
  76. GPR_ASSERT(got_tag == (void*)1);
  77. // ... and that the request was completed successfully. Note that "ok"
  78. // corresponds solely to the request for updates introduced by Finish().
  79. GPR_ASSERT(ok);
  80. // Act upon the status of the actual RPC.
  81. if (status.ok()) {
  82. return reply.message();
  83. } else {
  84. return "RPC failed";
  85. }
  86. }
  87. private:
  88. // Out of the passed in Channel comes the stub, stored here, our view of the
  89. // server's exposed services.
  90. std::unique_ptr<Greeter::Stub> stub_;
  91. };
  92. int main(int argc, char** argv) {
  93. // Instantiate the client. It requires a channel, out of which the actual RPCs
  94. // are created. This channel models a connection to an endpoint (in this case,
  95. // localhost at port 50051). We indicate that the channel isn't authenticated
  96. // (use of InsecureChannelCredentials()).
  97. GreeterClient greeter(grpc::CreateChannel(
  98. "localhost:50051", grpc::InsecureChannelCredentials()));
  99. std::string user("world");
  100. std::string reply = greeter.SayHello(user); // The actual RPC call!
  101. std::cout << "Greeter received: " << reply << std::endl;
  102. return 0;
  103. }