Goby3 3.7.0
2026.11.12
Loading...
Searching...
No Matches
subscriber.h
Go to the documentation of this file.
1// Copyright 2019-2026:
2// GobySoft, LLC (2013-)
3// Community contributors (see AUTHORS file)
4// File authors:
5// Toby Schneider <toby@gobysoft.org>
6//
7//
8// This file is part of the Goby Underwater Autonomy Project Libraries
9// ("The Goby Libraries").
10//
11// The Goby Libraries are free software: you can redistribute them and/or modify
12// them under the terms of the GNU Lesser General Public License as published by
13// the Free Software Foundation, either version 2.1 of the License, or
14// (at your option) any later version.
15//
16// The Goby Libraries are distributed in the hope that they will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU Lesser General Public License for more details.
20//
21// You should have received a copy of the GNU Lesser General Public License
22// along with Goby. If not, see <http://www.gnu.org/licenses/>.
23
24#ifndef GOBY_MIDDLEWARE_TRANSPORT_SUBSCRIBER_H
25#define GOBY_MIDDLEWARE_TRANSPORT_SUBSCRIBER_H
26
30
31namespace goby
32{
33namespace middleware
34{
36template <typename Data> class Subscriber
37{
38 public:
39 using group_func_type = std::function<Group(const Data&)>;
45 std::function<void(Data&, const intervehicle::protobuf::Header&)>;
46
55 group_func_type group_func = group_func_type(),
59 : cfg_(cfg),
60 group_func_(group_func),
61 subscribed_func_(subscribed_func),
62 subscribe_expired_func_(subscribe_expired_func),
63 set_link_data_func_(set_link_data_func)
64 {
65 }
66
76
99
101 explicit Subscriber(Options options)
102 : Subscriber(options.cfg, options.group_func, options.subscribed_func,
103 options.subscribe_expired_func, options.set_link_data_func)
104 {
105 group_in_message_ = options.group_in_message;
106 }
107
109
112
114 Group group(const Data& data) const
115 {
116 if (group_func_)
117 return group_func_(data);
118 else
120 }
121
123 subscribed_func_type subscribed_func() const { return subscribed_func_; }
125 subscribe_expired_func_type subscribe_expired_func() const { return subscribe_expired_func_; }
126
127 bool has_group_func() const { return bool(group_func_); }
128
130 bool group_in_message() const { return group_in_message_; }
131
133 void set_link_data(Data& data, const intervehicle::protobuf::Header& header) const
134 {
135 if (set_link_data_func_)
136 set_link_data_func_(data, header);
137 };
138
139 private:
141 group_func_type group_func_;
142 subscribed_func_type subscribed_func_;
143 subscribe_expired_func_type subscribe_expired_func_;
144 set_link_data_func_type set_link_data_func_;
145 bool group_in_message_{true};
146};
147} // namespace middleware
148} // namespace goby
149
150#endif
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
Definition group.h:60
static constexpr std::uint32_t broadcast_group
Special group number representing the broadcast group (used when no grouping is required for a given ...
Definition group.h:63
std::function< void(const Data &, const intervehicle::protobuf::AckData &)> acked_func_type
Definition publisher.h:44
std::function< void(const Data &, const intervehicle::protobuf::ExpireData &)> expired_func_type
Definition publisher.h:46
Class that holds additional metadata and callback functions related to a subscription (and is optiona...
Definition subscriber.h:37
bool group_in_message() const
False if the group isn't stored in the message (see Options::group_in_message)
Definition subscriber.h:130
typename Publisher< intervehicle::protobuf::Subscription >::expired_func_type subscribe_expired_func_type
Definition subscriber.h:43
subscribed_func_type subscribed_func() const
Definition subscriber.h:123
Group group(const Data &data) const
Definition subscriber.h:114
Subscriber(const goby::middleware::protobuf::TransporterConfig &cfg=goby::middleware::protobuf::TransporterConfig(), group_func_type group_func=group_func_type(), subscribed_func_type subscribed_func=subscribed_func_type(), subscribe_expired_func_type subscribe_expired_func=subscribe_expired_func_type(), set_link_data_func_type set_link_data_func=set_link_data_func_type())
Construct a Subscriber with all available metadata and callbacks.
Definition subscriber.h:53
Subscriber(Options options)
Construct a Subscriber from Options.
Definition subscriber.h:101
Subscriber(const goby::middleware::protobuf::TransporterConfig &cfg, subscribed_func_type subscribed_func, subscribe_expired_func_type subscribe_expired_func=subscribe_expired_func_type(), set_link_data_func_type set_link_data_func=set_link_data_func_type())
Construct a Subscriber but without the group_func callback.
Definition subscriber.h:68
const goby::middleware::protobuf::TransporterConfig & cfg() const
Definition subscriber.h:111
subscribe_expired_func_type subscribe_expired_func() const
Definition subscriber.h:125
void set_link_data(Data &data, const intervehicle::protobuf::Header &header) const
Sets the link data in the message using the set_link_data_func. Only intended to be called by the var...
Definition subscriber.h:133
typename Publisher< intervehicle::protobuf::Subscription >::acked_func_type subscribed_func_type
Definition subscriber.h:41
std::function< void(Data &, const intervehicle::protobuf::Header &)> set_link_data_func_type
Definition subscriber.h:45
std::function< Group(const Data &)> group_func_type
Definition subscriber.h:39
The global namespace for the Goby project.
Everything a Subscriber can be given, named, so that it can be initialized with C++20 designated init...
Definition subscriber.h:83
subscribe_expired_func_type subscribe_expired_func
Called when a subscription request expires without reaching any publishers.
Definition subscriber.h:91
set_link_data_func_type set_link_data_func
Sets link data (e.g. the sender) in received messages.
Definition subscriber.h:93
group_func_type group_func
Returns the group from the message (see the full constructor)
Definition subscriber.h:87
subscribed_func_type subscribed_func
Called when a subscription request reaches a publisher.
Definition subscriber.h:89
goby::middleware::protobuf::TransporterConfig cfg
Additional metadata for this subscribe.
Definition subscriber.h:85