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 <grpcpp/grpcpp.h>
24 #include <grpc/support/log.h>
25
26 #ifdef BAZEL_BUILD
27 #include "examples/protos/helloworld.grpc.pb.h"
28 #else
29 #include "helloworld.grpc.pb.h"
30 #endif
31
32 using grpc::Channel;
33 using grpc::ClientAsyncResponseReader;
34 using grpc::ClientContext;
35 using grpc::CompletionQueue;
36 using grpc::Status;
37 using helloworld::HelloRequest;
38 using helloworld::HelloReply;
39 using helloworld::Greeter;
40
41 class GreeterClient {
42 public:
GreeterClient(std::shared_ptr<Channel> channel)43 explicit GreeterClient(std::shared_ptr<Channel> channel)
44 : stub_(Greeter::NewStub(channel)) {}
45
46 // Assembles the client's payload, sends it and presents the response back
47 // from the server.
SayHello(const std::string & user)48 std::string SayHello(const std::string& user) {
49 // Data we are sending to the server.
50 HelloRequest request;
51 request.set_name(user);
52
53 // Container for the data we expect from the server.
54 HelloReply reply;
55
56 // Context for the client. It could be used to convey extra information to
57 // the server and/or tweak certain RPC behaviors.
58 ClientContext context;
59
60 // The producer-consumer queue we use to communicate asynchronously with the
61 // gRPC runtime.
62 CompletionQueue cq;
63
64 // Storage for the status of the RPC upon completion.
65 Status status;
66
67 // stub_->PrepareAsyncSayHello() creates an RPC object, returning
68 // an instance to store in "call" but does not actually start the RPC
69 // Because we are using the asynchronous API, we need to hold on to
70 // the "call" instance in order to get updates on the ongoing RPC.
71 std::unique_ptr<ClientAsyncResponseReader<HelloReply> > rpc(
72 stub_->PrepareAsyncSayHello(&context, request, &cq));
73
74 // StartCall initiates the RPC call
75 rpc->StartCall();
76
77 // Request that, upon completion of the RPC, "reply" be updated with the
78 // server's response; "status" with the indication of whether the operation
79 // was successful. Tag the request with the integer 1.
80 rpc->Finish(&reply, &status, (void*)1);
81 void* got_tag;
82 bool ok = false;
83 // Block until the next result is available in the completion queue "cq".
84 // The return value of Next should always be checked. This return value
85 // tells us whether there is any kind of event or the cq_ is shutting down.
86 GPR_ASSERT(cq.Next(&got_tag, &ok));
87
88 // Verify that the result from "cq" corresponds, by its tag, our previous
89 // request.
90 GPR_ASSERT(got_tag == (void*)1);
91 // ... and that the request was completed successfully. Note that "ok"
92 // corresponds solely to the request for updates introduced by Finish().
93 GPR_ASSERT(ok);
94
95 // Act upon the status of the actual RPC.
96 if (status.ok()) {
97 return reply.message();
98 } else {
99 return "RPC failed";
100 }
101 }
102
103 private:
104 // Out of the passed in Channel comes the stub, stored here, our view of the
105 // server's exposed services.
106 std::unique_ptr<Greeter::Stub> stub_;
107 };
108
main(int argc,char ** argv)109 int main(int argc, char** argv) {
110 // Instantiate the client. It requires a channel, out of which the actual RPCs
111 // are created. This channel models a connection to an endpoint (in this case,
112 // localhost at port 50051). We indicate that the channel isn't authenticated
113 // (use of InsecureChannelCredentials()).
114 GreeterClient greeter(grpc::CreateChannel(
115 "localhost:50051", grpc::InsecureChannelCredentials()));
116 std::string user("world");
117 std::string reply = greeter.SayHello(user); // The actual RPC call!
118 std::cout << "Greeter received: " << reply << std::endl;
119
120 return 0;
121 }
122