xref: /aosp_15_r20/external/grpc-grpc/examples/cpp/helloworld/xds_greeter_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 <iostream>
20 #include <memory>
21 #include <string>
22 
23 #include "absl/flags/flag.h"
24 #include "absl/flags/parse.h"
25 
26 #include <grpcpp/grpcpp.h>
27 
28 #ifdef BAZEL_BUILD
29 #include "examples/protos/helloworld.grpc.pb.h"
30 #else
31 #include "helloworld.grpc.pb.h"
32 #endif
33 
34 ABSL_FLAG(std::string, target, "xds:///helloworld:50051", "Target string");
35 ABSL_FLAG(bool, secure, true, "Secure mode");
36 
37 using grpc::Channel;
38 using grpc::ClientContext;
39 using grpc::Status;
40 using helloworld::Greeter;
41 using helloworld::HelloReply;
42 using helloworld::HelloRequest;
43 
44 class GreeterClient {
45  public:
GreeterClient(std::shared_ptr<Channel> channel)46   GreeterClient(std::shared_ptr<Channel> channel)
47       : stub_(Greeter::NewStub(channel)) {}
48 
49   // Assembles the client's payload, sends it and presents the response back
50   // from the server.
SayHello(const std::string & user)51   std::string SayHello(const std::string& user) {
52     // Data we are sending to the server.
53     HelloRequest request;
54     request.set_name(user);
55 
56     // Container for the data we expect from the server.
57     HelloReply reply;
58 
59     // Context for the client. It could be used to convey extra information to
60     // the server and/or tweak certain RPC behaviors.
61     ClientContext context;
62 
63     // The actual RPC.
64     Status status = stub_->SayHello(&context, request, &reply);
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 
76  private:
77   std::unique_ptr<Greeter::Stub> stub_;
78 };
79 
main(int argc,char ** argv)80 int main(int argc, char** argv) {
81   absl::ParseCommandLine(argc, argv);
82   GreeterClient greeter(grpc::CreateChannel(
83       absl::GetFlag(FLAGS_target),
84       absl::GetFlag(FLAGS_secure)
85           ? grpc::XdsCredentials(grpc::InsecureChannelCredentials())
86           : grpc::InsecureChannelCredentials()));
87   std::string user("world");
88   std::string reply = greeter.SayHello(user);
89   std::cout << "Greeter received: " << reply << std::endl;
90 
91   return 0;
92 }
93