24 #ifndef GOBY_MIDDLEWARE_TRANSPORT_DETAIL_INTERFACE_H
25 #define GOBY_MIDDLEWARE_TRANSPORT_DETAIL_INTERFACE_H
28 #include <condition_variable>
48 class NullTransporter;
56 template <
typename Transporter,
typename InnerTransporter,
typename Enable =
void>
65 static_assert(std::is_void<Enable>::value,
"InnerTransporterInterface must be specialized");
69 static_assert(std::is_void<Enable>::value,
"InnerTransporterInterface must be specialized");
74 template <
typename Transporter,
typename InnerTransporter>
76 Transporter, InnerTransporter,
77 typename std::
enable_if_t<!std::is_same<Transporter, NullTransporter>::value &&
78 !std::is_same<InnerTransporter, NullTransporter>::value>>
81 Transporter, InnerTransporter,
82 typename std::enable_if_t<!std::is_same<Transporter, NullTransporter>::value &&
83 !std::is_same<InnerTransporter, NullTransporter>::value>>;
87 InnerTransporter&
inner() {
return inner_; }
100 std::shared_ptr<InnerTransporter> own_inner_;
101 InnerTransporter& inner_;
105 template <
typename Transporter,
typename InnerTransporter>
107 Transporter, InnerTransporter,
108 typename std::
enable_if_t<!std::is_same<Transporter, NullTransporter>::value &&
109 std::is_same<InnerTransporter, NullTransporter>::value>>
115 InnerTransporter&
inner() {
return inner_; }
116 Transporter&
innermost() {
return *
static_cast<Transporter*
>(
this); }
125 std::shared_ptr<InnerTransporter> own_inner_;
126 InnerTransporter& inner_;
130 template <
typename Transporter,
typename InnerTransporter>
132 Transporter, InnerTransporter,
133 typename std::
enable_if_t<std::is_same<Transporter, NullTransporter>::value &&
134 std::is_same<InnerTransporter, NullTransporter>::value>>
146 template <
class Clock = std::chrono::system_clock,
class Duration =
typename Clock::duration>
147 int poll(
const std::chrono::time_point<Clock, Duration>& timeout =
148 std::chrono::time_point<Clock, Duration>::max());
154 template <
class Clock = std::chrono::system_clock,
class Duration =
typename Clock::duration>
155 int poll(Duration wait_for);
160 std::shared_ptr<std::timed_mutex>
poll_mutex() {
return poll_mutex_; }
166 std::shared_ptr<std::condition_variable_any>
cv() {
return cv_; }
170 std::shared_ptr<std::condition_variable_any>
cv)
176 template <
typename Transporter>
friend class Poller;
178 virtual int _transporter_poll(std::unique_ptr<std::unique_lock<std::timed_mutex>>&
lock) = 0;
182 template <
class Clock = std::chrono::system_clock,
class Duration =
typename Clock::duration>
183 int _poll_all(
const std::chrono::time_point<Clock, Duration>& timeout);
185 std::shared_ptr<std::timed_mutex> poll_mutex_;
187 std::shared_ptr<std::condition_variable_any> cv_;
202 template <
typename Transporter,
typename InnerTransporter>
214 int scheme = transporter_scheme<Data, Transporter>()>
217 static_cast<Transporter*
>(
this)->
template check_validity<group>();
218 static_cast<Transporter*
>(
this)->
template publish_dynamic<Data, scheme>(data,
group,
233 int scheme = transporter_scheme<Data, Transporter>()>
234 void publish(std::shared_ptr<const Data> data,
237 static_cast<Transporter*
>(
this)->
template check_validity<group>();
238 static_cast<Transporter*
>(
this)->
template publish_dynamic<Data, scheme>(data,
group,
253 int scheme = transporter_scheme<Data, Transporter>()>
256 publish<group, Data, scheme>(std::shared_ptr<const Data>(data), publisher);
268 int scheme = transporter_scheme<Data, Transporter>(),
273 static_cast<Transporter*
>(
this)->
template check_validity<group>();
274 static_cast<Transporter*
>(
this)->
template subscribe_dynamic<Data, scheme>(f,
group,
287 int scheme = transporter_scheme<Data, Transporter>(),
289 void subscribe(std::function<
void(std::shared_ptr<const Data>)> f,
292 static_cast<Transporter*
>(
this)->
template check_validity<group>();
293 static_cast<Transporter*
>(
this)->
template subscribe_dynamic<Data, scheme>(f,
group,
306 template <const Group& group, Necessity necessity = Necessity::OPTIONAL,
typename Func>
311 typename std::decay<detail::first_argument<Func>>
::type>
::type;
313 subscribe<group, Data, transporter_scheme<Data, Transporter>(), necessity>(f);
322 int scheme = transporter_scheme<Data, Transporter>()>
325 static_cast<Transporter*
>(
this)->
template check_validity<group>();
326 static_cast<Transporter*
>(
this)->
template unsubscribe_dynamic<Data, scheme>(
group,
344 template <
class Clock,
class Duration>
347 return _poll_all(timeout);
350 template <
class Clock,
class Duration>
353 if (wait_for == Duration::max())
356 return poll(Clock::now() + wait_for);
359 template <
class Clock,
class Duration>
360 int goby::middleware::PollerInterface::_poll_all(
361 const std::chrono::time_point<Clock, Duration>& timeout)
364 std::unique_ptr<std::unique_lock<std::timed_mutex>>
lock(
365 new std::unique_lock<std::timed_mutex>(*poll_mutex_));
368 int poll_items = _transporter_poll(
lock);
369 while (poll_items == 0)
373 "Poller lock was released by poll() but no poll items were returned"));
375 if (timeout == Clock::time_point::max())
378 poll_items = _transporter_poll(
lock);
389 if (cv_->wait_until(*
lock, timeout) == std::cv_status::no_timeout)
390 poll_items = _transporter_poll(
lock);