24#ifndef GOBY_MIDDLEWARE_TRANSPORT_DETAIL_INTERFACE_H
25#define GOBY_MIDDLEWARE_TRANSPORT_DETAIL_INTERFACE_H
28#include <condition_variable>
56template <
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");
74template <
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_;
105template <
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_;
130template <
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_;
202template <
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,
344template <
class Clock,
class Duration>
347 return _poll_all(timeout);
350template <
class Clock,
class Duration>
353 if (wait_for == Duration::max())
356 return poll(Clock::now() + wait_for);
359template <
class Clock,
class Duration>
360int 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);
simple exception class for goby applications
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
InnerTransporterInterface()
Generate a local instantiation of the inner transporter.
InnerTransporter & inner()
InnerTransporterInterface(InnerTransporter &inner)
Pass in an external inner transporter for use.
Transporter & innermost()
InnerTransporter InnerTransporterType
the InnerTransporter type (accessible for other uses)
Real transporter that has a real inner transporter.
InnerTransporter InnerTransporterType
the InnerTransporter type (accessible for other uses)
InnerTransporter & inner()
InnerTransporterInterface()
Generate a local instantiation of the inner transporter.
InnerTransporterInterface(InnerTransporter &inner)
Pass in an external inner transporter for use.
Recursive inner layer transporter storage or generator.
InnerTransporter InnerTransporterType
the InnerTransporter type (accessible for other uses)
InnerTransporter & inner()
Defines the common interface for polling for data on Goby transporters.
std::shared_ptr< std::condition_variable_any > cv()
access the condition variable used for poll synchronization
PollerInterface(std::shared_ptr< std::timed_mutex > poll_mutex, std::shared_ptr< std::condition_variable_any > cv)
int poll(const std::chrono::time_point< Clock, Duration > &timeout=std::chrono::time_point< Clock, Duration >::max())
poll for data. Blocks until a data event occurs or a timeout when a particular time has been reached
std::shared_ptr< std::timed_mutex > poll_mutex()
access the mutex used for poll synchronization
Utility class for allowing the various Goby middleware transporters to poll the underlying transport ...
Class that holds additional metadata and callback functions related to a publication (and is optional...
Defines the common interface for publishing and subscribing data using static (constexpr) groups on G...
void subscribe(Func f)
Simplified version of subscribe() that can deduce Data from the first argument of the function (lambd...
void subscribe(std::function< void(const Data &)> f, const Subscriber< Data > &subscriber=Subscriber< Data >())
Subscribe to a specific group and data type (const reference variant)
StaticTransporterInterface(InnerTransporter &inner)
void unsubscribe(const Subscriber< Data > &subscriber=Subscriber< Data >())
Unsubscribe to a specific group and data type.
void unsubscribe_all()
Unsubscribe to all messages that this transporter has subscribed to.
StaticTransporterInterface()
void publish(std::shared_ptr< const Data > data, const Publisher< Data > &publisher=Publisher< Data >())
Publish a message (shared pointer to const data variant)
void publish(std::shared_ptr< Data > data, const Publisher< Data > &publisher=Publisher< Data >())
Publish a message (shared pointer to mutable data variant)
void subscribe(std::function< void(std::shared_ptr< const Data >)> f, const Subscriber< Data > &subscriber=Subscriber< Data >())
Subscribe to a specific group and data type (shared pointer variant)
void publish(const Data &data, const Publisher< Data > &publisher=Publisher< Data >())
Publish a message (const reference variant)
Class that holds additional metadata and callback functions related to a subscription (and is optiona...
goby::util::logger::GroupSetter group(std::string n)
constexpr int scheme()
Placeholder to provide an interface for the scheme() function family.
Necessity
Used to tag subscriptions based on their necessity (e.g. required for correct functioning,...
The global namespace for the Goby project.