use crate::common::error::Result;
use crate::common::protocol::Frame;
use crate::transport::events::ConnectionEvent;
use async_trait::async_trait;
use std::sync::Arc;
#[async_trait]
pub trait MessageObserver: Send + Sync {
async fn handle_frame(
&self,
frame: &Frame,
connection_id: Option<&str>,
) -> Result<Option<Frame>> {
if let Some(cmd) = &frame.command {
match &cmd.r#type {
Some(crate::common::protocol::flare::core::commands::command::Type::System(
sys_cmd,
)) => {
self.handle_system_command(sys_cmd, frame, connection_id)
.await
}
Some(crate::common::protocol::flare::core::commands::command::Type::Payload(
msg_cmd,
)) => {
self.handle_message_command(msg_cmd, frame, connection_id)
.await
}
Some(
crate::common::protocol::flare::core::commands::command::Type::Notification(
notif_cmd,
),
) => {
self.handle_notification_command(notif_cmd, frame, connection_id)
.await
}
Some(crate::common::protocol::flare::core::commands::command::Type::Custom(
custom_cmd,
)) => {
self.handle_custom_command(custom_cmd, frame, connection_id)
.await
}
None => Ok(None),
}
} else {
Ok(None)
}
}
async fn handle_system_command(
&self,
_sys_cmd: &crate::common::protocol::flare::core::commands::SystemCommand,
_frame: &Frame,
_connection_id: Option<&str>,
) -> Result<Option<Frame>> {
Ok(None)
}
async fn handle_message_command(
&self,
_msg_cmd: &crate::common::protocol::PayloadCommand,
_frame: &Frame,
_connection_id: Option<&str>,
) -> Result<Option<Frame>> {
Ok(None)
}
async fn handle_notification_command(
&self,
_notif_cmd: &crate::common::protocol::NotificationCommand,
_frame: &Frame,
_connection_id: Option<&str>,
) -> Result<Option<Frame>> {
Ok(None)
}
async fn handle_custom_command(
&self,
_custom_cmd: &crate::common::protocol::CustomCommand,
_frame: &Frame,
_connection_id: Option<&str>,
) -> Result<Option<Frame>> {
Ok(None)
}
async fn handle_connection_event(
&self,
_event: &ConnectionEvent,
_connection_id: Option<&str>,
) -> Result<()> {
Ok(())
}
}
pub type ArcMessageObserver = Arc<dyn MessageObserver>;