feat: M1.1 完成 + M1.2 启动 — 全量更新
M1.1 收尾: - 24项 P0/P1/P2 bug 修复 (Rust 107 tests + Flutter 15 tests) - Flutter App v0.3: cupertino_icons 修复, 单元测试, 调试面板, APK 52.6MB - 示例插件完善: manifest.json + 请求/响应示范 + 7个测试 - API 文档重写 (以 routes.rs 为唯一权威) - MILESTONES.md 更新至 100% M1.2 启动: - P0: 插件管理 API 闭环 (handle_manager_message Custom 分支 + broadcast_plugin_states) - ServiceManager 集成测试 8/8 (tests/m1_2_service_manager.rs) - M1.2 测试计划 (docs/M1.2_TEST_PLAN.md, 18个E2E场景) - 动态插件系统: auto_rollback + version_manager GC + 路径穿越防护 总计: Rust 115/115 测试, Flutter 15/15 测试, 零 warning Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -11,7 +11,9 @@ use anyhow::{Context, Result};
|
||||
use serde::Serialize;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Condvar, Mutex};
|
||||
use std::thread::JoinHandle;
|
||||
use tokio::sync::broadcast;
|
||||
use tokio::sync::{oneshot, Mutex as AsyncMutex};
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct WsEvent<'a, T> {
|
||||
@@ -36,6 +38,7 @@ struct PendingWifiResponse {
|
||||
}
|
||||
|
||||
pub(crate) struct HttpState {
|
||||
wifi_request_lock: AsyncMutex<()>,
|
||||
wifi_response: Mutex<PendingWifiResponse>,
|
||||
wifi_response_cv: Condvar,
|
||||
last_wifi_result: Mutex<Option<String>>,
|
||||
@@ -60,6 +63,7 @@ impl HttpState {
|
||||
};
|
||||
|
||||
Self {
|
||||
wifi_request_lock: AsyncMutex::new(()),
|
||||
wifi_response: Mutex::new(PendingWifiResponse {
|
||||
version: 0,
|
||||
payload: None,
|
||||
@@ -202,6 +206,8 @@ impl HttpState {
|
||||
pub struct HttpPlugin {
|
||||
ctx: Option<PluginContext>,
|
||||
state: Option<Arc<HttpState>>,
|
||||
shutdown_tx: Option<oneshot::Sender<()>>,
|
||||
server_thread: Option<JoinHandle<()>>,
|
||||
}
|
||||
|
||||
impl HttpPlugin {
|
||||
@@ -209,6 +215,8 @@ impl HttpPlugin {
|
||||
Self {
|
||||
ctx: None,
|
||||
state: None,
|
||||
shutdown_tx: None,
|
||||
server_thread: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -244,6 +252,8 @@ impl Plugin for HttpPlugin {
|
||||
}
|
||||
|
||||
fn start(&mut self) -> Result<()> {
|
||||
self.stop()?;
|
||||
|
||||
let ctx = self
|
||||
.ctx
|
||||
.as_ref()
|
||||
@@ -263,7 +273,9 @@ impl Plugin for HttpPlugin {
|
||||
.context("http plugin state is not initialized")?,
|
||||
);
|
||||
|
||||
std::thread::spawn(move || {
|
||||
let (shutdown_tx, shutdown_rx) = oneshot::channel();
|
||||
|
||||
let server_thread = std::thread::spawn(move || {
|
||||
let runtime = match tokio::runtime::Builder::new_multi_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
@@ -294,10 +306,18 @@ impl Plugin for HttpPlugin {
|
||||
}
|
||||
|
||||
println!("[HttpPlugin] listening on http://{addr}");
|
||||
warp::serve(routes).run(addr).await;
|
||||
warp::serve(routes)
|
||||
.bind_with_graceful_shutdown(addr, async move {
|
||||
let _ = shutdown_rx.await;
|
||||
})
|
||||
.1
|
||||
.await;
|
||||
});
|
||||
});
|
||||
|
||||
self.shutdown_tx = Some(shutdown_tx);
|
||||
self.server_thread = Some(server_thread);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -344,6 +364,16 @@ impl Plugin for HttpPlugin {
|
||||
}
|
||||
|
||||
fn stop(&mut self) -> Result<()> {
|
||||
if let Some(shutdown_tx) = self.shutdown_tx.take() {
|
||||
let _ = shutdown_tx.send(());
|
||||
}
|
||||
|
||||
if let Some(server_thread) = self.server_thread.take() {
|
||||
server_thread
|
||||
.join()
|
||||
.map_err(|_| anyhow::anyhow!("http server thread panicked"))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user