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

[20439] Expose Authentication Handshake Properties #4435

Merged
merged 9 commits into from
Mar 1, 2024
191 changes: 108 additions & 83 deletions include/fastdds/rtps/attributes/PropertyPolicy.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,93 +28,118 @@ namespace rtps {

class PropertyPolicy
{
public:
RTPS_DllAPI PropertyPolicy() {}

RTPS_DllAPI PropertyPolicy(const PropertyPolicy& property_policy) :
properties_(property_policy.properties_),
binary_properties_(property_policy.binary_properties_) {}

RTPS_DllAPI PropertyPolicy(PropertyPolicy&& property_policy) :
properties_(std::move(property_policy.properties_)),
binary_properties_(std::move(property_policy.binary_properties_)) {}

RTPS_DllAPI PropertyPolicy& operator=(const PropertyPolicy& property_policy)
{
properties_ = property_policy.properties_;
binary_properties_ = property_policy.binary_properties_;
return *this;
}

RTPS_DllAPI PropertyPolicy& operator=(PropertyPolicy&& property_policy)
{
properties_ = std::move(property_policy.properties_);
binary_properties_= std::move(property_policy.binary_properties_);
return *this;
}

RTPS_DllAPI bool operator==(const PropertyPolicy& b) const
{
return (this->properties_ == b.properties_) &&
(this->binary_properties_ == b.binary_properties_);
}

//!Get properties
RTPS_DllAPI const PropertySeq& properties() const
{
return properties_;
}

//!Set properties
RTPS_DllAPI PropertySeq& properties()
{
return properties_;
}

//!Get binary_properties
RTPS_DllAPI const BinaryPropertySeq& binary_properties() const
{
return binary_properties_;
}

//!Set binary_properties
RTPS_DllAPI BinaryPropertySeq& binary_properties()
{
return binary_properties_;
}

private:
PropertySeq properties_;

BinaryPropertySeq binary_properties_;
public:

RTPS_DllAPI PropertyPolicy()
{
}

RTPS_DllAPI PropertyPolicy(
const PropertyPolicy& property_policy)
: properties_(property_policy.properties_)
, binary_properties_(property_policy.binary_properties_)
{
}

RTPS_DllAPI PropertyPolicy(
PropertyPolicy&& property_policy)
: properties_(std::move(property_policy.properties_))
, binary_properties_(std::move(property_policy.binary_properties_))
{
}

RTPS_DllAPI PropertyPolicy& operator =(
const PropertyPolicy& property_policy)
{
properties_ = property_policy.properties_;
binary_properties_ = property_policy.binary_properties_;
return *this;
}

RTPS_DllAPI PropertyPolicy& operator =(
PropertyPolicy&& property_policy)
{
properties_ = std::move(property_policy.properties_);
binary_properties_ = std::move(property_policy.binary_properties_);
return *this;
}

RTPS_DllAPI bool operator ==(
const PropertyPolicy& b) const
{
return (this->properties_ == b.properties_) &&
(this->binary_properties_ == b.binary_properties_);
}

//!Get properties
RTPS_DllAPI const PropertySeq& properties() const
{
return properties_;
}

//!Set properties
RTPS_DllAPI PropertySeq& properties()
{
return properties_;
}

//!Get binary_properties
RTPS_DllAPI const BinaryPropertySeq& binary_properties() const
{
return binary_properties_;
}

//!Set binary_properties
RTPS_DllAPI BinaryPropertySeq& binary_properties()
{
return binary_properties_;
}

private:

PropertySeq properties_;

BinaryPropertySeq binary_properties_;
};

class PropertyPolicyHelper
{
public:
/*!
* @brief Returns only the properties whose name starts with the prefix.
* Prefix is removed in returned properties.
* @param property_policy PropertyPolicy where properties will be searched.
* @param prefix Prefix used to search properties.
* @return A copy of properties whose name starts with the prefix.
*/
RTPS_DllAPI static PropertyPolicy get_properties_with_prefix(
const PropertyPolicy& property_policy,
const std::string& prefix);

//!Get the length of the property_policy
RTPS_DllAPI static size_t length(const PropertyPolicy& property_policy);

//!Look for a property_policy by name
RTPS_DllAPI static std::string* find_property(
PropertyPolicy& property_policy,
const std::string& name);

//!Retrieves a property_policy by name
RTPS_DllAPI static const std::string* find_property(
const PropertyPolicy& property_policy,
const std::string& name);
public:

/*!
* @brief Returns only the properties whose name starts with the prefix.
* Prefix is removed in returned properties.
* @param property_policy PropertyPolicy where properties will be searched.
* @param prefix Prefix used to search properties.
* @return A copy of properties whose name starts with the prefix.
*/
RTPS_DllAPI static PropertyPolicy get_properties_with_prefix(
const PropertyPolicy& property_policy,
const std::string& prefix);

//!Get the length of the property_policy
RTPS_DllAPI static size_t length(
const PropertyPolicy& property_policy);

//!Look for a property_policy by name
RTPS_DllAPI static std::string* find_property(
PropertyPolicy& property_policy,
const std::string& name);

//!Retrieves a property_policy by name
RTPS_DllAPI static const std::string* find_property(
const PropertyPolicy& property_policy,
const std::string& name);

/**
* @brief Retrieves a property by name
* @param property_policy PropertyPolicy where the property will be searched.
* @param name Name of the property to be searched.
* @return A pointer to the property if found, nullptr otherwise.
*/
RTPS_DllAPI static const Property* get_property(
const PropertyPolicy& property_policy,
const std::string& name);
};

} //namespace rtps
Expand Down
126 changes: 126 additions & 0 deletions include/fastdds/rtps/common/Property.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
#ifndef _FASTDDS_RTPS_COMMON_PROPERTYQOS_H_
#define _FASTDDS_RTPS_COMMON_PROPERTYQOS_H_

