Skip to content
Closed
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions include/fastdds/dds/publisher/DataWriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class DataWriterListener;
class DataWriterQos;
class Topic;

struct IContentFilter;

/**
* Class DataWriter, contains the actual implementation of the behaviour of the DataWriter.
*
Expand Down Expand Up @@ -596,6 +598,21 @@ class DataWriter : public DomainEntity
FASTDDS_EXPORTED_API ReturnCode_t get_publication_builtin_topic_data(
PublicationBuiltinTopicData& publication_data) const;

/**
* @brief Set a sample prefilter to be used. This filter is always
* evaluated before sending the sample to any DataReader and prior to
* any content filtering.
*
* @param prefilter The prefilter to be set.
*
* @return RETCODE_OK if the prefilter is set correctly,
* @return RETCODE_BAD_PARAMETER if the prefilter is null.
*
* @note Prefiltering is currently incompatible with DataSharing.
*/
FASTDDS_EXPORTED_API ReturnCode_t set_sample_prefilter(
std::shared_ptr<IContentFilter> prefilter);

protected:

DataWriterImpl* impl_;
Expand Down
15 changes: 15 additions & 0 deletions include/fastdds/dds/topic/IContentFilter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <fastdds/rtps/common/Guid.hpp>
#include <fastdds/rtps/common/SampleIdentity.hpp>
#include <fastdds/rtps/common/SerializedPayload.hpp>
#include <fastdds/rtps/common/WriteParams.hpp>

