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