Skip to content

Commit

Permalink
size limit on PartitionQosPolicy
Browse files Browse the repository at this point in the history
  • Loading branch information
IkerLuengo committed Feb 6, 2020
1 parent 3d75fb1 commit 6725e35
Show file tree
Hide file tree
Showing 11 changed files with 324 additions and 61 deletions.
242 changes: 230 additions & 12 deletions include/fastrtps/qos/QosPolicies.h
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ class UserDataQosPolicy : public Parameter_t, public QosPolicy
}

/**
* @return the maximuim size of the user data
* @return the maximum size of the user data
*/
size_t max_size () const
{
Expand Down Expand Up @@ -798,6 +798,52 @@ class PresentationQosPolicy : public Parameter_t, public QosPolicy
};



class Partition_t {

friend class PartitionQosPolicy;


private:

const char* partition_;

private:
Partition_t()
{
partition_ = nullptr;
}

public:
explicit Partition_t(const void* ptr)
{
partition_ = (char*)ptr;
}

bool operator ==(
const Partition_t& rhs) const
{
return (size() == rhs.size() &&
(size() == 0 || strcmp(partition_ + 4, rhs.partition_ + 4)));
}

bool operator !=(
const Partition_t& rhs) const
{
return !(*this == rhs);
}

size_t size() const
{
return *(uint32_t*)partition_;
}

const char* name() const
{
return partition_ + 4;
}
};

/**
* Class PartitionQosPolicy, to indicate the Partition Qos.
*/
Expand All @@ -806,32 +852,162 @@ class PartitionQosPolicy : public Parameter_t, public QosPolicy
friend class ParameterList;
friend class rtps::EDP;

public:

class const_iterator
{
public:
typedef const_iterator self_type;
typedef const Partition_t value_type;
typedef const Partition_t reference;
typedef const Partition_t* pointer;
typedef size_t difference_type;
typedef std::forward_iterator_tag iterator_category;

const_iterator(const rtps::octet* ptr)
: ptr_(ptr)
, value_ (ptr_)
{ }

self_type operator++()
{
self_type tmp = *this;
advance();
return tmp;
}

self_type operator++(int)
{
advance();
return *this;
}

reference operator*()
{
return value_;
}

pointer operator->()
{
return &value_;
}

bool operator==(const self_type& rhs)
{
return ptr_ == rhs.ptr_;
}

bool operator!=(const self_type& rhs)
{
return ptr_ != rhs.ptr_;
}

protected:

void advance()
{
//Size of the element (with alignment)
uint32_t size = *(uint32_t*)ptr_;
ptr_ += (4 + ((size + 3) & ~3));
value_ = Partition_t(ptr_);
}

private:
const rtps::octet* ptr_;
Partition_t value_;

};

public:

RTPS_DllAPI PartitionQosPolicy()
: Parameter_t(PID_PARTITION, 0)
, QosPolicy(false)
, names{}
, max_size_ (0)
, Npartitions_ (0)
{
}

RTPS_DllAPI PartitionQosPolicy(uint16_t in_length)
: Parameter_t(PID_PARTITION, in_length)
, QosPolicy(false)
, names{}
, max_size_ (in_length)
, partitions_(in_length)
, Npartitions_ (0)
{
}

RTPS_DllAPI PartitionQosPolicy(const PartitionQosPolicy& b)
: Parameter_t(b)
, QosPolicy(b)
, max_size_ (b.max_size_)
, partitions_(b.max_size_ != 0 ?
b.partitions_.max_size :
b.partitions_.length)
, Npartitions_ (b.Npartitions_)
{
partitions_.copy(&b.partitions_, b.max_size_ != 0);
}


virtual RTPS_DllAPI ~PartitionQosPolicy()
{
}

bool operator ==(
const PartitionQosPolicy& b) const
{
return (this->names == b.names) &&
Parameter_t::operator ==(b) &&
QosPolicy::operator ==(b);
return (this->max_size_ == b.max_size_) &&
(this->Npartitions_ == b.Npartitions_) &&
(this->partitions_ == b.partitions_) &&
Parameter_t::operator==(b) &&
QosPolicy::operator==(b);
}

PartitionQosPolicy& operator =(
const PartitionQosPolicy& b)
{
length = b.length;
max_size_ = b.max_size_;
partitions_.reserve(max_size_ != 0 ?
b.partitions_.max_size :
b.partitions_.length);
partitions_.copy(&b.partitions_, b.max_size_ != 0);
Npartitions_ = b.Npartitions_;
hasChanged = true;

return *this;
}

