xref: /aosp_15_r20/external/grpc-grpc/examples/cpp/deadline/client.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 // Copyright 2023 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <condition_variable>
16 #include <iostream>
17 #include <memory>
18 #include <mutex>
19 #include <string>
20 #include <vector>
21 
22 #include "absl/flags/flag.h"
23 #include "absl/flags/parse.h"
24 #include "absl/strings/str_cat.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, "localhost:50051", "Server address");
35 
36 using grpc::Channel;
37 using grpc::ClientContext;
38 using grpc::Status;
39 using grpc::StatusCode;
40 using helloworld::Greeter;
41 using helloworld::HelloReply;
42 using helloworld::HelloRequest;
43 
unaryCall(std::shared_ptr<Channel> channel,std::string label,std::string message,grpc::StatusCode expected_code)44 void unaryCall(std::shared_ptr<Channel> channel, std::string label,
45                std::string message, grpc::StatusCode expected_code) {
46   std::unique_ptr<Greeter::Stub> stub = Greeter::NewStub(channel);
47 
48   // Data we are sending to the server.
49   HelloRequest request;
50   request.set_name(message);
51 
52   // Container for the data we expect from the server.
53   HelloReply reply;
54 
55   // Context for the client. It could be used to convey extra information to
56   // the server and/or tweak certain RPC behaviors.
57   ClientContext context;
58 
59   // Set 1 second timeout
60   context.set_deadline(std::chrono::system_clock::now() +
61                        std::chrono::seconds(1));
62 
63   // The actual RPC.
64   std::mutex mu;
65   std::condition_variable cv;
66   bool done = false;
67   Status status;
68   stub->async()->SayHello(&context, &request, &reply,
69                           [&mu, &cv, &done, &status](Status s) {
70                             status = std::move(s);
71                             std::lock_guard<std::mutex> lock(mu);
72                             done = true;
73                             cv.notify_one();
74                           });
75 
76   std::unique_lock<std::mutex> lock(mu);
77   while (!done) {
78     cv.wait(lock);
79   }
80 
81   // Act upon its status.
82   std::cout << "[" << label << "] wanted = " << expected_code
83             << ", got = " << status.error_code() << std::endl;
84 }
85 
main(int argc,char ** argv)86 int main(int argc, char** argv) {
87   absl::ParseCommandLine(argc, argv);
88   // Instantiate the client. It requires a channel, out of which the actual RPCs
89   // are created. This channel models a connection to an endpoint specified by
90   // the argument "--target=" which is the only expected argument.
91   std::string target_str = absl::GetFlag(FLAGS_target);
92   // We indicate that the channel isn't authenticated (use of
93   // InsecureChannelCredentials()).
94   std::shared_ptr<Channel> channel =
95       grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials());
96   // Making test calls
97   unaryCall(channel, "Successful request", "world", grpc::StatusCode::OK);
98   unaryCall(channel, "Exceeds deadline", "delay",
99             grpc::StatusCode::DEADLINE_EXCEEDED);
100   unaryCall(channel, "Successful request with propagated deadline",
101             "[propagate me]world", grpc::StatusCode::OK);
102   unaryCall(channel, "Exceeds propagated deadline",
103             "[propagate me][propagate me]world",
104             grpc::StatusCode::DEADLINE_EXCEEDED);
105   return 0;
106 }
107