greeter_async_client2.cc
Go to the documentation of this file.
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;
37 using grpc::Status;
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.
51  request.set_name(user);
52 
53  // Call object to store rpc data
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.
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
101  // Container for the data we expect from the server.
103 
104  // Context for the client. It could be used to convey extra information to
105  // the server and/or tweak certain RPC behaviors.
107 
108  // Storage for the status of the RPC upon completion.
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.
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()).
129  "localhost:50051", grpc::InsecureChannelCredentials()));
130 
131  // Spawn reader thread that loops indefinitely
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 }
GreeterClient::AsyncClientCall::context
ClientContext context
Definition: greeter_async_client2.cc:106
log.h
benchmark.request
request
Definition: benchmark.py:77
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
GreeterClient::AsyncClientCall::response_reader
std::unique_ptr< ClientAsyncResponseReader< HelloReply > > response_reader
Definition: greeter_async_client2.cc:111
GreeterClient::SayHello
void SayHello(const std::string &user)
Definition: greeter_async_client2.cc:48
hellostreamingworld_pb2.HelloRequest
HelloRequest
Definition: hellostreamingworld_pb2.py:102
GreeterClient::AsyncClientCall::status
Status status
Definition: greeter_async_client2.cc:109
GreeterClient::AsyncCompleteRpc
void AsyncCompleteRpc()
Definition: greeter_async_client2.cc:75
call
FilterStackCall * call
Definition: call.cc:750
framework.rpc.grpc_channelz.Channel
Channel
Definition: grpc_channelz.py:32
channel
wrapped_grpc_channel * channel
Definition: src/php/ext/grpc/call.h:33
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
GreeterClient::AsyncClientCall::reply
HelloReply reply
Definition: greeter_async_client2.cc:102
grpcpp.h
GreeterClient
Definition: grpc-helloworld.cc:73
GreeterClient::GreeterClient
GreeterClient(std::shared_ptr< Channel > channel)
Definition: greeter_async_client2.cc:44
helloworld.Greeter
Definition: helloworld.py:32
grpc::CreateChannel
std::shared_ptr< Channel > CreateChannel(const grpc::string &target, const std::shared_ptr< ChannelCredentials > &creds)
GreeterClient::AsyncClientCall
Definition: greeter_async_client2.cc:100
grpc::ClientContext
Definition: grpcpp/impl/codegen/client_context.h:195
hellostreamingworld_pb2.HelloReply
HelloReply
Definition: hellostreamingworld_pb2.py:109
grpc::CompletionQueue::Next
bool Next(void **tag, bool *ok)
Definition: include/grpcpp/impl/codegen/completion_queue.h:179
main
int main(int argc, char **argv)
Definition: greeter_async_client2.cc:123
GreeterClient::cq_
CompletionQueue cq_
Definition: greeter_async_client2.cc:120
GreeterClient::SayHello
std::string SayHello(const std::string &user)
Definition: grpc-helloworld.cc:80
grpc::ClientAsyncResponseReader
Definition: grpcpp/impl/codegen/async_unary_call.h:37
grpc::protobuf::util::Status
GRPC_CUSTOM_UTIL_STATUS Status
Definition: include/grpcpp/impl/codegen/config_protobuf.h:93
grpc::Status
Definition: include/grpcpp/impl/codegen/status.h:35
ok
bool ok
Definition: async_end2end_test.cc:197
grpc::CompletionQueue
Definition: include/grpcpp/impl/codegen/completion_queue.h:104
grpc::InsecureChannelCredentials
std::shared_ptr< ChannelCredentials > InsecureChannelCredentials()
Credentials for an unencrypted, unauthenticated channel.
Definition: cpp/client/insecure_credentials.cc:69
to_string
static bool to_string(zval *from)
Definition: protobuf/php/ext/google/protobuf/convert.c:333
thread
static uv_thread_t thread
Definition: test-async-null-cb.c:29
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
GreeterClient::stub_
std::unique_ptr< Greeter::Stub > stub_
Definition: grpc-helloworld.cc:102
thread_
std::unique_ptr< std::thread > thread_
Definition: settings_timeout_test.cc:104


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:47