Debian Codegraph @

test/cpp/util/cli_call.h

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