Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #ifndef GRPC_TEST_CPP_UTIL_CLI_CALL_H |
| 20 | #define GRPC_TEST_CPP_UTIL_CLI_CALL_H |
| 21 | |
Yang Gao | 102eccb6 | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 22 | #include <map> |
| 23 | |
Vijay Pai | c90a856 | 2018-03-08 21:20:24 -0800 | [diff] [blame] | 24 | #include <grpcpp/channel.h> |
| 25 | #include <grpcpp/completion_queue.h> |
| 26 | #include <grpcpp/generic/generic_stub.h> |
| 27 | #include <grpcpp/support/status.h> |
| 28 | #include <grpcpp/support/string_ref.h> |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 29 | |
| 30 | namespace grpc { |
Yuchen Zeng | f932921 | 2016-09-09 14:27:12 -0700 | [diff] [blame] | 31 | |
| 32 | class ClientContext; |
| 33 | |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 34 | namespace testing { |
| 35 | |
Yuchen Zeng | 8d2d70c | 2016-09-16 15:42:57 -0700 | [diff] [blame] | 36 | // CliCall handles the sending and receiving of generic messages given the name |
| 37 | // of the remote method. This class is only used by GrpcTool. Its thread-safe |
| 38 | // and thread-unsafe methods should not be used together. |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 39 | class CliCall final { |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 40 | public: |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 41 | typedef std::multimap<grpc::string, grpc::string> OutgoingMetadataContainer; |
| 42 | typedef std::multimap<grpc::string_ref, grpc::string_ref> |
| 43 | IncomingMetadataContainer; |
Yuchen Zeng | f932921 | 2016-09-09 14:27:12 -0700 | [diff] [blame] | 44 | |
Noah Eisen | 58e0cbf | 2018-06-07 22:17:14 -0700 | [diff] [blame] | 45 | CliCall(const std::shared_ptr<grpc::Channel>& channel, |
| 46 | const grpc::string& method, |
Yuchen Zeng | f932921 | 2016-09-09 14:27:12 -0700 | [diff] [blame] | 47 | const OutgoingMetadataContainer& metadata); |
Yuchen Zeng | 8d2d70c | 2016-09-16 15:42:57 -0700 | [diff] [blame] | 48 | ~CliCall(); |
Yuchen Zeng | f932921 | 2016-09-09 14:27:12 -0700 | [diff] [blame] | 49 | |
Yuchen Zeng | 8d2d70c | 2016-09-16 15:42:57 -0700 | [diff] [blame] | 50 | // Perform an unary generic RPC. |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 51 | static Status Call(std::shared_ptr<grpc::Channel> channel, |
Yang Gao | 102eccb6 | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 52 | const grpc::string& method, const grpc::string& request, |
yang-g | e21908f | 2015-08-25 13:47:51 -0700 | [diff] [blame] | 53 | grpc::string* response, |
| 54 | const OutgoingMetadataContainer& metadata, |
| 55 | IncomingMetadataContainer* server_initial_metadata, |
| 56 | IncomingMetadataContainer* server_trailing_metadata); |
Yuchen Zeng | f932921 | 2016-09-09 14:27:12 -0700 | [diff] [blame] | 57 | |
Yuchen Zeng | 8d2d70c | 2016-09-16 15:42:57 -0700 | [diff] [blame] | 58 | // Send a generic request message in a synchronous manner. NOT thread-safe. |
Yuchen Zeng | f932921 | 2016-09-09 14:27:12 -0700 | [diff] [blame] | 59 | void Write(const grpc::string& request); |
| 60 | |
Yuchen Zeng | 8d2d70c | 2016-09-16 15:42:57 -0700 | [diff] [blame] | 61 | // Send a generic request message in a synchronous manner. NOT thread-safe. |
Yuchen Zeng | f932921 | 2016-09-09 14:27:12 -0700 | [diff] [blame] | 62 | void WritesDone(); |
| 63 | |
Yuchen Zeng | 8d2d70c | 2016-09-16 15:42:57 -0700 | [diff] [blame] | 64 | // Receive a generic response message in a synchronous manner.NOT thread-safe. |
Yuchen Zeng | d37f642 | 2016-09-09 20:05:37 -0700 | [diff] [blame] | 65 | bool Read(grpc::string* response, |
Yuchen Zeng | f932921 | 2016-09-09 14:27:12 -0700 | [diff] [blame] | 66 | IncomingMetadataContainer* server_initial_metadata); |
| 67 | |
Yuchen Zeng | 8d2d70c | 2016-09-16 15:42:57 -0700 | [diff] [blame] | 68 | // Thread-safe write. Must be used with ReadAndMaybeNotifyWrite. Send out a |
| 69 | // generic request message and wait for ReadAndMaybeNotifyWrite to finish it. |
| 70 | void WriteAndWait(const grpc::string& request); |
| 71 | |
| 72 | // Thread-safe WritesDone. Must be used with ReadAndMaybeNotifyWrite. Send out |
| 73 | // WritesDone for gereneric request messages and wait for |
| 74 | // ReadAndMaybeNotifyWrite to finish it. |
| 75 | void WritesDoneAndWait(); |
| 76 | |
| 77 | // Thread-safe Read. Blockingly receive a generic response message. Notify |
| 78 | // writes if they are finished when this read is waiting for a resposne. |
| 79 | bool ReadAndMaybeNotifyWrite( |
| 80 | grpc::string* response, |
| 81 | IncomingMetadataContainer* server_initial_metadata); |
| 82 | |
| 83 | // Finish the RPC. |
Yuchen Zeng | f932921 | 2016-09-09 14:27:12 -0700 | [diff] [blame] | 84 | Status Finish(IncomingMetadataContainer* server_trailing_metadata); |
| 85 | |
| 86 | private: |
Yuchen Zeng | f932921 | 2016-09-09 14:27:12 -0700 | [diff] [blame] | 87 | std::unique_ptr<grpc::GenericStub> stub_; |
| 88 | grpc::ClientContext ctx_; |
| 89 | std::unique_ptr<grpc::GenericClientAsyncReaderWriter> call_; |
| 90 | grpc::CompletionQueue cq_; |
Yuchen Zeng | 8d2d70c | 2016-09-16 15:42:57 -0700 | [diff] [blame] | 91 | gpr_mu write_mu_; |
| 92 | gpr_cv write_cv_; // Protected by write_mu_; |
| 93 | bool write_done_; // Portected by write_mu_; |
Yang Gao | b946b5e | 2015-03-27 13:20:59 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | } // namespace testing |
| 97 | } // namespace grpc |
| 98 | |
| 99 | #endif // GRPC_TEST_CPP_UTIL_CLI_CALL_H |