cli_call.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #ifndef GRPC_TEST_CPP_UTIL_CLI_CALL_H
  19. #define GRPC_TEST_CPP_UTIL_CLI_CALL_H
  20. #include <map>
  21. #include <grpcpp/channel.h>
  22. #include <grpcpp/completion_queue.h>
  23. #include <grpcpp/generic/generic_stub.h>
  24. #include <grpcpp/support/status.h>
  25. #include <grpcpp/support/string_ref.h>
  26. namespace grpc {
  27. class ClientContext;
  28. struct CliArgs {
  29. double timeout = -1;
  30. };
  31. namespace testing {
  32. // CliCall handles the sending and receiving of generic messages given the name
  33. // of the remote method. This class is only used by GrpcTool. Its thread-safe
  34. // and thread-unsafe methods should not be used together.
  35. class CliCall final {
  36. public:
  37. typedef std::multimap<std::string, std::string> OutgoingMetadataContainer;
  38. typedef std::multimap<grpc::string_ref, grpc::string_ref>
  39. IncomingMetadataContainer;
  40. CliCall(const std::shared_ptr<grpc::Channel>& channel,
  41. const std::string& method, const OutgoingMetadataContainer& metadata,
  42. CliArgs args);
  43. CliCall(const std::shared_ptr<grpc::Channel>& channel,
  44. const std::string& method, const OutgoingMetadataContainer& metadata)
  45. : CliCall(channel, method, metadata, CliArgs{}) {}
  46. ~CliCall();
  47. // Perform an unary generic RPC.
  48. Status Call(const std::string& request, std::string* response,
  49. IncomingMetadataContainer* server_initial_metadata,
  50. IncomingMetadataContainer* server_trailing_metadata);
  51. // Send a generic request message in a synchronous manner. NOT thread-safe.
  52. void Write(const std::string& request);
  53. // Send a generic request message in a synchronous manner. NOT thread-safe.
  54. void WritesDone();
  55. // Receive a generic response message in a synchronous manner.NOT thread-safe.
  56. bool Read(std::string* response,
  57. IncomingMetadataContainer* server_initial_metadata);
  58. // Thread-safe write. Must be used with ReadAndMaybeNotifyWrite. Send out a
  59. // generic request message and wait for ReadAndMaybeNotifyWrite to finish it.
  60. void WriteAndWait(const std::string& request);
  61. // Thread-safe WritesDone. Must be used with ReadAndMaybeNotifyWrite. Send out
  62. // WritesDone for gereneric request messages and wait for
  63. // ReadAndMaybeNotifyWrite to finish it.
  64. void WritesDoneAndWait();
  65. // Thread-safe Read. Blockingly receive a generic response message. Notify
  66. // writes if they are finished when this read is waiting for a resposne.
  67. bool ReadAndMaybeNotifyWrite(
  68. std::string* response,
  69. IncomingMetadataContainer* server_initial_metadata);
  70. // Finish the RPC.
  71. Status Finish(IncomingMetadataContainer* server_trailing_metadata);
  72. std::string peer() const { return ctx_.peer(); }
  73. private:
  74. std::unique_ptr<grpc::GenericStub> stub_;
  75. grpc::ClientContext ctx_;
  76. std::unique_ptr<grpc::GenericClientAsyncReaderWriter> call_;
  77. grpc::CompletionQueue cq_;
  78. gpr_mu write_mu_;
  79. gpr_cv write_cv_; // Protected by write_mu_;
  80. bool write_done_; // Portected by write_mu_;
  81. };
  82. } // namespace testing
  83. } // namespace grpc
  84. #endif // GRPC_TEST_CPP_UTIL_CLI_CALL_H