use crate::client::HybridClient;
use crate::client::builder::{BaseClientBuilderConfig, ClientWrapper};
use crate::common::error::Result;
use crate::common::protocol::Frame;
use crate::transport::events::ConnectionObserver;
use std::sync::Arc;
pub struct ObserverClientBuilder {
base: BaseClientBuilderConfig,
observer: Option<Arc<dyn ConnectionObserver>>,
event_handler: Option<Arc<dyn crate::client::events::handler::ClientEventHandler>>,
}
impl ObserverClientBuilder {
pub fn new(server_url: impl Into<String>) -> Self {
Self {
base: BaseClientBuilderConfig::new(server_url),
observer: None,
event_handler: None,
}
}
pub fn with_event_handler(
mut self,
event_handler: Arc<dyn crate::client::events::handler::ClientEventHandler>,
) -> Self {
self.event_handler = Some(event_handler);
self
}
pub fn with_observer(mut self, observer: Arc<dyn ConnectionObserver>) -> Self {
self.observer = Some(observer);
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_protocol_race(
mut self,
protocols: Vec<crate::common::config_types::TransportProtocol>,
) -> Self {
self.base = self.base.with_protocol_race(protocols);
self
}
pub fn with_protocol_url(
mut self,
protocol: crate::common::config_types::TransportProtocol,
url: String,
) -> Self {
self.base = self.base.with_protocol_url(protocol, url);
self
}
pub fn with_user_id(mut self, user_id: String) -> Self {
self.base = self.base.with_user_id(user_id);
self
}
pub fn with_format(mut self, format: crate::common::protocol::SerializationFormat) -> Self {
self.base = self.base.with_format(format);
self
}
pub fn with_compression(
mut self,
compression: crate::common::compression::CompressionAlgorithm,
) -> Self {
self.base = self.base.with_compression(compression);
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_connect_timeout(mut self, timeout: std::time::Duration) -> Self {
self.base = self.base.with_connect_timeout(timeout);
self
}
pub fn with_reconnect_interval(mut self, interval: std::time::Duration) -> Self {
self.base = self.base.with_reconnect_interval(interval);
self
}
pub fn with_max_reconnect_attempts(mut self, max: Option<u32>) -> Self {
self.base = self.base.with_max_reconnect_attempts(max);
self
}
pub fn enable_router(mut self) -> Self {
self.base = self.base.enable_router();
self
}
pub fn with_device_info(mut self, device_info: crate::common::device::DeviceInfo) -> Self {
self.base = self.base.with_device_info(device_info);
self
}
pub fn with_token(mut self, token: String) -> Self {
self.base = self.base.with_token(token);
self
}
pub async fn build_with_race(self) -> Result<ObserverClient> {
let observer = self.observer.ok_or_else(|| {
crate::common::error::FlareError::general_error("Observer is required")
})?;
let client = HybridClient::connect_with_race(self.base.config).await?;
let wrapper = ClientWrapper::new(client);
if let Some(event_handler) = self.event_handler {
wrapper.set_event_handler(Some(event_handler)).await;
}
wrapper
.add_observer(Arc::clone(&observer) as Arc<dyn ConnectionObserver>)
.await;
Ok(ObserverClient {
wrapper,
observer: Some(observer),
})
}
pub fn build(self) -> Result<ObserverClient> {
let observer = self.observer.ok_or_else(|| {
crate::common::error::FlareError::general_error("Observer is required")
})?;
let mut client = HybridClient::new(self.base.config)?;
if let Some(event_handler) = self.event_handler {
client.core_mut().set_event_handler(Some(event_handler));
}
let wrapper = ClientWrapper::new(client);
Ok(ObserverClient {
wrapper,
observer: Some(observer),
})
}
}
pub struct ObserverClient {
wrapper: ClientWrapper,
observer: Option<Arc<dyn ConnectionObserver>>,
}
impl ObserverClient {
pub async fn connect(&mut self) -> Result<()> {
if let Some(observer) = &self.observer {
self.wrapper
.add_observer(Arc::clone(observer) as Arc<dyn ConnectionObserver>)
.await;
}
self.wrapper.connect().await
}
pub async fn disconnect(&mut self) -> Result<()> {
self.wrapper.disconnect().await
}
pub async fn send_frame(&mut self, frame: &Frame) -> Result<()> {
self.wrapper.send_frame(frame).await
}
pub async fn send_frame_and_wait(
&mut self,
frame: &Frame,
timeout: std::time::Duration,
) -> Result<Frame> {
self.wrapper.send_frame_and_wait(frame, timeout).await
}
pub fn is_connected(&self) -> bool {
crate::client::runtime::run_client_async(self.wrapper.is_connected_async())
}
pub fn connection_id(&self) -> Option<String> {
crate::client::runtime::run_client_async(self.wrapper.connection_id_async())
}
pub fn active_protocol(&self) -> crate::common::config_types::TransportProtocol {
self.wrapper.active_protocol()
}
}