diff --git a/include/grpc++/channel_interface.h b/include/grpc++/channel_interface.h index c5a5072298f5bed3e4a60c9a47f7795738f5b959..65161275c651801d649e4cd74e5f7b6b0fc065ce 100644 --- a/include/grpc++/channel_interface.h +++ b/include/grpc++/channel_interface.h @@ -65,16 +65,24 @@ class ChannelInterface : public CallHook, // Return the tag on cq when the channel state is changed or deadline expires. // GetState needs to called to get the current state. - template <typename T> virtual void NotifyOnStateChange(grpc_connectivity_state last_observed, - const T& deadline, + gpr_timespec deadline, CompletionQueue* cq, void* tag) = 0; // Blocking wait for channel state change or deadline expires. // GetState needs to called to get the current state. - template <typename T> virtual bool WaitForStateChange(grpc_connectivity_state last_observed, - const T& deadline) = 0; + gpr_timespec deadline) = 0; +#ifndef GRPC_CXX0X_NO_CHRONO + virtual void NotifyOnStateChange( + grpc_connectivity_state last_observed, + const std::chrono::system_clock::time_point& deadline, + CompletionQueue* cq, void* tag) = 0; + virtual bool WaitForStateChange( + grpc_connectivity_state last_observed, + const std::chrono::system_clock::time_point& deadline) = 0; +#endif // !GRPC_CXX0X_NO_CHRONO + }; } // namespace grpc diff --git a/src/cpp/client/channel.cc b/src/cpp/client/channel.cc index ee143d68a0f8d931b790866ac2ff68d5f49251ee..6696f19d76c2457aa492f075503a99031e0d76c6 100644 --- a/src/cpp/client/channel.cc +++ b/src/cpp/client/channel.cc @@ -48,6 +48,7 @@ #include <grpc++/impl/call.h> #include <grpc++/impl/rpc_method.h> #include <grpc++/status.h> +#include <grpc++/time.h> namespace grpc { @@ -93,4 +94,48 @@ void* Channel::RegisterMethod(const char* method) { host_.empty() ? NULL : host_.c_str()); } +grpc_connectivity_state Channel::GetState(bool try_to_connect) { + return grpc_channel_check_connectivity_state(c_channel_, try_to_connect); +} + +void Channel::NotifyOnStateChange(grpc_connectivity_state last_observed, + gpr_timespec deadline, + CompletionQueue* cq, void* tag) { + grpc_channel_watch_connectivity_state(c_channel_, last_observed, deadline, + cq->cq(), tag); +} + +bool Channel::WaitForStateChange(grpc_connectivity_state last_observed, + gpr_timespec deadline) { + CompletionQueue cq; + bool ok = false; + void* tag = NULL; + NotifyOnStateChange(last_observed, deadline, &cq, NULL); + cq.Next(&tag, &ok); + GPR_ASSERT(tag == NULL); + return ok; +} + +#ifndef GRPC_CXX0X_NO_CHRONO +void Channel::NotifyOnStateChange( + grpc_connectivity_state last_observed, + const std::chrono::system_clock::time_point& deadline, + CompletionQueue* cq, void* tag) { + TimePoint<std::chrono::system_clock::time_point> deadline_tp(deadline); + grpc_channel_watch_connectivity_state(c_channel_, last_observed, + deadline_tp.raw_time(), cq->cq(), tag); +} + +bool Channel::WaitForStateChange( + grpc_connectivity_state last_observed, + const std::chrono::system_clock::time_point& deadline) { + CompletionQueue cq; + bool ok = false; + void* tag = NULL; + NotifyOnStateChange(last_observed, deadline, &cq, NULL); + cq.Next(&tag, &ok); + GPR_ASSERT(tag == NULL); + return ok; +} +#endif // !GRPC_CXX0X_NO_CHRONO } // namespace grpc diff --git a/src/cpp/client/channel.h b/src/cpp/client/channel.h index 8660146856cc951e0c056d69b637bbb4b5058bc0..fa3aedc9eb7ec101211f8c9fa186969de49f15fb 100644 --- a/src/cpp/client/channel.h +++ b/src/cpp/client/channel.h @@ -56,12 +56,30 @@ class Channel GRPC_FINAL : public GrpcLibrary, public ChannelInterface { Channel(const grpc::string& host, grpc_channel* c_channel); ~Channel() GRPC_OVERRIDE; - virtual void* RegisterMethod(const char* method) GRPC_OVERRIDE; - virtual Call CreateCall(const RpcMethod& method, ClientContext* context, + void* RegisterMethod(const char* method) GRPC_OVERRIDE; + Call CreateCall(const RpcMethod& method, ClientContext* context, CompletionQueue* cq) GRPC_OVERRIDE; - virtual void PerformOpsOnCall(CallOpSetInterface* ops, + void PerformOpsOnCall(CallOpSetInterface* ops, Call* call) GRPC_OVERRIDE; + grpc_connectivity_state GetState(bool try_to_connect) GRPC_OVERRIDE; + + void NotifyOnStateChange(grpc_connectivity_state last_observed, + gpr_timespec deadline, + CompletionQueue* cq, void* tag) GRPC_OVERRIDE; + + bool WaitForStateChange(grpc_connectivity_state last_observed, + gpr_timespec deadline) GRPC_OVERRIDE; + +#ifndef GRPC_CXX0X_NO_CHRONO + void NotifyOnStateChange( + grpc_connectivity_state last_observed, + const std::chrono::system_clock::time_point& deadline, + CompletionQueue* cq, void* tag) GRPC_OVERRIDE; + bool WaitForStateChange( + grpc_connectivity_state last_observed, + const std::chrono::system_clock::time_point& deadline) GRPC_OVERRIDE; +#endif // !GRPC_CXX0X_NO_CHRONO private: const grpc::string host_; grpc_channel* const c_channel_; // owned