Goby3 3.2.3
2025.05.13
Loading...
Searching...
No Matches
subscriber.h
Go to the documentation of this file.
1// Copyright 2019-2021:
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
78
80 const goby::middleware::protobuf::TransporterConfig& cfg() const { return cfg_; }
81
83 Group group(const Data& data) const
84 {
85 if (group_func_)
86 return group_func_(data);
87 else
89 }
90
92 subscribed_func_type subscribed_func() const { return subscribed_func_; }
94 subscribe_expired_func_type subscribe_expired_func() const { return subscribe_expired_func_; }
95
96 bool has_group_func() const { return bool(group_func_); }
97
99 void set_link_data(Data& data, const intervehicle::protobuf::Header& header) const
100 {
101 if (set_link_data_func_)
102 set_link_data_func_(data, header);
103 };
104
105 private:
107 group_func_type group_func_;
108 subscribed_func_type subscribed_func_;
109 subscribe_expired_func_type subscribe_expired_func_;
110 set_link_data_func_type set_link_data_func_;
111};
112} // namespace middleware
113} // namespace goby
114
115#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
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:92
Group group(const Data &data) const
Definition subscriber.h:83
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(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:80
subscribe_expired_func_type subscribe_expired_func() const
Definition subscriber.h:94
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:99
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.