flare_core/server/builder/
observer.rs1use crate::common::error::Result;
26use crate::common::protocol::Frame;
27use crate::server::HybridServer;
28use crate::server::builder::{BaseServerBuilderConfig, ServerWrapper};
29use crate::server::connection::ConnectionManager;
30use crate::server::handle::ServerHandle;
31use std::sync::Arc;
32use tracing::{error, info};
33
34pub struct ObserverServerBuilder {
49 base: BaseServerBuilderConfig,
50 connection_manager: Option<Arc<ConnectionManager>>,
51 device_manager: Option<Arc<crate::server::device::DeviceManager>>,
52 event_handler: Arc<dyn crate::server::events::handler::ServerEventHandler>,
53}
54
55impl ObserverServerBuilder {
56 pub fn new(
62 bind_address: impl Into<String>,
63 event_handler: Arc<dyn crate::server::events::handler::ServerEventHandler>,
64 ) -> Self {
65 Self {
66 base: BaseServerBuilderConfig::new(bind_address),
67 connection_manager: None,
68 device_manager: None,
69 event_handler,
70 }
71 }
72
73 pub fn with_authenticator(
81 mut self,
82 authenticator: Arc<dyn crate::server::auth::Authenticator>,
83 ) -> Self {
84 self.base = self.base.with_authenticator(authenticator);
85 self
86 }
87
88 pub fn enable_auth(mut self) -> Self {
90 self.base = self.base.enable_auth();
91 self
92 }
93
94 pub fn with_auth_timeout(mut self, timeout: std::time::Duration) -> Self {
96 self.base = self.base.with_auth_timeout(timeout);
97 self
98 }
99
100 pub fn with_device_manager(
102 mut self,
103 device_manager: Arc<crate::server::device::DeviceManager>,
104 ) -> Self {
105 self.device_manager = Some(device_manager);
106 self
107 }
108
109 pub fn with_connection_manager(mut self, manager: Arc<ConnectionManager>) -> Self {
111 self.connection_manager = Some(manager);
112 self
113 }
114
115 pub fn with_protocol(
117 mut self,
118 protocol: crate::common::config_types::TransportProtocol,
119 ) -> Self {
120 self.base = self.base.with_protocol(protocol);
121 self
122 }
123
124 pub fn with_protocols(
126 mut self,
127 protocols: Vec<crate::common::config_types::TransportProtocol>,
128 ) -> Self {
129 self.base = self.base.with_protocols(protocols);
130 self
131 }
132
133 pub fn with_protocol_address(
135 mut self,
136 protocol: crate::common::config_types::TransportProtocol,
137 address: String,
138 ) -> Self {
139 self.base = self.base.with_protocol_address(protocol, address);
140 self
141 }
142
143 pub fn with_max_connections(mut self, max: usize) -> Self {
145 self.base = self.base.with_max_connections(max);
146 self
147 }
148
149 pub fn with_handshake_timeout(mut self, timeout: std::time::Duration) -> Self {
151 self.base = self.base.with_handshake_timeout(timeout);
152 self
153 }
154
155 pub fn with_max_handshake_concurrency(mut self, max: usize) -> Self {
157 self.base = self.base.with_max_handshake_concurrency(max);
158 self
159 }
160
161 pub fn with_write_timeout(mut self, timeout: std::time::Duration) -> Self {
163 self.base = self.base.with_write_timeout(timeout);
164 self
165 }
166
167 pub fn with_fanout_concurrency(mut self, max: usize) -> Self {
169 self.base = self.base.with_fanout_concurrency(max);
170 self
171 }
172
173 pub fn with_heartbeat(
175 mut self,
176 heartbeat: crate::common::config_types::HeartbeatConfig,
177 ) -> Self {
178 self.base = self.base.with_heartbeat(heartbeat);
179 self
180 }
181
182 pub fn with_tls(mut self, tls: crate::common::config_types::TlsConfig) -> Self {
184 self.base = self.base.with_tls(tls);
185 self
186 }
187
188 pub fn with_default_format(
190 mut self,
191 format: crate::common::protocol::SerializationFormat,
192 ) -> Self {
193 self.base = self.base.with_default_format(format);
194 self
195 }
196
197 pub fn with_default_compression(
199 mut self,
200 compression: crate::common::compression::CompressionAlgorithm,
201 ) -> Self {
202 self.base = self.base.with_default_compression(compression);
203 self
204 }
205
206 pub fn build(self) -> Result<ObserverServer> {
216 crate::server::builder::common::validate_auth_config(
218 &self.base.config,
219 &self.base.authenticator,
220 )?;
221
222 info!(
226 "[ObserverServerBuilder] 开始构建服务端: bind_address={}, protocols={:?}",
227 self.base.config.bind_address,
228 self.base.config.get_protocols()
229 );
230
231 let server = HybridServer::with_connection_manager(
232 self.base.config,
233 self.connection_manager,
234 self.device_manager,
235 Some(self.event_handler),
236 self.base.authenticator,
237 )
238 .map_err(|e| {
239 error!("[ObserverServerBuilder] 构建服务端失败: {}", e);
240 e
241 })?;
242
243 info!("[ObserverServerBuilder] 服务端构建成功");
244 Ok(ObserverServer {
245 wrapper: ServerWrapper::new(server),
246 })
247 }
248}
249
250pub struct ObserverServer {
252 wrapper: ServerWrapper,
253}
254
255impl ObserverServer {
256 pub async fn start(&mut self) -> Result<()> {
258 self.wrapper.start().await
259 }
260
261 pub async fn stop(&mut self) -> Result<()> {
263 self.wrapper.stop().await
264 }
265
266 pub fn is_running(&self) -> bool {
268 self.wrapper.is_running()
269 }
270
271 pub fn connection_count(&self) -> usize {
273 self.wrapper.connection_count()
274 }
275
276 pub fn user_count(&self) -> usize {
278 self.wrapper.user_count()
279 }
280
281 pub async fn send_to(&self, connection_id: &str, frame: &Frame) -> Result<()> {
283 self.wrapper.send_to(connection_id, frame).await
284 }
285
286 pub async fn send_to_user(&self, user_id: &str, frame: &Frame) -> Result<()> {
288 self.wrapper.send_to_user(user_id, frame).await
289 }
290
291 pub async fn broadcast(&self, frame: &Frame) -> Result<()> {
293 self.wrapper.broadcast(frame).await
294 }
295
296 pub async fn broadcast_except(&self, frame: &Frame, exclude_connection_id: &str) -> Result<()> {
298 self.wrapper
299 .broadcast_except(frame, exclude_connection_id)
300 .await
301 }
302
303 pub async fn disconnect(&self, connection_id: &str) -> Result<()> {
305 self.wrapper.disconnect(connection_id).await
306 }
307
308 pub fn protocols(&self) -> Vec<crate::common::config_types::TransportProtocol> {
310 self.wrapper.protocols()
311 }
312
313 pub fn get_server_handle_components(
318 &self,
319 ) -> Option<Arc<dyn crate::server::connection::ConnectionManagerTrait>> {
320 self.wrapper.get_server_handle_components()
321 }
322
323 pub fn get_server_handle(&self) -> Option<Arc<dyn ServerHandle>> {
325 self.wrapper.get_server_handle()
326 }
327}