• 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 {
31 
32 class ClientContext;
33 
34 namespace testing {
35 
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.
39 class CliCall final {
40  public:
41   typedef std::multimap<grpc::string, grpc::string> OutgoingMetadataContainer;
42   typedef std::multimap<grpc::string_ref, grpc::string_ref>
43       IncomingMetadataContainer;
44 
45   CliCall(const std::shared_ptr<grpc::Channel>& channel,
46           const grpc::string& method,
47           const OutgoingMetadataContainer& metadata);
48   ~CliCall();
49 
50   // Perform an unary generic RPC.
51   static Status Call(std::shared_ptr<grpc::Channel> channel,
52                      const grpc::string& method, const grpc::string& request,
53                      grpc::string* response,
54                      const OutgoingMetadataContainer& metadata,
55                      IncomingMetadataContainer* server_initial_metadata,
56                      IncomingMetadataContainer* server_trailing_metadata);
57 
58   // Send a generic request message in a synchronous manner. NOT thread-safe.
59   void Write(const grpc::string& request);
60 
61   // Send a generic request message in a synchronous manner. NOT thread-safe.
62   void WritesDone();
63 
64   // Receive a generic response message in a synchronous manner.NOT thread-safe.
65   bool Read(grpc::string* response,
66             IncomingMetadataContainer* server_initial_metadata);
67 
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.
84   Status Finish(IncomingMetadataContainer* server_trailing_metadata);
85 
86  private:
87   std::unique_ptr<grpc::GenericStub> stub_;
88   grpc::ClientContext ctx_;
89   std::unique_ptr<grpc::GenericClientAsyncReaderWriter> call_;
90   grpc::CompletionQueue cq_;
91   gpr_mu write_mu_;
92   gpr_cv write_cv_;  // Protected by write_mu_;
93   bool write_done_;  // Portected by write_mu_;
94 };
95 
96 }  // namespace testing
97 }  // namespace grpc
98 
99 #endif  // GRPC_TEST_CPP_UTIL_CLI_CALL_H
100