GRPC C++  1.0.0
channel_interface.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2016, Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef GRPCXX_IMPL_CODEGEN_CHANNEL_INTERFACE_H
35 #define GRPCXX_IMPL_CODEGEN_CHANNEL_INTERFACE_H
36 
40 
41 namespace grpc {
42 class Call;
43 class ClientContext;
44 class RpcMethod;
45 class CallOpSetInterface;
46 class CompletionQueue;
47 
48 template <class R>
50 template <class W>
52 template <class W, class R>
54 template <class R>
55 class ClientAsyncReader;
56 template <class W>
57 class ClientAsyncWriter;
58 template <class W, class R>
60 template <class R>
62 
65  public:
66  virtual ~ChannelInterface() {}
69  virtual grpc_connectivity_state GetState(bool try_to_connect) = 0;
70 
73  template <typename T>
74  void NotifyOnStateChange(grpc_connectivity_state last_observed, T deadline,
75  CompletionQueue* cq, void* tag) {
76  TimePoint<T> deadline_tp(deadline);
77  NotifyOnStateChangeImpl(last_observed, deadline_tp.raw_time(), cq, tag);
78  }
79 
82  template <typename T>
83  bool WaitForStateChange(grpc_connectivity_state last_observed, T deadline) {
84  TimePoint<T> deadline_tp(deadline);
85  return WaitForStateChangeImpl(last_observed, deadline_tp.raw_time());
86  }
87 
89  template <typename T>
90  bool WaitForConnected(T deadline) {
92  while ((state = GetState(true)) != GRPC_CHANNEL_READY) {
93  if (!WaitForStateChange(state, deadline)) return false;
94  }
95  return true;
96  }
97 
98  private:
99  template <class R>
100  friend class ::grpc::ClientReader;
101  template <class W>
102  friend class ::grpc::ClientWriter;
103  template <class W, class R>
104  friend class ::grpc::ClientReaderWriter;
105  template <class R>
106  friend class ::grpc::ClientAsyncReader;
107  template <class W>
108  friend class ::grpc::ClientAsyncWriter;
109  template <class W, class R>
110  friend class ::grpc::ClientAsyncReaderWriter;
111  template <class R>
112  friend class ::grpc::ClientAsyncResponseReader;
113  template <class InputMessage, class OutputMessage>
114  friend Status BlockingUnaryCall(ChannelInterface* channel,
115  const RpcMethod& method,
116  ClientContext* context,
117  const InputMessage& request,
118  OutputMessage* result);
119  friend class ::grpc::RpcMethod;
120  virtual Call CreateCall(const RpcMethod& method, ClientContext* context,
121  CompletionQueue* cq) = 0;
122  virtual void PerformOpsOnCall(CallOpSetInterface* ops, Call* call) = 0;
123  virtual void* RegisterMethod(const char* method) = 0;
124  virtual void NotifyOnStateChangeImpl(grpc_connectivity_state last_observed,
125  gpr_timespec deadline,
126  CompletionQueue* cq, void* tag) = 0;
127  virtual bool WaitForStateChangeImpl(grpc_connectivity_state last_observed,
128  gpr_timespec deadline) = 0;
129 };
130 
131 } // namespace grpc
132 
133 #endif // GRPCXX_IMPL_CODEGEN_CHANNEL_INTERFACE_H
Definition: async_stream.h:249
Definition: channel_interface.h:49
An abstract collection of call ops, used to generate the grpc_call_op structure to pass down to the l...
Definition: call.h:563
void NotifyOnStateChange(grpc_connectivity_state last_observed, T deadline, CompletionQueue *cq, void *tag)
Return the tag on cq when the channel state is changed or deadline expires.
Definition: channel_interface.h:74
virtual grpc_connectivity_state GetState(bool try_to_connect)=0
Get the current channel state.
gpr_timespec raw_time()
Definition: time.h:57
Definition: time.h:54
Definition: async_unary_call.h:59
Definition: client_context.h:154
friend Status BlockingUnaryCall(ChannelInterface *channel, const RpcMethod &method, ClientContext *context, const InputMessage &request, OutputMessage *result)
Definition: client_unary_call.h:52
grpc_connectivity_state
Connectivity state of a channel.
Definition: connectivity_state.h:42
Definition: call.h:645
Definition: alarm.h:48
Codegen interface for grpc::Channel.
Definition: channel_interface.h:64
Definition: async_stream.h:177
channel is ready for work
Definition: connectivity_state.h:48
A thin wrapper around grpc_completion_queue (see / src/core/surface/completion_queue.h).
Definition: completion_queue.h:97
Definition: rpc_method.h:43
virtual ~ChannelInterface()
Definition: channel_interface.h:66
bool WaitForStateChange(grpc_connectivity_state last_observed, T deadline)
Blocking wait for channel state change or deadline expiration.
Definition: channel_interface.h:83
Did it work? If it didn&#39;t, why?
Definition: status.h:45
Definition: time.h:63
Definition: channel_interface.h:53
Definition: async_stream.h:111
bool WaitForConnected(T deadline)
Wait for this channel to be connected.
Definition: channel_interface.h:90
Definition: channel_interface.h:51