Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds an option to generate unique hex strings for new task ids, fix time stamp logic #319

Merged
merged 3 commits into from
Jan 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions rmf_task_ros2/src/rmf_task_ros2/Dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include <rmf_api_msgs/schemas/task_state.hpp>
#include <rmf_api_msgs/schemas/error.hpp>

#include <random>
#include <unordered_set>

namespace rmf_task_ros2 {
Expand Down Expand Up @@ -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

//==============================================================================
Expand Down Expand Up @@ -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<std::size_t, std::string> legacy_task_type_names =
{
Expand Down Expand Up @@ -215,6 +235,12 @@ 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<bool>("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<std::string> server_uri = std::nullopt;
const std::string uri =
Expand Down Expand Up @@ -483,10 +509,23 @@ class Dispatcher::Implementation
task_request_json["category"].get<std::string>()
+ ".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<int>(node->get_clock()->now().nanoseconds()/1e6));
static_cast<int>(std::round(node->get_clock()->now().seconds() *
1e3)));
}
else
{
Expand Down
Loading