use crate::common::error::Result;
use crate::common::protocol::Frame;
use crate::server::HybridServer;
use crate::server::builder::{BaseServerBuilderConfig, ServerWrapper};
use crate::server::connection::ConnectionManager;
use crate::server::handle::ServerHandle;
use std::sync::Arc;
use tracing::{error, info};
pub struct ObserverServerBuilder {
base: BaseServerBuilderConfig,
connection_manager: Option<Arc<ConnectionManager>>,
device_manager: Option<Arc<crate::server::device::DeviceManager>>,
event_handler: Arc<dyn crate::server::events::handler::ServerEventHandler>,
}
impl ObserverServerBuilder {
pub fn new(
bind_address: impl Into<String>,
event_handler: Arc<dyn crate::server::events::handler::ServerEventHandler>,
) -> Self {
Self {
base: BaseServerBuilderConfig::new(bind_address),
connection_manager: None,
device_manager: None,
event_handler,
}
}
pub fn with_authenticator(
mut self,
authenticator: Arc<dyn crate::server::auth::Authenticator>,
) -> Self {
self.base = self.base.with_authenticator(authenticator);
self
}
pub fn enable_auth(mut self) -> Self {
self.base = self.base.enable_auth();
self
}
pub fn with_auth_timeout(mut self, timeout: std::time::Duration) -> Self {
self.base = self.base.with_auth_timeout(timeout);
self
}
pub fn with_device_manager(
mut self,
device_manager: Arc<crate::server::device::DeviceManager>,
) -> Self {
self.device_manager = Some(device_manager);
self
}
pub fn with_connection_manager(mut self, manager: Arc<ConnectionManager>) -> Self {
self.connection_manager = Some(manager);
self
}
pub fn with_protocol(
mut self,
protocol: crate::common::config_types::TransportProtocol,
) -> Self {
self.base = self.base.with_protocol(protocol);
self
}
pub fn with_protocols(
mut self,
protocols: Vec<crate::common::config_types::TransportProtocol>,
) -> Self {
self.base = self.base.with_protocols(protocols);
self
}
pub fn with_protocol_address(
mut self,
protocol: crate::common::config_types::TransportProtocol,
address: String,
) -> Self {
self.base = self.base.with_protocol_address(protocol, address);
self
}
pub fn with_max_connections(mut self, max: usize) -> Self {
self.base = self.base.with_max_connections(max);
self
}
pub fn with_handshake_timeout(mut self, timeout: std::time::Duration) -> Self {
self.base = self.base.with_handshake_timeout(timeout);
self
}
pub fn with_max_handshake_concurrency(mut self, max: usize) -> Self {
self.base = self.base.with_max_handshake_concurrency(max);
self
}
pub fn with_write_timeout(mut self, timeout: std::time::Duration) -> Self {
self.base = self.base.with_write_timeout(timeout);
self
}
pub fn with_fanout_concurrency(mut self, max: usize) -> Self {
self.base = self.base.with_fanout_concurrency(max);
self
}
pub fn with_heartbeat(
mut self,
heartbeat: crate::common::config_types::HeartbeatConfig,
) -> Self {
self.base = self.base.with_heartbeat(heartbeat);
self
}
pub fn with_tls(mut self, tls: crate::common::config_types::TlsConfig) -> Self {
self.base = self.base.with_tls(tls);
self
}
pub fn with_default_format(
mut self,
format: crate::common::protocol::SerializationFormat,
) -> Self {
self.base = self.base.with_default_format(format);
self
}
pub fn with_default_compression(
mut self,
compression: crate::common::compression::CompressionAlgorithm,
) -> Self {
self.base = self.base.with_default_compression(compression);
self
}
pub fn build(self) -> Result<ObserverServer> {
crate::server::builder::common::validate_auth_config(
&self.base.config,
&self.base.authenticator,
)?;
info!(
"[ObserverServerBuilder] 开始构建服务端: bind_address={}, protocols={:?}",
self.base.config.bind_address,
self.base.config.get_protocols()
);
let server = HybridServer::with_connection_manager(
self.base.config,
self.connection_manager,
self.device_manager,
Some(self.event_handler),
self.base.authenticator,
)
.map_err(|e| {
error!("[ObserverServerBuilder] 构建服务端失败: {}", e);
e
})?;
info!("[ObserverServerBuilder] 服务端构建成功");
Ok(ObserverServer {
wrapper: ServerWrapper::new(server),
})
}
}
pub struct ObserverServer {
wrapper: ServerWrapper,
}
impl ObserverServer {
pub async fn start(&mut self) -> Result<()> {
self.wrapper.start().await
}
pub async fn stop(&mut self) -> Result<()> {
self.wrapper.stop().await
}
pub fn is_running(&self) -> bool {
self.wrapper.is_running()
}
pub fn connection_count(&self) -> usize {
self.wrapper.connection_count()
}
pub fn user_count(&self) -> usize {
self.wrapper.user_count()
}
pub async fn send_to(&self, connection_id: &str, frame: &Frame) -> Result<()> {
self.wrapper.send_to(connection_id, frame).await
}
pub async fn send_to_user(&self, user_id: &str, frame: &Frame) -> Result<()> {
self.wrapper.send_to_user(user_id, frame).await
}
pub async fn broadcast(&self, frame: &Frame) -> Result<()> {
self.wrapper.broadcast(frame).await
}
pub async fn broadcast_except(&self, frame: &Frame, exclude_connection_id: &str) -> Result<()> {
self.wrapper
.broadcast_except(frame, exclude_connection_id)
.await
}
pub async fn disconnect(&self, connection_id: &str) -> Result<()> {
self.wrapper.disconnect(connection_id).await
}
pub fn protocols(&self) -> Vec<crate::common::config_types::TransportProtocol> {
self.wrapper.protocols()
}
pub fn get_server_handle_components(
&self,
) -> Option<Arc<dyn crate::server::connection::ConnectionManagerTrait>> {
self.wrapper.get_server_handle_components()
}
pub fn get_server_handle(&self) -> Option<Arc<dyn ServerHandle>> {
self.wrapper.get_server_handle()
}
}