feat: 实现动态插件系统 (6阶段完成)
- 阶段1: 消息类型序列化 (Serialize/Deserialize, &'static str → String) - 阶段2: FFI 边界类型 + Plugin SDK (plugin_abi, showen-plugin-sdk crate) - 阶段3: PluginLoader + DynamicPlugin (libloading 动态加载 .so) - 阶段4: 版本管理 + 错误策略 (VersionManager, PluginState, 自动回退) - 阶段5: 远程仓库客户端 (HTTP 下载 + tar.gz 安装) - 阶段6: 示例插件 + HTTP 管理 API + 全目录 README 文档 54/54 测试通过,0 warnings。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
72
plugins/example-plugin/src/lib.rs
Normal file
72
plugins/example-plugin/src/lib.rs
Normal file
@@ -0,0 +1,72 @@
|
||||
//! 示例动态插件 — 展示如何使用 showen-plugin-sdk 编写插件
|
||||
//!
|
||||
//! 此插件仅打印日志,用于验证动态加载流程。
|
||||
|
||||
use showen_plugin_sdk::{
|
||||
export_plugin, Message, MessageSender, PluginInfo, ShowenPlugin,
|
||||
};
|
||||
|
||||
pub struct ExamplePlugin {
|
||||
sender: Option<MessageSender>,
|
||||
}
|
||||
|
||||
impl ExamplePlugin {
|
||||
pub fn new() -> Self {
|
||||
Self { sender: None }
|
||||
}
|
||||
}
|
||||
|
||||
impl ShowenPlugin for ExamplePlugin {
|
||||
fn info(&self) -> PluginInfo {
|
||||
PluginInfo {
|
||||
name: "example-plugin".to_string(),
|
||||
version: "0.1.0".to_string(),
|
||||
description: "示例动态插件".to_string(),
|
||||
platform: "Any".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn init(&mut self, config_json: &str, sender: MessageSender) -> Result<(), String> {
|
||||
eprintln!("[ExamplePlugin] init called, config length: {}", config_json.len());
|
||||
self.sender = Some(sender);
|
||||
|
||||
// 通知主程序就绪
|
||||
if let Some(sender) = &self.sender {
|
||||
sender.send_to_manager(
|
||||
"example-plugin",
|
||||
Message::PluginReady("example-plugin".to_string()),
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn start(&mut self) -> Result<(), String> {
|
||||
eprintln!("[ExamplePlugin] started");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_message(&mut self, message: Message) -> Result<(), String> {
|
||||
match &message {
|
||||
Message::Shutdown => {
|
||||
eprintln!("[ExamplePlugin] received shutdown");
|
||||
}
|
||||
Message::Custom { kind, payload } => {
|
||||
eprintln!("[ExamplePlugin] custom message: kind={kind}, payload={payload}");
|
||||
}
|
||||
_ => {
|
||||
eprintln!("[ExamplePlugin] received message: {:?}", message);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn stop(&mut self) -> Result<(), String> {
|
||||
eprintln!("[ExamplePlugin] stopped");
|
||||
self.sender = None;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
// 导出 FFI 接口
|
||||
export_plugin!(ExamplePlugin, ExamplePlugin::new());
|
||||
Reference in New Issue
Block a user