xds_greeter_client.cc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #include <iostream>
  19. #include <memory>
  20. #include <string>
  21. #include "absl/flags/flag.h"
  22. #include "absl/flags/parse.h"
  23. #include <grpcpp/grpcpp.h>
  24. #ifdef BAZEL_BUILD
  25. #include "examples/protos/helloworld.grpc.pb.h"
  26. #else
  27. #include "helloworld.grpc.pb.h"
  28. #endif
  29. ABSL_FLAG(std::string, target, "xds:///helloworld:50051", "Target string");
  30. ABSL_FLAG(bool, secure, true, "Secure mode");
  31. using grpc::Channel;
  32. using grpc::ClientContext;
  33. using grpc::Status;
  34. using helloworld::Greeter;
  35. using helloworld::HelloReply;
  36. using helloworld::HelloRequest;
  37. class GreeterClient {
  38. public:
  39. GreeterClient(std::shared_ptr<Channel> channel)
  40. : stub_(Greeter::NewStub(channel)) {}
  41. // Assembles the client's payload, sends it and presents the response back
  42. // from the server.
  43. std::string SayHello(const std::string& user) {
  44. // Data we are sending to the server.
  45. HelloRequest request;
  46. request.set_name(user);
  47. // Container for the data we expect from the server.
  48. HelloReply reply;
  49. // Context for the client. It could be used to convey extra information to
  50. // the server and/or tweak certain RPC behaviors.
  51. ClientContext context;
  52. // The actual RPC.
  53. Status status = stub_->SayHello(&context, request, &reply);
  54. // Act upon its status.
  55. if (status.ok()) {
  56. return reply.message();
  57. } else {
  58. std::cout << status.error_code() << ": " << status.error_message()
  59. << std::endl;
  60. return "RPC failed";
  61. }
  62. }
  63. private:
  64. std::unique_ptr<Greeter::Stub> stub_;
  65. };
  66. int main(int argc, char** argv) {
  67. absl::ParseCommandLine(argc, argv);
  68. GreeterClient greeter(grpc::CreateChannel(
  69. absl::GetFlag(FLAGS_target), absl::GetFlag(FLAGS_secure)
  70. ? grpc::experimental::XdsCredentials(
  71. grpc::InsecureChannelCredentials())
  72. : grpc::InsecureChannelCredentials()));
  73. std::string user("world");
  74. std::string reply = greeter.SayHello(user);
  75. std::cout << "Greeter received: " << reply << std::endl;
  76. return 0;
  77. }