25#ifndef GOBY_MIDDLEWARE_TRANSPORT_INTERPROCESS_H
26#define GOBY_MIDDLEWARE_TRANSPORT_INTERPROCESS_H
54template <
typename Derived,
typename InnerTransporter,
typename ImplementationTag>
57 InterProcessTransporterBase<Derived, InnerTransporter, ImplementationTag>,
59 public Poller<InterProcessTransporterBase<Derived, InnerTransporter, ImplementationTag>>
84 template <
typename Data>
static constexpr int scheme()
86 int scheme = goby::middleware::scheme<Data>();
100 template <typename Data, int scheme = InterProcessTransporterBase::scheme<Data>()>
105 static_cast<Derived*
>(
this)->
template _publish<Data, scheme>(data,
group, publisher);
106 this->
inner().template publish_dynamic<Data, scheme>(data,
group, publisher);
116 template <typename Data, int scheme = InterProcessTransporterBase::scheme<Data>()>
123 static_cast<Derived*
>(
this)->
template _publish<Data, scheme>(*data,
group, publisher);
124 this->
inner().template publish_dynamic<Data, scheme>(data,
group, publisher);
135 template <typename Data, int scheme = InterProcessTransporterBase::scheme<Data>()>
139 publish_dynamic<Data, scheme>(std::shared_ptr<const Data>(data),
group, publisher);
147 static_cast<Derived*
>(
this)->_publish_serialized(type_name,
scheme, bytes,
group);
158 template <typename Data, int scheme = InterProcessTransporterBase::scheme<Data>(),
164 static_cast<Derived*
>(
this)->
template _subscribe<Data, scheme>(
165 [=](std::shared_ptr<const Data> d) { f(*d); },
group, subscriber);
176 template <typename Data, int scheme = InterProcessTransporterBase::scheme<Data>(),
182 static_cast<Derived*
>(
this)->
template _subscribe<Data, scheme>(f,
group, subscriber);
190 template <typename Data, int scheme = InterProcessTransporterBase::scheme<Data>()>
195 static_cast<Derived*
>(
this)->
template _unsubscribe<Data, scheme>(
group, subscriber);
208 std::shared_ptr<SerializationSubscriptionRegex>
212 const std::set<int>& schemes,
const std::string& type_regex =
".*",
213 const std::string& group_regex =
".*")
215 return static_cast<Derived*
>(
this)->_subscribe_regex(f, schemes, type_regex, group_regex);
228 template <typename Data, int scheme = InterProcessTransporterBase::scheme<Data>(),
231 std::function<
void(std::shared_ptr<const Data>,
const std::string& type)> f,
232 const Group&
group,
const std::string& type_regex =
".*")
234 std::regex special_chars{R
"([-[\]{}()*+?.,\^$|#\s])"};
235 std::string sanitized_group =
236 std::regex_replace(std::string(group), special_chars, R"(\$&)");
238 auto regex_lambda = [f = std::move(f)](
const std::vector<unsigned char>& data,
int schm,
239 const std::string& type,
const Group& grp)
241 auto data_begin = data.begin(), data_end = data.end(), actual_end = data.end();
247 return static_cast<Derived*
>(
this)->_subscribe_regex(regex_lambda, {
scheme}, type_regex,
248 "^" + sanitized_group +
"$");
261 int scheme = InterProcessTransporterBase::scheme<Data>(),
264 std::function<
void(std::shared_ptr<const Data>,
const std::string& type)> f,
265 const std::string& type_regex =
".*")
275 static_assert((
group.c_str() !=
nullptr) && (
group.c_str()[0] !=
'\0'),
276 "goby::middleware::Group must have non-zero length string to publish on the "
277 "InterProcess layer");
283 if ((
group.c_str() ==
nullptr) || (
group.c_str()[0] ==
'\0'))
284 throw(
goby::Exception(
"Group must have a non-empty string for use on InterProcess"));
301 int _poll(std::unique_ptr<std::unique_lock<std::mutex>>& lock)
303 return static_cast<Derived*
>(
this)->_poll(lock);
312template <
typename InnerTransporter,
typename ImplementationTag =
void>
class InterProcessForwarder;
318template <
typename InnerTransporter,
typename ImplementationTag>
321 InnerTransporter, ImplementationTag>
326 InnerTransporter, ImplementationTag>;
338 std::shared_ptr<const goby::middleware::protobuf::SerializerTransporterMessage>
339 msg) { _receive_regex_data_forwarded(
msg); });
358 template <
typename Data,
int scheme>
363 std::string* sbytes =
new std::string(bytes.begin(), bytes.end());
364 auto msg = std::make_shared<goby::middleware::protobuf::SerializerTransporterMessage>();
365 auto* key =
msg->mutable_key();
367 key->set_marshalling_scheme(
scheme);
369 key->set_group(std::string(
group));
370 msg->set_allocated_data(sbytes);
372 *key->mutable_cfg() = publisher.
cfg();
374 this->
inner().template publish<Base::to_portal_group_>(
msg);
377 template <
typename Data,
int scheme>
378 void _subscribe(std::function<
void(std::shared_ptr<const Data> d)> f,
const Group&
group,
381 this->
inner().template subscribe_dynamic<Data, scheme>(f,
group);
387 auto inner_publication_lambda = [
this, alive,
group](std::shared_ptr<const Data> d)
390 this->
inner().template publish_dynamic<Data, scheme>(d,
group);
393 auto subscription = std::make_shared<SerializationSubscription<Data, scheme>>(
394 inner_publication_lambda,
group,
396 [=](
const Data& d) {
return group; }));
398 this->
inner().template publish<Base::to_portal_group_, SerializationHandlerBase<>>(
402 template <
typename Data,
int scheme>
403 void _unsubscribe(
const Group&
group,
const Subscriber<Data>& subscriber)
405 this->
inner().template unsubscribe_dynamic<Data, scheme>(
group, subscriber);
407 auto unsubscription = std::shared_ptr<SerializationHandlerBase<>>(
408 new SerializationUnSubscription<Data, scheme>(
group));
410 this->
inner().template publish<Base::to_portal_group_, SerializationHandlerBase<>>(
414 void _publish_serialized(std::string type_name,
int scheme,
const std::vector<char>& bytes,
417 auto msg = std::make_shared<goby::middleware::protobuf::SerializerTransporterMessage>();
418 auto* key =
msg->mutable_key();
420 key->set_marshalling_scheme(
scheme);
421 key->set_type(type_name);
422 key->set_group(std::string(
group));
423 msg->set_data(std::string(bytes.begin(), bytes.end()));
425 this->
inner().template publish<Base::to_portal_group_>(
msg);
428 void _unsubscribe_all()
430 regex_subscriptions_.clear();
431 auto all = std::make_shared<SerializationUnSubscribeAll>();
432 this->
inner().template publish<Base::to_portal_group_, SerializationUnSubscribeAll>(all);
435 std::shared_ptr<SerializationSubscriptionRegex>
436 _subscribe_regex(std::function<
void(
const std::vector<unsigned char>&,
int scheme,
437 const std::string& type,
const Group&
group)>
439 const std::set<int>& schemes,
const std::string& type_regex =
".*",
440 const std::string& group_regex =
".*")
443 auto inner_publication_lambda = [
this, alive](
const std::vector<unsigned char>& data,
449 std::shared_ptr<goby::middleware::protobuf::SerializerTransporterMessage>
451 forwarded_data->mutable_key()->set_marshalling_scheme(
scheme);
452 forwarded_data->mutable_key()->set_type(type);
453 forwarded_data->mutable_key()->set_group(
group);
454 forwarded_data->set_data(std::string(data.begin(), data.end()));
455 this->
inner().template publish<Base::regex_group_>(forwarded_data);
458 auto portal_subscription = std::make_shared<SerializationSubscriptionRegex>(
459 inner_publication_lambda, schemes, type_regex, group_regex);
460 this->
inner().template publish<Base::to_portal_group_, SerializationSubscriptionRegex>(
461 portal_subscription);
463 auto local_subscription = std::shared_ptr<SerializationSubscriptionRegex>(
464 new SerializationSubscriptionRegex(f, schemes, type_regex, group_regex));
465 regex_subscriptions_.insert(local_subscription);
466 return local_subscription;
469 void _receive_regex_data_forwarded(
470 std::shared_ptr<const goby::middleware::protobuf::SerializerTransporterMessage>
msg)
472 const auto& bytes =
msg->data();
473 for (
auto& sub : regex_subscriptions_)
474 sub->post(bytes.begin(), bytes.end(),
msg->key().marshalling_scheme(),
478 int _poll(std::unique_ptr<std::unique_lock<std::mutex>>& lock)
484 std::set<std::shared_ptr<const SerializationSubscriptionRegex>> regex_subscriptions_;
487 std::shared_ptr<std::atomic<bool>> alive_;
490template <
typename Derived,
typename InnerTransporter>
494 template <
typename Data,
int scheme>
504 std::function<
void(
const std::vector<unsigned char>&,
int scheme,
const std::string& type,
507 const std::set<int>& schemes,
const std::string& type_regex,
const std::string& group_regex)
509 auto new_sub = std::make_shared<middleware::SerializationSubscriptionRegex>(
510 f, schemes, type_regex, group_regex);
515 template <
typename Data,
int scheme>
516 void _subscribe(std::function<
void(std::shared_ptr<const Data> d)> f,
520 std::string identifier = this->
template _make_identifier<Data, scheme>(
523 auto subscription = std::make_shared<middleware::SerializationSubscription<Data, scheme>>(
526 [=](
const Data& ) {
return group; }));
528 if (forwarder_subscriptions_.count(identifier) == 0 &&
529 portal_subscriptions_.count(identifier) == 0)
530 static_cast<Derived*
>(
this)->_do_portal_subscribe(identifier);
532 portal_subscriptions_.insert(std::make_pair(identifier, subscription));
535 template <
typename Data,
int scheme>
540 std::string identifier = this->
template _make_identifier<Data, scheme>(
543 portal_subscriptions_.erase(identifier);
546 if (forwarder_subscriptions_.count(identifier) == 0)
547 static_cast<Derived*
>(
this)->_do_portal_unsubscribe(identifier);
550 const std::string& data)
555 std::string
group, type;
563 std::vector<std::weak_ptr<const middleware::SerializationHandlerBase<>>> subs_to_post;
564 auto portal_range = portal_subscriptions_.equal_range(identifier);
565 for (
auto it = portal_range.first; it != portal_range.second; ++it)
566 subs_to_post.push_back(it->second);
567 auto forwarder_it = forwarder_subscriptions_.find(identifier);
568 if (forwarder_it != forwarder_subscriptions_.end())
569 subs_to_post.push_back(forwarder_it->second);
573 auto null_delim_it = std::find(std::begin(data), std::end(data),
575 for (
auto& sub : subs_to_post)
577 if (
auto sub_sp = sub.lock())
578 sub_sp->post(null_delim_it + 1, data.end());
582 if (!regex_subscriptions_.empty())
584 auto null_delim_it = std::find(std::begin(data), std::end(data),
587 bool forwarder_subscription_posted =
false;
588 for (
auto& sub : regex_subscriptions_)
591 bool is_forwarded_sub =
593 if (is_forwarded_sub && forwarder_subscription_posted)
596 if (sub.second->post(null_delim_it + 1, data.end(),
scheme, type,
group) &&
598 forwarder_subscription_posted =
true;
609 for (
const auto& p : portal_subscriptions_)
611 const auto& identifier = p.first;
612 if (forwarder_subscriptions_.count(identifier) == 0)
613 static_cast<Derived*
>(
this)->_do_portal_unsubscribe(identifier);
615 portal_subscriptions_.clear();
619 while (forwarder_subscription_identifiers_[subscriber_id].size() > 0)
622 forwarder_subscription_identifiers_[subscriber_id].begin()->first);
626 if (regex_subscriptions_.size() > 0)
628 regex_subscriptions_.erase(subscriber_id);
629 if (regex_subscriptions_.empty())
630 static_cast<Derived*
>(
this)->_do_portal_wildcard_unsubscribe();
638 subscription->type_name(), subscription->scheme(), subscription->subscribed_group(),
642 goby::glog <<
"Received subscription forwarded for identifier [" << identifier
643 <<
"] from subscriber id: " << subscription->subscriber_id() << std::endl;
645 switch (subscription->action())
650 if (forwarder_subscription_identifiers_[subscription->subscriber_id()].count(
654 if (forwarder_subscriptions_.count(identifier) == 0)
657 if (portal_subscriptions_.count(identifier) == 0)
658 static_cast<Derived*
>(
this)->_do_portal_subscribe(identifier);
661 forwarder_subscriptions_.insert(std::make_pair(identifier, subscription));
663 forwarder_subscription_identifiers_[subscription->subscriber_id()].insert(
664 std::make_pair(identifier, forwarder_subscriptions_.find(identifier)));
681 auto it = forwarder_subscription_identifiers_[subscriber_id].find(identifier);
682 if (it != forwarder_subscription_identifiers_[subscriber_id].end())
684 bool no_forwarder_subscribers =
true;
685 for (
const auto& p : forwarder_subscription_identifiers_)
687 if (p.second.count(identifier) != 0)
689 no_forwarder_subscribers =
false;
695 if (no_forwarder_subscribers)
698 forwarder_subscriptions_.erase(it->second);
701 if (portal_subscriptions_.count(identifier) == 0)
702 static_cast<Derived*
>(
this)->_do_portal_unsubscribe(identifier);
705 forwarder_subscription_identifiers_[subscriber_id].erase(it);
710 const std::shared_ptr<const middleware::SerializationSubscriptionRegex>& new_sub)
712 if (regex_subscriptions_.empty())
713 static_cast<Derived*
>(
this)->_do_portal_wildcard_subscribe();
715 regex_subscriptions_.insert(std::make_pair(new_sub->subscriber_id(), new_sub));
721 std::string identifier =
724 static_cast<Derived*
>(
this)->_do_publish(identifier, bytes);
729 std::unordered_multimap<std::string,
730 std::shared_ptr<const middleware::SerializationHandlerBase<>>>
731 portal_subscriptions_;
733 std::unordered_map<std::string, std::shared_ptr<const middleware::SerializationHandlerBase<>>>
734 forwarder_subscriptions_;
738 std::string, std::unordered_map<
739 std::string,
typename decltype(forwarder_subscriptions_)::const_iterator>>
740 forwarder_subscription_identifiers_;
743 std::unordered_multimap<std::string,
744 std::shared_ptr<const middleware::SerializationSubscriptionRegex>>
745 regex_subscriptions_;
748template <
typename Derived,
typename InnerTransporter,
typename ImplementationTag>
768 this->
inner().template subscribe<Base::to_portal_group_, SerializerTransporterMessage>(
769 [
this](std::shared_ptr<const SerializerTransporterMessage> d)
771 std::vector<char> data(d->data().begin(), d->data().end());
773 d->key().type(), d->key().marshalling_scheme(), data,
777 this->
inner().template subscribe<Base::to_portal_group_, SerializationHandlerBase<>>(
778 [
this](std::shared_ptr<const middleware::SerializationHandlerBase<>> s)
781 this->
inner().template subscribe<Base::to_portal_group_, SerializationSubscriptionRegex>(
782 [
this](std::shared_ptr<const middleware::SerializationSubscriptionRegex> s)
785 this->
inner().template subscribe<Base::to_portal_group_, SerializationUnSubscribeAll>(
786 [
this](std::shared_ptr<const middleware::SerializationUnSubscribeAll> s)
806template <
typename InnerTransporter>
811 using Base = InterProcessForwarder<InnerTransporter, zeromq::detail::InterProcessTag>;
813 [[deprecated(
"Use zeromq::InterProcessForwarder<> or udpm::InterProcessForwarder<> instead of "
814 "middleware::InterProcessForwarder<>")]]
simple exception class for goby applications
Implementation of Group for dynamic (run-time) instantiations. Use Group directly for static (compile...
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
InnerTransporter & inner()
Implements the forwarder concept for the interprocess layer.
InterProcessForwarder(InnerTransporter &inner)
Construct a forwarder for the interprocess layer.
InterProcessTransporterBase< InterProcessForwarder< InnerTransporter, ImplementationTag >, InnerTransporter, ImplementationTag > Base
virtual ~InterProcessForwarder()
std::string _make_identifier(const goby::middleware::Group &group, IdentifierWildcard wildcard)
static std::tuple< std::string, int, std::string, int, std::size_t > parse_identifier(const std::string &identifier)
static const char end_delimiter
InterProcessPortalBase(InnerTransporter &inner)
virtual ~InterProcessPortalBase()
void _subscribe(std::function< void(std::shared_ptr< const Data > d)> f, const goby::middleware::Group &group, const middleware::Subscriber< Data > &)
void _handle_received_data(std::unique_ptr< std::unique_lock< std::mutex > > &lock, const std::string &data)
void _publish_serialized(std::string type_name, int scheme, const std::vector< char > &bytes, const goby::middleware::Group &group)
void _publish(const Data &d, const goby::middleware::Group &group, const middleware::Publisher< Data > &)
void _unsubscribe_all(const std::string &subscriber_id=middleware::identifier_part_to_string(std::this_thread::get_id()))
void _forwarder_unsubscribe(const std::string &subscriber_id, const std::string &identifier)
void _unsubscribe(const goby::middleware::Group &group, const middleware::Subscriber< Data > &=middleware::Subscriber< Data >())
std::shared_ptr< middleware::SerializationSubscriptionRegex > _subscribe_regex(std::function< void(const std::vector< unsigned char > &, int scheme, const std::string &type, const goby::middleware::Group &group)> f, const std::set< int > &schemes, const std::string &type_regex, const std::string &group_regex)
void _subscribe_regex_serialized(const std::shared_ptr< const middleware::SerializationSubscriptionRegex > &new_sub)
void _receive_subscription_forwarded(const std::shared_ptr< const middleware::SerializationHandlerBase<> > &subscription)
Base class for implementing transporters (both portal and forwarder) for the interprocess layer.
virtual ~InterProcessTransporterBase()
void check_validity()
Check validity of the Group for interthread use (at compile time)
static constexpr int scheme()
returns the marshalling scheme id for a given data type on this layer
static constexpr auto from_portal_group_name_
void subscribe_type_regex(std::function< void(std::shared_ptr< const Data >, const std::string &type)> f, const std::string &type_regex=".*")
Subscribe to a number of types within a given group and scheme using a regular expression.
void unsubscribe_dynamic(const Group &group, const Subscriber< Data > &subscriber=Subscriber< Data >())
Unsubscribe to a specific run-time defined group and data type. Where possible, prefer the static var...
static constexpr Group to_portal_group_
void check_validity_runtime(const Group &group)
Check validity of the Group for interthread use (for DynamicGroup at run time)
ImplementationTag implementation_tag
The ImplementationTag for this transporter (allows InterVehiclePortal to match the driver thread's fo...
static constexpr Group regex_group_
void publish_dynamic(const Data &data, const Group &group, const Publisher< Data > &publisher=Publisher< Data >())
Publish a message using a run-time defined DynamicGroup (const reference variant)....
static constexpr Group from_portal_group_
void publish_serialized(std::string type_name, int scheme, const std::vector< char > &bytes, const goby::middleware::Group &group)
Publish a message that has already been serialized for the given scheme.
InterProcessTransporterBase(InnerTransporter &inner)
std::shared_ptr< SerializationSubscriptionRegex > subscribe_type_regex(std::function< void(std::shared_ptr< const Data >, const std::string &type)> f, const Group &group, const std::string &type_regex=".*")
Subscribe to a number of types within a given group and scheme using a regular expression.
static constexpr auto to_portal_group_name_
void publish_dynamic(std::shared_ptr< Data > data, const Group &group, const Publisher< Data > &publisher=Publisher< Data >())
Publish a message using a run-time defined DynamicGroup (shared pointer to mutable data variant)....
InterProcessTransporterBase()
std::shared_ptr< SerializationSubscriptionRegex > subscribe_regex(std::function< void(const std::vector< unsigned char > &, int scheme, const std::string &type, const Group &group)> f, const std::set< int > &schemes, const std::string &type_regex=".*", const std::string &group_regex=".*")
Subscribe to multiple groups and/or types at once using regular expressions.
void subscribe_dynamic(std::function< void(const Data &)> f, const Group &group, const Subscriber< Data > &subscriber=Subscriber< Data >())
Subscribe to a specific run-time defined group and data type (const reference variant)....
void publish_dynamic(std::shared_ptr< const Data > data, const Group &group, const Publisher< Data > &publisher=Publisher< Data >())
Publish a message using a run-time defined DynamicGroup (shared pointer to const data variant)....
void unsubscribe_all()
Unsubscribe from all current subscriptions.
static constexpr auto regex_group_name_
void subscribe_dynamic(std::function< void(std::shared_ptr< const Data >)> f, const Group &group, const Subscriber< Data > &subscriber=Subscriber< Data >())
Subscribe to a specific run-time defined group and data type (shared pointer variant)....
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...
const goby::middleware::protobuf::TransporterConfig & cfg() const
Returns the metadata configuration.
Base class for handling posting callbacks for serialized data types (interprocess and outer)
Defines the common interface for publishing and subscribing data using static (constexpr) groups on G...
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)
Class that holds additional metadata and callback functions related to a subscription (and is optiona...
goby::util::logger::GroupSetter group(std::string n)
constexpr auto concat(const char(&a)[N1], const char(&b)[N2])
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,...
@ PROCESS_THREAD_WILDCARD
std::string identifier_part_to_string(int i)
middleware::InterProcessForwarder< InnerTransporter, detail::InterProcessTag > InterProcessForwarder
The global namespace for the Goby project.
extern ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< ::PROTOBUF_NAMESPACE_ID::MessageOptions, ::PROTOBUF_NAMESPACE_ID::internal::MessageTypeTraits< ::goby::GobyMessageOptions >, 11, false > msg
util::FlexOstream glog
Access the Goby logger through this object.
type
Categories for the various JSON types used in JWTs.
Class for parsing and serializing a given marshalling scheme. Must be specialized for a particular sc...
static std::string type_name()
The marshalling scheme specific string name for this type.
static std::shared_ptr< DataType > parse(CharIterator bytes_begin, CharIterator bytes_end, CharIterator &actual_end, const std::string &type=type_name())
Given a beginning and end iterator to bytes, parse the data and return it.