From e572634ea892878c84aaf973319801d6679f59cb Mon Sep 17 00:00:00 2001 From: Chris Lalancette Date: Wed, 18 Dec 2024 19:26:37 +0000 Subject: [PATCH] Remove the create_map_and_set_sequence_num function. With the recent changes to get data out of it, and the change to zenoh-cpp, it can mostly be replaced with a couple of inline pieces of code. However, we do add in a new function to help us explicitly get system clock time from std::chrono in nanoseconds. Signed-off-by: Chris Lalancette --- rmw_zenoh_cpp/src/detail/rmw_client_data.cpp | 5 ++-- .../src/detail/rmw_publisher_data.cpp | 12 +++++----- rmw_zenoh_cpp/src/detail/rmw_service_data.cpp | 8 +++---- rmw_zenoh_cpp/src/detail/zenoh_utils.cpp | 24 ++++++------------- rmw_zenoh_cpp/src/detail/zenoh_utils.hpp | 8 ++----- 5 files changed, 21 insertions(+), 36 deletions(-) diff --git a/rmw_zenoh_cpp/src/detail/rmw_client_data.cpp b/rmw_zenoh_cpp/src/detail/rmw_client_data.cpp index 2cb07ffb..78848bd8 100644 --- a/rmw_zenoh_cpp/src/detail/rmw_client_data.cpp +++ b/rmw_zenoh_cpp/src/detail/rmw_client_data.cpp @@ -373,8 +373,9 @@ rmw_ret_t ClientData::send_request( // Send request zenoh::Session::GetOptions opts = zenoh::Session::GetOptions::create_default(); - std::array local_gid = entity_->copy_gid(); - opts.attachment = rmw_zenoh_cpp::create_map_and_set_sequence_num(*sequence_id, local_gid); + int64_t source_timestamp = rmw_zenoh_cpp::get_system_time_in_ns(); + opts.attachment = rmw_zenoh_cpp::AttachmentData( + *sequence_id, source_timestamp, entity_->copy_gid()).serialize_to_zbytes(); opts.target = Z_QUERY_TARGET_ALL_COMPLETE; // The default timeout for a z_get query is 10 seconds and if a response is not received within // this window, the queryable will return an invalid reply. However, it is common for actions, diff --git a/rmw_zenoh_cpp/src/detail/rmw_publisher_data.cpp b/rmw_zenoh_cpp/src/detail/rmw_publisher_data.cpp index dcdd8787..21b31547 100644 --- a/rmw_zenoh_cpp/src/detail/rmw_publisher_data.cpp +++ b/rmw_zenoh_cpp/src/detail/rmw_publisher_data.cpp @@ -252,10 +252,10 @@ rmw_ret_t PublisherData::publish( // session use different encoding formats. In our case, all key expressions // will be encoded with CDR so it does not really matter. zenoh::ZResult result; - int64_t source_timestamp = 0; + int64_t source_timestamp = rmw_zenoh_cpp::get_system_time_in_ns(); auto options = zenoh::Publisher::PutOptions::create_default(); - options.attachment = create_map_and_set_sequence_num( - sequence_number_++, entity_->copy_gid(), &source_timestamp); + options.attachment = rmw_zenoh_cpp::AttachmentData( + sequence_number_++, source_timestamp, entity_->copy_gid()).serialize_to_zbytes(); // TODO(ahcorde): shmbuf std::vector raw_data( @@ -300,10 +300,10 @@ rmw_ret_t PublisherData::publish_serialized_message( // session use different encoding formats. In our case, all key expressions // will be encoded with CDR so it does not really matter. zenoh::ZResult result; - int64_t source_timestamp = 0; + int64_t source_timestamp = rmw_zenoh_cpp::get_system_time_in_ns(); auto options = zenoh::Publisher::PutOptions::create_default(); - options.attachment = create_map_and_set_sequence_num( - sequence_number_++, entity_->copy_gid(), &source_timestamp); + options.attachment = rmw_zenoh_cpp::AttachmentData( + sequence_number_++, source_timestamp, entity_->copy_gid()).serialize_to_zbytes(); std::vector raw_data( serialized_message->buffer, diff --git a/rmw_zenoh_cpp/src/detail/rmw_service_data.cpp b/rmw_zenoh_cpp/src/detail/rmw_service_data.cpp index 7248394a..65407077 100644 --- a/rmw_zenoh_cpp/src/detail/rmw_service_data.cpp +++ b/rmw_zenoh_cpp/src/detail/rmw_service_data.cpp @@ -444,11 +444,9 @@ rmw_ret_t ServiceData::send_response( zenoh::Query::ReplyOptions options = zenoh::Query::ReplyOptions::create_default(); std::array writer_gid; memcpy(writer_gid.data(), request_id->writer_guid, RMW_GID_STORAGE_SIZE); - int64_t source_timestamp = 0; - options.attachment = create_map_and_set_sequence_num( - request_id->sequence_number, - writer_gid, - &source_timestamp); + int64_t source_timestamp = rmw_zenoh_cpp::get_system_time_in_ns(); + options.attachment = rmw_zenoh_cpp::AttachmentData( + request_id->sequence_number, source_timestamp, writer_gid).serialize_to_zbytes(); std::vector raw_bytes( reinterpret_cast(response_bytes), diff --git a/rmw_zenoh_cpp/src/detail/zenoh_utils.cpp b/rmw_zenoh_cpp/src/detail/zenoh_utils.cpp index 9e75c1f7..061ceb0f 100644 --- a/rmw_zenoh_cpp/src/detail/zenoh_utils.cpp +++ b/rmw_zenoh_cpp/src/detail/zenoh_utils.cpp @@ -26,23 +26,6 @@ namespace rmw_zenoh_cpp { -///============================================================================= -zenoh::Bytes create_map_and_set_sequence_num( - int64_t sequence_number, - std::array gid, - int64_t * source_timestamp) -{ - auto now = std::chrono::system_clock::now().time_since_epoch(); - auto now_ns = std::chrono::duration_cast(now); - int64_t timestamp = now_ns.count(); - if (nullptr != source_timestamp) { - *source_timestamp = timestamp; - } - - rmw_zenoh_cpp::AttachmentData data(sequence_number, timestamp, gid); - return data.serialize_to_zbytes(); -} - ///============================================================================= ZenohQuery::ZenohQuery( const zenoh::Query & query, @@ -87,4 +70,11 @@ std::chrono::nanoseconds::rep ZenohReply::get_received_timestamp() const { return received_timestamp_; } + +int64_t get_system_time_in_ns() +{ + auto now = std::chrono::system_clock::now().time_since_epoch(); + return std::chrono::duration_cast(now).count(); +} + } // namespace rmw_zenoh_cpp diff --git a/rmw_zenoh_cpp/src/detail/zenoh_utils.hpp b/rmw_zenoh_cpp/src/detail/zenoh_utils.hpp index afde9ca4..a88b132e 100644 --- a/rmw_zenoh_cpp/src/detail/zenoh_utils.hpp +++ b/rmw_zenoh_cpp/src/detail/zenoh_utils.hpp @@ -26,12 +26,6 @@ namespace rmw_zenoh_cpp { -///============================================================================= -zenoh::Bytes create_map_and_set_sequence_num( - int64_t sequence_number, - std::array gid, - int64_t * source_timestamp = nullptr); - ///============================================================================= // A class to store the replies to service requests. class ZenohReply final @@ -67,6 +61,8 @@ class ZenohQuery final zenoh::Query query_; std::chrono::nanoseconds::rep received_timestamp_; }; + +int64_t get_system_time_in_ns(); } // namespace rmw_zenoh_cpp #endif // DETAIL__ZENOH_UTILS_HPP_