cyclonedds_rs/
dds_domain.rs

1/*
2    Copyright 2020 Sojan James
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15*/
16
17
18use cyclonedds_sys::{dds_error::DDSError, DdsDomainId, DdsEntity};
19use std::convert::From;
20use std::ffi::CString;
21
22pub struct DdsDomain(DdsEntity);
23
24impl DdsDomain {
25    ///Create a domain with a specified domain id
26    pub fn create(domain: DdsDomainId, config: Option<&str>) -> Result<Self, DDSError> {
27        unsafe {
28            if let Some(cfg) = config {
29                let domain_name = CString::new(cfg).expect("Unable to create new config string");
30                let d = cyclonedds_sys::dds_create_domain(domain, domain_name.as_ptr());
31                // negative return value signify an error
32                if d > 0 {
33                    Ok(DdsDomain(DdsEntity::new(d)))
34                } else {
35                    Err(DDSError::from(d))
36                }
37            } else {
38                let d = cyclonedds_sys::dds_create_domain(domain, std::ptr::null());
39
40                if d > 0 {
41                    Ok(DdsDomain(DdsEntity::new(d)))
42                } else {
43                    Err(DDSError::from(d))
44                }
45            }
46        }
47    }
48}
49
50impl PartialEq for DdsDomain {
51    fn eq(&self, other: &Self) -> bool {
52        unsafe { self.0.entity() == other.0.entity() }
53    }
54}
55
56impl Eq for DdsDomain {}
57
58impl Drop for DdsDomain {
59    fn drop(&mut self) {
60        unsafe {
61            let ret: DDSError = cyclonedds_sys::dds_delete(self.0.entity()).into();
62            if DDSError::DdsOk != ret {
63                panic!("cannot delete domain: {}", ret);
64            }
65        }
66    }
67}
68
69#[cfg(test)]
70mod dds_domain_tests {
71    use cyclonedds_sys::{DDSError};
72    use crate::dds_domain::DdsDomain;
73
74    #[test]
75    fn test_create_domain_with_bad_config() {
76        assert!(Err(DDSError::DdsOk) != DdsDomain::create(0, Some("blah")));
77    }
78    
79    
80    
81}