Skip to content

Commit b68021d

Browse files
Apply Examples suggestions
Signed-off-by: Eugenio Collado <eugeniocollado@eprosima.com>
1 parent b6cfce8 commit b68021d

9 files changed

Lines changed: 88 additions & 140 deletions

File tree

ddsenabler/examples/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
# Install persistence directory
16+
install(
17+
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/persistence
18+
DESTINATION bin
19+
)
20+
1521
# Add subdirectories for examples
1622
add_subdirectory(utils)
1723
add_subdirectory(publish)

ddsenabler/examples/action/CLIParser.hpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -293,24 +293,12 @@ class CLIParser
293293
print_help(EXIT_FAILURE);
294294
}
295295

296-
if (config.config_file_path.empty())
297-
{
298-
std::cerr << "Configuration file path is required" << std::endl;
299-
print_help(EXIT_FAILURE);
300-
}
301-
302296
if (client_flag && config.goals_path.empty())
303297
{
304298
std::cerr << "--goals-path is required in client mode" << std::endl;
305299
print_help(EXIT_FAILURE);
306300
}
307301

308-
if (server_flag && config.expected_requests == 0)
309-
{
310-
std::cerr << "--expected-requests is required in server mode and must be greater than 0" << std::endl;
311-
print_help(EXIT_FAILURE);
312-
}
313-
314302
return config;
315303
}
316304

ddsenabler/examples/action/CMakeLists.txt

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,11 @@ target_link_libraries(ddsenabler_example_action PRIVATE ddsenabler utils)
2222

2323
# Install rule
2424
install(TARGETS ddsenabler_example_action
25-
RUNTIME DESTINATION bin
26-
)
27-
28-
# Copy the json files over to the build directory
29-
file(GLOB_RECURSE JSON_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.json)
30-
foreach(JSON_FILE_COMPLETE_PATH ${JSON_FILES})
31-
get_filename_component(JSON_FILE ${JSON_FILE_COMPLETE_PATH} NAME_WE)
32-
configure_file(
33-
${JSON_FILE_COMPLETE_PATH}
34-
${CMAKE_CURRENT_BINARY_DIR}/${JSON_FILE}.json
35-
COPYONLY)
36-
install(FILES ${JSON_FILE_COMPLETE_PATH}
37-
DESTINATION bin)
38-
endforeach()
39-
40-
# Copy the yaml files over to the build directory
41-
file(GLOB_RECURSE YAML_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.yml)
42-
foreach(YAML_FILE_COMPLETE_PATH ${YAML_FILES})
43-
get_filename_component(YAML_FILE ${YAML_FILE_COMPLETE_PATH} NAME_WE)
44-
configure_file(
45-
${YAML_FILE_COMPLETE_PATH}
46-
${CMAKE_CURRENT_BINARY_DIR}/${YAML_FILE}.yml
47-
COPYONLY)
48-
install(FILES ${YAML_FILE_COMPLETE_PATH}
49-
DESTINATION bin)
50-
endforeach()
51-
52-
# Install persistence directory
53-
install(
54-
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../persistence
55-
DESTINATION bin
25+
RUNTIME DESTINATION bin/examples/action
5626
)
5727

5828
# Install goals directory
5929
install(
6030
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/goals
61-
DESTINATION bin
31+
DESTINATION bin/examples/action
6232
)

ddsenabler/examples/action/Readme.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ Users are encouraged to implement their own functional server logic as needed in
1010

1111
### CLIENT
1212
```bash
13-
./install/ddsenabler/bin/ddsenabler_example_action client \
14-
--persistence-path ./install/ddsenabler/bin/persistence/ \
15-
--goals-path ./install/ddsenabler/bin/goals/ \
16-
--config ./install/ddsenabler/bin/config_service.yml \
13+
./install/ddsenabler/bin/examples/action/ddsenabler_example_action client \
14+
--persistence-path ./install/ddsenabler/bin/examples/persistence/ \
15+
--goals-path ./install/ddsenabler/bin/examples/action/goals/ \
1716
--request-initial-wait 3 \
1817
--cancel-requests false
18+
<optional> --config $PATH_TO_CONFIG_FILE
1919
```
2020

2121
### SERVER
2222
```bash
23-
./install/ddsenabler/bin/ddsenabler_example_action server \
24-
--persistence-path ./install/ddsenabler/bin/persistence/ \
25-
--config ./install/ddsenabler/bin/config_service.yml \
26-
--expected-requests 3
23+
./install/ddsenabler/bin/examples/action/ddsenabler_example_action server \
24+
--persistence-path ./install/ddsenabler/bin/examples/persistence/
25+
<optional> --config $PATH_TO_CONFIG_FILE
26+
<optional> --expected-requests N
2727
```

