Goby v2
mac_manager.h
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Goby Underwater Autonomy Project Libraries
8 // ("The Goby Libraries").
9 //
10 // The Goby Libraries are free software: you can redistribute them and/or modify
11 // them under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // The Goby Libraries are distributed in the hope that they will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
22 
23 #ifndef MAC20091019H
24 #define MAC20091019H
25 
26 #include <boost/asio.hpp>
27 #include <boost/asio/time_traits.hpp>
28 #include <boost/bind.hpp>
29 #include <boost/foreach.hpp>
30 #include <boost/function.hpp>
31 #include <boost/lexical_cast.hpp>
32 
33 #include "goby/acomms/modem_driver.h"
34 #include "goby/acomms/protobuf/amac.pb.h"
35 #include "goby/acomms/protobuf/amac_config.pb.h"
36 #include "goby/acomms/protobuf/modem_message.pb.h"
37 #include "goby/common/time.h"
38 #include "goby/util/as.h"
39 
40 namespace goby
41 {
42 namespace acomms
43 {
45 
46 
51 class MACManager : public std::list<protobuf::ModemTransmission>
52 {
53  public:
55 
56  MACManager();
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() { io_.poll(); }
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 
101 
102  unsigned cycle_count() { return std::list<protobuf::ModemTransmission>::size(); }
103  double cycle_duration();
104 
105  boost::asio::io_service& get_io_service() { return io_; }
106 
107  const std::string& glog_mac_group() const { return glog_mac_group_; }
108 
109  private:
110  void begin_slot(const boost::system::error_code&);
111  boost::posix_time::ptime next_cycle_time();
112 
113  void increment_slot();
114 
115  void restart_timer();
116  void stop_timer();
117 
118  unsigned cycle_sum();
119  void position_blank();
120 
121  // allowed offset from actual end of slot
122  enum
123  {
124  ALLOWED_SKEW_SECONDS = 2
125  };
126 
127  private:
128  MACManager(const MACManager&);
129  MACManager& operator=(const MACManager&);
130 
131  protobuf::MACConfig cfg_;
132 
133  // asynchronous timer
134  boost::asio::io_service io_;
135 
136  boost::asio::basic_deadline_timer<goby::common::GobyTime> timer_;
137  // give the io_service some work to do forever
138  boost::asio::io_service::work work_;
139 
140  boost::posix_time::ptime next_cycle_t_;
141  boost::posix_time::ptime next_slot_t_;
142 
143  std::list<protobuf::ModemTransmission>::iterator current_slot_;
144 
145  unsigned cycles_since_reference_;
146 
147  bool started_up_;
148 
149  std::string glog_mac_group_;
150  static int count_;
151 };
152 
153 namespace protobuf
154 {
155 inline bool operator==(const ModemTransmission& a, const ModemTransmission& b)
156 {
157  return a.SerializeAsString() == b.SerializeAsString();
158 }
159 } // namespace protobuf
160 
161 inline std::ostream& operator<<(std::ostream& os, const MACManager& mac)
162 {
163  for (std::list<protobuf::ModemTransmission>::const_iterator it = mac.begin(), n = mac.end();
164  it != n; ++it)
165  { os << *it; } return os;
166 }
167 
168 } // namespace acomms
169 } // namespace goby
170 
171 #endif
void startup(const protobuf::MACConfig &cfg)
Starts the MAC with given configuration.
Definition: mac_manager.cpp:65
void do_work()
Allows the MAC timer to do its work. Does not block. If you prefer more control you can directly cont...
Definition: mac_manager.h:76
provides an API to the goby-acomms MAC library. MACManager is essentially a std::list<protobuf::Modem...
Definition: mac_manager.h:51
MACManager()
Default constructor.
Definition: mac_manager.cpp:43
void shutdown()
Shutdown the MAC.
The global namespace for the Goby project.
boost::signals2::signal< void(const protobuf::ModemTransmission &m)> signal_slot_start
Signals the start of all transmissions (even when we don&#39;t transmit)
Definition: mac_manager.h:97
void restart()
Restarts the MAC with original configuration.
Definition: mac_manager.cpp:97
void update()
You must call this after any change to the underlying list that would invalidate iterators or change ...
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