xref: /aosp_15_r20/external/grpc-grpc/examples/cpp/systemd_socket_activation/client.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 // Copyright 2022 the 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 <iostream>
16 #include <memory>
17 #include <string>
18 
19 #include "examples/protos/helloworld.grpc.pb.h"
20 
21 #include <grpcpp/grpcpp.h>
22 
23 using grpc::Channel;
24 using grpc::ClientContext;
25 using grpc::Status;
26 using helloworld::Greeter;
27 using helloworld::HelloReply;
28 using helloworld::HelloRequest;
29 
30 class GreeterClient {
31  public:
GreeterClient(std::shared_ptr<Channel> channel)32   GreeterClient(std::shared_ptr<Channel> channel)
33       : stub_(Greeter::NewStub(channel)) {}
34 
35   // Assembles the client's payload, sends it and presents the response back
36   // from the server.
SayHello(const std::string & user)37   std::string SayHello(const std::string& user) {
38     // Data we are sending to the server.
39     HelloRequest request;
40     request.set_name(user);
41 
42     // Container for the data we expect from the server.
43     HelloReply reply;
44 
45     // Context for the client. It could be used to convey extra information to
46     // the server and/or tweak certain RPC behaviors.
47     ClientContext context;
48 
49     // The actual RPC.
50     Status status = stub_->SayHello(&context, request, &reply);
51 
52     // Act upon its status.
53     if (status.ok()) {
54       return reply.message();
55     } else {
56       std::cout << status.error_code() << ": " << status.error_message()
57                 << std::endl;
58       return "RPC failed";
59     }
60   }
61 
62  private:
63   std::unique_ptr<Greeter::Stub> stub_;
64 };
65 
main(int argc,char ** argv)66 int main(int argc, char** argv) {
67   // Instantiate the client. It requires a channel, out of which the actual RPCs
68   // are created. This channel models a connection to an endpoint specified by
69   // the argument "--target=" which is the only expected argument.
70   // We indicate that the channel isn't authenticated (use of
71   // InsecureChannelCredentials()).
72   std::string target_str;
73   std::string arg_str("--target");
74   if (argc > 1) {
75     std::string arg_val = argv[1];
76     size_t start_pos = arg_val.find(arg_str);
77     if (start_pos != std::string::npos) {
78       start_pos += arg_str.size();
79       if (arg_val[start_pos] == '=') {
80         target_str = arg_val.substr(start_pos + 1);
81       } else {
82         std::cout << "The only correct argument syntax is --target="
83                   << std::endl;
84         return 0;
85       }
86     } else {
87       std::cout << "The only acceptable argument is --target=" << std::endl;
88       return 0;
89     }
90   } else {
91     target_str = "unix:/tmp/server";
92   }
93   GreeterClient greeter(
94       grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials()));
95   std::string user("world");
96   std::string reply(greeter.SayHello(user));
97   std::cout << "Greeter received: " << reply << std::endl;
98 
99   return 0;
100 }
101