xds_greeter_client.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2021 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 "absl/flags/flag.h"
24 #include "absl/flags/parse.h"
25 
26 #include <grpcpp/grpcpp.h>
27 
28 #ifdef BAZEL_BUILD
29 #include "examples/protos/helloworld.grpc.pb.h"
30 #else
31 #include "helloworld.grpc.pb.h"
32 #endif
33 
34 ABSL_FLAG(std::string, target, "xds:///helloworld:50051", "Target string");
35 ABSL_FLAG(bool, secure, true, "Secure mode");
36 
37 using grpc::Channel;
39 using grpc::Status;
43 
44 class GreeterClient {
45  public:
46  GreeterClient(std::shared_ptr<Channel> channel)
47  : stub_(Greeter::NewStub(channel)) {}
48 
49  // Assembles the client's payload, sends it and presents the response back
50  // from the server.
52  // Data we are sending to the server.
54  request.set_name(user);
55 
56  // Container for the data we expect from the server.
57  HelloReply reply;
58 
59  // Context for the client. It could be used to convey extra information to
60  // the server and/or tweak certain RPC behaviors.
62 
63  // The actual RPC.
64  Status status = stub_->SayHello(&context, request, &reply);
65 
66  // Act upon its status.
67  if (status.ok()) {
68  return reply.message();
69  } else {
70  std::cout << status.error_code() << ": " << status.error_message()
71  << std::endl;
72  return "RPC failed";
73  }
74  }
75 
76  private:
77  std::unique_ptr<Greeter::Stub> stub_;
78 };
79 
80 int main(int argc, char** argv) {
81  absl::ParseCommandLine(argc, argv);
83  absl::GetFlag(FLAGS_target), absl::GetFlag(FLAGS_secure)
87  std::string user("world");
88  std::string reply = greeter.SayHello(user);
89  std::cout << "Greeter received: " << reply << std::endl;
90 
91  return 0;
92 }
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
absl::ParseCommandLine
std::vector< char * > ParseCommandLine(int argc, char *argv[])
Definition: abseil-cpp/absl/flags/parse.cc:815
status
absl::Status status
Definition: rls.cc:251
grpc::experimental::XdsCredentials
std::shared_ptr< ChannelCredentials > XdsCredentials(const std::shared_ptr< ChannelCredentials > &fallback_creds)
Definition: cpp/client/xds_credentials.cc:48
ABSL_FLAG
ABSL_FLAG(std::string, target, "xds:///helloworld:50051", "Target string")
framework.rpc.grpc_channelz.Channel
Channel
Definition: grpc_channelz.py:32
channel
wrapped_grpc_channel * channel
Definition: src/php/ext/grpc/call.h:33
main
int main(int argc, char **argv)
Definition: xds_greeter_client.cc:80
grpcpp.h
GreeterClient
Definition: grpc-helloworld.cc:73
absl::GetFlag
ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag< T > &flag)
Definition: abseil-cpp/absl/flags/flag.h:98
GreeterClient::GreeterClient
GreeterClient(std::shared_ptr< Channel > channel)
Definition: xds_greeter_client.cc:46
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: xds_greeter_client.cc:51
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
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::InsecureChannelCredentials
std::shared_ptr< ChannelCredentials > InsecureChannelCredentials()
Credentials for an unencrypted, unauthenticated channel.
Definition: cpp/client/insecure_credentials.cc:69
setup.target
target
Definition: third_party/bloaty/third_party/protobuf/python/setup.py:179
GreeterClient::stub_
std::unique_ptr< Greeter::Stub > stub_
Definition: grpc-helloworld.cc:102


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:58