flare_im_core/server/
sys_handler.rs

1use flare_core::context::AppContext;
2use flare_core::error::{FlareErr, Result};
3use crate::server::handlers::CommandHandler;
4use crate::server::server::ConnectionInfo;
5use async_trait::async_trait;
6use log::debug;
7use flare_core::flare_net::net::{Command, ResCode, Response};
8
9#[async_trait]
10pub trait SystemHandler: Send + Sync {
11    /// 处理新链接
12    async fn handle_new_connection(&self, ctx:  &AppContext,conn : &ConnectionInfo) -> Result<Response>;
13    /// 设置后台运行
14    async fn handle_set_background(&self, ctx:  &AppContext, background: bool) -> Result<Response>;
15    /// 设置语言
16    async fn handle_set_language(&self, ctx:  &AppContext, language: String) -> Result<Response>;
17    /// 关闭
18    async fn handle_close(&self, ctx:  &AppContext) -> Result<Response>;
19}
20
21/// 系统命令处理器
22pub struct SystemCommandHandler<T>(pub T);
23
24impl<T> SystemCommandHandler<T> {
25    pub fn new(handler: T) -> Self {
26        Self(handler)
27    }
28}
29
30// 实现 SystemHandler
31#[async_trait]
32impl<T: SystemHandler + Send + Sync> SystemHandler for SystemCommandHandler<T> {
33    async fn handle_new_connection(&self, ctx:  &AppContext, conn: &ConnectionInfo) -> Result<Response> {
34        self.0.handle_new_connection(ctx, conn).await
35    }
36
37    async fn handle_set_background(&self, ctx:  &AppContext, background: bool) -> Result<Response> {
38        self.0.handle_set_background(ctx, background).await
39    }
40
41    async fn handle_set_language(&self, ctx:  &AppContext, language: String) -> Result<Response> {
42        self.0.handle_set_language(ctx, language).await
43    }
44    async fn handle_close(&self, ctx:  &AppContext) -> Result<Response> {
45        self.0.handle_close(ctx).await
46    }
47}
48
49#[async_trait]
50impl<T: SystemHandler + Send + Sync> CommandHandler for SystemCommandHandler<T> {
51    async fn handle_command(&self, ctx:  &AppContext) -> Result<Response> {
52        let command = ctx.command().ok_or_else(|| 
53            FlareErr::invalid_command("Missing command"))?;
54
55        if !self.supports_command(command) {
56            return Ok(Response {
57                code: ResCode::InvalidCommand as i32,
58                message: format!("Unsupported command: {:?}", command),
59                data: Vec::new(),
60            });
61        }
62
63        match command {
64            Command::SetBackground => {
65                let background = ctx.bool_data()?;
66                self.handle_set_background(ctx, background).await
67            }
68            Command::SetLanguage => {
69                let language = ctx.string_data()?;
70                self.handle_set_language(ctx, language).await
71            }
72            Command::Close => self.handle_close(ctx).await,
73            _ => Ok(Response {
74                code: ResCode::InvalidCommand as i32,
75                message: format!("Unexpected command: {:?}", command),
76                data: Vec::new(),
77            })
78        }
79    }
80
81    fn supported_commands(&self) -> Vec<Command> {
82        vec![Command::SetBackground, Command::SetLanguage, Command::Close]
83    }
84}
85
86/// 默认的系统处理器实现
87#[derive(Default)]
88pub struct DefSystemHandler;
89
90impl DefSystemHandler {
91    pub fn new() -> Self {
92        Self {}
93    }
94}
95
96#[async_trait]
97impl SystemHandler for DefSystemHandler {
98    async fn handle_new_connection(&self, ctx:  &AppContext, conn: &ConnectionInfo) -> Result<Response> {
99        debug!("处理新连接请求 - addr: {}, conn_id: {}", ctx.remote_addr(), conn.get_conn_id());
100        Ok(Response {
101            code: ResCode::Success as i32,
102            message: "连接已建立".into(),
103            data: Vec::new(),
104        })
105    }
106    
107    async fn handle_set_background(&self, ctx:  &AppContext, background: bool) -> Result<Response> {
108        debug!("处理设置后台运行请求 - addr: {}, background: {}", ctx.remote_addr(), background);
109
110        // 这里可以添加实际的后台运行设置逻辑
111        Ok(Response {
112            code: ResCode::Success as i32,
113            message: format!("后台运行已{}",if background {"开启"} else {"关闭"}),
114            data: Vec::new(),
115        })
116    }
117
118    async fn handle_set_language(&self, ctx:  &AppContext, language: String) -> Result<Response> {
119        debug!("处理设置语言请求 - addr: {}, language: {}", ctx.remote_addr(), language);
120
121        // 这里可以添加语言设置验证逻辑
122        if language.is_empty() {
123            return Ok(Response {
124                code: ResCode::InvalidParams as i32,
125                message: "Language cannot be empty".into(),
126                data: Vec::new(),
127            });
128        }
129
130        Ok(Response {
131            code: ResCode::Success as i32,
132            message: format!("语言已设置为 {}", language),
133            data: Vec::new(),
134        })
135    }
136
137    async fn handle_close(&self, ctx:  &AppContext) -> Result<Response> {
138        debug!("处理关闭连接请求 - addr: {}", ctx.remote_addr());
139        
140        if let Some(user_id) = ctx.user_id() {
141            debug!("用户 {} 请求关闭连接", user_id);
142        }
143
144        Ok(Response {
145            code: ResCode::Success as i32,
146            message: "连接即将关闭".into(),
147            data: Vec::new(),
148        })
149    }
150}