1 // Copyright 2023 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 "absl/flags/flag.h"
20 #include "absl/flags/parse.h"
21
22 #include <grpcpp/grpcpp.h>
23
24 #ifdef BAZEL_BUILD
25 #include "examples/protos/helloworld.grpc.pb.h"
26 #else
27 #include "helloworld.grpc.pb.h"
28 #endif
29
30 ABSL_FLAG(bool, crash_on_errors, false,
31 "Crash the application on client errors");
32 ABSL_FLAG(std::string, target, "localhost:50051", "Server address");
33
34 using grpc::Channel;
35 using grpc::ClientContext;
36 using grpc::Status;
37 using helloworld::Greeter;
38 using helloworld::HelloReply;
39 using helloworld::HelloRequest;
40
41 class GreeterClient {
42 public:
GreeterClient(std::shared_ptr<Channel> channel)43 GreeterClient(std::shared_ptr<Channel> channel)
44 : stub_(Greeter::NewStub(channel)) {}
45
46 // Assembles the client's payload, sends it and presents the response back
47 // from the server.
SayHello(const std::string & user)48 std::string SayHello(const std::string& user) {
49 // Data we are sending to the server.
50 HelloRequest request;
51 request.set_name(user);
52
53 // Container for the data we expect from the server.
54 HelloReply reply;
55
56 // Context for the client. It could be used to convey extra information to
57 // the server and/or tweak certain RPC behaviors.
58 ClientContext context;
59
60 // The actual RPC.
61 Status status = stub_->SayHello(&context, request, &reply);
62
63 // Act upon the status of the actual RPC.
64 if (absl::GetFlag(FLAGS_crash_on_errors)) {
65 assert(status.ok());
66 }
67 if (status.ok()) {
68 return reply.message();
69 } else {
70 return "RPC failed";
71 }
72 }
73
74 private:
75 std::unique_ptr<Greeter::Stub> stub_;
76 };
77
main(int argc,char ** argv)78 int main(int argc, char** argv) {
79 absl::ParseCommandLine(argc, argv);
80 // Instantiate the client. It requires a channel, out of which the actual RPCs
81 // are created. This channel models a connection to an endpoint specified by
82 // the argument "--target=" which is the only expected argument.
83 // We indicate that the channel isn't authenticated (use of
84 // InsecureChannelCredentials()).
85 GreeterClient greeter(grpc::CreateChannel(
86 absl::GetFlag(FLAGS_target), 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