blob: 3f279095a42b72d7530620fb744abc4f7d44c0df [file] [log] [blame]
Yang Gaob946b5e2015-03-27 13:20:59 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Yang Gaob946b5e2015-03-27 13:20:59 -07004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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 Gaob946b5e2015-03-27 13:20:59 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Yang Gaob946b5e2015-03-27 13:20:59 -070010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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 Gaob946b5e2015-03-27 13:20:59 -070016 *
17 */
18
19#ifndef GRPC_TEST_CPP_UTIL_CLI_CALL_H
20#define GRPC_TEST_CPP_UTIL_CLI_CALL_H
21
Yang Gao102eccb2015-06-16 00:43:25 -070022#include <map>
23
Vijay Paic90a8562018-03-08 21:20:24 -080024#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 Gaob946b5e2015-03-27 13:20:59 -070029
30namespace grpc {
Yuchen Zengf9329212016-09-09 14:27:12 -070031
32class ClientContext;
33
Yang Gaob946b5e2015-03-27 13:20:59 -070034namespace testing {
35
Yuchen Zeng8d2d70c2016-09-16 15:42:57 -070036// 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 Paic0b2acb2016-11-01 16:31:56 -070039class CliCall final {
Yang Gaob946b5e2015-03-27 13:20:59 -070040 public:
yang-ge21908f2015-08-25 13:47:51 -070041 typedef std::multimap<grpc::string, grpc::string> OutgoingMetadataContainer;
42 typedef std::multimap<grpc::string_ref, grpc::string_ref>
43 IncomingMetadataContainer;
Yuchen Zengf9329212016-09-09 14:27:12 -070044
Noah Eisen58e0cbf2018-06-07 22:17:14 -070045 CliCall(const std::shared_ptr<grpc::Channel>& channel,
46 const grpc::string& method,
Yuchen Zengf9329212016-09-09 14:27:12 -070047 const OutgoingMetadataContainer& metadata);
Yuchen Zeng8d2d70c2016-09-16 15:42:57 -070048 ~CliCall();
Yuchen Zengf9329212016-09-09 14:27:12 -070049
Yuchen Zeng8d2d70c2016-09-16 15:42:57 -070050 // Perform an unary generic RPC.
yang-g8c2be9f2015-08-19 16:28:09 -070051 static Status Call(std::shared_ptr<grpc::Channel> channel,
Yang Gao102eccb2015-06-16 00:43:25 -070052 const grpc::string& method, const grpc::string& request,
yang-ge21908f2015-08-25 13:47:51 -070053 grpc::string* response,
54 const OutgoingMetadataContainer& metadata,
55 IncomingMetadataContainer* server_initial_metadata,
56 IncomingMetadataContainer* server_trailing_metadata);
Yuchen Zengf9329212016-09-09 14:27:12 -070057
Yuchen Zeng8d2d70c2016-09-16 15:42:57 -070058 // Send a generic request message in a synchronous manner. NOT thread-safe.
Yuchen Zengf9329212016-09-09 14:27:12 -070059 void Write(const grpc::string& request);
60
Yuchen Zeng8d2d70c2016-09-16 15:42:57 -070061 // Send a generic request message in a synchronous manner. NOT thread-safe.
Yuchen Zengf9329212016-09-09 14:27:12 -070062 void WritesDone();
63
Yuchen Zeng8d2d70c2016-09-16 15:42:57 -070064 // Receive a generic response message in a synchronous manner.NOT thread-safe.
Yuchen Zengd37f6422016-09-09 20:05:37 -070065 bool Read(grpc::string* response,
Yuchen Zengf9329212016-09-09 14:27:12 -070066 IncomingMetadataContainer* server_initial_metadata);
67
Yuchen Zeng8d2d70c2016-09-16 15:42:57 -070068 // 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 Zengf9329212016-09-09 14:27:12 -070084 Status Finish(IncomingMetadataContainer* server_trailing_metadata);
85
86 private:
Yuchen Zengf9329212016-09-09 14:27:12 -070087 std::unique_ptr<grpc::GenericStub> stub_;
88 grpc::ClientContext ctx_;
89 std::unique_ptr<grpc::GenericClientAsyncReaderWriter> call_;
90 grpc::CompletionQueue cq_;
Yuchen Zeng8d2d70c2016-09-16 15:42:57 -070091 gpr_mu write_mu_;
92 gpr_cv write_cv_; // Protected by write_mu_;
93 bool write_done_; // Portected by write_mu_;
Yang Gaob946b5e2015-03-27 13:20:59 -070094};
95
96} // namespace testing
97} // namespace grpc
98
99#endif // GRPC_TEST_CPP_UTIL_CLI_CALL_H