24 #ifndef GOBY_MIDDLEWARE_TRANSPORT_INTERMODULE_H
25 #define GOBY_MIDDLEWARE_TRANSPORT_INTERMODULE_H
29 #include <sys/types.h>
58 template <
typename Derived,
typename InnerTransporter>
65 template <
typename InnerTransporter>
82 template <
typename Data,
int scheme>
88 std::string* sbytes =
new std::string(bytes.begin(), bytes.end());
90 auto* key =
msg.mutable_key();
92 key->set_marshalling_scheme(
scheme);
94 key->set_group(std::string(
group));
95 msg.set_allocated_data(sbytes);
97 *key->mutable_cfg() = publisher.
cfg();
98 this->
inner().template publish<Base::to_portal_group_>(
msg);
101 template <
typename Data,
int scheme>
102 void _subscribe(std::function<
void(std::shared_ptr<const Data> d)> f,
const Group&
group,
105 if (subscriptions_.empty())
110 auto range = subscriptions_.equal_range(
msg.key());
111 for (
auto it = range.first; it != range.second; ++it)
112 { it->second->post(
msg.data().begin(),
msg.data().end()); }
115 auto local_subscription = std::make_shared<SerializationSubscription<Data, scheme>>(
118 [=](
const Data& d) {
return group; }));
121 Subscription subscription;
123 subscription.mutable_key()->set_marshalling_scheme(
scheme);
125 subscription.mutable_key()->set_group(std::string(
group));
126 subscription.set_action(Subscription::SUBSCRIBE);
128 this->
inner().template publish<Base::to_portal_group_>(subscription);
130 subscriptions_.insert(std::make_pair(subscription.key(), local_subscription));
133 template <
typename Data,
int scheme>
void _unsubscribe(
const Group&
group)
136 Subscription unsubscription;
138 unsubscription.mutable_key()->set_marshalling_scheme(
scheme);
140 unsubscription.mutable_key()->set_group(std::string(
group));
141 unsubscription.set_action(Subscription::UNSUBSCRIBE);
142 this->
inner().template publish<Base::to_portal_group_>(unsubscription);
144 subscriptions_.erase(unsubscription.key());
146 if (subscriptions_.empty())
149 protobuf::SerializerTransporterMessage>();
152 void _unsubscribe_all()
155 Subscription unsubscription;
157 unsubscription.set_action(Subscription::UNSUBSCRIBE_ALL);
158 this->
inner().template publish<Base::to_portal_group_>(unsubscription);
160 subscriptions_.clear();
163 protobuf::SerializerTransporterMessage>();
175 int _poll(std::unique_ptr<std::unique_lock<std::timed_mutex>>&
lock)
181 std::multimap<protobuf::SerializerTransporterKey,
182 std::shared_ptr<const middleware::SerializationHandlerBase<>>>
186 template <
typename Derived,
typename InnerTransporter>
202 this->
inner().template subscribe<Base::to_portal_group_, SerializerTransporterMessage>(
203 [
this](
const SerializerTransporterMessage& d) {
204 static_cast<Derived*
>(
this)->_receive_publication_forwarded(d);
207 this->
inner().template subscribe<Base::to_portal_group_, Subscription>(
208 [
this](
const Subscription& s) {
209 auto on_subscribe = [
this](
const SerializerTransporterMessage& d) {
210 this->
inner().template publish<Base::from_portal_group_>(d);
212 auto sub = std::make_shared<SerializationInterModuleSubscription>(on_subscribe, s);
216 case Subscription::SUBSCRIBE:
217 case Subscription::UNSUBSCRIBE:
218 static_cast<Derived*
>(
this)->_receive_subscription_forwarded(sub);
220 case Subscription::UNSUBSCRIBE_ALL:
221 static_cast<Derived*
>(
this)->_unsubscribe_all(s.id());