use rustorb_dds::{DataReaderQos, DataWriterQos, DurabilityKind, History, Reliability};
use crate::{Error, Result};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct QosProfile {
pub reliable: bool,
pub transient_local: bool,
pub depth: Option<usize>,
}
impl QosProfile {
pub const DEFAULT: QosProfile = QosProfile {
reliable: true,
transient_local: false,
depth: Some(10),
};
pub const SENSOR_DATA: QosProfile = QosProfile {
reliable: false,
transient_local: false,
depth: Some(5),
};
pub const SERVICES_DEFAULT: QosProfile = QosProfile::DEFAULT;
pub const PARAMETERS: QosProfile = QosProfile {
reliable: true,
transient_local: false,
depth: Some(1000),
};
pub const PARAMETER_EVENTS: QosProfile = QosProfile::PARAMETERS;
pub const SYSTEM_DEFAULT: QosProfile = QosProfile {
reliable: true,
transient_local: false,
depth: Some(1),
};
pub const ROSOUT: QosProfile = QosProfile {
reliable: true,
transient_local: true,
depth: Some(1000),
};
pub const CLOCK: QosProfile = QosProfile {
reliable: false,
transient_local: false,
depth: Some(1),
};
pub const TRANSIENT_LOCAL: QosProfile = QosProfile {
reliable: true,
transient_local: true,
depth: Some(1),
};
pub fn from_name(name: &str) -> Result<QosProfile> {
Ok(match name {
"default" => QosProfile::DEFAULT,
"sensor_data" => QosProfile::SENSOR_DATA,
"services_default" => QosProfile::SERVICES_DEFAULT,
"parameters" => QosProfile::PARAMETERS,
"parameter_events" => QosProfile::PARAMETER_EVENTS,
"system_default" => QosProfile::SYSTEM_DEFAULT,
"rosout" => QosProfile::ROSOUT,
"clock" => QosProfile::CLOCK,
"transient_local" => QosProfile::TRANSIENT_LOCAL,
_ => {
return Err(Error::Name(format!(
"QoS profile {name:?}: use default, sensor_data, services_default, \
parameters, parameter_events, system_default, rosout, clock or \
transient_local"
)));
}
})
}
pub fn with_depth(mut self, depth: usize) -> QosProfile {
self.depth = Some(depth);
self
}
fn parts(&self) -> (Reliability, DurabilityKind, History) {
(
if self.reliable {
Reliability::reliable()
} else {
Reliability::best_effort()
},
if self.transient_local {
DurabilityKind::TransientLocal
} else {
DurabilityKind::Volatile
},
match self.depth {
Some(d) => History::KeepLast(d),
None => History::KeepAll,
},
)
}
pub fn writer_qos(&self, user_data: Vec<u8>) -> DataWriterQos {
let (reliability, durability, history) = self.parts();
DataWriterQos {
reliability,
durability,
history,
user_data,
..DataWriterQos::default()
}
}
pub fn reader_qos(&self, user_data: Vec<u8>) -> DataReaderQos {
let (reliability, durability, history) = self.parts();
DataReaderQos {
reliability,
durability,
history,
user_data,
..DataReaderQos::default()
}
}
}
impl Default for QosProfile {
fn default() -> Self {
QosProfile::DEFAULT
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn names() {
assert_eq!(
QosProfile::from_name("default").unwrap(),
QosProfile::DEFAULT
);
let s = QosProfile::from_name("sensor_data").unwrap();
assert!(!s.reliable);
assert_eq!(s.reader_qos(Vec::new()).history, History::KeepLast(5));
let t = QosProfile::from_name("transient_local").unwrap();
assert_eq!(
t.writer_qos(b"x".to_vec()).durability,
DurabilityKind::TransientLocal
);
assert!(QosProfile::from_name("best_available").is_err());
assert_eq!(QosProfile::DEFAULT.with_depth(3).depth, Some(3));
}
}