Skip to content

Commit ce8cdf1

Browse files
authored
Implement one shot argument (#17)
* Implement one shot argument Signed-off-by: jparisu <[email protected]> * Remove dependency for cpp utils PR Signed-off-by: jparisu <[email protected]> * uncrustify Signed-off-by: jparisu <[email protected]> --------- Signed-off-by: jparisu <[email protected]>
1 parent 9f1a7b1 commit ce8cdf1

12 files changed

+199
-141
lines changed

fastddsspy_tool/src/cpp/main.cpp

+20-2
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,19 @@ int main(
5858
std::string log_filter = "(DDSPIPE|FASTDDSSPY)";
5959
eprosima::fastdds::dds::Log::Kind log_verbosity = eprosima::fastdds::dds::Log::Kind::Warning;
6060

61+
// One shot command
62+
std::vector<std::string> one_shot_command;
63+
6164
// Parse arguments
6265
eprosima::spy::ProcessReturnCode arg_parse_result =
63-
eprosima::spy::parse_arguments(argc, argv, file_path, reload_time, log_filter, log_verbosity);
66+
eprosima::spy::parse_arguments(
67+
argc,
68+
argv,
69+
file_path,
70+
reload_time,
71+
log_filter,
72+
log_verbosity,
73+
one_shot_command);
6474

6575
if (arg_parse_result == eprosima::spy::ProcessReturnCode::help_argument)
6676
{
@@ -204,7 +214,15 @@ int main(
204214
reload_time);
205215
}
206216

207-
spy.run();
217+
// If one-shot is required, do one shot run. Otherwise run along
218+
if (one_shot_command.empty())
219+
{
220+
spy.run();
221+
}
222+
else
223+
{
224+
spy.one_shot_run(one_shot_command);
225+
}
208226

209227
// Before stopping the Fast DDS Spy erase event handlers that reload configuration
210228
if (periodic_handler)

fastddsspy_tool/src/cpp/tool/Command.hpp

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
#pragma once
1616

17-
#include <cpp_utils/user_interface/CommandReader.hpp>
1817
#include <cpp_utils/macros/custom_enumeration.hpp>
1918
#include <cpp_utils/enum/EnumBuilder.hpp>
2019

fastddsspy_tool/src/cpp/tool/Controller.cpp

+65-52
Original file line numberDiff line numberDiff line change
@@ -40,64 +40,77 @@ Controller::Controller(
4040
: backend_(configuration)
4141
, model_(backend_.model())
4242
{
43-
view_.print_initial();
43+
// Do nothing
4444
}
4545

4646
void Controller::run()
4747
{
48+
view_.print_initial();
4849
utils::Command<CommandValue> command;
4950
command.command = CommandValue::participant;
5051
while (command.command != CommandValue::exit)
5152
{
5253
command = input_.wait_next_command();
54+
run_command_(command);
55+
}
56+
}
5357

54-
switch (command.command)
55-
{
56-
case CommandValue::participant:
57-
participants_command(command.arguments);
58-
break;
58+
void Controller::one_shot_run(
59+
const std::vector<std::string>& args)
60+
{
61+
utils::sleep_for(configuration_.one_shot_wait_time_ms);
62+
run_command_(input_.parse_as_command(args));
63+
}
5964

60-
case CommandValue::datareader:
61-
readers_command(command.arguments);
62-
break;
65+
utils::ReturnCode Controller::reload_allowed_topics(
66+
const std::shared_ptr<ddspipe::core::AllowedTopicList>& allowed_topics)
67+
{
68+
return backend_.reload_allowed_topics(allowed_topics);
69+
}
6370

64-
case CommandValue::datawriter:
65-
writers_command(command.arguments);
66-
break;
71+
void Controller::run_command_(
72+
const utils::Command<CommandValue>& command)
73+
{
74+
switch (command.command)
75+
{
76+
case CommandValue::participant:
77+
participants_command_(command.arguments);
78+
break;
6779

68-
case CommandValue::topic:
69-
topics_command(command.arguments);
70-
break;
80+
case CommandValue::datareader:
81+
readers_command_(command.arguments);
82+
break;
7183

72-
case CommandValue::print:
73-
print_command(command.arguments);
74-
break;
84+
case CommandValue::datawriter:
85+
writers_command_(command.arguments);
86+
break;
7587

76-
case CommandValue::version:
77-
version_command(command.arguments);
78-
break;
88+
case CommandValue::topic:
89+
topics_command_(command.arguments);
90+
break;
7991

80-
case CommandValue::help:
81-
help_command(command.arguments);
82-
break;
92+
case CommandValue::print:
93+
print_command_(command.arguments);
94+
break;
8395

84-
case CommandValue::error_input:
85-
error_command(command.arguments);
86-
break;
96+
case CommandValue::version:
97+
version_command_(command.arguments);
98+
break;
8799

88-
default:
89-
break;
90-
}
91-
}
92-
}
100+
case CommandValue::help:
101+
help_command_(command.arguments);
102+
break;
93103

94-
utils::ReturnCode Controller::reload_allowed_topics(
95-
const std::shared_ptr<ddspipe::core::AllowedTopicList>& allowed_topics)
96-
{
97-
return backend_.reload_allowed_topics(allowed_topics);
104+
case CommandValue::error_input:
105+
error_command_(command.arguments);
106+
break;
107+
108+
default:
109+
break;
110+
}
98111
}
99112

100-
fastrtps::types::DynamicData_ptr Controller::get_dynamic_data(
113+
fastrtps::types::DynamicData_ptr Controller::get_dynamic_data_(
101114
const fastrtps::types::DynamicType_ptr& dyn_type,
102115
const ddspipe::core::types::RtpsPayloadData& data) noexcept
103116
{
@@ -119,7 +132,7 @@ void Controller::data_stream_callback_(
119132
const ddspipe::core::types::RtpsPayloadData& data)
120133
{
121134
// Get deserializad data
122-
auto dyn_data = get_dynamic_data(dyn_type, data);
135+
auto dyn_data = get_dynamic_data_(dyn_type, data);
123136

124137
// TODO this does not make much sense as print does not allow to choose target
125138
// change in dyn types to be able to print it in view
@@ -146,15 +159,15 @@ void Controller::data_stream_callback_verbose_(
146159
view_.show(yml);
147160

148161
// Get deserializad data
149-
auto dyn_data = get_dynamic_data(dyn_type, data);
162+
auto dyn_data = get_dynamic_data_(dyn_type, data);
150163

151164
// Print data
152165
view_.show("data:\n---");
153166
fastrtps::types::DynamicDataHelper::print(dyn_data.get());
154167
view_.show("---\n");
155168
}
156169

157-
bool Controller::verbose_argument(
170+
bool Controller::verbose_argument_(
158171
const std::string& argument) const noexcept
159172
{
160173
return (
@@ -165,15 +178,15 @@ bool Controller::verbose_argument(
165178
|| (argument == "V"));
166179
}
167180

168-
bool Controller::all_argument(
181+
bool Controller::all_argument_(
169182
const std::string& argument) const noexcept
170183
{
171184
return (
172185
(argument == "all")
173186
|| (argument == "a"));
174187
}
175188

176-
void Controller::participants_command(
189+
void Controller::participants_command_(
177190
const std::vector<std::string>& arguments) noexcept
178191
{
179192
dds_entity_command__(
@@ -194,7 +207,7 @@ void Controller::participants_command(
194207
);
195208
}
196209

197-
void Controller::writers_command(
210+
void Controller::writers_command_(
198211
const std::vector<std::string>& arguments) noexcept
199212
{
200213
dds_entity_command__(
@@ -215,7 +228,7 @@ void Controller::writers_command(
215228
);
216229
}
217230

218-
void Controller::readers_command(
231+
void Controller::readers_command_(
219232
const std::vector<std::string>& arguments) noexcept
220233
{
221234
dds_entity_command__(
@@ -236,7 +249,7 @@ void Controller::readers_command(
236249
);
237250
}
238251

239-
void Controller::topics_command(
252+
void Controller::topics_command_(
240253
const std::vector<std::string>& arguments) noexcept
241254
{
242255
Yaml yml;
@@ -246,7 +259,7 @@ void Controller::topics_command(
246259
// all participants simple
247260
ddspipe::yaml::set(yml, participants::ModelParser::topics(*model_));
248261
}
249-
else if (verbose_argument(arguments[1]))
262+
else if (verbose_argument_(arguments[1]))
250263
{
251264
// verbose
252265
ddspipe::yaml::set(yml, participants::ModelParser::topics_verbose(*model_));
@@ -267,7 +280,7 @@ void Controller::topics_command(
267280
view_.show(yml);
268281
}
269282

270-
void Controller::print_command(
283+
void Controller::print_command_(
271284
const std::vector<std::string>& arguments) noexcept
272285
{
273286
// Check the number of arguments is correct
@@ -281,7 +294,7 @@ void Controller::print_command(
281294
}
282295

283296
// Print all data
284-
if (all_argument(arguments[1]))
297+
if (all_argument_(arguments[1]))
285298
{
286299
bool activated = model_->activate_all(
287300
std::make_shared<participants::DataStreamer::CallbackType>(
@@ -330,7 +343,7 @@ void Controller::print_command(
330343
bool verbose = false;
331344
if (arguments.size() >= 3)
332345
{
333-
verbose = verbose_argument(arguments[2]);
346+
verbose = verbose_argument_(arguments[2]);
334347
}
335348

336349
std::shared_ptr<participants::DataStreamer::CallbackType> callback;
@@ -380,7 +393,7 @@ void Controller::print_command(
380393
model_->deactivate();
381394
}
382395

383-
void Controller::version_command(
396+
void Controller::version_command_(
384397
const std::vector<std::string>& arguments) noexcept
385398
{
386399
view_.show(STR_ENTRY
@@ -390,15 +403,15 @@ void Controller::version_command(
390403
<< FASTDDSSPY_PARTICIPANTS_COMMIT_HASH);
391404
}
392405

393-
void Controller::help_command(
406+
void Controller::help_command_(
394407
const std::vector<std::string>& arguments) noexcept
395408
{
396409
// TODO
397410
view_.show_error(STR_ENTRY
398411
<< "<" << arguments[0] << "> command is not implemented yet. Please be patient.");
399412
}
400413

401-
void Controller::error_command(
414+
void Controller::error_command_(
402415
const std::vector<std::string>& arguments) noexcept
403416
{
404417
view_.show_error(STR_ENTRY

fastddsspy_tool/src/cpp/tool/Controller.hpp

+19-11
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,20 @@ class Controller
3838

3939
void run();
4040

41+
void one_shot_run(
42+
const std::vector<std::string>& args);
43+
4144
utils::ReturnCode reload_allowed_topics(
4245
const std::shared_ptr<ddspipe::core::AllowedTopicList>& allowed_topics);
4346

4447
protected:
4548

49+
void run_command_(
50+
const utils::Command<CommandValue>& command);
51+
4652
////////////////////////////
4753
// DATA STREAM CALLBACKS
48-
static fastrtps::types::DynamicData_ptr get_dynamic_data(
54+
static fastrtps::types::DynamicData_ptr get_dynamic_data_(
4955
const fastrtps::types::DynamicType_ptr& dyn_type,
5056
const ddspipe::core::types::RtpsPayloadData& data) noexcept;
5157

@@ -61,38 +67,38 @@ class Controller
6167

6268
/////////////////////
6369
// PARSE ARGUMENTS
64-
bool verbose_argument(
70+
bool verbose_argument_(
6571
const std::string& argument) const noexcept;
6672

67-
bool all_argument(
73+
bool all_argument_(
6874
const std::string& argument) const noexcept;
6975

7076
////////////////////////////////////////////////////////////////////////////////////
7177
// COMMANDS ROUTINES
7278

7379
/////////////////////
7480
// ENTITIES
75-
void participants_command(
81+
void participants_command_(
7682
const std::vector<std::string>& arguments) noexcept;
77-
void writers_command(
83+
void writers_command_(
7884
const std::vector<std::string>& arguments) noexcept;
79-
void readers_command(
85+
void readers_command_(
8086
const std::vector<std::string>& arguments) noexcept;
81-
void topics_command(
87+
void topics_command_(
8288
const std::vector<std::string>& arguments) noexcept;
8389

8490
/////////////////////
8591
// DATA STREAM
86-
void print_command(
92+
void print_command_(
8793
const std::vector<std::string>& arguments) noexcept;
8894

8995
/////////////////////
9096
// AUXILIARY
91-
void version_command(
97+
void version_command_(
9298
const std::vector<std::string>& arguments) noexcept;
93-
void help_command(
99+
void help_command_(
94100
const std::vector<std::string>& arguments) noexcept;
95-
void error_command(
101+
void error_command_(
96102
const std::vector<std::string>& arguments) noexcept;
97103

98104
/////////////////////
@@ -105,6 +111,8 @@ class Controller
105111

106112
std::shared_ptr<eprosima::spy::participants::SpyModel> model_;
107113

114+
yaml::Configuration configuration_;
115+
108116
private:
109117

110118
template <typename SimpleF, typename VerboseF, typename specificF>

fastddsspy_tool/src/cpp/tool/Input.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,21 @@ void Input::wait_something()
4747
reader_.read_next_command(_);
4848
}
4949

50+
utils::Command<CommandValue> Input::parse_as_command(
51+
const std::vector<std::string>& args)
52+
{
53+
// TODO use the cpp_utils method once it is merged
54+
utils::Command<CommandValue> command;
55+
// Set args in command, and the enum value will be set string_to_enumeration
56+
command.arguments = args;
57+
// Check if command exists
58+
auto res = CommandBuilder::get_instance()->string_to_enumeration(command.arguments[0], command.command);
59+
if (!res)
60+
{
61+
command.command = CommandValue::error_input;
62+
}
63+
return command;
64+
}
65+
5066
} /* namespace spy */
5167
} /* namespace eprosima */

0 commit comments

Comments
 (0)