16 #ifndef HAZELCAST_CLIENT_INTERNAL_CLIENTDELEGATINGFUTURE_H 17 #define HAZELCAST_CLIENT_INTERNAL_CLIENTDELEGATINGFUTURE_H 20 #include <boost/enable_shared_from_this.hpp> 22 #include <hazelcast/client/spi/InternalCompletableFuture.h> 23 #include <hazelcast/client/spi/impl/ClientInvocationFuture.h> 24 #include <hazelcast/client/serialization/pimpl/SerializationService.h> 25 #include <hazelcast/client/impl/ClientMessageDecoder.h> 26 #include <hazelcast/client/spi/impl/ClientInvocation.h> 41 :
public spi::InternalCompletableFuture<V>,
42 public boost::enable_shared_from_this<ClientDelegatingFuture<V> > {
45 const boost::shared_ptr<spi::impl::ClientInvocationFuture> &clientInvocationFuture,
46 serialization::pimpl::SerializationService &serializationService,
47 const boost::shared_ptr<impl::ClientMessageDecoder<V> > &clientMessageDecoder,
48 const boost::shared_ptr<V> &defaultValue) : future(
49 clientInvocationFuture), serializationService(serializationService), clientMessageDecoder(
50 clientMessageDecoder), defaultValue(defaultValue), decodedResponse(
51 boost::static_pointer_cast<V>(VOIDOBJ)) {
52 const boost::shared_ptr<spi::impl::ClientInvocation> invocation = clientInvocationFuture->getInvocation();
53 if (invocation.get()) {
54 userExecutor = invocation->getUserExecutor();
59 const boost::shared_ptr<spi::impl::ClientInvocationFuture> &clientInvocationFuture,
60 serialization::pimpl::SerializationService &serializationService,
61 const boost::shared_ptr<impl::ClientMessageDecoder<V> > &clientMessageDecoder) : future(
62 clientInvocationFuture), serializationService(serializationService), clientMessageDecoder(
63 clientMessageDecoder), decodedResponse(boost::static_pointer_cast<V>(VOIDOBJ)) {
64 const boost::shared_ptr<spi::impl::ClientInvocation> invocation = clientInvocationFuture->getInvocation();
65 if (invocation.get()) {
66 userExecutor = invocation->getUserExecutor();
72 new DelegatingExecutionCallback(
74 callback)), userExecutor);
78 const boost::shared_ptr<Executor> &executor) {
80 new DelegatingExecutionCallback(
82 callback)), executor);
85 virtual bool cancel(
bool mayInterruptIfRunning) {
86 return future->cancel(mayInterruptIfRunning);
89 virtual bool isCancelled() {
90 return future->isCancelled();
93 virtual bool isDone() {
94 return future->isDone();
97 virtual boost::shared_ptr<V>
get() {
98 return get(INT64_MAX, hazelcast::util::concurrent::TimeUnit::MILLISECONDS());
101 virtual boost::shared_ptr<V>
get(int64_t timeout,
const TimeUnit &unit) {
102 boost::shared_ptr<protocol::ClientMessage> response = future->get(timeout, unit);
103 return resolveResponse(response);
106 virtual boost::shared_ptr<V> join() {
110 util::ExceptionUtil::rethrow(e);
112 return boost::shared_ptr<V>();
115 virtual bool complete(
const boost::shared_ptr<V> &value) {
120 virtual bool complete(
const boost::shared_ptr<exception::IException> &value) {
121 return future->complete(value);
125 const boost::shared_ptr<spi::impl::ClientInvocationFuture> &getFuture()
const {
130 class DelegatingExecutionCallback :
public ExecutionCallback<protocol::ClientMessage> {
132 DelegatingExecutionCallback(
135 delegatingFuture), callback(callback) {}
137 virtual void onResponse(
const boost::shared_ptr<protocol::ClientMessage> &message) {
138 boost::shared_ptr<V> response = delegatingFuture->resolveResponse(message);
139 callback->onResponse(response);
142 virtual void onFailure(
const boost::shared_ptr<exception::IException> &e) {
143 callback->onFailure(e);
147 boost::shared_ptr<ClientDelegatingFuture> delegatingFuture;
148 const boost::shared_ptr<ExecutionCallback<V> > callback;
152 decodeResponse(
const boost::shared_ptr<protocol::ClientMessage> &clientMessage) {
153 if (decodedResponse != boost::static_pointer_cast<V>(VOIDOBJ)) {
154 return decodedResponse;
157 boost::shared_ptr<V> newDecodedResponse = clientMessageDecoder->decodeClientMessage(clientMessage,
158 serializationService);
160 decodedResponse.compareAndSet(boost::static_pointer_cast<V>(VOIDOBJ), newDecodedResponse);
161 return newDecodedResponse;
166 resolveResponse(
const boost::shared_ptr<protocol::ClientMessage> &clientMessage) {
167 if (defaultValue.get() != NULL) {
171 return decodeResponse(clientMessage);
174 static const boost::shared_ptr<void> VOIDOBJ;
175 const boost::shared_ptr<spi::impl::ClientInvocationFuture> future;
176 serialization::pimpl::SerializationService &serializationService;
177 const boost::shared_ptr<impl::ClientMessageDecoder<V> > clientMessageDecoder;
178 const boost::shared_ptr<V> defaultValue;
179 boost::shared_ptr<hazelcast::util::Executor> userExecutor;
180 hazelcast::util::Atomic<boost::shared_ptr<V> > decodedResponse;
The Client Delegating Future is used to delegate ClientInvocationFuture to a user type to be used wit...
Definition: ClientDelegatingFuture.h:40
Base class for all exception originated from Hazelcast methods.
Definition: IException.h:53
ExecutionCallback allows to asynchronously get notified when the execution is completed, either successfully or with error.
Definition: ExecutionCallback.h:41
PN (Positive-Negative) CRDT counter.
Definition: MapEntryView.h:32