Goby v2
zeromq_application_base.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 ZEROMQAPPLICATIONBASE20110418H
24 #define ZEROMQAPPLICATIONBASE20110418H
25 
26 #include "application_base.h"
27 #include "zeromq_service.h"
28 
29 #include "goby/common/logger.h"
30 #include "goby/common/time.h"
31 
32 namespace goby
33 {
34 namespace common
35 {
37 {
38  protected:
40  : ApplicationBase(cfg), zeromq_service_(*service)
41  {
42  set_loop_freq(base_cfg().loop_freq());
43 
44  // we are started
45  t_start_ = goby::common::goby_time();
46  // start the loop() on the next even second
47  t_next_loop_ =
48  boost::posix_time::second_clock::universal_time() + boost::posix_time::seconds(1);
49  }
50 
51  virtual ~ZeroMQApplicationBase() {}
52 
53  virtual void loop() = 0;
54 
58  void set_loop_period(boost::posix_time::time_duration p) { loop_period_ = p; }
62  void set_loop_period(long milliseconds)
63  {
64  set_loop_period(boost::posix_time::milliseconds(milliseconds));
65  }
66 
70  void set_loop_freq(double hertz)
71  {
72  set_loop_period(boost::posix_time::milliseconds(1000.0 / hertz));
73  }
74 
76  boost::posix_time::time_duration loop_period() { return loop_period_; }
78  long loop_freq() { return 1000 / loop_period_.total_milliseconds(); }
80  boost::posix_time::ptime t_start() { return t_start_; }
81 
82  private:
83  void iterate()
84  {
85  using goby::glog;
86 
87  // sit and wait on a message until the next time to call loop() is up
88  long timeout = (t_next_loop_ - goby::common::goby_time()).total_microseconds();
89  if (timeout < 0)
90  timeout = 0;
91 
92  glog.is(goby::common::logger::DEBUG3) && glog << "timeout set to: " << timeout
93  << " microseconds." << std::endl;
94  bool had_events = zeromq_service_.poll(timeout);
95  if (!had_events)
96  {
97  // no message, time to call loop()
98  loop();
99  t_next_loop_ += loop_period_;
100  }
101  }
102 
103  private:
104  ZeroMQService& zeromq_service_;
105 
106  // how long to wait between calls to loop()
107  boost::posix_time::time_duration loop_period_;
108 
109  // time this process was started
110  boost::posix_time::ptime t_start_;
111  // time of the next call to loop()
112  boost::posix_time::ptime t_next_loop_;
113 };
114 } // namespace common
115 } // namespace goby
116 
117 #endif
void set_loop_period(long milliseconds)
set the interval in milliseconds between calls to loop. Alternative to set_loop_freq().
long loop_freq()
frequency of calls to loop() in Hertz
void set_loop_freq(double hertz)
set the frequency with which loop() is called. Alternative to set_loop_period().
ReturnType goby_time()
Returns current UTC time as a boost::posix_time::ptime.
Definition: time.h:104
void set_loop_period(boost::posix_time::time_duration p)
set the interval (with a boost::posix_time::time_duration) between calls to loop. Alternative to set_...
boost::posix_time::time_duration loop_period()
interval between calls to loop()
common::FlexOstream glog
Access the Goby logger through this object.
The global namespace for the Goby project.