Debian Codegraph @

examples/cpp/helloworld/greeter_callback_client.cc

raw
1 /*
2 *
3 * Copyright 2021 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 <condition_variable>
20 #include <iostream>
21 #include <memory>
22 #include <mutex>
23 #include <string>
24
25 #include <grpcpp/grpcpp.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 using grpc::Channel;
34 using grpc::ClientContext;
35 using grpc::Status;
36 using helloworld::Greeter;
37 using helloworld::HelloReply;
38 using helloworld::HelloRequest;
39
40 class GreeterClient {
41 public:
42 GreeterClient(std::shared_ptr<Channel> channel)
43 : stub_(Greeter::NewStub(channel)) {}
44
45 // Assembles the client's payload, sends it and presents the response back
46 // from the server.
47 std::string SayHello(const std::string& user) {
48 // Data we are sending to the server.
49 HelloRequest request;
50 request.set_name(user);
51
52 // Container for the data we expect from the server.
53 HelloReply reply;
54
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
59 // The actual RPC.
60 std::mutex mu;
61 std::condition_variable cv;
62 bool done = false;
63 Status status;
64 stub_->async()->SayHello(&context, &request, &reply,
65 [&mu, &cv, &done, &status](Status s) {
66 status = std::move(s);
67 std::lock_guard<std::mutex> lock(mu);
68 done = true;
69 cv.notify_one();
70 });
71
72 std::unique_lock<std::mutex> lock(mu);
73 while (!done) {
74 cv.wait(lock);
75 }
76
77 // Act upon its status.
78 if (status.ok()) {
79 return reply.message();
80 } else {
81 std::cout << status.error_code() << ": " << status.error_message()
82 << std::endl;
83 return "RPC failed";
84 }
85 }
86
87 private:
88 std::unique_ptr<Greeter::Stub> stub_;
89 };
90
91 int main(int argc, char** 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 // We indicate that the channel isn't authenticated (use of
96 // InsecureChannelCredentials()).
97 std::string target_str;
98 std::string arg_str("--target");
99 if (argc > 1) {
100 std::string arg_val = argv[1];
101 size_t start_pos = arg_val.find(arg_str);
102 if (start_pos != std::string::npos) {
103 start_pos += arg_str.size();
104 if (arg_val[start_pos] == '=') {
105 target_str = arg_val.substr(start_pos + 1);
106 } else {
107 std::cout << "The only correct argument syntax is --target="
108 << std::endl;
109 return 0;
110 }
111 } else {
112 std::cout << "The only acceptable argument is --target=" << std::endl;
113 return 0;
114 }
115 } else {
116 target_str = "localhost:50051";
117 }
118 GreeterClient greeter(
119 grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials()));
120 std::string user("world");
121 std::string reply = greeter.SayHello(user);
122 std::cout << "Greeter received: " << reply << std::endl;
123
124 return 0;
125 }