flare_core/common/
message_observer.rs1use crate::common::error::Result;
6use crate::common::protocol::Frame;
7use crate::transport::events::ConnectionEvent;
8use async_trait::async_trait;
9use std::sync::Arc;
10
11#[async_trait]
21pub trait MessageObserver: Send + Sync {
22 async fn handle_frame(
37 &self,
38 frame: &Frame,
39 connection_id: Option<&str>,
40 ) -> Result<Option<Frame>> {
41 if let Some(cmd) = &frame.command {
42 match &cmd.r#type {
43 Some(crate::common::protocol::flare::core::commands::command::Type::System(
44 sys_cmd,
45 )) => {
46 self.handle_system_command(sys_cmd, frame, connection_id)
47 .await
48 }
49 Some(crate::common::protocol::flare::core::commands::command::Type::Payload(
50 msg_cmd,
51 )) => {
52 self.handle_message_command(msg_cmd, frame, connection_id)
53 .await
54 }
55 Some(
56 crate::common::protocol::flare::core::commands::command::Type::Notification(
57 notif_cmd,
58 ),
59 ) => {
60 self.handle_notification_command(notif_cmd, frame, connection_id)
61 .await
62 }
63 Some(crate::common::protocol::flare::core::commands::command::Type::Custom(
64 custom_cmd,
65 )) => {
66 self.handle_custom_command(custom_cmd, frame, connection_id)
67 .await
68 }
69 None => Ok(None),
70 }
71 } else {
72 Ok(None)
73 }
74 }
75
76 async fn handle_system_command(
80 &self,
81 _sys_cmd: &crate::common::protocol::flare::core::commands::SystemCommand,
82 _frame: &Frame,
83 _connection_id: Option<&str>,
84 ) -> Result<Option<Frame>> {
85 Ok(None)
86 }
87
88 async fn handle_message_command(
92 &self,
93 _msg_cmd: &crate::common::protocol::PayloadCommand,
94 _frame: &Frame,
95 _connection_id: Option<&str>,
96 ) -> Result<Option<Frame>> {
97 Ok(None)
98 }
99
100 async fn handle_notification_command(
104 &self,
105 _notif_cmd: &crate::common::protocol::NotificationCommand,
106 _frame: &Frame,
107 _connection_id: Option<&str>,
108 ) -> Result<Option<Frame>> {
109 Ok(None)
110 }
111
112 async fn handle_custom_command(
116 &self,
117 _custom_cmd: &crate::common::protocol::CustomCommand,
118 _frame: &Frame,
119 _connection_id: Option<&str>,
120 ) -> Result<Option<Frame>> {
121 Ok(None)
122 }
123
124 async fn handle_connection_event(
128 &self,
129 _event: &ConnectionEvent,
130 _connection_id: Option<&str>,
131 ) -> Result<()> {
132 Ok(())
133 }
134}
135
136pub type ArcMessageObserver = Arc<dyn MessageObserver>;