Goby3  3.1.4
2024.02.22
mac_manager.h
Go to the documentation of this file.
1 // Copyright 2010-2023:
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 // File authors:
6 // Toby Schneider <toby@gobysoft.org>
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_ACOMMS_AMAC_MAC_MANAGER_H
26 #define GOBY_ACOMMS_AMAC_MAC_MANAGER_H
27 
28 #include <boost/signals2/signal.hpp> // for signal
29 #include <boost/smart_ptr/shared_ptr.hpp> // for shared_ptr
30 #include <chrono> // for seconds
31 #include <iosfwd> // for ostream
32 #include <list> // for list, list<>::ite...
33 #include <string> // for operator==, string
34 
35 #include "goby/acomms/protobuf/amac_config.pb.h" // for MACConfig
36 #include "goby/acomms/protobuf/modem_message.pb.h" // for ModemTransmission
37 #include "goby/time/system_clock.h" // for SystemClock, Syst...
38 
39 namespace goby
40 {
41 namespace acomms
42 {
44 
45 
50 class MACManager : public std::list<protobuf::ModemTransmission>
51 {
52  public:
54 
55  MACManager();
57  MACManager(int id);
58  ~MACManager();
60 
62 
63 
67  void startup(const protobuf::MACConfig& cfg);
68 
70  void restart();
71 
73  void shutdown();
74 
76  void do_work();
77 
79  void update();
80 
81  bool running() { return started_up_; }
82 
84 
86 
87  boost::signals2::signal<void(const protobuf::ModemTransmission& m)>
92 
96 
97  boost::signals2::signal<void(const protobuf::ModemTransmission& m)> signal_slot_start;
98 
100 
101  unsigned cycle_count() { return std::list<protobuf::ModemTransmission>::size(); }
103 
104  const std::string& glog_mac_group() const { return glog_mac_group_; }
105 
106  private:
107  void begin_slot();
108  time::SystemClock::time_point next_cycle_time();
109 
110  void increment_slot();
111 
112  void restart_timer();
113  void stop_timer();
114 
115  unsigned cycle_sum();
116  void position_blank();
117 
118  // allowed offset from actual end of slot
119 
120  const time::SystemClock::duration allowed_skew_{std::chrono::seconds(2)};
121 
122  private:
123  MACManager(const MACManager&);
124  MACManager& operator=(const MACManager&);
125 
126  protobuf::MACConfig cfg_;
127 
128  // start of the next cycle (cycle = all slots)
129  time::SystemClock::time_point next_cycle_t_;
130  // start of the next slot
131  time::SystemClock::time_point next_slot_t_;
132 
133  std::list<protobuf::ModemTransmission>::iterator current_slot_;
134 
135  unsigned cycles_since_reference_;
136 
137  bool started_up_{false};
138 
139  std::string glog_mac_group_;
140  static int count_;
141 };
142 
143 namespace protobuf
144 {
145 inline bool operator==(const ModemTransmission& a, const ModemTransmission& b)
146 {
147  return a.SerializeAsString() == b.SerializeAsString();
148 }
149 } // namespace protobuf
150 
151 std::ostream& operator<<(std::ostream& os, const MACManager& mac);
152 
153 } // namespace acomms
154 } // namespace goby
155 
156 #endif
goby::acomms::protobuf::ModemTransmission
Definition: modem_message.pb.h:166
amac_config.pb.h
system_clock.h
goby::acomms::MACManager::signal_initiate_transmission
boost::signals2::signal< void(const protobuf::ModemTransmission &m)> signal_initiate_transmission
Signals when it is time for this platform to begin transmission of an acoustic message at the start o...
Definition: mac_manager.h:91
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
goby::acomms::MACManager::update
void update()
You must call this after any change to the underlying list that would invalidate iterators or change ...
goby::acomms::MACManager::glog_mac_group
const std::string & glog_mac_group() const
Definition: mac_manager.h:104
goby::acomms::MACManager::cycle_count
unsigned cycle_count()
Definition: mac_manager.h:101
goby::acomms::MACManager::cycle_duration
time::SystemClock::duration cycle_duration()
modem_message.pb.h
goby::acomms::MACManager::shutdown
void shutdown()
Shutdown the MAC.
goby::acomms::MACManager
provides an API to the goby-acomms MAC library. MACManager is essentially a std::list<protobuf::Modem...
Definition: mac_manager.h:50
goby::time::SystemClock::duration
std::chrono::microseconds duration
Duration type.
Definition: system_clock.h:52
detail::void
j template void())
Definition: json.hpp:4822
goby::acomms::MACManager::signal_slot_start
boost::signals2::signal< void(const protobuf::ModemTransmission &m)> signal_slot_start
Signals the start of all transmissions (even when we don't transmit)
Definition: mac_manager.h:97
goby::acomms::operator<<
std::ostream & operator<<(std::ostream &os, const MACManager &mac)
goby::acomms::MACManager::restart
void restart()
Restarts the MAC with original configuration.
goby::time::SystemClock::time_point
std::chrono::time_point< SystemClock > time_point
Definition: system_clock.h:55
goby::acomms::MACManager::do_work
void do_work()
Allows the MAC timer to do its work. Does not block.
goby::acomms::MACManager::~MACManager
~MACManager()
goby::acomms::MACManager::startup
void startup(const protobuf::MACConfig &cfg)
Starts the MAC with given configuration.
goby::acomms::MACManager::running
bool running()
Definition: mac_manager.h:81
google::protobuf::MessageLite::SerializeAsString
string SerializeAsString() const
goby::acomms::protobuf::MACConfig
Definition: amac_config.pb.h:117
goby::acomms::MACManager::MACManager
MACManager()
Default constructor.
goby::acomms::protobuf::operator==
bool operator==(const ModemTransmission &a, const ModemTransmission &b)
Definition: mac_manager.h:145