zerodds_cli_common/
lib.rs1#![warn(missing_docs)]
18#![allow(clippy::module_name_repetitions)]
19
20use std::sync::Arc;
21use std::sync::atomic::{AtomicBool, Ordering};
22use std::time::{Duration, SystemTime, UNIX_EPOCH};
23
24use zerodds_dcps::runtime::UserReaderConfig;
25use zerodds_qos::{DeadlineQosPolicy, DurabilityKind, LivelinessQosPolicy, OwnershipKind};
26use zerodds_rtps::wire_types::GuidPrefix;
27
28#[must_use]
34pub fn stable_prefix(marker: u8) -> GuidPrefix {
35 let mut bytes = [0u8; 12];
36 let pid = std::process::id();
37 bytes[0..4].copy_from_slice(&pid.to_le_bytes());
38 let nanos = SystemTime::now()
39 .duration_since(UNIX_EPOCH)
40 .unwrap_or_default()
41 .subsec_nanos();
42 bytes[4..8].copy_from_slice(&nanos.to_le_bytes());
43 bytes[8] = marker;
44 GuidPrefix::from_bytes(bytes)
45}
46
47#[must_use]
50pub fn participant_guid(prefix: GuidPrefix) -> [u8; 16] {
51 let mut g = [0u8; 16];
52 g[..12].copy_from_slice(&prefix.0);
53 g[12..15].copy_from_slice(&[0, 0, 0]);
54 g[15] = 0xC1;
55 g
56}
57
58#[must_use]
60pub fn unix_ns_now() -> i64 {
61 let dur = SystemTime::now()
62 .duration_since(UNIX_EPOCH)
63 .unwrap_or_default();
64 let total = dur
65 .as_secs()
66 .saturating_mul(1_000_000_000)
67 .saturating_add(u64::from(dur.subsec_nanos()));
68 i64::try_from(total).unwrap_or(i64::MAX)
69}
70
71#[derive(Debug, Clone, PartialEq, Eq)]
73pub struct DurationParseError {
74 pub input: String,
76}
77
78impl std::fmt::Display for DurationParseError {
79 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80 write!(f, "invalid duration spec: {}", self.input)
81 }
82}
83
84impl std::error::Error for DurationParseError {}
85
86pub fn parse_duration(s: &str) -> Result<Duration, DurationParseError> {
91 let bad = || DurationParseError {
92 input: s.to_string(),
93 };
94 let (num, unit) = s
95 .find(|c: char| c.is_alphabetic())
96 .map_or((s, "s"), |idx| (&s[..idx], &s[idx..]));
97 let n: u64 = num.parse().map_err(|_| bad())?;
98 let secs = match unit {
99 "s" | "" => n,
100 "m" => n.checked_mul(60).ok_or_else(bad)?,
101 "h" => n.checked_mul(3600).ok_or_else(bad)?,
102 _ => return Err(bad()),
103 };
104 Ok(Duration::from_secs(secs))
105}
106
107pub fn install_signal_handler(stop: Arc<AtomicBool>) {
111 install_inner(stop);
112}
113
114#[cfg(unix)]
115fn install_inner(stop: Arc<AtomicBool>) {
116 use std::sync::Mutex;
117 static HOOK: Mutex<Option<Arc<AtomicBool>>> = Mutex::new(None);
118 if let Ok(mut g) = HOOK.lock() {
119 *g = Some(stop);
120 }
121 extern "C" fn handler(_: i32) {
122 if let Ok(g) = HOOK.lock() {
123 if let Some(s) = g.as_ref() {
124 s.store(true, Ordering::Relaxed);
125 }
126 }
127 }
128 unsafe {
131 libc::signal(libc::SIGINT, handler as usize);
132 libc::signal(libc::SIGTERM, handler as usize);
133 }
134}
135
136#[cfg(not(unix))]
137fn install_inner(_stop: Arc<AtomicBool>) {}
138
139#[must_use]
141pub fn raw_reader_config(topic: &str) -> UserReaderConfig {
142 UserReaderConfig {
143 topic_name: topic.to_string(),
144 type_name: "zerodds::RawBytes".to_string(),
145 reliable: true,
146 durability: DurabilityKind::Volatile,
147 deadline: DeadlineQosPolicy::default(),
148 liveliness: LivelinessQosPolicy::default(),
149 ownership: OwnershipKind::Shared,
150 partition: Vec::new(),
151 user_data: Vec::new(),
152 topic_data: Vec::new(),
153 group_data: Vec::new(),
154 type_identifier: zerodds_types::TypeIdentifier::None,
155 type_consistency: zerodds_types::qos::TypeConsistencyEnforcement::default(),
156 data_representation_offer: None,
157 }
158}
159
160#[cfg(test)]
161#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
162mod tests {
163 use super::*;
164
165 #[test]
166 fn parse_duration_seconds() {
167 assert_eq!(parse_duration("5").unwrap(), Duration::from_secs(5));
168 assert_eq!(parse_duration("5s").unwrap(), Duration::from_secs(5));
169 }
170
171 #[test]
172 fn parse_duration_minutes() {
173 assert_eq!(parse_duration("3m").unwrap(), Duration::from_secs(180));
174 }
175
176 #[test]
177 fn parse_duration_hours() {
178 assert_eq!(parse_duration("2h").unwrap(), Duration::from_secs(7200));
179 }
180
181 #[test]
182 fn parse_duration_rejects_garbage() {
183 assert!(parse_duration("3x").is_err());
184 assert!(parse_duration("abc").is_err());
185 }
186
187 #[test]
188 fn stable_prefix_carries_marker() {
189 let p = stable_prefix(0xAB);
190 assert_eq!(p.0[8], 0xAB);
191 }
192
193 #[test]
194 fn participant_guid_has_participant_eid() {
195 let prefix = stable_prefix(0x42);
196 let g = participant_guid(prefix);
197 assert_eq!(&g[..12], &prefix.0[..]);
198 assert_eq!(&g[12..], &[0, 0, 0, 0xC1]);
199 }
200
201 #[test]
202 fn unix_ns_now_is_positive() {
203 assert!(unix_ns_now() > 0);
204 }
205}