From 8a0e3d5b6b02ef722b0dc8729466cf969336f343 Mon Sep 17 00:00:00 2001 From: Aaron Chong Date: Mon, 22 Jan 2024 09:13:41 +0000 Subject: [PATCH 1/3] Adds an option to generate unique hex strings for new task ids, fix time stamp logic too Signed-off-by: Aaron Chong --- .../src/rmf_task_ros2/Dispatcher.cpp | 41 ++++++++++++++++++- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/rmf_task_ros2/src/rmf_task_ros2/Dispatcher.cpp b/rmf_task_ros2/src/rmf_task_ros2/Dispatcher.cpp index 07c973927..e728baa0a 100644 --- a/rmf_task_ros2/src/rmf_task_ros2/Dispatcher.cpp +++ b/rmf_task_ros2/src/rmf_task_ros2/Dispatcher.cpp @@ -46,6 +46,7 @@ #include #include +#include #include namespace rmf_task_ros2 { @@ -90,6 +91,24 @@ nlohmann::json_schema::json_validator make_validator(nlohmann::json schema) return nlohmann::json_schema::json_validator( std::move(schema), schema_loader); } + +//============================================================================== +std::string generate_random_hex_string(const std::size_t length = 3) +{ + std::stringstream ss; + for (std::size_t i = 0; i < length; ++i) + { + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_int_distribution<> dis(0, 255); + const auto random_char = dis(gen); + std::stringstream hexstream; + hexstream << std::hex << random_char; + auto hex = hexstream.str(); + ss << (hex.length() < 2 ? '0' + hex : hex); + } + return ss.str(); +} } // anonymous namespace //============================================================================== @@ -176,6 +195,7 @@ class Dispatcher::Implementation std::size_t terminated_tasks_max_size; int publish_active_tasks_period; bool use_timestamp_for_task_id; + bool use_unique_hex_string_with_task_id; std::unordered_map legacy_task_type_names = { @@ -215,6 +235,11 @@ class Dispatcher::Implementation RCLCPP_INFO(node->get_logger(), " Use timestamp with task_id: %s", (use_timestamp_for_task_id ? "true" : "false")); + use_unique_hex_string_with_task_id = + node->declare_parameter("use_unique_hex_string_with_task_id", false); + RCLCPP_INFO(node->get_logger(), + " Use unique hex string with task_id: %s", + (use_unique_hex_string_with_task_id ? "true" : "false")); std::optional server_uri = std::nullopt; const std::string uri = @@ -483,10 +508,22 @@ class Dispatcher::Implementation task_request_json["category"].get() + ".dispatch-"; - if (use_timestamp_for_task_id) + if (use_unique_hex_string_with_task_id) + { + if (use_timestamp_for_task_id) + { + RCLCPP_WARN( + node->get_logger(), + "Overwriting use_timestamp_for_task_id option with " + "use_unique_hex_string_with_task_id" + ); + } + task_id += generate_random_hex_string(5); + } + else if (use_timestamp_for_task_id) { task_id += std::to_string( - static_cast(node->get_clock()->now().nanoseconds()/1e6)); + (int)(std::round(node->get_clock()->now().seconds() * 1e3))); } else { From 73da20ca3bcf7b4a3c378976d6316a3f02556163 Mon Sep 17 00:00:00 2001 From: Aaron Chong Date: Mon, 22 Jan 2024 09:30:25 +0000 Subject: [PATCH 2/3] Use static_cast Signed-off-by: Aaron Chong --- rmf_task_ros2/src/rmf_task_ros2/Dispatcher.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rmf_task_ros2/src/rmf_task_ros2/Dispatcher.cpp b/rmf_task_ros2/src/rmf_task_ros2/Dispatcher.cpp index e728baa0a..39fbeced9 100644 --- a/rmf_task_ros2/src/rmf_task_ros2/Dispatcher.cpp +++ b/rmf_task_ros2/src/rmf_task_ros2/Dispatcher.cpp @@ -523,7 +523,7 @@ class Dispatcher::Implementation else if (use_timestamp_for_task_id) { task_id += std::to_string( - (int)(std::round(node->get_clock()->now().seconds() * 1e3))); + static_cast(std::round(node->get_clock()->now().seconds() * 1e3))); } else { From c11b157a4b51793bbd6aeeb419d0468db3a16cba Mon Sep 17 00:00:00 2001 From: Aaron Chong Date: Mon, 22 Jan 2024 09:55:49 +0000 Subject: [PATCH 3/3] Lint Signed-off-by: Aaron Chong --- rmf_task_ros2/src/rmf_task_ros2/Dispatcher.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/rmf_task_ros2/src/rmf_task_ros2/Dispatcher.cpp b/rmf_task_ros2/src/rmf_task_ros2/Dispatcher.cpp index 39fbeced9..f6801f375 100644 --- a/rmf_task_ros2/src/rmf_task_ros2/Dispatcher.cpp +++ b/rmf_task_ros2/src/rmf_task_ros2/Dispatcher.cpp @@ -236,7 +236,8 @@ class Dispatcher::Implementation " Use timestamp with task_id: %s", (use_timestamp_for_task_id ? "true" : "false")); use_unique_hex_string_with_task_id = - node->declare_parameter("use_unique_hex_string_with_task_id", false); + node->declare_parameter("use_unique_hex_string_with_task_id", + false); RCLCPP_INFO(node->get_logger(), " Use unique hex string with task_id: %s", (use_unique_hex_string_with_task_id ? "true" : "false")); @@ -523,7 +524,8 @@ class Dispatcher::Implementation else if (use_timestamp_for_task_id) { task_id += std::to_string( - static_cast(std::round(node->get_clock()->now().seconds() * 1e3))); + static_cast(std::round(node->get_clock()->now().seconds() * + 1e3))); } else {