Goby3 3.5.1
2026.06.04
Loading...
Searching...
No Matches
interface.h
Go to the documentation of this file.
1// Copyright 2011-2026:
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// Copilot <198982749+Copilot@users.noreply.github.com>
8//
9//
10// This file is part of the Goby Underwater Autonomy Project Libraries
11// ("The Goby Libraries").
12//
13// The Goby Libraries are free software: you can redistribute them and/or modify
14// them under the terms of the GNU Lesser General Public License as published by
15// the Free Software Foundation, either version 2.1 of the License, or
16// (at your option) any later version.
17//
18// The Goby Libraries are distributed in the hope that they will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU Lesser General Public License for more details.
22//
23// You should have received a copy of the GNU Lesser General Public License
24// along with Goby. If not, see <http://www.gnu.org/licenses/>.
25
26#ifndef GOBY_MIDDLEWARE_APPLICATION_INTERFACE_H
27#define GOBY_MIDDLEWARE_APPLICATION_INTERFACE_H
28
29#include <chrono>
30#include <csignal>
31#include <cstdlib>
32#include <iostream>
33#include <sys/types.h>
34#include <unistd.h>
35
36#include <boost/format.hpp>
37
38#include "goby/exception.h"
42#include "goby/time.h"
44#include "goby/util/geodesy.h"
45
46namespace goby
47{
54template <typename App>
56
63template <typename App,
64 typename Configurator = middleware::ProtobufConfigurator<typename App::ConfigType>>
65int run(int argc, char* argv[])
66{
67 return run<App>(Configurator(argc, argv));
68}
69
70namespace middleware
71{
72
73namespace julia
74{
75template <typename App> class ApplicationWrapper;
76}
77
79template <typename Config> class Application
80{
81 public:
83 virtual ~Application()
84 {
85 goby::glog.is_debug2() && goby::glog << "Application: destructing cleanly" << std::endl;
86 }
87
88 using ConfigType = Config;
89
90 protected:
92 virtual void pre_initialize() {};
93
95 virtual void initialize() {};
96
98 virtual void post_initialize() {};
99
101 virtual void run() = 0;
102
104 virtual void pre_finalize() {};
105
107 virtual void finalize() {};
108
110 virtual void post_finalize() {};
111
115 void quit(int return_value = 0)
116 {
117 alive_ = false;
118 return_value_ = return_value;
119 }
120
122 const Config& app_cfg() { return *app_cfg_; }
123
126 {
127 if (geodesy_)
128 return *geodesy_;
129 else
130 throw(goby::Exception("No lat_origin and lon_origin defined for requested UTMGeodesy"));
131 }
132
134 bool has_geodesy() { return geodesy_ ? true : false; }
135
136 std::string app_name() { return app3_base_configuration_->name(); }
137
138 bool app_alive() { return alive_; }
139
140 boost::units::quantity<boost::units::si::frequency>
141 choose_loop_freq(boost::units::quantity<boost::units::si::frequency> compiled_loop_freq)
142 {
143 if (app3_base_configuration_->has_loop_frequency())
144 return app3_base_configuration_->loop_frequency_with_units();
145 else
146 return compiled_loop_freq;
147 }
148
149 protected:
151
152 private:
153 template <typename App>
154 friend int ::goby::run(
156
157 template <typename App> friend class goby::middleware::julia::ApplicationWrapper;
158
159 // main loop that exits on quit(); returns the desired return value
160 int __run();
161
162 void run_one() { run(); }
163
164 void configure_logger();
165 void configure_glog_file();
166 void configure_intervehicle();
167 void check_rotate_glog_file();
168
169 private:
170 // sets configuration (before Application construction)
171 static std::unique_ptr<Config> app_cfg_;
172 static std::unique_ptr<protobuf::AppConfig> app3_base_configuration_;
173
174 bool alive_;
175 int return_value_;
176
177 // static here allows fout_ to live until program exit to log glog output
178 static std::unique_ptr<std::ofstream> fout_;
179
180 goby::time::SteadyClock::time_point next_log_rotate_time_;
181
182 std::unique_ptr<goby::util::UTMGeodesy> geodesy_;
183};
184} // namespace middleware
185
186} // namespace goby
187
188template <typename Config>
189std::unique_ptr<std::ofstream> goby::middleware::Application<Config>::fout_;
190
191template <typename Config> std::unique_ptr<Config> goby::middleware::Application<Config>::app_cfg_;
192
193template <typename Config>
194std::unique_ptr<goby::middleware::protobuf::AppConfig>
196
197template <typename Config> goby::middleware::Application<Config>::Application() : alive_(true)
198{
199 using goby::glog;
200
201 configure_logger();
202 if (app3_base_configuration_->has_geodesy())
203 configure_geodesy({app3_base_configuration_->geodesy().lat_origin_with_units(),
204 app3_base_configuration_->geodesy().lon_origin_with_units()});
205
206 if (app3_base_configuration_->has_intervehicle_cfg())
207 configure_intervehicle();
208
209 if (!app3_base_configuration_->IsInitialized())
210 throw(middleware::ConfigException("Invalid base configuration"));
211
212 glog.is_debug2() && glog << "Application: constructed with PID: " << getpid() << std::endl;
213 glog.is_debug1() && glog << "App name is " << app3_base_configuration_->name() << std::endl;
214 glog.is_debug2() && glog << "Configuration is: " << app_cfg_->DebugString() << std::endl;
215}
216
217template <typename Config> void goby::middleware::Application<Config>::configure_logger()
218{
219 using goby::glog;
220
221 // set up the logger
222 glog.set_name(app3_base_configuration_->name());
224 app3_base_configuration_->glog_config().tty_verbosity()),
225 &std::cout);
226
227 if (app3_base_configuration_->glog_config().show_gui())
229
230 if (app3_base_configuration_->glog_config().has_file_log())
231 configure_glog_file();
232
233 if (app3_base_configuration_->glog_config().show_dccl_log())
235}
236
238{
240 app3_base_configuration_->intervehicle_cfg();
241
242 if (intervehicle_cfg.has_dccl_passphrase())
243 {
245 intervehicle_cfg.dccl_passphrase());
246 }
247}
248
250{
251 using goby::glog;
252 if (app3_base_configuration_->glog_config().has_file_log() &&
253 app3_base_configuration_->glog_config().file_log().has_log_rotate_sec() &&
254 goby::time::SteadyClock::now() > next_log_rotate_time_)
255 {
256 glog.remove_stream(fout_.get());
257 configure_glog_file();
258 }
259}
260
262{
263 const auto& file_log = app3_base_configuration_->glog_config().file_log();
264 std::string file_format_str;
265
266 std::string file_name_format = file_log.file_name();
267
268 if (!file_log.has_file_name() && file_log.omit().file_timestamp())
269 file_name_format = "%2%.txt";
270
271 if (file_log.has_file_dir() && !file_log.file_dir().empty())
272 {
273 auto file_dir = file_log.file_dir();
274 if (file_dir.back() != '/')
275 file_dir += "/";
276 file_format_str = file_dir + file_name_format;
277 }
278 else
279 {
280 file_format_str = file_name_format;
281 }
282
283 boost::format file_format(file_format_str);
284
285 if (!file_log.omit().file_timestamp())
286 {
287 if (file_format_str.find("%1") == std::string::npos)
288 glog.is_die() &&
289 glog << "file_name string must contain \"%1%\" which is expanded to the current "
290 "application start time (e.g. 20190201T184925), unless omit.file_timestamp "
291 "== true. Erroneous file_name is: "
292 << file_format_str << std::endl;
293 }
294
295 file_format.exceptions(boost::io::all_error_bits ^
296 (boost::io::too_many_args_bit | boost::io::too_few_args_bit));
297
298 std::string file_name =
299 (file_format % goby::time::file_str() % app3_base_configuration_->name()).str();
300 glog.is_verbose() && glog << "logging output to file: " << file_name << std::endl;
301
302 fout_.reset(new std::ofstream(file_name.c_str()));
303
304 if (!fout_->is_open())
305 glog.is_die() && glog << "cannot write glog output to requested file: " << file_name
306 << std::endl;
307
308 if (!file_log.omit().latest_symlink())
309 {
310 std::string file_symlink =
311 (file_format % "latest" % app3_base_configuration_->name()).str();
312 remove(file_symlink.c_str());
313 char* resolved = realpath(file_name.c_str(), nullptr);
314 if (resolved != nullptr)
315 {
316 int result = symlink(resolved, file_symlink.c_str());
317 free(resolved);
318 if (result != 0)
319 glog.is_warn() &&
320 glog << "Cannot create symlink to latest file. Continuing onwards anyway"
321 << std::endl;
322 }
323 }
324
325 glog.add_stream(file_log.verbosity(), fout_.get());
326
327 if (file_log.has_log_rotate_sec())
328 next_log_rotate_time_ =
329 goby::time::SteadyClock::now() + std::chrono::seconds(file_log.log_rotate_sec());
330}
331
332template <typename Config>
338
339template <typename Config> int goby::middleware::Application<Config>::__run()
340{
341 // block SIGWINCH (change window size) in all threads
342 sigset_t signal_mask;
343 sigemptyset(&signal_mask);
344 sigaddset(&signal_mask, SIGWINCH);
345 pthread_sigmask(SIG_BLOCK, &signal_mask, NULL);
346
347 this->pre_initialize();
348 this->initialize();
349 this->post_initialize();
350 // continue to run while we are alive (quit() has not been called)
351 while (alive_)
352 {
353 this->run_one();
354 this->check_rotate_glog_file();
355 }
356 this->pre_finalize();
357 this->finalize();
358 this->post_finalize();
359 return return_value_;
360}
361
362template <typename App>
364{
365 int return_value = 0;
366 try
367 {
368 try
369 {
370 cfgtor.validate();
371 }
373 {
374 cfgtor.handle_config_error(e);
375 return 1;
376 }
377
378 // simply print the configuration and exit
379 if (cfgtor.app_configuration().debug_cfg())
380 {
381 std::cout << cfgtor.str() << std::endl;
382 exit(EXIT_SUCCESS);
383 }
384
385 // set configuration
386 App::app_cfg_.reset(new typename App::ConfigType(cfgtor.cfg()));
387 App::app3_base_configuration_.reset(
389
390 // set up simulation time
391 if (App::app3_base_configuration_->simulation().time().use_sim_time())
392 {
395 App::app3_base_configuration_->simulation().time().warp_factor();
396 if (App::app3_base_configuration_->simulation().time().has_reference_microtime())
398 std::chrono::system_clock::time_point(std::chrono::microseconds(
399 App::app3_base_configuration_->simulation().time().reference_microtime()));
400 }
401
402 // instantiate the application (with the configuration already set)
403 App app;
404 return_value = app.__run();
405 }
406 catch (std::exception& e)
407 {
408 // some other exception
409 std::cerr << "Application:: uncaught exception: " << e.what() << std::endl;
410 throw;
411 }
412
413 goby::glog.is_debug2() && goby::glog << "goby::run: exiting cleanly with code: " << return_value
414 << std::endl;
415 return return_value;
416}
417
418#endif
simple exception class for goby applications
Definition exception.h:35
Base class for Goby applications. Generally you will want to use SingleThreadApplication or MultiThre...
Definition interface.h:80
const util::UTMGeodesy & geodesy()
Accesses the geodetic conversion tool if lat_origin and lon_origin were provided.
Definition interface.h:125
virtual void pre_finalize()
Called just before finalize.
Definition interface.h:104
virtual void post_initialize()
Called just after initialize.
Definition interface.h:98
virtual void initialize()
Perform any initialize tasks that couldn't be done in the constructor.
Definition interface.h:95
virtual void pre_initialize()
Called just before initialize.
Definition interface.h:92
virtual void run()=0
Runs once.
bool has_geodesy()
Returns if the geodesy tool is configured with a datum.
Definition interface.h:134
virtual void finalize()
Perform any final cleanup actions just before the destructor is called.
Definition interface.h:107
void quit(int return_value=0)
Requests a clean exit.
Definition interface.h:115
void configure_geodesy(goby::util::UTMGeodesy::LatLonPoint datum)
Definition interface.h:333
boost::units::quantity< boost::units::si::frequency > choose_loop_freq(boost::units::quantity< boost::units::si::frequency > compiled_loop_freq)
Definition interface.h:141
virtual void post_finalize()
Called just after finalize.
Definition interface.h:110
const Config & app_cfg()
Accesses configuration object passed at launch.
Definition interface.h:122
indicates a problem with the runtime command line or .cfg file configuration (or –help was given)
Defines the interface to a "configurator", a class that can read command line parameters (argc,...
virtual void validate() const
Override to validate the configuration.
virtual void handle_config_error(middleware::ConfigException &e) const
Override to customize how ConfigException errors are handled.
virtual const protobuf::AppConfig & app_configuration() const
Subset of the configuration used to configure the Application itself.
const Config & cfg() const
The configuration object produced from the command line parameters.
virtual std::string str() const =0
Override to output the configuration object as a string.
void remove_stream(std::ostream *os=nullptr)
void set_name(const std::string &s)
Set the name of the application that the logger is serving.
void add_stream(logger::Verbosity verbosity=logger::VERBOSE, std::ostream *os=nullptr)
Attach a stream object (e.g. std::cout, std::ofstream, ...) to the logger with desired verbosity.
std::string str(TimeType value=SystemClock::now< TimeType >())
Returns the provided time (or current time if omitted) as a human-readable string.
Definition convert.h:187
std::string file_str(TimeType value=SystemClock::now< TimeType >())
Returns the provided time (or current time if omitted) as an ISO string suitable for file names (no s...
Definition convert.h:200
The global namespace for the Goby project.
util::FlexOstream glog
Access the Goby logger through this object.
int run(const goby::middleware::ConfiguratorInterface< typename App::ConfigType > &cfgtor)
Run a Goby application using the provided Configurator.
Definition interface.h:363
static void set_crypto_passphrase(const std::string &passphrase, const std::set< dccl::int32 > &do_not_encrypt_ids=std::set< dccl::int32 >())
static void setup_dlog()
Enable dlog output to glog using same verbosity settings as glog.
static bool using_sim_time
Enables simulation time if true (if false, none of the remaining parameters are used)
Definition simulation.h:38
static std::chrono::system_clock::time_point reference_time
Reference time when calculating SystemClock::now(). If this is unset, the default is 1 January of the...
Definition simulation.h:42
static int warp_factor
Warp factor to speed up (or slow time) the time values returned by SteadyClock::now() and SystemClock...
Definition simulation.h:40
std::chrono::time_point< SteadyClock > time_point
static time_point now() noexcept
Returns the current steady time unless SimulatorSettings::using_sim_time == true in which case a simu...