namespace eprosima {
namespace fastdds {
Expand All @@ -34,6 +35,8 @@ namespace dds {
*/
struct IContentFilter
{
virtual ~IContentFilter() = default;

using SerializedPayload = eprosima::fastdds::rtps::SerializedPayload_t;
using GUID_t = fastdds::rtps::GUID_t;

Expand All @@ -45,10 +48,22 @@ struct IContentFilter
{
using SampleIdentity = eprosima::fastdds::rtps::SampleIdentity;

FilterSampleInfo() = default;

explicit FilterSampleInfo(
const rtps::WriteParams& wparams)
: sample_identity(wparams.sample_identity())
, related_sample_identity(wparams.related_sample_identity())
, user_write_data(wparams.user_write_data())
{
}

/// Identity of the sample being filtered.
SampleIdentity sample_identity;
/// Identity of a sample related to the one being filtered.
SampleIdentity related_sample_identity;
/// Extra write information that can be used by the prefilter.
std::shared_ptr<fastdds::rtps::WriteParams::UserWriteData> user_write_data;
};

/**
Expand Down
55 changes: 55 additions & 0 deletions include/fastdds/rtps/common/WriteParams.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#ifndef FASTDDS_RTPS_COMMON__WRITEPARAMS_HPP
#define FASTDDS_RTPS_COMMON__WRITEPARAMS_HPP

#include <memory>

#include <fastdds/rtps/common/SampleIdentity.hpp>
#include <fastdds/rtps/common/Time_t.hpp>

Expand All @@ -34,6 +36,21 @@ class FASTDDS_EXPORTED_API WriteParams
{
public:

/**
* @brief Base class storing custom information in the
* WriteParams structure for later filtering.
*
* This struct serves as a base class that allows derived
* classes to be deleted safely through a pointer to this base type.
* It is intended to be user-extensible.
*/
struct FASTDDS_EXPORTED_API UserWriteData
{
UserWriteData() = default;

virtual ~UserWriteData() = default;
};

/**
* Set the value of the sample_identity member.
*
Expand Down Expand Up @@ -178,6 +195,30 @@ class FASTDDS_EXPORTED_API WriteParams
return *this;
}

/**
* @brief Retrieves the user write data.
*
* @return Shared pointer to the user write data.
*/
std::shared_ptr<UserWriteData> user_write_data() const
{
return user_write_data_;
}

/**
* Set the user write data.
*
* @param write_data New value for the user_write_data member.
*
* @return Reference to the modified object in order to allow daisy chaining.
*/
WriteParams& user_write_data(
std::shared_ptr<UserWriteData> write_data)
{
user_write_data_ = write_data;
return *this;
}

static WriteParams WRITE_PARAM_DEFAULT;

/**
Expand All @@ -198,12 +239,26 @@ class FASTDDS_EXPORTED_API WriteParams

private:

class FASTDDS_EXPORTED_API UserWriteDataPtr : public std::shared_ptr<UserWriteData>
{
public:

UserWriteDataPtr(
std::shared_ptr<UserWriteData> ptr)
: std::shared_ptr<UserWriteData>(ptr)
{
}

};

/// Attribute that holds sample_identity member value
SampleIdentity sample_identity_;
/// Attribute that holds related_sample_identity member value
SampleIdentity related_sample_identity_;
/// Attribute that holds source_timestamp member value
Time_t source_timestamp_{ -1, TIME_T_INFINITE_NANOSECONDS };
/// User write data
UserWriteDataPtr user_write_data_{nullptr};
};

} // namespace rtps
Expand Down
6 changes: 6 additions & 0 deletions src/cpp/fastdds/publisher/DataWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,12 @@ ReturnCode_t DataWriter::get_publication_builtin_topic_data(
return impl_->get_publication_builtin_topic_data(publication_data);
}

ReturnCode_t DataWriter::set_sample_prefilter(
std::shared_ptr<IContentFilter> prefilter)
{
return impl_->set_sample_prefilter(prefilter);
}

} // namespace dds
} // namespace fastdds
} // namespace eprosima
48 changes: 45 additions & 3 deletions src/cpp/fastdds/publisher/DataWriterImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1500,6 +1500,30 @@ ReturnCode_t DataWriterImpl::get_publication_matched_status(
return RETCODE_OK;
}

ReturnCode_t DataWriterImpl::set_sample_prefilter(
std::shared_ptr<IContentFilter> prefilter)
{
ReturnCode_t ret_code = RETCODE_OK;
if (prefilter)
{
if (!reader_filters_)
{
// Set DataWriterImpl as the implementor
writer_->reader_data_filter(this);
}

std::lock_guard<std::mutex> lock(sample_prefilter_mutex_);
sample_prefilter_ = prefilter;
}
else
{
ret_code = RETCODE_BAD_PARAMETER;
EPROSIMA_LOG_WARNING(DATA_WRITER, "Setting a null prefilter is not allowed");
}

return ret_code;
}

bool DataWriterImpl::deadline_timer_reschedule()
{
assert(qos_.deadline().period != dds::c_TimeInfinite);
Expand Down Expand Up @@ -2372,9 +2396,27 @@ bool DataWriterImpl::is_relevant(
const fastdds::rtps::CacheChange_t& change,
const fastdds::rtps::GUID_t& reader_guid) const
{
assert(reader_filters_);
const DataWriterFilteredChange& writer_change = static_cast<const DataWriterFilteredChange&>(change);
return writer_change.is_relevant_for(reader_guid);
assert(reader_filters_ || sample_prefilter_);
bool is_relevant_for_reader = true;

{
std::lock_guard<std::mutex> lock(sample_prefilter_mutex_);
if (sample_prefilter_)
{
IContentFilter::FilterSampleInfo filter_sample_info(change.write_params);
is_relevant_for_reader = sample_prefilter_->evaluate(change.serializedPayload,
filter_sample_info,
reader_guid);
}
}

if (is_relevant_for_reader && reader_filters_)
{
const DataWriterFilteredChange& writer_change = static_cast<const DataWriterFilteredChange&>(change);
is_relevant_for_reader = writer_change.is_relevant_for(reader_guid);
}

return is_relevant_for_reader;
}

} // namespace dds
Expand Down
18 changes: 18 additions & 0 deletions src/cpp/fastdds/publisher/DataWriterImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,21 @@ class DataWriterImpl : protected rtps::IReaderDataFilter
ReturnCode_t get_publication_builtin_topic_data(
PublicationBuiltinTopicData& publication_data) const;

/**
* @brief Set a sample prefilter to be used. This filter is always
* evaluated before sending the sample to any DataReader and prior to
* any content filtering.
*
* @param prefilter The prefilter to be set.
*
* @return RETCODE_OK if the prefilter is set correctly,
* @return RETCODE_BAD_PARAMETER if the prefilter is null.
*
* @note Prefiltering is currently incompatible with DataSharing.
*/
ReturnCode_t set_sample_prefilter(
std::shared_ptr<IContentFilter> prefilter);

protected:

using IChangePool = eprosima::fastdds::rtps::IChangePool;
Expand Down Expand Up @@ -537,6 +552,9 @@ class DataWriterImpl : protected rtps::IReaderDataFilter

DataRepresentationId_t data_representation_ {DEFAULT_DATA_REPRESENTATION};

mutable std::mutex sample_prefilter_mutex_;
std::shared_ptr<IContentFilter> sample_prefilter_;

ReturnCode_t check_write_preconditions(
const void* const data,
const InstanceHandle_t& handle,
Expand Down
36 changes: 32 additions & 4 deletions test/blackbox/api/dds-pim/PubSubWriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,25 @@ class PubSubWriter

void send(
std::list<type>& msgs,
uint32_t milliseconds = 0)
uint32_t milliseconds = 0,
eprosima::fastdds::rtps::WriteParams* write_params = nullptr)
{
auto it = msgs.begin();

while (it != msgs.end())
{
if (eprosima::fastdds::dds::RETCODE_OK == datawriter_->write((void*)&(*it)))
auto ret = eprosima::fastdds::dds::RETCODE_OK;

if (nullptr != write_params)
{
ret = datawriter_->write((void*)&(*it), *write_params);
}
else
{
ret = datawriter_->write((void*)&(*it));
}

if (eprosima::fastdds::dds::RETCODE_OK == ret)
{
default_send_print<type>(*it);
it = msgs.erase(it);
Expand Down Expand Up @@ -557,10 +569,20 @@ class PubSubWriter
}

bool send_sample(
type& msg)
type& msg,
eprosima::fastdds::rtps::WriteParams* write_params = nullptr)
{
default_send_print(msg);
return (eprosima::fastdds::dds::RETCODE_OK == datawriter_->write((void*)&msg));
auto ret = eprosima::fastdds::dds::RETCODE_OK;
if (write_params != nullptr)
{
ret = datawriter_->write((void*)&msg, *write_params);
}
else
{
ret = datawriter_->write((void*)&msg);
}
return (eprosima::fastdds::dds::RETCODE_OK == ret);
}

eprosima::fastdds::dds::ReturnCode_t send_sample(
Expand Down Expand Up @@ -1858,6 +1880,12 @@ class PubSubWriter
return type_;
}

eprosima::fastdds::dds::ReturnCode_t set_sample_prefilter(
std::shared_ptr<eprosima::fastdds::dds::IContentFilter> prefilter)
{
return datawriter_->set_sample_prefilter(prefilter);
}

protected:

void participant_matched()
Expand Down
Loading