1 /*
2 *
3 * Copyright 2024 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <grpcpp/grpcpp.h>
20
21 #include <condition_variable>
22 #include <iostream>
23 #include <memory>
24 #include <mutex>
25 #include <string>
26
27 #include "absl/flags/flag.h"
28 #include "absl/flags/parse.h"
29 #include "helper.h"
30
31 #ifdef BAZEL_BUILD
32 #include "examples/protos/helloworld.grpc.pb.h"
33 #else
34 #include "helloworld.grpc.pb.h"
35 #endif
36
37 ABSL_FLAG(uint16_t, port, 50051, "Server port for the service");
38
39 using grpc::Channel;
40 using grpc::ClientContext;
41 using grpc::Status;
42 using helloworld::Greeter;
43 using helloworld::HelloReply;
44 using helloworld::HelloRequest;
45
46 class GreeterClient {
47 public:
GreeterClient(std::shared_ptr<Channel> channel)48 GreeterClient(std::shared_ptr<Channel> channel)
49 : stub_(Greeter::NewStub(channel)) {}
50
51 // Assembles the client's payload, sends it and presents the response back
52 // from the server.
SayHello(const std::string & user)53 std::string SayHello(const std::string& user) {
54 // Data we are sending to the server.
55 HelloRequest request;
56 request.set_name(user);
57
58 // Container for the data we expect from the server.
59 HelloReply reply;
60
61 // Context for the client. It could be used to convey extra information to
62 // the server and/or tweak certain RPC behaviors.
63 ClientContext context;
64
65 // The actual RPC.
66 std::mutex mu;
67 std::condition_variable cv;
68 bool done = false;
69 Status status;
70 stub_->async()->SayHello(&context, &request, &reply,
71 [&mu, &cv, &done, &status](Status s) {
72 status = std::move(s);
73 std::lock_guard<std::mutex> lock(mu);
74 done = true;
75 cv.notify_one();
76 });
77
78 std::unique_lock<std::mutex> lock(mu);
79 while (!done) {
80 cv.wait(lock);
81 }
82
83 // Act upon its status.
84 if (status.ok()) {
85 return reply.message();
86 } else {
87 std::cout << status.error_code() << ": " << status.error_message()
88 << std::endl;
89 return "RPC failed";
90 }
91 }
92
93 private:
94 std::unique_ptr<Greeter::Stub> stub_;
95 };
96
97 #ifdef BAZEL_BUILD
98 constexpr char kRootCertificate[] = "examples/cpp/auth/credentials/root.crt";
99 #else
100 constexpr char kRootCertificate[] = "credentials/root.crt";
101 #endif
102
main(int argc,char ** argv)103 int main(int argc, char** argv) {
104 absl::ParseCommandLine(argc, argv);
105 // Instantiate the client. It requires a channel, out of which the actual RPCs
106 // are created. This channel models a connection to an endpoint specified by
107 // the argument "--target=" which is the only expected argument.
108 std::string target_str =
109 absl::StrFormat("localhost:%d", absl::GetFlag(FLAGS_port));
110 // Build a SSL options for the channel
111 grpc::SslCredentialsOptions ssl_options;
112 ssl_options.pem_root_certs = LoadStringFromFile(kRootCertificate);
113 // Create a channel with SSL credentials
114 GreeterClient greeter(
115 grpc::CreateChannel(target_str, grpc::SslCredentials(ssl_options)));
116 std::string user("world");
117 std::string reply = greeter.SayHello(user);
118 std::cout << "Greeter received: " << reply << std::endl;
119
120 return 0;
121 }
122