const_iterator begin() const
{
return const_iterator(partitions_.data);
}

const_iterator end() const
{
return const_iterator(partitions_.data + partitions_.length);
}

size_t size() const
{
return Npartitions_;
}

size_t empty() const
{
return Npartitions_ == 0;
}

void max_size (size_t size)
{
partitions_.reserve(size);
max_size_ = size;
}

size_t max_size ()
{
return max_size_;
}

/**
Expand All @@ -856,23 +1032,59 @@ class PartitionQosPolicy : public Parameter_t, public QosPolicy
RTPS_DllAPI inline void push_back(
const char* name)
{
names.push_back(std::string(name)); hasChanged = true;
//Realloc if needed;
uint32_t size = strlen(name)+1;
uint32_t alignment = ((size + 3) & ~3) - size;

if (max_size_ != 0 && (partitions_.max_size < partitions_.length +
size + alignment + 4))
{
return;
}

partitions_.reserve(partitions_.length +
size + alignment + 4);

rtps::octet* o = (rtps::octet*)&size;
memcpy(partitions_.data + partitions_.length,
o, 4);
partitions_.length += 4;

memcpy(partitions_.data + partitions_.length,
name, size);
partitions_.length += size;

memset(partitions_.data + partitions_.length, 0, alignment);
partitions_.length += alignment;

++Npartitions_;
hasChanged = true;
}

/**
* Clears list of partition names
*/
RTPS_DllAPI inline void clear()
{
names.clear();
partitions_.length = 0;
Npartitions_ = 0;
hasChanged = true;
}

/**
* Returns partition names.
* @return Vector of partition name strings.
*/
RTPS_DllAPI inline std::vector<std::string> getNames() const
RTPS_DllAPI inline const std::vector<std::string> getNames() const
{
std::vector<std::string> names;
if (Npartitions_ > 0)
{
for (auto it = begin(); it != end(); ++it)
{
names.push_back(it->name());
}
}
return names;
}

Expand All @@ -883,12 +1095,18 @@ class PartitionQosPolicy : public Parameter_t, public QosPolicy
RTPS_DllAPI inline void setNames(
std::vector<std::string>& nam)
{
names = nam; hasChanged = true;
clear();
for (auto it = nam.begin(); it != nam.end(); ++it)
{
push_back(it->c_str());
}
hasChanged = true;
}

private:

std::vector<std::string> names;
uint32_t max_size_;
rtps::SerializedPayload_t partitions_;
size_t Npartitions_;
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ struct VariableLengthDataLimits
size_t max_properties = 0;
//! Defines the maximum size (in octets) of user data in the local or remote participant
size_t max_user_data = 0;
//! Defines the maximum size (in octets) of partitions data
size_t max_partitions = 0;
};

/**
Expand Down
7 changes: 7 additions & 0 deletions include/fastrtps/rtps/common/SerializedPayload.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ namespace eprosima{
this->empty();
}

bool operator== (const SerializedPayload_t& other) const
{
return ((encapsulation == other.encapsulation) &&
(length == other.length) &&
(memcmp(data, other.data, length)));
}

/*!
* Copy another structure (including allocating new space for the data.)
* @param[in] serData Pointer to the structure to copy
Expand Down
1 change: 1 addition & 0 deletions include/fastrtps/xmlparser/XMLParserCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ extern const char* PREALLOCATED_NUMBER;
extern const char* DYNAMIC_LC;
extern const char* MAX_PROPERTIES;
extern const char* MAX_USER_DATA;
extern const char* MAX_PARTITIONS;

/// Publisher-subscriber attributes
extern const char* TOPIC;
Expand Down
1 change: 1 addition & 0 deletions resources/xsd/fastRTPS_profiles.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@
<xs:element name="send_buffers" type="sendBuffersAllocationConfigType" minOccurs="0"/>
<xs:element name="max_properties" type="uint32Type" minOccurs="0"/>
<xs:element name="max_user_data" type="uint32Type" minOccurs="0"/>
<xs:element name="max_partitions" type="uint32Type" minOccurs="0"/>
</xs:all>
</xs:complexType>

Expand Down
Loading

0 comments on commit 6725e35

Please sign in to comment.