Goby3 3.7.0
2026.11.12
Loading...
Searching...
No Matches
interprocess.h
Go to the documentation of this file.
1// Copyright 2016-2026:
2// GobySoft, LLC (2013-)
3// Community contributors (see AUTHORS file)
4// File authors:
5// Toby Schneider <toby@gobysoft.org>
6// Ryan Govostes <rgovostes+git@gmail.com>
7//
8//
9// This file is part of the Goby Underwater Autonomy Project Libraries
10// ("The Goby Libraries").
11//
12// The Goby Libraries are free software: you can redistribute them and/or modify
13// them under the terms of the GNU Lesser General Public License as published by
14// the Free Software Foundation, either version 2.1 of the License, or
15// (at your option) any later version.
16//
17// The Goby Libraries are distributed in the hope that they will be useful,
18// but WITHOUT ANY WARRANTY; without even the implied warranty of
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20// GNU Lesser General Public License for more details.
21//
22// You should have received a copy of the GNU Lesser General Public License
23// along with Goby. If not, see <http://www.gnu.org/licenses/>.
24
25#ifndef GOBY_MIDDLEWARE_TRANSPORT_INTERPROCESS_H
26#define GOBY_MIDDLEWARE_TRANSPORT_INTERPROCESS_H
27
28#include <atomic>
29#include <functional>
30#include <sys/types.h>
31#include <thread>
32#include <tuple>
33#include <unistd.h>
34
36
43
44namespace goby
45{
46namespace middleware
47{
48
54template <typename Derived, typename InnerTransporter, typename ImplementationTag>
57 InterProcessTransporterBase<Derived, InnerTransporter, ImplementationTag>,
58 InnerTransporter>,
59 public Poller<InterProcessTransporterBase<Derived, InnerTransporter, ImplementationTag>>
60{
63 InnerTransporter>;
64
65 using PollerType =
67
68 public:
70 using implementation_tag = ImplementationTag;
71
74 {
75 }
77
79
84 template <typename Data> static constexpr int scheme()
85 {
86 int scheme = goby::middleware::scheme<Data>();
87 // if default returns DCCL, use PROTOBUF instead
90 return scheme;
91 }
92
100 template <typename Data, int scheme = InterProcessTransporterBase::scheme<Data>()>
101 void publish_dynamic(const Data& data, const Group& group,
102 const Publisher<Data>& publisher = Publisher<Data>())
103 {
105 static_cast<Derived*>(this)->template _publish<Data, scheme>(data, group, publisher);
106 this->inner().template publish_dynamic<Data, scheme>(data, group, publisher);
107 }
108
116 template <typename Data, int scheme = InterProcessTransporterBase::scheme<Data>()>
117 void publish_dynamic(std::shared_ptr<const Data> data, const Group& group,
118 const Publisher<Data>& publisher = Publisher<Data>())
119 {
120 if (data)
121 {
123 static_cast<Derived*>(this)->template _publish<Data, scheme>(*data, group, publisher);
124 this->inner().template publish_dynamic<Data, scheme>(data, group, publisher);
125 }
126 }
127
135 template <typename Data, int scheme = InterProcessTransporterBase::scheme<Data>()>
136 void publish_dynamic(std::shared_ptr<Data> data, const Group& group,
137 const Publisher<Data>& publisher = Publisher<Data>())
138 {
139 publish_dynamic<Data, scheme>(std::shared_ptr<const Data>(data), group, publisher);
140 }
141
143 void publish_serialized(std::string type_name, int scheme, const std::vector<char>& bytes,
145 {
147 static_cast<Derived*>(this)->_publish_serialized(type_name, scheme, bytes, group);
148 }
149
158 template <typename Data, int scheme = InterProcessTransporterBase::scheme<Data>(),
159 Necessity necessity = Necessity::OPTIONAL>
160 void subscribe_dynamic(std::function<void(const Data&)> f, const Group& group,
161 const Subscriber<Data>& subscriber = Subscriber<Data>())
162 {
164 static_cast<Derived*>(this)->template _subscribe<Data, scheme>(
165 [=](std::shared_ptr<const Data> d) { f(*d); }, group, subscriber);
166 }
167
176 template <typename Data, int scheme = InterProcessTransporterBase::scheme<Data>(),
177 Necessity necessity = Necessity::OPTIONAL>
178 void subscribe_dynamic(std::function<void(std::shared_ptr<const Data>)> f, const Group& group,
179 const Subscriber<Data>& subscriber = Subscriber<Data>())
180 {
182 static_cast<Derived*>(this)->template _subscribe<Data, scheme>(f, group, subscriber);
183 }
184
190 template <typename Data, int scheme = InterProcessTransporterBase::scheme<Data>()>
192 const Subscriber<Data>& subscriber = Subscriber<Data>())
193 {
195 static_cast<Derived*>(this)->template _unsubscribe<Data, scheme>(group, subscriber);
196 }
197
199 void unsubscribe_all() { static_cast<Derived*>(this)->_unsubscribe_all(); }
200
208 std::shared_ptr<SerializationSubscriptionRegex>
209 subscribe_regex(std::function<void(const std::vector<unsigned char>&, int scheme,
210 const std::string& type, const Group& group)>
211 f,
212 const std::set<int>& schemes, const std::string& type_regex = ".*",
213 const std::string& group_regex = ".*")
214 {
215 return static_cast<Derived*>(this)->_subscribe_regex(f, schemes, type_regex, group_regex);
216 }
217
228 template <typename Data, int scheme = InterProcessTransporterBase::scheme<Data>(),
229 Necessity necessity = Necessity::OPTIONAL>
230 std::shared_ptr<SerializationSubscriptionRegex> subscribe_type_regex(
231 std::function<void(std::shared_ptr<const Data>, const std::string& type)> f,
232 const Group& group, const std::string& type_regex = ".*")
233 {
234 std::regex special_chars{R"([-[\]{}()*+?.,\^$|#\s])"};
235 std::string sanitized_group =
236 std::regex_replace(std::string(group), special_chars, R"(\$&)");
237
238 auto regex_lambda = [f = std::move(f)](const std::vector<unsigned char>& data, int schm,
239 const std::string& type, const Group& grp)
240 {
241 auto data_begin = data.begin(), data_end = data.end(), actual_end = data.end();
242 auto msg =
243 SerializerParserHelper<Data, scheme>::parse(data_begin, data_end, actual_end, type);
244 f(msg, type);
245 };
246
247 return static_cast<Derived*>(this)->_subscribe_regex(regex_lambda, {scheme}, type_regex,
248 "^" + sanitized_group + "$");
249 }
250
260 template <const Group& group, typename Data,
261 int scheme = InterProcessTransporterBase::scheme<Data>(),
262 Necessity necessity = Necessity::OPTIONAL>
264 std::function<void(std::shared_ptr<const Data>, const std::string& type)> f,
265 const std::string& type_regex = ".*")
266 {
267 subscribe_type_regex(f, group, type_regex);
268 }
269
273 template <const Group& group> void check_validity()
274 {
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");
278 }
279
282 {
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"));
285 }
286
287 protected:
288 inline static constexpr auto to_portal_group_name_ =
289 detail::concat(ImplementationTag::prefix, "::to_portal");
290 inline static constexpr auto regex_group_name_ =
291 detail::concat(ImplementationTag::prefix, "::regex");
292 inline static constexpr auto from_portal_group_name_ =
293 detail::concat(ImplementationTag::prefix, "::from_portal");
294
295 inline static constexpr Group to_portal_group_{to_portal_group_name_.data()};
296 inline static constexpr Group regex_group_{regex_group_name_.data()};
297 inline static constexpr Group from_portal_group_{from_portal_group_name_.data()};
298
299 private:
300 friend PollerType;
301 int _poll(std::unique_ptr<std::unique_lock<std::mutex>>& lock)
302 {
303 return static_cast<Derived*>(this)->_poll(lock);
304 }
305};
306
312template <typename InnerTransporter, typename ImplementationTag = void> class InterProcessForwarder;
313
318template <typename InnerTransporter, typename ImplementationTag>
320 : public InterProcessTransporterBase<InterProcessForwarder<InnerTransporter, ImplementationTag>,
321 InnerTransporter, ImplementationTag>
322{
323 public:
324 using Base =
326 InnerTransporter, ImplementationTag>;
327
331 InterProcessForwarder(InnerTransporter& inner)
332 : Base(inner), alive_(std::make_shared<std::atomic<bool>>(true))
333 {
334 this->inner()
337 [this](
338 std::shared_ptr<const goby::middleware::protobuf::SerializerTransporterMessage>
339 msg) { _receive_regex_data_forwarded(msg); });
340 }
342 {
343 // Mark as no longer alive so that forwarding lambdas (captured by the
344 // portal's subscription handlers) become no-ops. This avoids
345 // use-after-free when the portal invokes a stale callback after the
346 // forwarder has been destroyed.
347 *alive_ = false;
348
349 this->unsubscribe_all();
350
351 // TODO - remove by adding in an explicit handshake with the unsubscribe_all publication so that we don't delete ourself (and thus our inner()) before the Portal has deleted all the subscriptions
352 usleep(1e5);
353 }
354
355 friend Base;
356
357 private:
358 template <typename Data, int scheme>
359 void _publish(const Data& d, const Group& group, const Publisher<Data>& publisher)
360 {
361 // create and forward publication to edge
362 std::vector<char> bytes(SerializerParserHelper<Data, scheme>::serialize(d));
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();
366
367 key->set_marshalling_scheme(scheme);
369 key->set_group(std::string(group));
370 msg->set_allocated_data(sbytes);
371
372 *key->mutable_cfg() = publisher.cfg();
373
374 this->inner().template publish<Base::to_portal_group_>(msg);
375 }
376
377 template <typename Data, int scheme>
378 void _subscribe(std::function<void(std::shared_ptr<const Data> d)> f, const Group& group,
379 const Subscriber<Data>& subscriber)
380 {
381 this->inner().template subscribe_dynamic<Data, scheme>(f, group);
382
383 // forward subscription to edge
384 // Capture alive_ by value (shared_ptr copy) so the lambda can detect
385 // when the forwarder has been destroyed and avoid use-after-free.
386 auto alive = alive_;
387 auto inner_publication_lambda = [this, alive, group](std::shared_ptr<const Data> d)
388 {
389 if (*alive)
390 this->inner().template publish_dynamic<Data, scheme>(d, group);
391 };
392
393 auto subscription = std::make_shared<SerializationSubscription<Data, scheme>>(
394 inner_publication_lambda, group,
396 [=](const Data& d) { return group; }));
397
398 this->inner().template publish<Base::to_portal_group_, SerializationHandlerBase<>>(
399 subscription);
400 }
401
402 template <typename Data, int scheme>
403 void _unsubscribe(const Group& group, const Subscriber<Data>& subscriber)
404 {
405 this->inner().template unsubscribe_dynamic<Data, scheme>(group, subscriber);
406
407 auto unsubscription = std::shared_ptr<SerializationHandlerBase<>>(
408 new SerializationUnSubscription<Data, scheme>(group));
409
410 this->inner().template publish<Base::to_portal_group_, SerializationHandlerBase<>>(
411 unsubscription);
412 }
413
414 void _publish_serialized(std::string type_name, int scheme, const std::vector<char>& bytes,
416 {
417 auto msg = std::make_shared<goby::middleware::protobuf::SerializerTransporterMessage>();
418 auto* key = msg->mutable_key();
419
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()));
424
425 this->inner().template publish<Base::to_portal_group_>(msg);
426 }
427
428 void _unsubscribe_all()
429 {
430 regex_subscriptions_.clear();
431 auto all = std::make_shared<SerializationUnSubscribeAll>();
432 this->inner().template publish<Base::to_portal_group_, SerializationUnSubscribeAll>(all);
433 }
434
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)>
438 f,
439 const std::set<int>& schemes, const std::string& type_regex = ".*",
440 const std::string& group_regex = ".*")
441 {
442 auto alive = alive_;
443 auto inner_publication_lambda = [this, alive](const std::vector<unsigned char>& data,
444 int scheme, const std::string& type,
445 const Group& group)
446 {
447 if (!*alive)
448 return;
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);
456 };
457
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);
462
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;
467 }
468
469 void _receive_regex_data_forwarded(
470 std::shared_ptr<const goby::middleware::protobuf::SerializerTransporterMessage> msg)
471 {
472 const auto& bytes = msg->data();
473 for (auto& sub : regex_subscriptions_)
474 sub->post(bytes.begin(), bytes.end(), msg->key().marshalling_scheme(),
475 msg->key().type(), msg->key().group());
476 }
477
478 int _poll(std::unique_ptr<std::unique_lock<std::mutex>>& lock)
479 {
480 return 0;
481 } // A forwarder is a shell, only the inner Transporter has data
482
483 private:
484 std::set<std::shared_ptr<const SerializationSubscriptionRegex>> regex_subscriptions_;
485 // Shared flag checked by forwarding lambdas to avoid accessing `this`
486 // after the forwarder has been destroyed (set to false in the destructor).
487 std::shared_ptr<std::atomic<bool>> alive_;
488};
489
490template <typename Derived, typename InnerTransporter>
492{
493 protected:
494 template <typename Data, int scheme>
495 void _publish(const Data& d, const goby::middleware::Group& group,
496 const middleware::Publisher<Data>& /*publisher*/)
497 {
500 _publish_serialized(type_name, scheme, bytes, group);
501 }
502
503 std::shared_ptr<middleware::SerializationSubscriptionRegex> _subscribe_regex(
504 std::function<void(const std::vector<unsigned char>&, int scheme, const std::string& type,
506 f,
507 const std::set<int>& schemes, const std::string& type_regex, const std::string& group_regex)
508 {
509 auto new_sub = std::make_shared<middleware::SerializationSubscriptionRegex>(
510 f, schemes, type_regex, group_regex);
511 static_cast<Derived*>(this)->_subscribe_regex_serialized(new_sub);
512 return new_sub;
513 }
514
515 template <typename Data, int scheme>
516 void _subscribe(std::function<void(std::shared_ptr<const Data> d)> f,
518 const middleware::Subscriber<Data>& /*subscriber*/)
519 {
520 std::string identifier = this->template _make_identifier<Data, scheme>(
522
523 auto subscription = std::make_shared<middleware::SerializationSubscription<Data, scheme>>(
524 f, group,
526 [=](const Data& /*d*/) { return group; }));
527
528 if (forwarder_subscriptions_.count(identifier) == 0 &&
529 portal_subscriptions_.count(identifier) == 0)
530 static_cast<Derived*>(this)->_do_portal_subscribe(identifier);
531
532 portal_subscriptions_.insert(std::make_pair(identifier, subscription));
533 }
534
535 template <typename Data, int scheme>
539 {
540 std::string identifier = this->template _make_identifier<Data, scheme>(
542
543 portal_subscriptions_.erase(identifier);
544
545 // If no forwarded subscriptions, do the actual unsubscribe
546 if (forwarder_subscriptions_.count(identifier) == 0)
547 static_cast<Derived*>(this)->_do_portal_unsubscribe(identifier);
548 }
549 void _handle_received_data(std::unique_ptr<std::unique_lock<std::mutex>>& lock,
550 const std::string& data)
551 {
552 if (lock)
553 lock.reset();
554
555 std::string group, type;
556 int scheme, process;
557 std::size_t thread;
558 std::tie(group, scheme, type, process, thread) = this->parse_identifier(data);
559 std::string identifier = this->_make_identifier(
561
562 // build a set so if any of the handlers unsubscribes, we still have a pointer to the middleware::SerializationHandlerBase<>
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);
570
571 // actually post the data
572 {
573 auto null_delim_it = std::find(std::begin(data), std::end(data),
575 for (auto& sub : subs_to_post)
576 {
577 if (auto sub_sp = sub.lock())
578 sub_sp->post(null_delim_it + 1, data.end());
579 }
580 }
581
582 if (!regex_subscriptions_.empty())
583 {
584 auto null_delim_it = std::find(std::begin(data), std::end(data),
586
587 bool forwarder_subscription_posted = false;
588 for (auto& sub : regex_subscriptions_)
589 {
590 // only post at most once for forwarders as the threads will filter
591 bool is_forwarded_sub =
592 sub.first != middleware::identifier_part_to_string(std::this_thread::get_id());
593 if (is_forwarded_sub && forwarder_subscription_posted)
594 continue;
595
596 if (sub.second->post(null_delim_it + 1, data.end(), scheme, type, group) &&
597 is_forwarded_sub)
598 forwarder_subscription_posted = true;
599 }
600 }
601 }
602
603 void _unsubscribe_all(const std::string& subscriber_id =
604 middleware::identifier_part_to_string(std::this_thread::get_id()))
605 {
606 // portal unsubscribe
607 if (subscriber_id == middleware::identifier_part_to_string(std::this_thread::get_id()))
608 {
609 for (const auto& p : portal_subscriptions_)
610 {
611 const auto& identifier = p.first;
612 if (forwarder_subscriptions_.count(identifier) == 0)
613 static_cast<Derived*>(this)->_do_portal_unsubscribe(identifier);
614 }
615 portal_subscriptions_.clear();
616 }
617 else // forwarder unsubscribe
618 {
619 while (forwarder_subscription_identifiers_[subscriber_id].size() > 0)
621 subscriber_id,
622 forwarder_subscription_identifiers_[subscriber_id].begin()->first);
623 }
624
625 // regex
626 if (regex_subscriptions_.size() > 0)
627 {
628 regex_subscriptions_.erase(subscriber_id);
629 if (regex_subscriptions_.empty())
630 static_cast<Derived*>(this)->_do_portal_wildcard_unsubscribe();
631 }
632 }
633
635 const std::shared_ptr<const middleware::SerializationHandlerBase<>>& subscription)
636 {
637 std::string identifier = this->_make_identifier(
638 subscription->type_name(), subscription->scheme(), subscription->subscribed_group(),
640
642 goby::glog << "Received subscription forwarded for identifier [" << identifier
643 << "] from subscriber id: " << subscription->subscriber_id() << std::endl;
644
645 switch (subscription->action())
646 {
648 {
649 // insert if this thread hasn't already subscribed
650 if (forwarder_subscription_identifiers_[subscription->subscriber_id()].count(
651 identifier) == 0)
652 {
653 // first to subscribe from a Forwarder
654 if (forwarder_subscriptions_.count(identifier) == 0)
655 {
656 // first to subscribe (locally or forwarded)
657 if (portal_subscriptions_.count(identifier) == 0)
658 static_cast<Derived*>(this)->_do_portal_subscribe(identifier);
659
660 // create Forwarder subscription
661 forwarder_subscriptions_.insert(std::make_pair(identifier, subscription));
662 }
663 forwarder_subscription_identifiers_[subscription->subscriber_id()].insert(
664 std::make_pair(identifier, forwarder_subscriptions_.find(identifier)));
665 }
666 }
667 break;
668
670 {
671 _forwarder_unsubscribe(subscription->subscriber_id(), identifier);
672 }
673 break;
674
675 default: break;
676 }
677 }
678
679 void _forwarder_unsubscribe(const std::string& subscriber_id, const std::string& identifier)
680 {
681 auto it = forwarder_subscription_identifiers_[subscriber_id].find(identifier);
682 if (it != forwarder_subscription_identifiers_[subscriber_id].end())
683 {
684 bool no_forwarder_subscribers = true;
685 for (const auto& p : forwarder_subscription_identifiers_)
686 {
687 if (p.second.count(identifier) != 0)
688 {
689 no_forwarder_subscribers = false;
690 break;
691 }
692 }
693
694 // if no Forwarder subscriptions left
695 if (no_forwarder_subscribers)
696 {
697 // erase the Forwarder subscription
698 forwarder_subscriptions_.erase(it->second);
699
700 // do the actual unsubscribe if we aren't subscribe locally as well
701 if (portal_subscriptions_.count(identifier) == 0)
702 static_cast<Derived*>(this)->_do_portal_unsubscribe(identifier);
703 }
704
705 forwarder_subscription_identifiers_[subscriber_id].erase(it);
706 }
707 }
708
710 const std::shared_ptr<const middleware::SerializationSubscriptionRegex>& new_sub)
711 {
712 if (regex_subscriptions_.empty())
713 static_cast<Derived*>(this)->_do_portal_wildcard_subscribe();
714
715 regex_subscriptions_.insert(std::make_pair(new_sub->subscriber_id(), new_sub));
716 }
717
718 void _publish_serialized(std::string type_name, int scheme, const std::vector<char>& bytes,
720 {
721 std::string identifier =
724 static_cast<Derived*>(this)->_do_publish(identifier, bytes);
725 }
726
727 private:
728 // portal_subscriptions_ and forwarder_subscriptions_: maps identifier to subscription
729 std::unordered_multimap<std::string,
730 std::shared_ptr<const middleware::SerializationHandlerBase<>>>
731 portal_subscriptions_;
732 // only one subscription for each forwarded identifier
733 std::unordered_map<std::string, std::shared_ptr<const middleware::SerializationHandlerBase<>>>
734 forwarder_subscriptions_;
735
736 // maps subscriber_id [thread id as string] to map of identifier to forwarder subscription
737 std::unordered_map<
738 std::string, std::unordered_map<
739 std::string, typename decltype(forwarder_subscriptions_)::const_iterator>>
740 forwarder_subscription_identifiers_;
741
742 // subscriber id to subscription
743 std::unordered_multimap<std::string,
744 std::shared_ptr<const middleware::SerializationSubscriptionRegex>>
745 regex_subscriptions_;
746};
747
748template <typename Derived, typename InnerTransporter, typename ImplementationTag>
750 : public InterProcessTransporterBase<Derived, InnerTransporter, ImplementationTag>,
751 public InterProcessPortalCommon<Derived, InnerTransporter>
752{
753 public:
756
757 InterProcessPortalBase(InnerTransporter& inner) : Base(inner) { _init(); }
759
761
762 friend Base;
763
764 private:
765 void _init()
766 {
768 this->inner().template subscribe<Base::to_portal_group_, SerializerTransporterMessage>(
769 [this](std::shared_ptr<const SerializerTransporterMessage> d)
770 {
771 std::vector<char> data(d->data().begin(), d->data().end());
772 static_cast<Derived*>(this)->_publish_serialized(
773 d->key().type(), d->key().marshalling_scheme(), data,
774 goby::middleware::DynamicGroup(d->key().group()));
775 });
776
777 this->inner().template subscribe<Base::to_portal_group_, SerializationHandlerBase<>>(
778 [this](std::shared_ptr<const middleware::SerializationHandlerBase<>> s)
779 { static_cast<Derived*>(this)->_receive_subscription_forwarded(s); });
780
781 this->inner().template subscribe<Base::to_portal_group_, SerializationSubscriptionRegex>(
782 [this](std::shared_ptr<const middleware::SerializationSubscriptionRegex> s)
783 { static_cast<Derived*>(this)->_subscribe_regex_serialized(s); });
784
785 this->inner().template subscribe<Base::to_portal_group_, SerializationUnSubscribeAll>(
786 [this](std::shared_ptr<const middleware::SerializationUnSubscribeAll> s)
787 { static_cast<Derived*>(this)->_unsubscribe_all(s->subscriber_id()); });
788 }
789};
790
791} // namespace middleware
792} // namespace goby
793
795
796namespace goby
797{
798namespace middleware
799{
800
806template <typename InnerTransporter>
807class InterProcessForwarder<InnerTransporter, void>
808 : public InterProcessForwarder<InnerTransporter, zeromq::detail::InterProcessTag>
809{
810 public:
811 using Base = InterProcessForwarder<InnerTransporter, zeromq::detail::InterProcessTag>;
812
813 [[deprecated("Use zeromq::InterProcessForwarder<> or udpm::InterProcessForwarder<> instead of "
814 "middleware::InterProcessForwarder<>")]]
815 explicit InterProcessForwarder(InnerTransporter& inner)
816 : Base(inner)
817 {
818 }
819};
820
821} // namespace middleware
822} // namespace goby
823
824#endif
simple exception class for goby applications
Definition exception.h:35
Implementation of Group for dynamic (run-time) instantiations. Use Group directly for static (compile...
Definition group.h:120
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
Definition group.h:60
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
std::string _make_identifier(const goby::middleware::Group &group, IdentifierWildcard wildcard)
Definition identifier.h:88
static std::tuple< std::string, int, std::string, int, std::size_t > parse_identifier(const std::string &identifier)
InterProcessPortalBase(InnerTransporter &inner)
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.
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
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...
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...
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)....
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.
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)....
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.
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 ...
Definition poller.h:38
Class that holds additional metadata and callback functions related to a publication (and is optional...
Definition publisher.h:40
const goby::middleware::protobuf::TransporterConfig & cfg() const
Returns the metadata configuration.
Definition publisher.h:108
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...
Definition interface.h:234
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)
Definition interface.h:300
Class that holds additional metadata and callback functions related to a subscription (and is optiona...
Definition subscriber.h:37
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.
Definition cstr.h:65
Necessity
Used to tag subscriptions based on their necessity (e.g. required for correct functioning,...
Definition interface.h:222
std::string identifier_part_to_string(int i)
Definition identifier.h:60
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.
Definition jwt.h:2311
STL namespace.
Class for parsing and serializing a given marshalling scheme. Must be specialized for a particular sc...
Definition interface.h:98
static std::string type_name()
The marshalling scheme specific string name for this type.
Definition interface.h:107
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.
Definition interface.h:129