dimas_com/traits/
communicator.rs1#[doc(hidden)]
7extern crate alloc;
8
9#[cfg(feature = "std")]
10extern crate std;
11
12use super::LivelinessSubscriber;
14use super::{CommunicatorMethods, Observer, Publisher, Querier, Responder};
15use crate::error::Error;
16use alloc::{boxed::Box, string::String, sync::Arc, vec::Vec};
17use dimas_core::{enums::OperationState, error::Result, traits::Capability};
18#[cfg(feature = "std")]
19use std::{collections::HashMap, sync::RwLock};
20use tracing::error;
21use zenoh::Session;
22pub trait Communicator: Capability + CommunicatorMethods + Send + Sync {
27 #[must_use]
29 fn liveliness_subscribers(&self)
30 -> Arc<RwLock<HashMap<String, Box<dyn LivelinessSubscriber>>>>;
31
32 #[must_use]
34 fn observers(&self) -> Arc<RwLock<HashMap<String, Box<dyn Observer>>>>;
35
36 #[must_use]
38 fn publishers(&self) -> Arc<RwLock<HashMap<String, Box<dyn Publisher>>>>;
39
40 #[must_use]
42 fn queriers(&self) -> Arc<RwLock<HashMap<String, Box<dyn Querier>>>>;
43
44 #[must_use]
46 fn responders(&self) -> Arc<RwLock<HashMap<String, Box<dyn Responder>>>>;
47
48 fn upgrade_capabilities(&self, new_state: &OperationState) -> Result<()> {
60 self.liveliness_subscribers()
62 .write()
63 .map_err(|_| Error::ModifyStruct("liveliness subscribers".into()))?
64 .iter_mut()
65 .for_each(|subscriber| {
66 let _ = subscriber.1.manage_operation_state(new_state);
67 });
68
69 self.responders()
71 .write()
72 .map_err(|_| Error::ModifyStruct("subscribers".into()))?
73 .iter_mut()
74 .for_each(|subscriber| {
75 let _ = subscriber.1.manage_operation_state(new_state);
76 });
77
78 self.publishers()
80 .write()
81 .map_err(|_| Error::ModifyStruct("publishers".into()))?
82 .iter_mut()
83 .for_each(|publisher| {
84 if let Err(reason) = publisher.1.manage_operation_state(new_state) {
85 error!(
86 "could not initialize publisher for {}, reason: {}",
87 publisher.1.selector(),
88 reason
89 );
90 }
91 });
92
93 self.observers()
95 .write()
96 .map_err(|_| Error::ModifyStruct("observers".into()))?
97 .iter_mut()
98 .for_each(|observer| {
99 if let Err(reason) = observer.1.manage_operation_state(new_state) {
100 error!(
101 "could not initialize observer for {}, reason: {}",
102 observer.1.selector(),
103 reason
104 );
105 }
106 });
107
108 self.queriers()
110 .write()
111 .map_err(|_| Error::ModifyStruct("queries".into()))?
112 .iter_mut()
113 .for_each(|query| {
114 if let Err(reason) = query.1.manage_operation_state(new_state) {
115 error!(
116 "could not initialize query for {}, reason: {}",
117 query.1.selector(),
118 reason
119 );
120 }
121 });
122
123 Ok(())
124 }
125
126 fn downgrade_capabilities(&self, new_state: &OperationState) -> Result<()> {
133 self.queriers()
136 .write()
137 .map_err(|_| Error::ModifyStruct("queries".into()))?
138 .iter_mut()
139 .for_each(|query| {
140 if let Err(reason) = query.1.manage_operation_state(new_state) {
141 error!(
142 "could not de-initialize query for {}, reason: {}",
143 query.1.selector(),
144 reason
145 );
146 }
147 });
148
149 self.observers()
151 .write()
152 .map_err(|_| Error::ModifyStruct("observers".into()))?
153 .iter_mut()
154 .for_each(|observer| {
155 if let Err(reason) = observer.1.manage_operation_state(new_state) {
156 error!(
157 "could not de-initialize observer for {}, reason: {}",
158 observer.1.selector(),
159 reason
160 );
161 }
162 });
163
164 self.publishers()
166 .write()
167 .map_err(|_| Error::ModifyStruct("publishers".into()))?
168 .iter_mut()
169 .for_each(|publisher| {
170 let _ = publisher.1.manage_operation_state(new_state);
171 });
172
173 self.responders()
175 .write()
176 .map_err(|_| Error::ModifyStruct("subscribers".into()))?
177 .iter_mut()
178 .for_each(|subscriber| {
179 let _ = subscriber.1.manage_operation_state(new_state);
180 });
181
182 self.liveliness_subscribers()
184 .write()
185 .map_err(|_| Error::ModifyStruct("liveliness subscribers".into()))?
186 .iter_mut()
187 .for_each(|subscriber| {
188 let _ = subscriber.1.manage_operation_state(new_state);
189 });
190
191 Ok(())
192 }
193
194 #[must_use]
196 fn uuid(&self) -> String;
197
198 #[must_use]
200 fn mode(&self) -> &String;
201
202 fn default_session(&self) -> Arc<Session>;
204
205 fn session(&self, id: &str) -> Option<Arc<Session>>;
207
208 fn sessions(&self) -> Vec<Arc<Session>>;
210}