假如DataWriter先起來,并且已經寫了一些數據,之后有新的DataReader起來,那么新起來的DataReader能不能接收到它啟動之前,DataWriter發布的數據呢。DurabilityQosPolicy用來做這種控制。
VOLATILE_DURABILITY_QOS:易失的,新上線的DataReader只能讀取上線之后,DataWriter發布的數據。
TRANSIENT_LOCAL_DURABILITY_QOS:會在內存中保存數據,新上線的DataReader也能讀取上線之前的數據,前提是DataWriter還在,如果這個時候DataWriter已經退出了,那么DataReader就無法獲取到。
/*** Enum DurabilityQosPolicyKind_t, different kinds of durability for DurabilityQosPolicy.*/
typedef enum DurabilityQosPolicyKind : fastdds::rtps::octet
{/*** The Service does not need to keep any samples of data-instances on behalf of any DataReader that is not* known by the DataWriter at the time the instance is written. In other words the Service will only attempt* to provide the data to existing subscribers*/VOLATILE_DURABILITY_QOS,/*** For TRANSIENT_LOCAL, the service is only required to keep the data in the memory of the DataWriter that* wrote the data and the data is not required to survive the DataWriter.*/TRANSIENT_LOCAL_DURABILITY_QOS,/*** For TRANSIENT, the service is only required to keep the data in memory and not in permanent storage; but* the data is not tied to the lifecycle of the DataWriter and will, in general, survive it.*/TRANSIENT_DURABILITY_QOS,/*** Data is kept on permanent storage, so that they can outlive a system session.** @warning Not Supported*/PERSISTENT_DURABILITY_QOS
} DurabilityQosPolicyKind_t;
1hello world
仍然以fastdds自帶的hello world做為例子進行測試。
默認情況下,qos類型為VOLATILE_DURABILITY_QOS,即易失的。如果直接像下邊這樣進行修改,不生效的,因為DurabilityQosPolicy的使用要同時結合HistoryQosPolicy和ResourceLimitsQosPolicy以及ReliabilityQosPolicy進行統一配置。
- ReliabilityQosPolicyKind需要配置可靠類型
- HistoryQosPolicy配置KEEP_LAST_HISTORY_QOS,則depth需要大于0;或者配置為KEEP_ALL_HISTORY_QOS
- ResourceLimitsQosPolicy中ResourceLimitsQosPolicy不可小于HistoryQosPolicy中的depth
? ? DataWriterQos writer_qos = DATAWRITER_QOS_DEFAULT;
publisher_->get_default_datawriter_qos(writer_qos);? ? auto max_samples = 500;
writer_qos.reliability().kind = ReliabilityQosPolicyKind::RELIABLE_RELIABILITY_QOS;
writer_qos.durability().kind = DurabilityQosPolicyKind::TRANSIENT_LOCAL_DURABILITY_QOS;
writer_qos.history().kind = HistoryQosPolicyKind::KEEP_LAST_HISTORY_QOS;
writer_qos.history().depth = max_samples;
writer_qos.resource_limits().ResourceLimitsQosPolicy= max_samples;
writer_qos.resource_limits().max_samples = writer_qos.resource_limits().max_instances * max_samples;
writer_ = publisher_->create_datawriter(topic_, writer_qos, this, StatusMask::all());