开始
前置准备
在进行插件开发之前,您需要在操作系统中安装 RUST 开发环境,此部分您可以通过访问[RUST](Rust Programming Language) 官方网站进行查询。
在安装完RUST开发环境后,通过以下指令安装 cargo-generate,用以方便快速的生成插件开发基础项目。
cargo install cargo-generate
开发
快速创建项目
您可以通过如下命令来快速创建整个插件框架:
cargo generate --git https://github.com/designershao/YedMQ-Plugin-Template.git
⚠️ Favorite `YedMQ_Plugin_Template` not found in config, using it as a local path: YedMQ_Plugin_Template
🤷 Project Name: example-plugin
🔧 Destination: /home/yedmq/example-plugin ...
🔧 project-name: example-plugin ...
🔧 Generating template ...
🤷 Your plugin name?: example-plugin
🤷 Description for the crate?: just an example
7:1 | "plugin_name: ExamplePlugin"
🔧 Moving generated files into: `/home/yedmq/example-plugin`...
🔧 Initializing a fresh Git repository
✨ Done! New project created /home/yedmq/example-plugin
最后,生成的插件文件夹结构如下:
.
├── Cargo.lock
├── Cargo.toml
├── plugin.toml
├── README.md
└── src
└── lib.rs
- plugin.toml: YedMQ 插件描述元文件。
- lib.rs: 插件具体实现文件。
plugin.toml 样例:
[plugin]
name = "example-plugin"
author = "shaoyan"
description = "just an example"
version = "0.0.1"
entry = "./libexample_plugin.so"
priority = 1000
| key | Description |
|---|---|
| name | 插件名称 |
| author | 作者信息 |
| description | 插件描述 |
| version | 插件版本 |
| entry | 插件动态链接库地址 |
| priority | 插件优先级,优先级越高的插件会被优先执行 |
| 使用编辑器打开lib.rs,会看到相应的基础框架实现: |
use anyhow::Result;
use yedmq_plugin::{plugin::{AuthenticationResult, AuthenticationResultValue, AuthorizationResult}, register_plugin};
pub struct ExamplePlugin {}
impl ExamplePlugin {
pub fn new(_context: yedmq_plugin::context::Context) -> Result<ExamplePlugin, anyhow::Error> {
Ok(
ExamplePlugin {}
)
}
}
impl yedmq_plugin::plugin::Plugin for ExamplePlugin {
fn on_activate(&mut self) -> Result<()> {
println!("example plugin on_activate example-plugin");
Ok(())
}
fn on_deactivate(&self) -> Result<()> {
println!("example plugin on_deactivate");
Ok(())
}
fn connect_authenticate(&self, _packet: &yedmq_mqtt::v3::connect::ConnectPacket) -> Result<AuthenticationResult> {
return Ok(AuthenticationResult::Result(AuthenticationResultValue::Success("tenant_id".into())));
}
fn authorizate_acl_check(&self, _client: &yedmq_plugin::plugin::Client, _topic: &String, _action: yedmq_plugin::plugin::Action) -> Result<AuthorizationResult> {
return Ok(AuthorizationResult::Result(true));
}
fn on_publish(&self, client: &yedmq_plugin::plugin::Client, packet: &yedmq_mqtt::v3::publish::PublishPacket) {
println!("client {} receive publish packet {:?}", client.client_identifier, packet)
}
fn on_disconnect(&self, client: &yedmq_plugin::plugin::Client) {
println!("client {} disconnect", client.client_identifier)
}
}
impl Drop for ExamplePlugin {
fn drop(&mut self) {
println!("example plugin drop");
}
}
register_plugin!(ExamplePlugin, ExamplePlugin::new);
核心部分是实现 yedmq_plugin::plugin::Plugin 这个 trait, 以下是相关trait中的函数说明:
| Function | Description |
|---|---|
| on_activate | 插件被加载并初始化的时候执行。 |
| on_deactivate | 插件被卸载的时候执行。 |
| connect_authenticate | 客户端连接客Broker的时候需要进行鉴权时候执行。 |
| authorizate_acl_check | 客户端发布或者订阅指定topic的时候执行。 |
| on_publish | 客户端发布消息的时候执行。 |
| on_disconnect | 客户端断开连接的时候执行。 |
编译:
cargo build
在YedMQ的插件目录下,新建插件文件夹,并将插件元信息与动态连接库拷贝进去,文件结构如下所示:
.
├── libexample_plugin.so
└── plugin.toml