flare_core/client/builder/
observer.rs1use crate::client::HybridClient;
22use crate::client::builder::{BaseClientBuilderConfig, ClientWrapper};
23use crate::common::error::Result;
24use crate::common::protocol::Frame;
25use crate::transport::events::ConnectionObserver;
26use std::sync::Arc;
27
28pub struct ObserverClientBuilder {
42 base: BaseClientBuilderConfig,
43 observer: Option<Arc<dyn ConnectionObserver>>,
44 event_handler: Option<Arc<dyn crate::client::events::handler::ClientEventHandler>>,
45}
46
47impl ObserverClientBuilder {
48 pub fn new(server_url: impl Into<String>) -> Self {
50 Self {
51 base: BaseClientBuilderConfig::new(server_url),
52 observer: None,
53 event_handler: None,
54 }
55 }
56
57 pub fn with_event_handler(
59 mut self,
60 event_handler: Arc<dyn crate::client::events::handler::ClientEventHandler>,
61 ) -> Self {
62 self.event_handler = Some(event_handler);
63 self
64 }
65
66 pub fn with_observer(mut self, observer: Arc<dyn ConnectionObserver>) -> Self {
68 self.observer = Some(observer);
69 self
70 }
71
72 pub fn with_protocol(
74 mut self,
75 protocol: crate::common::config_types::TransportProtocol,
76 ) -> Self {
77 self.base = self.base.with_protocol(protocol);
78 self
79 }
80
81 pub fn with_protocol_race(
85 mut self,
86 protocols: Vec<crate::common::config_types::TransportProtocol>,
87 ) -> Self {
88 self.base = self.base.with_protocol_race(protocols);
89 self
90 }
91
92 pub fn with_protocol_url(
94 mut self,
95 protocol: crate::common::config_types::TransportProtocol,
96 url: String,
97 ) -> Self {
98 self.base = self.base.with_protocol_url(protocol, url);
99 self
100 }
101
102 pub fn with_user_id(mut self, user_id: String) -> Self {
104 self.base = self.base.with_user_id(user_id);
105 self
106 }
107
108 pub fn with_format(mut self, format: crate::common::protocol::SerializationFormat) -> Self {
110 self.base = self.base.with_format(format);
111 self
112 }
113
114 pub fn with_compression(
116 mut self,
117 compression: crate::common::compression::CompressionAlgorithm,
118 ) -> Self {
119 self.base = self.base.with_compression(compression);
120 self
121 }
122
123 pub fn with_heartbeat(
125 mut self,
126 heartbeat: crate::common::config_types::HeartbeatConfig,
127 ) -> Self {
128 self.base = self.base.with_heartbeat(heartbeat);
129 self
130 }
131
132 pub fn with_tls(mut self, tls: crate::common::config_types::TlsConfig) -> Self {
134 self.base = self.base.with_tls(tls);
135 self
136 }
137
138 pub fn with_connect_timeout(mut self, timeout: std::time::Duration) -> Self {
140 self.base = self.base.with_connect_timeout(timeout);
141 self
142 }
143
144 pub fn with_reconnect_interval(mut self, interval: std::time::Duration) -> Self {
146 self.base = self.base.with_reconnect_interval(interval);
147 self
148 }
149
150 pub fn with_max_reconnect_attempts(mut self, max: Option<u32>) -> Self {
152 self.base = self.base.with_max_reconnect_attempts(max);
153 self
154 }
155
156 pub fn enable_router(mut self) -> Self {
160 self.base = self.base.enable_router();
161 self
162 }
163
164 pub fn with_device_info(mut self, device_info: crate::common::device::DeviceInfo) -> Self {
166 self.base = self.base.with_device_info(device_info);
167 self
168 }
169
170 pub fn with_token(mut self, token: String) -> Self {
172 self.base = self.base.with_token(token);
173 self
174 }
175
176 pub async fn build_with_race(self) -> Result<ObserverClient> {
178 let observer = self.observer.ok_or_else(|| {
179 crate::common::error::FlareError::general_error("Observer is required")
180 })?;
181
182 let client = HybridClient::connect_with_race(self.base.config).await?;
183 let wrapper = ClientWrapper::new(client);
184
185 if let Some(event_handler) = self.event_handler {
187 wrapper.set_event_handler(Some(event_handler)).await;
188 }
189
190 wrapper
191 .add_observer(Arc::clone(&observer) as Arc<dyn ConnectionObserver>)
192 .await;
193
194 Ok(ObserverClient {
195 wrapper,
196 observer: Some(observer),
197 })
198 }
199
200 pub fn build(self) -> Result<ObserverClient> {
202 let observer = self.observer.ok_or_else(|| {
203 crate::common::error::FlareError::general_error("Observer is required")
204 })?;
205
206 let mut client = HybridClient::new(self.base.config)?;
207
208 if let Some(event_handler) = self.event_handler {
210 client.core_mut().set_event_handler(Some(event_handler));
211 }
212
213 let wrapper = ClientWrapper::new(client);
214
215 Ok(ObserverClient {
216 wrapper,
217 observer: Some(observer),
218 })
219 }
220}
221
222pub struct ObserverClient {
224 wrapper: ClientWrapper,
225 observer: Option<Arc<dyn ConnectionObserver>>,
226}
227
228impl ObserverClient {
229 pub async fn connect(&mut self) -> Result<()> {
231 if let Some(observer) = &self.observer {
233 self.wrapper
234 .add_observer(Arc::clone(observer) as Arc<dyn ConnectionObserver>)
235 .await;
236 }
237
238 self.wrapper.connect().await
239 }
240
241 pub async fn disconnect(&mut self) -> Result<()> {
243 self.wrapper.disconnect().await
244 }
245
246 pub async fn send_frame(&mut self, frame: &Frame) -> Result<()> {
248 self.wrapper.send_frame(frame).await
249 }
250
251 pub async fn send_frame_and_wait(
253 &mut self,
254 frame: &Frame,
255 timeout: std::time::Duration,
256 ) -> Result<Frame> {
257 self.wrapper.send_frame_and_wait(frame, timeout).await
258 }
259
260 pub fn is_connected(&self) -> bool {
262 crate::client::runtime::run_client_async(self.wrapper.is_connected_async())
263 }
264
265 pub fn connection_id(&self) -> Option<String> {
267 crate::client::runtime::run_client_async(self.wrapper.connection_id_async())
268 }
269
270 pub fn active_protocol(&self) -> crate::common::config_types::TransportProtocol {
272 self.wrapper.active_protocol()
273 }
274}