| 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 |
#include <thread> |
| 23 |
|
| 24 |
#include <grpc/support/log.h> |
| 25 |
#include <grpcpp/grpcpp.h> |
| 26 |
|
| 27 |
#ifdef BAZEL_BUILD |
| 28 |
#include "examples/protos/helloworld.grpc.pb.h" |
| 29 |
#else |
| 30 |
#include "helloworld.grpc.pb.h" |
| 31 |
#endif |
| 32 |
|
| 33 |
using grpc::Channel; |
| 34 |
using grpc::ClientAsyncResponseReader; |
| 35 |
using grpc::ClientContext; |
| 36 |
using grpc::CompletionQueue; |
| 37 |
using grpc::Status; |
| 38 |
using helloworld::Greeter; |
| 39 |
using helloworld::HelloReply; |
| 40 |
using helloworld::HelloRequest; |
| 41 |
|
| 42 |
class GreeterClient { |
| 43 |
public: |
| 44 |
explicit GreeterClient(std::shared_ptr<Channel> channel) |
| 45 |
: stub_(Greeter::NewStub(channel)) {} |
| 46 |
|
| 47 |
// Assembles the client's payload and sends it to the server. |
| 48 |
void SayHello(const std::string& user) { |
| 49 |
// Data we are sending to the server. |
| 50 |
HelloRequest request; |
| 51 |
request.set_name(user); |
| 52 |
|
| 53 |
// Call object to store rpc data |
| 54 |
AsyncClientCall* call = new AsyncClientCall; |
| 55 |
|
| 56 |
// stub_->PrepareAsyncSayHello() creates an RPC object, returning |
| 57 |
// an instance to store in "call" but does not actually start the RPC |
| 58 |
// Because we are using the asynchronous API, we need to hold on to |
| 59 |
// the "call" instance in order to get updates on the ongoing RPC. |
| 60 |
call->response_reader = |
| 61 |
stub_->PrepareAsyncSayHello(&call->context, request, &cq_); |
| 62 |
|
| 63 |
// StartCall initiates the RPC call |
| 64 |
call->response_reader->StartCall(); |
| 65 |
|
| 66 |
// Request that, upon completion of the RPC, "reply" be updated with the |
| 67 |
// server's response; "status" with the indication of whether the operation |
| 68 |
// was successful. Tag the request with the memory address of the call |
| 69 |
// object. |
| 70 |
call->response_reader->Finish(&call->reply, &call->status, (void*)call); |
| 71 |
} |
| 72 |
|
| 73 |
// Loop while listening for completed responses. |
| 74 |
// Prints out the response from the server. |
| 75 |
void AsyncCompleteRpc() { |
| 76 |
void* got_tag; |
| 77 |
bool ok = false; |
| 78 |
|
| 79 |
// Block until the next result is available in the completion queue "cq". |
| 80 |
while (cq_.Next(&got_tag, &ok)) { |
| 81 |
// The tag in this example is the memory location of the call object |
| 82 |
AsyncClientCall* call = static_cast<AsyncClientCall*>(got_tag); |
| 83 |
|
| 84 |
// Verify 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 |
if (call->status.ok()) |
| 89 |
std::cout << "Greeter received: " << call->reply.message() << std::endl; |
| 90 |
else |
| 91 |
std::cout << "RPC failed" << std::endl; |
| 92 |
|
| 93 |
// Once we're complete, deallocate the call object. |
| 94 |
delete call; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
private: |
| 99 |
// struct for keeping state and data information |
| 100 |
struct AsyncClientCall { |
| 101 |
// Container for the data we expect from the server. |
| 102 |
HelloReply reply; |
| 103 |
|
| 104 |
// Context for the client. It could be used to convey extra information to |
| 105 |
// the server and/or tweak certain RPC behaviors. |
| 106 |
ClientContext context; |
| 107 |
|
| 108 |
// Storage for the status of the RPC upon completion. |
| 109 |
Status status; |
| 110 |
|
| 111 |
std::unique_ptr<ClientAsyncResponseReader<HelloReply>> response_reader; |
| 112 |
}; |
| 113 |
|
| 114 |
// Out of the passed in Channel comes the stub, stored here, our view of the |
| 115 |
// server's exposed services. |
| 116 |
std::unique_ptr<Greeter::Stub> stub_; |
| 117 |
|
| 118 |
// The producer-consumer queue we use to communicate asynchronously with the |
| 119 |
// gRPC runtime. |
| 120 |
CompletionQueue cq_; |
| 121 |
}; |
| 122 |
|
| 123 |
int main(int argc, char** argv) { |
| 124 |
// Instantiate the client. It requires a channel, out of which the actual RPCs |
| 125 |
// are created. This channel models a connection to an endpoint (in this case, |
| 126 |
// localhost at port 50051). We indicate that the channel isn't authenticated |
| 127 |
// (use of InsecureChannelCredentials()). |
| 128 |
GreeterClient greeter(grpc::CreateChannel( |
| 129 |
"localhost:50051", grpc::InsecureChannelCredentials())); |
| 130 |
|
| 131 |
// Spawn reader thread that loops indefinitely |
| 132 |
std::thread thread_ = std::thread(&GreeterClient::AsyncCompleteRpc, &greeter); |
| 133 |
|
| 134 |
for (int i = 0; i < 100; i++) { |
| 135 |
std::string user("world " + std::to_string(i)); |
| 136 |
greeter.SayHello(user); // The actual RPC call! |
| 137 |
} |
| 138 |
|
| 139 |
std::cout << "Press control-c to quit" << std::endl << std::endl; |
| 140 |
thread_.join(); // blocks forever |
| 141 |
|
| 142 |
return 0; |
| 143 |
} |