Goby3  3.1.5
2024.05.14
single_thread.h
Go to the documentation of this file.
1 // Copyright 2016-2023:
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_APPLICATION_SINGLE_THREAD_H
25 #define GOBY_MIDDLEWARE_APPLICATION_SINGLE_THREAD_H
26 
27 #include <boost/units/systems/si.hpp>
28 
32 
37 
40 
41 namespace goby
42 {
43 namespace middleware
44 {
49 template <class Config, template <class = NullTransporter> class InterProcessPortal>
51  : public goby::middleware::Application<Config>,
52  public Thread<Config, InterVehicleForwarder<InterProcessPortal<>>>,
53  public coroner::Application<SingleThreadApplication<Config, InterProcessPortal>>,
54  public terminate::Application<SingleThreadApplication<Config, InterProcessPortal>>
55 {
56  private:
58 
59  InterProcessPortal<> interprocess_;
61 
64 
65  public:
69  SingleThreadApplication(double loop_freq_hertz = 0)
70  : SingleThreadApplication(loop_freq_hertz * boost::units::si::hertz)
71  {
72  }
73 
77  SingleThreadApplication(boost::units::quantity<boost::units::si::frequency> loop_freq)
78  : MainThread(this->app_cfg(), loop_freq),
79  interprocess_(
81  intervehicle_(interprocess_)
82  {
83  this->set_transporter(&intervehicle_);
84 
85  this->subscribe_terminate();
86  this->subscribe_coroner();
87 
88  this->interprocess().template subscribe<goby::middleware::groups::datum_update>(
89  [this](const protobuf::DatumUpdate& datum_update)
90  {
91  this->configure_geodesy(
92  {datum_update.datum().lat_with_units(), datum_update.datum().lon_with_units()});
93  });
94 
95  this->interprocess().template publish<goby::middleware::groups::configuration>(
96  this->app_cfg());
97  }
98 
100 
101  protected:
102  InterProcessPortal<>& interprocess() { return interprocess_; }
104 
105  virtual void health(goby::middleware::protobuf::ThreadHealth& health) override
106  {
107  health.set_name(this->app_name());
108  health.set_state(goby::middleware::protobuf::HEALTH__OK);
109  }
110 
112  virtual void post_initialize() override { interprocess().ready(); };
113 
114  private:
115  void run() override { MainThread::run_once(); }
116 };
117 
118 } // namespace middleware
119 } // namespace goby
120 
121 #endif
Base class for Goby applications. Generally you will want to use SingleThreadApplication or MultiThre...
Definition: interface.h:72
const Config & app_cfg()
Accesses configuration object passed at launch.
Definition: interface.h:114
void configure_geodesy(goby::util::UTMGeodesy::LatLonPoint datum)
Definition: interface.h:288
Implements the forwarder concept for the intervehicle layer.
Definition: intervehicle.h:578
Implements an Application for a two layer middleware setup ([ intervehicle [ interprocess ] ]) based ...
Definition: single_thread.h:55
InterVehicleForwarder< InterProcessPortal<> > & intervehicle()
SingleThreadApplication(boost::units::quantity< boost::units::si::frequency > loop_freq)
Construct the application calling loop() at the given frequency (boost::units overload)
Definition: single_thread.h:77
virtual void post_initialize() override
Assume all required subscriptions are done in the Constructor or in initialize(). If this isn't the c...
virtual void health(goby::middleware::protobuf::ThreadHealth &health) override
Called when HealthRequest is made by goby_coroner.
SingleThreadApplication(double loop_freq_hertz=0)
Construct the application calling loop() at the given frequency (double overload)
Definition: single_thread.h:69
Represents a thread of execution within the Goby middleware, interleaving periodic events (loop()) wi...
Definition: thread.h:61
void set_transporter(InterVehicleForwarder< InterProcessPortal<> > *transporter)
Definition: thread.h:186
void subscribe_terminate(bool do_quit=true)
Definition: terminate.h:78
detail namespace with internal helper functions
Definition: json.hpp:247
Config make_interprocess_config(Config cfg, std::string app_name)
constexpr goby::middleware::Group datum_update
Definition: groups.h:36
InterProcessPortalImplementation< InnerTransporter, middleware::InterProcessPortalBase > InterProcessPortal
Definition: interprocess.h:827
The global namespace for the Goby project.
int run(const goby::middleware::ConfiguratorInterface< typename App::ConfigType > &cfgtor)
Run a Goby application using the provided Configurator.
Definition: interface.h:318