1 /*
2 *
3 * Copyright 2015 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 "absl/flags/flag.h"
24 #include "absl/flags/parse.h"
25
26 #include <grpc/support/log.h>
27 #include <grpcpp/grpcpp.h>
28
29 #ifdef BAZEL_BUILD
30 #include "examples/protos/helloworld.grpc.pb.h"
31 #else
32 #include "helloworld.grpc.pb.h"
33 #endif
34
35 ABSL_FLAG(std::string, target, "localhost:50051", "Server address");
36
37 using grpc::Channel;
38 using grpc::ClientAsyncResponseReader;
39 using grpc::ClientContext;
40 using grpc::CompletionQueue;
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 explicit 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 producer-consumer queue we use to communicate asynchronously with the
66 // gRPC runtime.
67 CompletionQueue cq;
68
69 // Storage for the status of the RPC upon completion.
70 Status status;
71
72 std::unique_ptr<ClientAsyncResponseReader<HelloReply> > rpc(
73 stub_->AsyncSayHello(&context, request, &cq));
74
75 // Request that, upon completion of the RPC, "reply" be updated with the
76 // server's response; "status" with the indication of whether the operation
77 // was successful. Tag the request with the integer 1.
78 rpc->Finish(&reply, &status, (void*)1);
79 void* got_tag;
80 bool ok = false;
81 // Block until the next result is available in the completion queue "cq".
82 // The return value of Next should always be checked. This return value
83 // tells us whether there is any kind of event or the cq_ is shutting down.
84 GPR_ASSERT(cq.Next(&got_tag, &ok));
85
86 // Verify that the result from "cq" corresponds, by its tag, our previous
87 // request.
88 GPR_ASSERT(got_tag == (void*)1);
89 // ... and that the request was completed successfully. Note that "ok"
90 // corresponds solely to the request for updates introduced by Finish().
91 GPR_ASSERT(ok);
92
93 // Act upon the status of the actual RPC.
94 if (status.ok()) {
95 return reply.message();
96 } else {
97 return "RPC failed";
98 }
99 }
100
101 private:
102 // Out of the passed in Channel comes the stub, stored here, our view of the
103 // server's exposed services.
104 std::unique_ptr<Greeter::Stub> stub_;
105 };
106
main(int argc,char ** argv)107 int main(int argc, char** argv) {
108 absl::ParseCommandLine(argc, argv);
109 // Instantiate the client. It requires a channel, out of which the actual RPCs
110 // are created. This channel models a connection to an endpoint specified by
111 // the argument "--target=" which is the only expected argument.
112 std::string target_str = absl::GetFlag(FLAGS_target);
113 // We indicate that the channel isn't authenticated (use of
114 // InsecureChannelCredentials()).
115 GreeterClient greeter(
116 grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials()));
117 std::string user("world");
118 std::string reply = greeter.SayHello(user); // The actual RPC call!
119 std::cout << "Greeter received: " << reply << std::endl;
120
121 return 0;
122 }
123