22 #ifndef __SSRC_WISP_PROTOCOL_CONTINUATION_CALLER_H
23 #define __SSRC_WISP_PROTOCOL_CONTINUATION_CALLER_H
29 #include <boost/multi_index/member.hpp>
30 #include <boost/multi_index/ordered_index.hpp>
45 virtual void resume(MessageInfo & msginfo) = 0;
52 template<
typename CallTraits_,
typename Closure>
64 continuation_function _continue;
69 continuation_function && continue_) :
71 _future(future), _result(),
72 _continue(
std::forward<continuation_function>(continue_))
77 virtual void resume(MessageInfo & msginfo) {
78 _future->unpack(_result, msginfo);
84 template<
typename CallTraits_>
86 std::function<void (const typename CallTraits_::return_type &)> >:
98 continuation_function _continue;
103 continuation_function && continue_) :
105 _future(future), _result(),
106 _continue(
std::forward<continuation_function>(continue_))
111 virtual void resume(MessageInfo & msginfo) {
112 _future->unpack(_result, msginfo);
117 template<
typename PT = BinaryPackingTraits>
119 typedef protocol::Caller<PT> super;
121 enum { ByHash, ByOrder };
123 typedef boost::multi_index_container<
125 boost::multi_index::indexed_by<
126 boost::multi_index::hashed_unique<
129 boost::multi_index::ordered_non_unique<
131 &Continuation::token> >
132 > > continuation_map;
135 typename continuation_map::template nth_index<ByHash>::type index_by_hash;
137 typename continuation_map::template nth_index<ByOrder>::type index_by_order;
139 continuation_map _continuations;
145 const std::string & name =
"",
146 const unsigned int message_capacity = Message::DefaultCapacity,
148 super(connection, name, message_capacity, group_membership),
153 return _continuations.size();
156 template<
typename CallTraits_,
typename Closure,
typename DestinationType>
159 const DestinationType & dest,
160 const typename CallTraits_::parameter_type & param,
162 SSRC_DECL_THROW(
boost::archive::archive_exception,
std::ios_base::failure)
164 return std::make_shared<FutureContinuation<CallTraits_, Closure>>(super::template call<CallTraits_>(dest, param, service), std::forward<Closure>(continue_));
167 void schedule(
const continuation_ptr & continuation) {
168 _continuations.insert(continuation);
171 template<
typename CallTraits_,
typename Closure,
typename DestinationType>
173 const DestinationType & dest,
174 const typename CallTraits_::parameter_type & param,
176 SSRC_DECL_THROW(
boost::archive::archive_exception,
std::ios_base::failure)
178 schedule(split_call<CallTraits_>(std::forward<Closure>(continue_),
179 dest, param, service));
183 void cancel_range(wisp_call_token min_token, wisp_call_token max_token) {
184 index_by_order & index = _continuations.template get<ByOrder>();
187 if(min_token < max_token)
188 index.erase(index.lower_bound(min_token), index.upper_bound(max_token));
189 else if(min_token > max_token) {
190 index.erase(index.lower_bound(min_token), index.end());
197 _continuations.clear();
200 continuation_ptr
cancel(wisp_call_token token) {
201 continuation_map::iterator it = _continuations.find(token);
203 if(it != _continuations.end()) {
204 continuation_ptr result = *it;
205 _continuations.erase(it);
213 continuation_ptr result =
cancel(msginfo.token());
216 result->resume(msginfo);
223 template<
typename CallTraits_,
typename Closure,
typename DestinationType>
225 const DestinationType & dest,
227 SSRC_DECL_THROW(
boost::archive::archive_exception,
std::ios_base::failure)
229 future_call<CallTraits_>(std::forward<Closure>(continue_),
230 dest,
typename CallTraits_::parameter_type(),
234 template<
typename CallTraits_,
typename Closure,
typename DestinationType,
237 Closure && continue_,
238 const DestinationType & dest, P && ...p)
239 SSRC_DECL_THROW(
boost::archive::archive_exception,
std::ios_base::failure)
241 future_call<CallTraits_>(std::forward<Closure>(continue_), dest,
242 typename CallTraits_::parameter_type(std::forward<P>(p)...),
246 template<
typename CallTraits_,
typename Closure,
typename DestinationType,
249 const DestinationType & dest, P && ...p)
250 SSRC_DECL_THROW(
boost::archive::archive_exception,
std::ios_base::failure)
252 future_call<CallTraits_>(std::forward<Closure>(continue_), dest,
253 typename CallTraits_::parameter_type(std::forward<P>(p)...));