Skip to content

Commit 600e95a

Browse files
authored
Implement timeout tool argument (#254)
* Implement timeout Signed-off-by: jparisu <javierparis@eprosima.com> * Add RTD documentation Signed-off-by: jparisu <javierparis@eprosima.com> * uncrustify Signed-off-by: jparisu <javierparis@eprosima.com>
1 parent 326b7cb commit 600e95a

7 files changed

Lines changed: 185 additions & 132 deletions

File tree

docs/rst/notes/forthcoming_version.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22
.. todo:
33
remove orphan tag when new info added to this file
44
5-
:orphan:
5+
:orphan:
66
77
###################
88
Forthcoming Version
99
###################
10+
11+
Next version will add the following **features**:
12+
13+
- Timeout argument ``--timeout`` to close the application after the time set has passed.
14+
Check section :ref:`user_manual_user_interface_timeout_argument` for more information.

docs/rst/notes/notes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
uncomment the include of forthcoming when new info is added
77
.. include:: forthcoming_version.rst
88
9+
.. include:: forthcoming_version.rst
10+
911
##############
1012
Version v0.4.0
1113
##############

docs/rst/user_manual/user_interface.rst

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ It shows the usage information of the application.
9090
Value 0 does not reload file. [Default: 0].
9191
-d --debug Activate debug Logs (be aware that some logs may require specific CMAKE compilation options).
9292
-v --version Print version, branch and commit hash.
93-
93+
-t --timeout Set a maximum time in seconds for the Router to run. Value 0 does not set maximum. [Default: 0].
9494
9595
.. _user_manual_user_interface_configuration_file_argument:
9696

@@ -134,6 +134,17 @@ Version Argument
134134
It shows the current version of the DDS Router and the hash of the last commit of the compiled code.
135135

136136

137+
.. _user_manual_user_interface_timeout_argument:
138+
139+
Timeout Argument
140+
^^^^^^^^^^^^^^^^
141+
142+
This argument allow to set a maximum time while the application will be running.
143+
Setting this argument will set the number of seconds the application will run until it is killed.
144+
While the application is waiting for timeout, it is still possible to kill it via signal.
145+
Default value ``0`` means that the application will run forever (until kill via signal).
146+
147+
137148
.. _user_manual_user_interface_configuration_file:
138149

139150
Configuration File
@@ -208,3 +219,9 @@ SIGTERM
208219
Send an interruption ``SIGTERM`` signal *(signal value 15)* to the process.
209220
Write command ``kill <pid>`` in a different terminal, where ``<pid>`` is the id of the process running the |ddsrouter|.
210221
Use ``ps`` or ``top`` programs to check the process ids.
222+
223+
TIMEOUT
224+
^^^^^^^
225+
226+
Setting a maximum amount of seconds that the application will work using argument ``--timeout`` will close the
227+
application once the time has expired.

tools/ddsrouter_tool/src/cpp/main.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,15 @@ int main(
4747
// Reload time
4848
utils::Duration_ms reload_time = 0;
4949

50+
// Maximum timeout
51+
utils::Duration_ms timeout = 0;
52+
5053
// Debug option active
5154
bool activate_debug = false;
5255

5356
// Parse arguments
5457
ui::ProcessReturnCode arg_parse_result =
55-
ui::parse_arguments(argc, argv, file_path, reload_time, activate_debug);
58+
ui::parse_arguments(argc, argv, file_path, reload_time, activate_debug, timeout);
5659

5760
if (arg_parse_result == ui::ProcessReturnCode::help_argument)
5861
{
@@ -105,14 +108,26 @@ int main(
105108
// Encapsulating execution in block to erase all memory correctly before closing process
106109
try
107110
{
108-
// First of all, create signal handler so SIGINT and SIGTERM do not break the program while initializing
109-
event::MultipleEventHandler signal_handlers;
111+
// Create a multiple event handler that handles all events that make the router stop
112+
event::MultipleEventHandler close_handler;
110113

111-
signal_handlers.register_event_handler<event::EventHandler<event::Signal>, event::Signal>(
114+
// First of all, create signal handler so SIGINT and SIGTERM do not break the program while initializing
115+
close_handler.register_event_handler<event::EventHandler<event::Signal>, event::Signal>(
112116
std::make_unique<event::SignalEventHandler<event::Signal::sigint>>()); // Add SIGINT
113-
signal_handlers.register_event_handler<event::EventHandler<event::Signal>, event::Signal>(
117+
close_handler.register_event_handler<event::EventHandler<event::Signal>, event::Signal>(
114118
std::make_unique<event::SignalEventHandler<event::Signal::sigterm>>()); // Add SIGTERM
115119

120+
// If it must be a maximum time, register a periodic handler to finish handlers
121+
if (timeout > 0)
122+
{
123+
close_handler.register_event_handler<event::PeriodicEventHandler>(
124+
std::make_unique<event::PeriodicEventHandler>(
125+
[]()
126+
{
127+
/* Do nothing */ },
128+
timeout));
129+
}
130+
116131
/////
117132
// DDS Router Initialization
118133

@@ -193,9 +208,9 @@ int main(
193208
logUser(DDSROUTER_EXECUTION, "DDS Router running.");
194209

195210
// Wait until signal arrives
196-
signal_handlers.wait_for_event();
211+
close_handler.wait_for_event();
197212

198-
logUser(DDSROUTER_EXECUTION, "Signal received, stopping DDS Router.");
213+
logUser(DDSROUTER_EXECUTION, "Stopping DDS Router.");
199214

200215
// Before stopping the Router erase event handlers that reload configuration
201216
if (periodic_handler)

tools/ddsrouter_tool/src/cpp/user_interface/arguments_configuration.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ const option::Descriptor usage[] = {
9696
" -v \t--version\t \t" \
9797
"Print version, branch and commit hash." \
9898
},
99+
{
100+
optionIndex::TIMEOUT,
101+
0,
102+
"t",
103+
"timeout",
104+
Arg::Numeric,
105+
" -t \t--timeout\t \t" \
106+
"Set a maximum time in seconds for the Router to run. " \
107+
"Value 0 does not set maximum. [Default: 0]."
108+
},
99109

100110
{ 0, 0, 0, 0, 0, 0 }
101111
};
@@ -111,7 +121,8 @@ ProcessReturnCode parse_arguments(
111121
char** argv,
112122
std::string& file_path,
113123
utils::Duration_ms& reload_time,
114-
bool& activate_debug)
124+
bool& activate_debug,
125+
utils::Duration_ms& timeout)
115126
{
116127
// Variable to pretty print usage help
117128
int columns;
@@ -188,6 +199,10 @@ ProcessReturnCode parse_arguments(
188199
activate_debug = true;
189200
break;
190201

202+
case optionIndex::TIMEOUT:
203+
timeout = std::stol(opt.arg) * 1000; // pass to milliseconds
204+
break;
205+
191206
case optionIndex::UNKNOWN_OPT:
192207
logError(DDSROUTER_ARGS, opt << " is not a valid argument.");
193208
option::printUsage(fwrite, stdout, usage, columns);

tools/ddsrouter_tool/src/cpp/user_interface/arguments_configuration.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ enum optionIndex
8585
RELOAD_TIME,
8686
ACTIVATE_DEBUG,
8787
VERSION,
88+
TIMEOUT,
8889
};
8990

9091
/**
@@ -102,8 +103,9 @@ extern const option::Descriptor usage[];
102103
* @param [in] argc number of process arguments
103104
* @param [in] argv process arguments array (with size \c argc )
104105
* @param [out] file_path path to the configuration file
105-
* @param [out] reload_time time in seconds to reload the configuration file
106+
* @param [out] reload_time time in milliseconds to reload the configuration file
106107
* @param [out] activate_debug activate log info
108+
* @param [out] timeout time in milliseconds to maximum router execution time
107109
*
108110
* @return \c SUCCESS if everything OK
109111
* @return \c INCORRECT_ARGUMENT if arguments were incorrect (unknown or incorrect value)
@@ -115,7 +117,8 @@ ProcessReturnCode parse_arguments(
115117
char** argv,
116118
std::string& file_path,
117119
utils::Duration_ms& reload_time,
118-
bool& activate_debug);
120+
bool& activate_debug,
121+
utils::Duration_ms& timeout);
119122

120123
//! \c Option to stream serializator
121124
std::ostream& operator <<(

0 commit comments

Comments
 (0)