fastdds中的domain participant、topic、publisher、subscriber、data writer、data reader均可以设置qos。qos几乎是dds配置的代名词了,所有的配置都在qos中。
设置qos,可以在代码中指定,也可以通过xml格式的配置文件来指定。如下是HistoryQosPolicy的配置方式:
代码:
// This example uses a DataWriter, but it can also be applied to DataReader and Topic entities
DataWriterQos writer_qos;
// The HistoryQosPolicy is constructed with kind = KEEP_LAST and depth = 1 by default
// It is possible to adjust the depth and keep the kind as KEEP_LAST
writer_qos.history().depth = 20;
// Or you can also change the kind to KEEP_ALL (depth will not be used).
writer_qos.history().kind = KEEP_ALL_HISTORY_QOS;
// Use modified QoS in the creation of the corresponding entity
writer_ = publisher_->create_datawriter(topic_, writer_qos);
xml:
<topic>
<historyQos>
<kind>KEEP_LAST</kind> <!-- string -->
<depth>20</depth> <!-- uint32 -->
</historyQos>
</topic>
1participant qos
(1)create_participant
需要传入qos
①如果传入的直接是PARTICIPANT_QOS_DEFAULT,那么如果xml文件中有qos profile设置了is_default_profile为true,则使用xml中的qos;否则使用PARTICIPANT_QOS_DEFAULT。
②如下代码所示,通过PARTICIPANT_QOS_DEFAULT构造了pqos,pqos不做任何修改,内容和PARTICIPANT_QOS_DEFAULT是完全一样的,那么即使xml中有qos profile设置了is_default_profile为true,也不会使用xml中的配置,而是使用pqos的配置。
cpp
DomainParticipantQos pqos = PARTICIPANT_QOS_DEFAULT;
pqos.name("static_edp_delivery_pub_participant");
auto factory = DomainParticipantFactory::get_instance();
if (RETCODE_OK != factory->check_xml_static_discovery(xml_file_name))
{
throw std::runtime_error("Error parsing publisher xml file");
}
participant_ = factory->create_participant(config.domain, pqos, nullptr, StatusMask::none());
(2)create_participant_with_default_profile
如果xml中有profile配置了is_default_profile为true,则使用xml配置;否则,使用PARTICIPANT_QOS_DEFAULT。
(3)create_participant_with_profile
指定profile名,如果xml不存在该profile,则创建失败。
2topic、subscriber、publisher
创建topic、subscriber、publisher,也有两个创建api:create_xxx,create_xxx_with_profile。
topic、subscriber、publisher均是域参与者的资源,在DomainParticipant中创建。
data writer和data reader也有类似的两个创建的api,data writer属于publisher的资源,data reader属于subscriber的资源。
cpp
/**
* Create a Publisher in this Participant.
*
* @param qos QoS of the Publisher.
* @param listener Pointer to the listener (default: nullptr)
* @param mask StatusMask that holds statuses the listener responds to (default: all)
* @return Pointer to the created Publisher.
*/
FASTDDS_EXPORTED_API Publisher* create_publisher(
const PublisherQos& qos,
PublisherListener* listener = nullptr,
const StatusMask& mask = StatusMask::all());
/**
* Create a Publisher in this Participant.
*
* @param profile_name Publisher profile name.
* @param listener Pointer to the listener (default: nullptr)
* @param mask StatusMask that holds statuses the listener responds to (default: all)
* @return Pointer to the created Publisher.
*/
FASTDDS_EXPORTED_API Publisher* create_publisher_with_profile(
const std::string& profile_name,
PublisherListener* listener = nullptr,
const StatusMask& mask = StatusMask::all());
/**
* Create a Subscriber in this Participant.
*
* @param qos QoS of the Subscriber.
* @param listener Pointer to the listener (default: nullptr)
* @param mask StatusMask that holds statuses the listener responds to (default: all)
* @return Pointer to the created Subscriber.
*/
FASTDDS_EXPORTED_API Subscriber* create_subscriber(
const SubscriberQos& qos,
SubscriberListener* listener = nullptr,
const StatusMask& mask = StatusMask::all());
/**
* Create a Subscriber in this Participant.
*
* @param profile_name Subscriber profile name.
* @param listener Pointer to the listener (default: nullptr)
* @param mask StatusMask that holds statuses the listener responds to (default: all)
* @return Pointer to the created Subscriber.
*/
FASTDDS_EXPORTED_API Subscriber* create_subscriber_with_profile(
const std::string& profile_name,
SubscriberListener* listener = nullptr,
const StatusMask& mask = StatusMask::all());
/**
* Create a Topic in this Participant.
*
* @param topic_name Name of the Topic.
* @param type_name Data type of the Topic.
* @param qos QoS of the Topic.
* @param listener Pointer to the listener (default: nullptr)
* @param mask StatusMask that holds statuses the listener responds to (default: all)
* @return Pointer to the created Topic.
*/
FASTDDS_EXPORTED_API Topic* create_topic(
const std::string& topic_name,
const std::string& type_name,
const TopicQos& qos,
TopicListener* listener = nullptr,
const StatusMask& mask = StatusMask::all());
/**
* Create a Topic in this Participant.
*
* @param topic_name Name of the Topic.
* @param type_name Data type of the Topic.
* @param profile_name Topic profile name.
* @param listener Pointer to the listener (default: nullptr)
* @param mask StatusMask that holds statuses the listener responds to (default: all)
* @return Pointer to the created Topic.
*/
FASTDDS_EXPORTED_API Topic* create_topic_with_profile(
const std::string& topic_name,
const std::string& type_name,
const std::string& profile_name,
TopicListener* listener = nullptr,
const StatusMask& mask = StatusMask::all());
3判断profile是否存在
在实际开发中,首先判断某个profile是否存在,如果存在则使用该profile,如果不存在,则使用默认qos,可以通过如下api来判断。
get_participant_qos_from_profile(name,qos)
get_publisher_qos_from_profile(name, qos)
get_subscriber_qos_from_profile(name, qos)
get_datawriter_qos_from_profile(name, qos)
get_datareader_qos_from_profile(name, qos)
get_topic_qos_from_profile(name, qos)