Goby3  3.1.5a
2024.05.23
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 
27 #include "goby/middleware/group.h"
30 
31 namespace goby
32 {
33 namespace middleware
34 {
36 template <typename Data> class Subscriber
37 {
38  public:
39  using group_func_type = std::function<Group(const Data&)>;
40  using subscribed_func_type =
45  std::function<void(Data&, const intervehicle::protobuf::Header&)>;
46 
55  group_func_type group_func = group_func_type(),
58  set_link_data_func_type set_link_data_func = set_link_data_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 
71  set_link_data_func_type set_link_data_func = set_link_data_func_type())
73  set_link_data_func)
74  {
75  }
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
goby::middleware::Subscriber::Subscriber
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
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
goby::middleware::Subscriber
Class that holds additional metadata and callback functions related to a subscription (and is optiona...
Definition: subscriber.h:36
goby::middleware::Subscriber::group
Group group(const Data &data) const
Definition: subscriber.h:83
goby::middleware::Subscriber::~Subscriber
~Subscriber()
Definition: subscriber.h:77
goby::middleware::Subscriber::group_func_type
std::function< Group(const Data &)> group_func_type
Definition: subscriber.h:39
group.h
goby::middleware::Subscriber::subscribe_expired_func
subscribe_expired_func_type subscribe_expired_func() const
Definition: subscriber.h:94
goby::middleware::Subscriber::set_link_data_func_type
std::function< void(Data &, const intervehicle::protobuf::Header &)> set_link_data_func_type
Definition: subscriber.h:45
goby::middleware::Publisher
Class that holds additional metadata and callback functions related to a publication (and is optional...
Definition: driver_thread.h:69
goby::middleware::Subscriber::Subscriber
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
transporter_config.pb.h
detail::void
j template void())
Definition: json.hpp:4822
goby::middleware::protobuf::TransporterConfig
Definition: transporter_config.pb.h:74
goby::middleware::Subscriber::has_group_func
bool has_group_func() const
Definition: subscriber.h:96
bool
goby::middleware::Subscriber::subscribe_expired_func_type
typename Publisher< intervehicle::protobuf::Subscription >::expired_func_type subscribe_expired_func_type
Definition: subscriber.h:43
goby::middleware::Subscriber::cfg
const goby::middleware::protobuf::TransporterConfig & cfg() const
Definition: subscriber.h:80
goby::middleware::intervehicle::protobuf::Header
Definition: intervehicle.pb.h:1066
publisher.h
goby::middleware::Group
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
Definition: group.h:59
goby::middleware::Group::broadcast_group
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
goby::middleware::Subscriber::subscribed_func_type
typename Publisher< intervehicle::protobuf::Subscription >::acked_func_type subscribed_func_type
Definition: subscriber.h:41
goby::middleware::Subscriber::subscribed_func
subscribed_func_type subscribed_func() const
Definition: subscriber.h:92
goby::middleware::Subscriber::set_link_data
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