#include <functional>
#include <stdexcept>
#include <string>
#include <vector>

#include <fastdds/rtps/exceptions/Exception.h>

namespace eprosima {
namespace fastrtps {
namespace rtps {
Expand Down Expand Up @@ -214,6 +218,128 @@ class PropertyHelper

};

struct PropertyParser
{

/**
* @brief Parse a property value as an integer
* @param property Property to parse
* @param check_upper_bound If true, check that the value is lower than upper_bound
* @param upper_bound Upper bound to check
* @param check_lower_bound If true, check that the value is greater than lower_bound
* @param lower_bound Lower bound to check
* @param exception Exception to throw if the value is not a valid integer or if it is out of bounds
* @return The parsed integer value
*
* @warning May throw an exception_t if the value is not a valid integer
* or if it is out of bounds.
*/
template<typename exception_t>
inline static int as_int(
const Property& property,
const bool& check_upper_bound,
const int& upper_bound,
const bool& check_lower_bound,
const int& lower_bound,
const exception_t& exception)
{
return parse_value(
std::function<int(const Property& property)>(
[](const Property& property)
{
return std::stoi(property.value());
}
),
property,
check_upper_bound,
upper_bound,
check_lower_bound,
lower_bound,
exception);
}

/**
* @brief Parse a property value as a double
* @param property Property to parse
* @param check_upper_bound If true, check that the value is lower than upper_bound
* @param upper_bound Upper bound to check
* @param check_lower_bound If true, check that the value is greater than lower_bound
* @param lower_bound Lower bound to check
* @param exception Exception to throw if the value is not a valid double or if it is out of bounds
* @return The parsed double value
*
* @warning May throw an exception_t if the value is not a valid double
* or if it is out of bounds.
*/
template<typename exception_t>
inline static double as_double(
const Property& property,
const bool& check_upper_bound,
const double& upper_bound,
const bool& check_lower_bound,
const double& lower_bound,
const exception_t& exception)
{
return parse_value(
std::function<double(const Property& property)>(
[](const Property& property)
{
return std::stod(property.value());
}
),
property,
check_upper_bound,
upper_bound,
check_lower_bound,
lower_bound,
exception);
}

private:

template <typename value_t,
typename exception_t>
inline static value_t parse_value(
const std::function<value_t(const Property&)>& conversor,
const Property& property,
const bool& check_upper_bound,
const value_t& upper_bound,
const bool& check_lower_bound,
const value_t& lower_bound,
const exception_t& exception)
{
try
{
value_t converted_value = conversor(property);

if (check_lower_bound && converted_value < lower_bound)
{
throw exception_t("Value '" + property.value() +
"' for " + property.name() + " must be greater or equal to " +
std::to_string(lower_bound));
}

if (check_upper_bound && converted_value > upper_bound)
{
throw exception_t("Value '" + property.value() +
"' for " + property.name() + " must be lower or equal to " +
std::to_string(upper_bound));
}

return converted_value;
}
catch (const std::invalid_argument&)
{
throw exception;
}
catch (const std::out_of_range&)
{
throw exception;
}
}

};

} //namespace eprosima
} //namespace fastrtps
} //namespace rtps
Expand Down
Loading
Loading