• 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 <grpcpp/grpcpp.h>
20 
21 #include <condition_variable>
22 #include <iostream>
23 #include <memory>
24 #include <mutex>
25 #include <string>
26 
27 #include "absl/flags/flag.h"
28 #include "absl/flags/parse.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, "xds:///helloworld:50051", "Target string");
37 ABSL_FLAG(bool, xds_creds, true, "Secure mode");
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)53   std::string SayHello(const std::string& user) {
54     // Data we are sending to the server.
55     HelloRequest request;
56     request.set_name(user);
57 
58     // Container for the data we expect from the server.
59     HelloReply reply;
60 
61     // Context for the client. It could be used to convey extra information to
62     // the server and/or tweak certain RPC behaviors.
63     ClientContext context;
64 
65     // The actual RPC.
66     std::mutex mu;
67     std::condition_variable cv;
68     bool done = false;
69     Status status;
70     stub_->async()->SayHello(&context, &request, &reply,
71                              [&mu, &cv, &done, &status](Status s) {
72                                status = std::move(s);
73                                std::lock_guard<std::mutex> lock(mu);
74                                done = true;
75                                cv.notify_one();
76                              });
77 
78     std::unique_lock<std::mutex> lock(mu);
79     while (!done) {
80       cv.wait(lock);
81     }
82 
83     // Act upon its status.
84     if (status.ok()) {
85       return reply.message();
86     } else {
87       std::cout << status.error_code() << ": " << status.error_message()
88                 << std::endl;
89       return "RPC failed";
90     }
91   }
92 
93  private:
94   std::unique_ptr<Greeter::Stub> stub_;
95 };
96 
main(int argc,char ** argv)97 int main(int argc, char** argv) {
98   absl::ParseCommandLine(argc, argv);
99   GreeterClient greeter(grpc::CreateChannel(
100       absl::GetFlag(FLAGS_target),
101       absl::GetFlag(FLAGS_xds_creds)
102           ? grpc::XdsCredentials(grpc::InsecureChannelCredentials())
103           : grpc::InsecureChannelCredentials()));
104   std::string user("world");
105   std::string reply = greeter.SayHello(user);
106   std::cout << "Greeter received: " << reply << std::endl;
107 
108   return 0;
109 }
110