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