24#ifndef GOBY_MIDDLEWARE_TRANSPORT_INTERMODULE_H
25#define GOBY_MIDDLEWARE_TRANSPORT_INTERMODULE_H
58template <
typename Derived,
typename InnerTransporter,
typename ImplementationTag>
67template <
typename InnerTransporter,
typename ImplementationTag =
void>
class InterModuleForwarder;
73template <
typename InnerTransporter,
typename ImplementationTag>
76 InnerTransporter, ImplementationTag>
81 InnerTransporter, ImplementationTag>;
92 template <
typename Data,
int scheme>
98 std::string* sbytes =
new std::string(bytes.begin(), bytes.end());
100 auto* key =
msg.mutable_key();
102 key->set_marshalling_scheme(
scheme);
104 key->set_group(std::string(
group));
105 msg.set_allocated_data(sbytes);
107 *key->mutable_cfg() = publisher.
cfg();
108 this->
inner().template publish<Base::to_portal_group_>(
msg);
111 template <
typename Data,
int scheme>
112 void _subscribe(std::function<
void(std::shared_ptr<const Data> d)> f,
const Group&
group,
115 if (subscriptions_.empty())
121 auto range = subscriptions_.equal_range(
msg.key());
122 for (
auto it = range.first; it != range.second; ++it)
124 it->second->post(
msg.data().begin(),
msg.data().end());
128 auto local_subscription = std::make_shared<SerializationSubscription<Data, scheme>>(
131 [=](
const Data& d) {
return group; }));
134 Subscription subscription;
136 subscription.mutable_key()->set_marshalling_scheme(
scheme);
138 subscription.mutable_key()->set_group(std::string(
group));
139 subscription.set_action(Subscription::SUBSCRIBE);
141 this->
inner().template publish<Base::to_portal_group_>(subscription);
143 subscriptions_.insert(std::make_pair(subscription.key(), local_subscription));
146 template <
typename Data,
int scheme>
void _unsubscribe(
const Group&
group)
149 Subscription unsubscription;
151 unsubscription.mutable_key()->set_marshalling_scheme(
scheme);
153 unsubscription.mutable_key()->set_group(std::string(
group));
154 unsubscription.set_action(Subscription::UNSUBSCRIBE);
155 this->
inner().template publish<Base::to_portal_group_>(unsubscription);
157 subscriptions_.erase(unsubscription.key());
159 if (subscriptions_.empty())
162 protobuf::SerializerTransporterMessage>();
165 void _unsubscribe_all()
168 Subscription unsubscription;
170 unsubscription.set_action(Subscription::UNSUBSCRIBE_ALL);
171 this->
inner().template publish<Base::to_portal_group_>(unsubscription);
173 subscriptions_.clear();
176 protobuf::SerializerTransporterMessage>();
188 int _poll(std::unique_ptr<std::unique_lock<std::mutex>>& lock)
194 std::multimap<protobuf::SerializerTransporterKey,
195 std::shared_ptr<const middleware::SerializationHandlerBase<>>>
199template <
typename Derived,
typename InnerTransporter,
typename ImplementationTag>
218 this->
inner().template subscribe<Base::to_portal_group_, SerializerTransporterMessage>(
219 [
this](
const SerializerTransporterMessage& d)
221 std::vector<char> data(d.data().begin(), d.data().end());
223 d.key().type(), d.key().marshalling_scheme(), data,
227 this->
inner().template subscribe<Base::to_portal_group_, Subscription>(
228 [
this](
const Subscription& s)
230 auto on_subscribe = [
this](
const SerializerTransporterMessage& d)
231 { this->
inner().template publish<Base::from_portal_group_>(d); };
232 auto sub = std::make_shared<SerializationInterModuleSubscription>(on_subscribe, s);
236 case Subscription::SUBSCRIBE:
237 case Subscription::UNSUBSCRIBE:
240 case Subscription::UNSUBSCRIBE_ALL:
262template <
typename InnerTransporter>
267 using Base = InterModuleForwarder<InnerTransporter, zeromq::detail::InterModuleTag>;
269 [[deprecated(
"Use zeromq::InterModuleForwarder<> or udpm::InterModuleForwarder<> instead of "
270 "middleware::InterModuleForwarder<>")]]
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 intermodule layer.
InterModuleTransporterBase< InterModuleForwarder< InnerTransporter, ImplementationTag >, InnerTransporter, ImplementationTag > Base
InterModuleForwarder(InnerTransporter &inner)
Construct a forwarder for the intermodule layer.
virtual ~InterModuleForwarder()
InterModulePortalBase(InnerTransporter &inner)
virtual ~InterModulePortalBase()
void _publish_serialized(std::string type_name, int scheme, const std::vector< char > &bytes, const goby::middleware::Group &group)
void _unsubscribe_all(const std::string &subscriber_id=middleware::identifier_part_to_string(std::this_thread::get_id()))
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.
static constexpr int scheme()
returns the marshalling scheme id for a given data type on this layer
static constexpr Group from_portal_group_
void unsubscribe_all()
Unsubscribe from all current subscriptions.
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.
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)
void unsubscribe(const Subscriber< Data > &subscriber=Subscriber< Data >())
Unsubscribe to a specific group and data type.
Class that holds additional metadata and callback functions related to a subscription (and is optiona...
void set_id(ArgT0 &&arg0, ArgT... args)
const std::string & type() const
int32_t marshalling_scheme() const
const std::string & group() const
goby::util::logger::GroupSetter group(std::string n)
bool operator<(const TCPEndPoint &ep_a, const TCPEndPoint &ep_b)
std::string full_process_and_thread_id(std::thread::id i=std::this_thread::get_id())
middleware::InterModuleForwarder< InnerTransporter, detail::InterModuleTag > InterModuleForwarder
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
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.