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