greeter_async_client.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 
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;
36 using grpc::Status;
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.
49  // Data we are sending to the server.
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.
59 
60  // The producer-consumer queue we use to communicate asynchronously with the
61  // gRPC runtime.
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 
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()).
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 }
log.h
main
int main(int argc, char **argv)
Definition: greeter_async_client.cc:109
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
hellostreamingworld_pb2.HelloRequest
HelloRequest
Definition: hellostreamingworld_pb2.py:102
status
absl::Status status
Definition: rls.cc:251
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
grpcpp.h
GreeterClient
Definition: grpc-helloworld.cc:73
GreeterClient::GreeterClient
GreeterClient(std::shared_ptr< Channel > channel)
Definition: greeter_async_client.cc:43
helloworld.Greeter
Definition: helloworld.py:32
grpc::CreateChannel
std::shared_ptr< Channel > CreateChannel(const grpc::string &target, const std::shared_ptr< ChannelCredentials > &creds)
grpc::ClientContext
Definition: grpcpp/impl/codegen/client_context.h:195
hellostreamingworld_pb2.HelloReply
HelloReply
Definition: hellostreamingworld_pb2.py:109
GreeterClient::SayHello
std::string SayHello(const std::string &user)
Definition: greeter_async_client.cc:48
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
absl::Status::ok
ABSL_MUST_USE_RESULT bool ok() const
Definition: third_party/abseil-cpp/absl/status/status.h:802
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
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
cq
static grpc_completion_queue * cq
Definition: test/core/fling/client.cc:37
GreeterClient::stub_
std::unique_ptr< Greeter::Stub > stub_
Definition: grpc-helloworld.cc:102


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