• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2024 gRPC authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <grpcpp/grpcpp.h>
18 #include <grpcpp/support/status.h>
19 
20 #include <iostream>
21 #include <memory>
22 #include <string>
23 
24 #include "absl/strings/string_view.h"
25 
26 #ifdef BAZEL_BUILD
27 #include "examples/protos/helloworld.grpc.pb.h"
28 #else
29 #include "helloworld.grpc.pb.h"
30 #endif
31 
32 using grpc::Channel;
33 using grpc::ClientContext;
34 using grpc::Status;
35 using helloworld::Greeter;
36 using helloworld::HelloReply;
37 using helloworld::HelloRequest;
38 
39 constexpr absl::string_view kTargetAddress = "localhost:50052";
40 
41 // clang-format off
42 constexpr absl::string_view kRetryPolicy =
43     "{\"methodConfig\" : [{"
44     "   \"name\" : [{\"service\": \"helloworld.Greeter\"}],"
45     "   \"waitForReady\": true,"
46     "   \"retryPolicy\": {"
47     "     \"maxAttempts\": 4,"
48     "     \"initialBackoff\": \"1s\","
49     "     \"maxBackoff\": \"120s\","
50     "     \"backoffMultiplier\": 1.0,"
51     "     \"retryableStatusCodes\": [\"UNAVAILABLE\"]"
52     "    }"
53     "}]}";
54 // clang-format on
55 
56 class GreeterClient {
57  public:
GreeterClient(std::shared_ptr<Channel> channel)58   GreeterClient(std::shared_ptr<Channel> channel)
59       : stub_(Greeter::NewStub(channel)) {}
60 
61   // Assembles the client's payload, sends it and presents the response back
62   // from the server.
SayHello(const std::string & user)63   std::string SayHello(const std::string& user) {
64     // Data we are sending to the server.
65     HelloRequest request;
66     request.set_name(user);
67     // Container for the data we expect from the server.
68     HelloReply reply;
69     // Context for the client. It could be used to convey extra information to
70     // the server and/or tweak certain RPC behaviors.
71     ClientContext context;
72     // The actual RPC.
73     Status status = stub_->SayHello(&context, request, &reply);
74     // Act upon its status.
75     if (status.ok()) {
76       return reply.message();
77     } else {
78       std::cout << status.error_code() << ": " << status.error_message()
79                 << std::endl;
80       return "RPC failed";
81     }
82   }
83 
84  private:
85   std::unique_ptr<Greeter::Stub> stub_;
86 };
87 
main()88 int main() {
89   auto channel_args = grpc::ChannelArguments();
90   channel_args.SetServiceConfigJSON(std::string(kRetryPolicy));
91   GreeterClient greeter(grpc::CreateCustomChannel(
92       std::string(kTargetAddress), grpc::InsecureChannelCredentials(),
93       channel_args));
94   std::string user("world");
95   std::string reply = greeter.SayHello(user);
96   std::cout << "Greeter received: " << reply << std::endl;
97   return 0;
98 }
99