• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2023 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 <condition_variable>
20 #include <iostream>
21 #include <memory>
22 #include <mutex>
23 #include <string>
24 #include <thread>
25 
26 #include "absl/flags/flag.h"
27 #include "absl/flags/parse.h"
28 
29 #include <grpcpp/grpcpp.h>
30 
31 #ifdef BAZEL_BUILD
32 #include "examples/protos/helloworld.grpc.pb.h"
33 #else
34 #include "helloworld.grpc.pb.h"
35 #endif
36 
37 ABSL_FLAG(std::string, target, "localhost:50051", "Server address");
38 
39 using grpc::Channel;
40 using grpc::ClientContext;
41 using grpc::Status;
42 using helloworld::Greeter;
43 using helloworld::HelloReply;
44 using helloworld::HelloRequest;
45 
46 class GreeterClient {
47  public:
GreeterClient(std::shared_ptr<Channel> channel)48   GreeterClient(std::shared_ptr<Channel> channel)
49       : stub_(Greeter::NewStub(channel)) {}
50 
51   // Assembles the client's payload, sends it and presents the response back
52   // from the server.
SayHello(const std::string & user,bool wait_for_ready)53   std::string SayHello(const std::string& user, bool wait_for_ready) {
54     HelloRequest request;
55     request.set_name(user);
56     HelloReply reply;
57     ClientContext context;
58     context.set_wait_for_ready(wait_for_ready);
59     std::mutex mu;
60     std::condition_variable cv;
61     bool done = false;
62     Status status;
63     stub_->async()->SayHello(&context, &request, &reply,
64                              [&mu, &cv, &done, &status](Status s) {
65                                status = std::move(s);
66                                std::lock_guard<std::mutex> lock(mu);
67                                done = true;
68                                cv.notify_one();
69                              });
70 
71     std::unique_lock<std::mutex> lock(mu);
72     while (!done) {
73       cv.wait(lock);
74     }
75     if (status.ok()) {
76       return reply.message();
77     } else {
78       std::cout << status.error_code() << ": " << status.error_message()
79                 << "\n";
80       return "RPC failed";
81     }
82   }
83 
84  private:
85   std::unique_ptr<Greeter::Stub> stub_;
86 };
87 
main(int argc,char ** argv)88 int main(int argc, char** argv) {
89   absl::ParseCommandLine(argc, argv);
90   std::string target_str = absl::GetFlag(FLAGS_target);
91   GreeterClient greeter(
92       grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials()));
93   std::string user("world");
94   // First send an RPC without wait_for_ready. If the server is not running,
95   // this RPC will fail immediately.
96   std::cout << "Greeter received: "
97             << greeter.SayHello(user, /*wait_for_ready=*/false) << "\n";
98   std::cout << "\nWe will now send RPCs with wait_for_ready set. If the "
99                "server is not running already, please start it now.\n";
100   // Now send RPC with wait_for_ready for set. Even if the server is not
101   // running, the RPC will still wait for the deadline to expire before
102   // failing.
103   std::cout << "Greeter received: "
104             << greeter.SayHello(user, /*wait_for_ready=*/true) << "\n";
105 
106   return 0;
107 }
108