| 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 <grpc/support/log.h> |
| 24 |
#include <grpcpp/grpcpp.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::Greeter; |
| 38 |
using helloworld::HelloReply; |
| 39 |
using helloworld::HelloRequest; |
| 40 |
|
| 41 |
class GreeterClient { |
| 42 |
public: |
| 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. |
| 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 |
std::unique_ptr<ClientAsyncResponseReader<HelloReply> > rpc( |
| 68 |
stub_->AsyncSayHello(&context, request, &cq)); |
| 69 |
|
| 70 |
// Request that, upon completion of the RPC, "reply" be updated with the |
| 71 |
// server's response; "status" with the indication of whether the operation |
| 72 |
// was successful. Tag the request with the integer 1. |
| 73 |
rpc->Finish(&reply, &status, (void*)1); |
| 74 |
void* got_tag; |
| 75 |
bool ok = false; |
| 76 |
// Block until the next result is available in the completion queue "cq". |
| 77 |
// The return value of Next should always be checked. This return value |
| 78 |
// tells us whether there is any kind of event or the cq_ is shutting down. |
| 79 |
GPR_ASSERT(cq.Next(&got_tag, &ok)); |
| 80 |
|
| 81 |
// Verify that the result from "cq" corresponds, by its tag, our previous |
| 82 |
// request. |
| 83 |
GPR_ASSERT(got_tag == (void*)1); |
| 84 |
// ... and that the request was completed successfully. Note that "ok" |
| 85 |
// corresponds solely to the request for updates introduced by Finish(). |
| 86 |
GPR_ASSERT(ok); |
| 87 |
|
| 88 |
// Act upon the status of the actual RPC. |
| 89 |
if (status.ok()) { |
| 90 |
return reply.message(); |
| 91 |
} else { |
| 92 |
return "RPC failed"; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
private: |
| 97 |
// Out of the passed in Channel comes the stub, stored here, our view of the |
| 98 |
// server's exposed services. |
| 99 |
std::unique_ptr<Greeter::Stub> stub_; |
| 100 |
}; |
| 101 |
|
| 102 |
int main(int argc, char** argv) { |
| 103 |
// Instantiate the client. It requires a channel, out of which the actual RPCs |
| 104 |
// are created. This channel models a connection to an endpoint (in this case, |
| 105 |
// localhost at port 50051). We indicate that the channel isn't authenticated |
| 106 |
// (use of InsecureChannelCredentials()). |
| 107 |
GreeterClient greeter(grpc::CreateChannel( |
| 108 |
"localhost:50051", grpc::InsecureChannelCredentials())); |
| 109 |
std::string user("world"); |
| 110 |
std::string reply = greeter.SayHello(user); // The actual RPC call! |
| 111 |
std::cout << "Greeter received: " << reply << std::endl; |
| 112 |
|
| 113 |
return 0; |
| 114 |
} |