xref: /aosp_15_r20/external/grpc-grpc/examples/cpp/multiplex/multiplex_client.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 #include <condition_variable>
19 #include <iostream>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 #include <vector>
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 #include "examples/protos/route_guide.grpc.pb.h"
33 #else
34 #include "helloworld.grpc.pb.h"
35 #include "route_guide.grpc.pb.h"
36 #endif
37 
38 ABSL_FLAG(std::string, target, "localhost:50051", "Server address");
39 
40 using grpc::Channel;
41 using grpc::ClientContext;
42 using grpc::Status;
43 
main(int argc,char ** argv)44 int main(int argc, char** argv) {
45   absl::ParseCommandLine(argc, argv);
46   // Instantiate the client. It requires a channel, out of which the actual RPCs
47   // are created. This channel models a connection to an endpoint specified by
48   // the argument "--target=" which is the only expected argument.
49   std::string target_str = absl::GetFlag(FLAGS_target);
50   // We indicate that the channel isn't authenticated (use of
51   // InsecureChannelCredentials()).
52   auto channel =
53       grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials());
54 
55   std::mutex mu;
56   std::condition_variable cv;
57   int done_count = 0;
58 
59   // Callbacks will be called on background threads
60   std::unique_lock<std::mutex> lock(mu);
61 
62   ClientContext hello_context;
63   helloworld::HelloRequest hello_request;
64   helloworld::HelloReply hello_response;
65   Status hello_status;
66 
67   ClientContext feature_context;
68   routeguide::Point feature_request;
69   routeguide::Feature feature_response;
70   Status feature_status;
71   // Request to a Greeter service
72   hello_request.set_name("user");
73   helloworld::Greeter::NewStub(channel)->async()->SayHello(
74       &hello_context, &hello_request, &hello_response, [&](Status status) {
75         std::lock_guard<std::mutex> lock(mu);
76         done_count++;
77         hello_status = std::move(status);
78         cv.notify_all();
79       });
80   // Request to a RouteGuide service
81   feature_request.set_latitude(50);
82   feature_request.set_longitude(100);
83   routeguide::RouteGuide::NewStub(channel)->async()->GetFeature(
84       &feature_context, &feature_request, &feature_response,
85       [&](Status status) {
86         std::lock_guard<std::mutex> lock(mu);
87         done_count++;
88         feature_status = std::move(status);
89         cv.notify_all();
90       });
91   // Wait for both requests to finish
92   cv.wait(lock, [&]() { return done_count == 2; });
93   if (hello_status.ok()) {
94     std::cout << "Greeter received: " << hello_response.message() << std::endl;
95   } else {
96     std::cerr << "Greeter failed: " << hello_status.error_message()
97               << std::endl;
98   }
99   if (feature_status.ok()) {
100     std::cout << "Found feature: " << feature_response.name() << std::endl;
101   } else {
102     std::cerr << "Getting feature failed: " << feature_status.error_message()
103               << std::endl;
104   }
105   return 0;
106 }
107