use std::convert::From;
pub use cyclonedds_sys::{DDSError, DdsDomainId, DdsEntity};
use crate::{DdsReadable, DdsWritable, Entity, dds_listener::DdsListener, dds_qos::DdsQos};
pub struct ParticipantBuilder {
maybe_domain : Option<DdsDomainId>,
maybe_qos : Option<DdsQos>,
maybe_listener : Option<DdsListener>,
}
impl ParticipantBuilder {
pub fn new() -> Self {
ParticipantBuilder {
maybe_domain: None,
maybe_qos: None,
maybe_listener: None,
}
}
pub fn with_domain(mut self, domain: DdsDomainId) -> Self {
self.maybe_domain = Some(domain);
self
}
pub fn with_qos(mut self, qos : DdsQos) -> Self {
self.maybe_qos = Some(qos);
self
}
pub fn with_listener(mut self, listener: DdsListener) -> Self {
self.maybe_listener = Some(listener);
self
}
pub fn create(self) -> Result<DdsParticipant, DDSError> {
DdsParticipant::create(self.maybe_domain, self.maybe_qos, self.maybe_listener)
}
}
pub struct DdsParticipant(DdsEntity, Option<DdsListener>);
impl DdsParticipant {
pub fn create(
maybe_domain: Option<DdsDomainId>,
maybe_qos: Option<DdsQos>,
maybe_listener: Option<DdsListener>,
) -> Result<Self, DDSError> {
unsafe {
let p = cyclonedds_sys::dds_create_participant(
maybe_domain.unwrap_or(0xFFFF_FFFF),
maybe_qos.map_or(std::ptr::null(), |d| d.into()),
maybe_listener.as_ref().map_or(std::ptr::null(), |l| l.into()),
);
if p > 0 {
Ok(DdsParticipant(DdsEntity::new(p), maybe_listener))
} else {
Err(DDSError::from(p))
}
}
}
}
impl DdsWritable for DdsParticipant {
fn entity(&self) -> &DdsEntity {
&self.0
}
}
impl DdsReadable for DdsParticipant {
fn entity(&self) -> &DdsEntity {
&self.0
}
}
impl Entity for DdsParticipant {
fn entity(&self) -> &DdsEntity {
&self.0
}
}
#[cfg(test)]
mod dds_participant_tests {
use super::*;
#[test]
fn test_create() {
let mut qos = DdsQos::create().unwrap();
qos.set_lifespan(std::time::Duration::from_nanos(1000));
let _par = DdsParticipant::create(None, Some(qos), None);
}
}