ddsenabler/examples/action/main.cpp

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,23 @@ uint32_t received_results_ = 0;
4141
std::vector<std::pair<eprosima::ddsenabler::participants::UUID, std::string>> received_requests_;
4242
std::mutex app_mutex_;
4343
std::condition_variable app_cv_;
44+
bool stop_app_ = false;
4445

4546
const std::string REQUESTS_SUBDIR = "goals";
4647
const std::string TYPES_SUBDIR = "types";
4748
const std::string ACTION_SUBDIR = "actions";
4849

50+
void signal_handler(
51+
int signum)
52+
{
53+
std::cout << "Signal " << CLIParser::parse_signal(signum) << " received, stopping..." << std::endl;
54+
{
55+
std::lock_guard<std::mutex> lock(app_mutex_);
56+
stop_app_ = true;
57+
}
58+
app_cv_.notify_all();
59+
}
60+
4961
// Static log callback
5062
void test_log_callback(
5163
const char* file_name,
@@ -268,10 +280,10 @@ bool wait_for_action_discovery(
268280
std::condition_variable& app_cv)
269281
{
270282
std::unique_lock<std::mutex> lock(app_mutex);
271-
if (!app_cv.wait_for(lock, std::chrono::seconds(timeout),
283+
if (stop_app_ || !app_cv.wait_for(lock, std::chrono::seconds(timeout),
272284
[]()
273285
{
274-
return action_discovered_;
286+
return stop_app_ || action_discovered_;
275287
}))
276288
{
277289
std::cerr << "Timeout waiting for service discovery." << std::endl;
@@ -288,10 +300,10 @@ bool wait_for_action_request(
288300
std::string& goal_json)
289301
{
290302
std::unique_lock<std::mutex> lock(app_mutex);
291-
if (!app_cv.wait_for(lock, std::chrono::seconds(timeout),
303+
if (stop_app_ || !app_cv.wait_for(lock, std::chrono::seconds(timeout),
292304
[]()
293305
{
294-
return !received_requests_.empty();
306+
return stop_app_ || !received_requests_.empty();
295307
}))
296308
{
297309
std::cerr << "Timeout waiting for service request." << std::endl;
@@ -311,10 +323,10 @@ bool wait_for_action_result(
311323
uint32_t sent_requests)
312324
{
313325
std::unique_lock<std::mutex> lock(app_mutex);
314-
if (!app_cv.wait_for(lock, std::chrono::seconds(timeout),
326+
if (stop_app_ || !app_cv.wait_for(lock, std::chrono::seconds(timeout),
315327
[&sent_requests]()
316328
{
317-
return received_results_ >= sent_requests;
329+
return stop_app_ || received_results_ >= sent_requests;
318330
}))
319331
{
320332
std::cerr << "Timeout waiting for action result." << std::endl;
@@ -452,7 +464,7 @@ bool server_routine(
452464

453465
std::cout << "Action announced: " << action_name << std::endl;
454466

455-
while (true)
467+
while (!stop_app_)
456468
{
457469
eprosima::ddsenabler::participants::UUID request_id;
458470
std::string goal_json;
@@ -477,10 +489,10 @@ bool server_routine(
477489
return false;
478490
}
479491

480-
// Check if we have received the expected number of requests
492+
// Check if we have received the expected number of requests (or run indefinitely if expected_requests is 0)
481493
{
482494
std::lock_guard<std::mutex> lock(app_mutex_);
483-
if (++received_results_ >= expected_requests)
495+
if (expected_requests != 0 && ++received_results_ >= expected_requests)
484496
{
485497
break;
486498
}
@@ -537,7 +549,12 @@ int main(
537549
};
538550

539551
std::shared_ptr<DDSEnabler> enabler;
540-
if (!create_dds_enabler(config.config_file_path.c_str(), callbacks, enabler))
552+
bool enabler_created = false;
553+
if (config.config_file_path.empty())
554+
enabler_created = create_dds_enabler(yaml::EnablerConfiguration(""), callbacks, enabler);
555+
else
556+
enabler_created = create_dds_enabler(config.config_file_path.c_str(), callbacks, enabler);
557+
if (!enabler_created)
541558
{
542559
std::cerr << "Failed to create DDSEnabler instance." << std::endl;
543560
return EXIT_FAILURE;
@@ -552,17 +569,13 @@ int main(
552569
}
553570
else
554571
{
555-
std::string goal_path;
556-
if (!config.goals_path.empty())
572+
if (config.goals_path.empty())
557573
{
558-
goal_path = config.goals_path;
574+
std::cerr << "Request path is not set." << std::endl;
575+
return EXIT_FAILURE;
559576
}
560-
else
561-
{
562-
goal_path = config.persistence_path.empty() ? std::string() :
563-
(std::filesystem::path(config.persistence_path) / REQUESTS_SUBDIR).string();
564-
}
565-
ret = client_routine(enabler, config.action_name, goal_path, config.timeout, config.request_initial_wait,
577+
578+
ret = client_routine(enabler, config.action_name, config.goals_path, config.timeout, config.request_initial_wait,
566579
config.cancel_requests);
567580
}
568581

ddsenabler/examples/service/CLIParser.hpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -274,24 +274,12 @@ class CLIParser
274274
print_help(EXIT_FAILURE);
275275
}
276276

277-
if (config.config_file_path.empty())
278-
{
279-
std::cerr << "Configuration file path is required" << std::endl;
280-
print_help(EXIT_FAILURE);
281-
}
282-
283277
if (client_flag && config.requests_path.empty())
284278
{
285279
std::cerr << "--requests-path is required in client mode" << std::endl;
286280
print_help(EXIT_FAILURE);
287281
}
288282

289-
if (server_flag && config.expected_requests == 0)
290-
{
291-
std::cerr << "--expected-requests is required in server mode and must be greater than 0" << std::endl;
292-
print_help(EXIT_FAILURE);
293-
}
294-
295283
if (config.persistence_path.empty())
296284
{
297285
std::cerr << "Warning: persistence path is not set, persistence features will be disabled" <<

ddsenabler/examples/service/CMakeLists.txt

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,11 @@ target_link_libraries(ddsenabler_example_service PRIVATE ddsenabler utils)
2222

2323
# Install rule
2424
install(TARGETS ddsenabler_example_service
25-
RUNTIME DESTINATION bin
26-
)
27-
28-
# Copy the json files over to the build directory
29-
file(GLOB_RECURSE JSON_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.json)
30-
foreach(JSON_FILE_COMPLETE_PATH ${JSON_FILES})
31-
get_filename_component(JSON_FILE ${JSON_FILE_COMPLETE_PATH} NAME_WE)
32-
configure_file(
33-
${JSON_FILE_COMPLETE_PATH}
34-
${CMAKE_CURRENT_BINARY_DIR}/${JSON_FILE}.json
35-
COPYONLY)
36-
install(FILES ${JSON_FILE_COMPLETE_PATH}
37-
DESTINATION bin)
38-
endforeach()
39-
40-
# Copy the yaml files over to the build directory
41-
file(GLOB_RECURSE YAML_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.yml)
42-
foreach(YAML_FILE_COMPLETE_PATH ${YAML_FILES})
43-
get_filename_component(YAML_FILE ${YAML_FILE_COMPLETE_PATH} NAME_WE)
44-
configure_file(
45-
${YAML_FILE_COMPLETE_PATH}
46-
${CMAKE_CURRENT_BINARY_DIR}/${YAML_FILE}.yml
47-
COPYONLY)
48-
install(FILES ${YAML_FILE_COMPLETE_PATH}
49-
DESTINATION bin)
50-
endforeach()
51-
52-
# Install persistence directory
53-
install(
54-
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../persistence
55-
DESTINATION bin
25+
RUNTIME DESTINATION bin/examples/service
5626
)
5727

5828
# Install requests directory
5929
install(
6030
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/requests
61-
DESTINATION bin
31+
DESTINATION bin/examples/service
6232
)

ddsenabler/examples/service/Readme.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ Users are encouraged to implement their own functional server logic as needed in
1010

1111
### CLIENT
1212
```bash
13-
./install/ddsenabler/bin/ddsenabler_example_service client \
14-
--persistence-path ./install/ddsenabler/bin/persistence/ \
15-
--requests-path ./install/ddsenabler/bin/requests \
16-
--config ./install/ddsenabler/bin/config_service.yml \
13+
./install/ddsenabler/bin/examples/service/ddsenabler_example_service client \
14+
--persistence-path ./install/ddsenabler/bin/examples/persistence/ \
15+
--requests-path ./install/ddsenabler/bin/examples/service/requests \
1716
--request-initial-wait 3
17+
<optional> --config $PATH_TO_CONFIG_FILE
1818
```
1919

2020
### SERVER
2121
```bash
22-
./install/ddsenabler/bin/ddsenabler_example_service server \
23-
--persistence-path ./install/ddsenabler/bin/persistence/ \
24-
--config ./install/ddsenabler/bin/config_service.yml \
25-
--expected-requests 3
22+
./install/ddsenabler/bin/examples/service/ddsenabler_example_service server \
23+
--persistence-path ./install/ddsenabler/bin/examples/persistence/
24+
<optional> --expected-requests N
25+
<optional> --config $PATH_TO_CONFIG_FILE
2626
```

0 commit comments

Comments
 (0)