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