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 <grpcpp/grpcpp.h>
16
17 #include <condition_variable>
18 #include <iostream>
19 #include <memory>
20 #include <mutex>
21 #include <string>
22
23 #include "absl/flags/flag.h"
24 #include "absl/flags/parse.h"
25 #include "absl/strings/str_format.h"
26
27 #ifdef BAZEL_BUILD
28 #include "examples/protos/helloworld.grpc.pb.h"
29 #else
30 #include "helloworld.grpc.pb.h"
31 #endif
32
33 ABSL_FLAG(std::string, target, "localhost:50051", "Server address");
34
35 using grpc::Channel;
36 using grpc::ClientContext;
37 using grpc::Status;
38 using helloworld::Greeter;
39 using helloworld::HelloReply;
40 using helloworld::HelloRequest;
41
42 class GreeterClient {
43 public:
GreeterClient(std::shared_ptr<Channel> channel)44 GreeterClient(std::shared_ptr<Channel> channel)
45 : stub_(Greeter::NewStub(channel)) {}
46
47 // Assembles the client's payload, sends it and prints the response back
48 // from the server.
SayHello(const std::string & user)49 void SayHello(const std::string& user) {
50 // Data we are sending to the server.
51 HelloRequest request;
52 request.set_name(user);
53 // Container for the data we expect from the server.
54 HelloReply reply;
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 // The actual RPC.
59 std::mutex mu;
60 std::condition_variable cv;
61 bool done = false;
62 Status status;
63 std::cout << absl::StrFormat("### Send: SayHello(name=%s)", user)
64 << std::endl;
65 stub_->async()->SayHello(&context, &request, &reply, [&](Status s) {
66 status = std::move(s);
67 std::lock_guard<std::mutex> lock(mu);
68 done = true;
69 cv.notify_one();
70 });
71 std::unique_lock<std::mutex> lock(mu);
72 while (!done) {
73 cv.wait(lock);
74 }
75 // Handles the reply
76 if (status.ok()) {
77 std::cout << absl::StrFormat("Ok. ReplyMessage=%s", reply.message())
78 << std::endl;
79 } else {
80 std::cout << absl::StrFormat("Failed. Code=%d Message=%s",
81 status.error_code(), status.error_message())
82 << std::endl;
83 }
84 }
85
86 private:
87 std::unique_ptr<Greeter::Stub> stub_;
88 };
89
main(int argc,char ** argv)90 int main(int argc, char** argv) {
91 absl::ParseCommandLine(argc, argv);
92 // Instantiate the client. It requires a channel, out of which the actual RPCs
93 // are created. This channel models a connection to an endpoint specified by
94 // the argument "--target=" which is the only expected argument.
95 std::string target_str = absl::GetFlag(FLAGS_target);
96 // We indicate that the channel isn't authenticated (use of
97 // InsecureChannelCredentials()).
98 GreeterClient greeter(
99 grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials()));
100 // Sends an empty name, expecting INVALID_ARGUMENT
101 greeter.SayHello("");
102 // Sends a too long name, expecting INVALID_ARGUMENT
103 greeter.SayHello("ItsTooLongName");
104 // Sends a first new name, expecting OK
105 greeter.SayHello("World");
106 return 0;
107 }
108