diff --git a/Cargo.lock b/Cargo.lock index aa70648..a553fcb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4557,7 +4557,7 @@ dependencies = [ [[package]] name = "zenoh" version = "0.11.0-dev" -source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#b669489bc814a758741f09c671ecc3a0683697a0" +source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#8cd786f2192fd2aa7387432ae93cdd78f5db1df2" dependencies = [ "async-global-executor", "async-std", @@ -4621,7 +4621,7 @@ dependencies = [ [[package]] name = "zenoh-buffers" version = "0.11.0-dev" -source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#b669489bc814a758741f09c671ecc3a0683697a0" +source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#8cd786f2192fd2aa7387432ae93cdd78f5db1df2" dependencies = [ "zenoh-collections", ] @@ -4629,7 +4629,7 @@ dependencies = [ [[package]] name = "zenoh-codec" version = "0.11.0-dev" -source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#b669489bc814a758741f09c671ecc3a0683697a0" +source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#8cd786f2192fd2aa7387432ae93cdd78f5db1df2" dependencies = [ "log", "serde", @@ -4641,12 +4641,12 @@ dependencies = [ [[package]] name = "zenoh-collections" version = "0.11.0-dev" -source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#b669489bc814a758741f09c671ecc3a0683697a0" +source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#8cd786f2192fd2aa7387432ae93cdd78f5db1df2" [[package]] name = "zenoh-config" version = "0.11.0-dev" -source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#b669489bc814a758741f09c671ecc3a0683697a0" +source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#8cd786f2192fd2aa7387432ae93cdd78f5db1df2" dependencies = [ "flume 0.11.0", "json5", @@ -4665,7 +4665,7 @@ dependencies = [ [[package]] name = "zenoh-core" version = "0.11.0-dev" -source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#b669489bc814a758741f09c671ecc3a0683697a0" +source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#8cd786f2192fd2aa7387432ae93cdd78f5db1df2" dependencies = [ "async-std", "lazy_static", @@ -4675,7 +4675,7 @@ dependencies = [ [[package]] name = "zenoh-crypto" version = "0.11.0-dev" -source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#b669489bc814a758741f09c671ecc3a0683697a0" +source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#8cd786f2192fd2aa7387432ae93cdd78f5db1df2" dependencies = [ "aes 0.8.3", "hmac 0.12.1", @@ -4688,7 +4688,7 @@ dependencies = [ [[package]] name = "zenoh-ext" version = "0.11.0-dev" -source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#b669489bc814a758741f09c671ecc3a0683697a0" +source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#8cd786f2192fd2aa7387432ae93cdd78f5db1df2" dependencies = [ "async-std", "bincode", @@ -4708,7 +4708,7 @@ dependencies = [ [[package]] name = "zenoh-keyexpr" version = "0.11.0-dev" -source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#b669489bc814a758741f09c671ecc3a0683697a0" +source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#8cd786f2192fd2aa7387432ae93cdd78f5db1df2" dependencies = [ "hashbrown 0.14.0", "keyed-set", @@ -4722,7 +4722,7 @@ dependencies = [ [[package]] name = "zenoh-link" version = "0.11.0-dev" -source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#b669489bc814a758741f09c671ecc3a0683697a0" +source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#8cd786f2192fd2aa7387432ae93cdd78f5db1df2" dependencies = [ "async-std", "async-trait", @@ -4741,7 +4741,7 @@ dependencies = [ [[package]] name = "zenoh-link-commons" version = "0.11.0-dev" -source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#b669489bc814a758741f09c671ecc3a0683697a0" +source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#8cd786f2192fd2aa7387432ae93cdd78f5db1df2" dependencies = [ "async-std", "async-trait", @@ -4758,7 +4758,7 @@ dependencies = [ [[package]] name = "zenoh-link-quic" version = "0.11.0-dev" -source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#b669489bc814a758741f09c671ecc3a0683697a0" +source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#8cd786f2192fd2aa7387432ae93cdd78f5db1df2" dependencies = [ "async-rustls", "async-std", @@ -4784,7 +4784,7 @@ dependencies = [ [[package]] name = "zenoh-link-tcp" version = "0.11.0-dev" -source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#b669489bc814a758741f09c671ecc3a0683697a0" +source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#8cd786f2192fd2aa7387432ae93cdd78f5db1df2" dependencies = [ "async-std", "async-trait", @@ -4800,7 +4800,7 @@ dependencies = [ [[package]] name = "zenoh-link-tls" version = "0.11.0-dev" -source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#b669489bc814a758741f09c671ecc3a0683697a0" +source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#8cd786f2192fd2aa7387432ae93cdd78f5db1df2" dependencies = [ "async-rustls", "async-std", @@ -4825,7 +4825,7 @@ dependencies = [ [[package]] name = "zenoh-link-udp" version = "0.11.0-dev" -source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#b669489bc814a758741f09c671ecc3a0683697a0" +source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#8cd786f2192fd2aa7387432ae93cdd78f5db1df2" dependencies = [ "async-std", "async-trait", @@ -4844,7 +4844,7 @@ dependencies = [ [[package]] name = "zenoh-link-unixsock_stream" version = "0.11.0-dev" -source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#b669489bc814a758741f09c671ecc3a0683697a0" +source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#8cd786f2192fd2aa7387432ae93cdd78f5db1df2" dependencies = [ "async-std", "async-trait", @@ -4862,7 +4862,7 @@ dependencies = [ [[package]] name = "zenoh-link-ws" version = "0.11.0-dev" -source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#b669489bc814a758741f09c671ecc3a0683697a0" +source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#8cd786f2192fd2aa7387432ae93cdd78f5db1df2" dependencies = [ "async-std", "async-trait", @@ -4882,7 +4882,7 @@ dependencies = [ [[package]] name = "zenoh-macros" version = "0.11.0-dev" -source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#b669489bc814a758741f09c671ecc3a0683697a0" +source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#8cd786f2192fd2aa7387432ae93cdd78f5db1df2" dependencies = [ "proc-macro2", "quote", @@ -4923,12 +4923,13 @@ dependencies = [ [[package]] name = "zenoh-plugin-rest" version = "0.11.0-dev" -source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#b669489bc814a758741f09c671ecc3a0683697a0" +source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#8cd786f2192fd2aa7387432ae93cdd78f5db1df2" dependencies = [ "anyhow", "async-std", "base64 0.21.4", "clap 4.4.11", + "const_format", "env_logger", "flume 0.11.0", "futures", @@ -4951,11 +4952,14 @@ dependencies = [ [[package]] name = "zenoh-plugin-trait" version = "0.11.0-dev" -source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#b669489bc814a758741f09c671ecc3a0683697a0" +source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#8cd786f2192fd2aa7387432ae93cdd78f5db1df2" dependencies = [ + "const_format", "libloading", "log", + "serde", "serde_json", + "zenoh-keyexpr", "zenoh-macros", "zenoh-result", "zenoh-util", @@ -4964,7 +4968,7 @@ dependencies = [ [[package]] name = "zenoh-protocol" version = "0.11.0-dev" -source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#b669489bc814a758741f09c671ecc3a0683697a0" +source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#8cd786f2192fd2aa7387432ae93cdd78f5db1df2" dependencies = [ "const_format", "hex", @@ -4980,7 +4984,7 @@ dependencies = [ [[package]] name = "zenoh-result" version = "0.11.0-dev" -source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#b669489bc814a758741f09c671ecc3a0683697a0" +source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#8cd786f2192fd2aa7387432ae93cdd78f5db1df2" dependencies = [ "anyhow", ] @@ -4988,7 +4992,7 @@ dependencies = [ [[package]] name = "zenoh-sync" version = "0.11.0-dev" -source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#b669489bc814a758741f09c671ecc3a0683697a0" +source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#8cd786f2192fd2aa7387432ae93cdd78f5db1df2" dependencies = [ "async-std", "event-listener 4.0.0", @@ -5003,7 +5007,7 @@ dependencies = [ [[package]] name = "zenoh-transport" version = "0.11.0-dev" -source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#b669489bc814a758741f09c671ecc3a0683697a0" +source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#8cd786f2192fd2aa7387432ae93cdd78f5db1df2" dependencies = [ "async-executor", "async-global-executor", @@ -5034,7 +5038,7 @@ dependencies = [ [[package]] name = "zenoh-util" version = "0.11.0-dev" -source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#b669489bc814a758741f09c671ecc3a0683697a0" +source = "git+https://github.com/eclipse-zenoh/zenoh.git?branch=main#8cd786f2192fd2aa7387432ae93cdd78f5db1df2" dependencies = [ "async-std", "async-trait", diff --git a/README.md b/README.md index c9d4b4b..662d328 100644 --- a/README.md +++ b/README.md @@ -148,19 +148,8 @@ Then, you may clone the repository on your machine: ```bash $ git clone https://github.com/eclipse-zenoh/zenoh-plugin-mqtt.git $ cd zenoh-plugin-mqtt +$ cargo build --release ``` -> :warning: **WARNING** :warning: : On Windows and Linux, don't use `cargo build` command without specifying a package with `-p`. Building both `zenoh-plugin-mqtt` (plugin library) and `zenoh-bridge-mqtt` (standalone executable) together will lead to a `multiple definition of `load_plugin'` error at link time. See [#19](https://github.com/eclipse-zenoh/zenoh-plugin-mqtt/issues/19#issuecomment-1754742678) for explanations. - -You can then choose between building the zenoh bridge for MQTT: -- as a plugin library that can be dynamically loaded by the zenoh router (`zenohd`): -```bash -$ cargo build --release -p zenoh-plugin-mqtt -``` -The plugin shared library (`*.so` on Linux, `*.dylib` on Mac OS, `*.dll` on Windows) will be generated in the `target/release` subdirectory. - -- or as a standalone executable binary: -```bash -$ cargo build --release -p zenoh-bridge-mqtt -``` -The **`zenoh-bridge-mqtt`** binary will be generated in the `target/release` sub-directory. +The standalone executable binary `zenoh-bridge-mqtt` and a plugin shared library (`*.so` on Linux, `*.dylib` on Mac OS, `*.dll` on Windows) to be dynamically +loaded by the zenoh router `zenohd` will be generated in the `target/release` subdirectory. diff --git a/zenoh-bridge-mqtt/src/main.rs b/zenoh-bridge-mqtt/src/main.rs index 6163929..6b8188d 100644 --- a/zenoh-bridge-mqtt/src/main.rs +++ b/zenoh-bridge-mqtt/src/main.rs @@ -15,6 +15,7 @@ use clap::{App, Arg}; use std::str::FromStr; use zenoh::config::{Config, ModeDependentValue}; use zenoh::prelude::*; +use zenoh_plugin_trait::Plugin; macro_rules! insert_json5 { ($config: expr, $args: expr, $key: expr, if $name: expr) => { @@ -36,8 +37,8 @@ macro_rules! insert_json5 { fn parse_args() -> Config { let app = App::new("zenoh bridge for MQTT") - .version(zenoh_plugin_mqtt::GIT_VERSION) - .long_version(zenoh_plugin_mqtt::LONG_VERSION.as_str()) + .version(zenoh_plugin_mqtt::MqttPlugin::PLUGIN_VERSION) + .long_version(zenoh_plugin_mqtt::MqttPlugin::PLUGIN_LONG_VERSION) // // zenoh related arguments: // @@ -168,7 +169,10 @@ r#"-w, --generalise-pub=[String]... 'A list of key expression to use for gener #[async_std::main] async fn main() { env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("z=info")).init(); - log::info!("zenoh-bridge-mqtt {}", *zenoh_plugin_mqtt::LONG_VERSION); + log::info!( + "zenoh-bridge-mqtt {}", + zenoh_plugin_mqtt::MqttPlugin::PLUGIN_LONG_VERSION + ); let config = parse_args(); let rest_plugin = config.plugin("rest").is_some(); diff --git a/zenoh-plugin-mqtt/Cargo.toml b/zenoh-plugin-mqtt/Cargo.toml index 5335aef..dc4af37 100644 --- a/zenoh-plugin-mqtt/Cargo.toml +++ b/zenoh-plugin-mqtt/Cargo.toml @@ -28,7 +28,7 @@ crate-type = ["cdylib", "rlib"] [features] default = ["no_mangle"] -no_mangle = ["zenoh-plugin-trait/no_mangle"] +no_mangle = [] stats = ["zenoh/stats"] [dependencies] diff --git a/zenoh-plugin-mqtt/src/lib.rs b/zenoh-plugin-mqtt/src/lib.rs index fd6465a..39b4139 100644 --- a/zenoh-plugin-mqtt/src/lib.rs +++ b/zenoh-plugin-mqtt/src/lib.rs @@ -11,20 +11,21 @@ // Contributors: // ZettaScale Zenoh Team, // -use git_version::git_version; use ntex::service::{fn_factory_with_config, fn_service}; use ntex::util::Ready; use ntex_mqtt::{v3, v5, MqttServer}; use serde_json::Value; use std::env; use std::sync::Arc; -use zenoh::plugins::{Plugin, RunningPluginTrait, Runtime, ZenohPlugin}; +use zenoh::plugins::{RunningPluginTrait, ZenohPlugin}; use zenoh::prelude::r#async::*; use zenoh::queryable::Query; +use zenoh::runtime::Runtime; use zenoh::Result as ZResult; use zenoh::Session; +use zenoh_core::zerror; use zenoh_core::zresult::ZError; -use zenoh_core::{bail, zerror}; +use zenoh_plugin_trait::{plugin_long_version, plugin_version, Plugin, PluginControl}; #[macro_use] extern crate zenoh_core; @@ -41,14 +42,13 @@ macro_rules! ke_for_sure { }; } -pub const GIT_VERSION: &str = git_version!(prefix = "v", cargo_prefix = "v"); lazy_static::lazy_static! { - pub static ref LONG_VERSION: String = format!("{} built with {}", GIT_VERSION, env!("RUSTC_VERSION")); static ref KE_PREFIX_ADMIN_SPACE: &'static keyexpr = ke_for_sure!("@/service"); static ref ADMIN_SPACE_KE_VERSION: &'static keyexpr = ke_for_sure!("version"); static ref ADMIN_SPACE_KE_CONFIG: &'static keyexpr = ke_for_sure!("config"); } +#[cfg(feature = "no_mangle")] zenoh_plugin_trait::declare_plugin!(MqttPlugin); pub struct MqttPlugin; @@ -56,9 +56,11 @@ pub struct MqttPlugin; impl ZenohPlugin for MqttPlugin {} impl Plugin for MqttPlugin { type StartArgs = Runtime; - type RunningPlugin = zenoh::plugins::RunningPlugin; + type Instance = zenoh::plugins::RunningPlugin; - const STATIC_NAME: &'static str = "zenoh-plugin-mqtt"; + const DEFAULT_NAME: &'static str = "zenoh-plugin-mqtt"; + const PLUGIN_LONG_VERSION: &'static str = plugin_long_version!(); + const PLUGIN_VERSION: &'static str = plugin_version!(); fn start(name: &str, runtime: &Self::StartArgs) -> ZResult { // Try to initiate login. @@ -66,7 +68,7 @@ impl Plugin for MqttPlugin { // But cannot be done twice in case of static link. let _ = env_logger::try_init(); - let runtime_conf = runtime.config.lock(); + let runtime_conf = runtime.config().lock(); let plugin_conf = runtime_conf .plugin(name) .ok_or_else(|| zerror!("Plugin `{}`: missing config", name))?; @@ -77,40 +79,15 @@ impl Plugin for MqttPlugin { } } -impl RunningPluginTrait for MqttPlugin { - fn config_checker(&self) -> zenoh::plugins::ValidationFunction { - Arc::new(|_, _, _| bail!("zenoh-plugin-mqtt does not support hot configuration changes.")) - } - - fn adminspace_getter<'a>( - &'a self, - selector: &'a Selector<'a>, - plugin_status_key: &str, - ) -> ZResult> { - log::error!( - "adminspace_getter {} - plugin_status_key: {}", - selector, - plugin_status_key - ); - let mut responses = Vec::new(); - let version_key = [plugin_status_key, "/__version__"].concat(); - if selector.key_expr.intersects(ke_for_sure!(&version_key)) { - log::error!("adminspace_getter reply version"); - responses.push(zenoh::plugins::Response::new( - version_key, - GIT_VERSION.into(), - )); - } - Ok(responses) - } -} +impl PluginControl for MqttPlugin {} +impl RunningPluginTrait for MqttPlugin {} async fn run(runtime: Runtime, config: Config) { // Try to initiate login. // Required in case of dynamic lib, otherwise no logs. // But cannot be done twice in case of static link. let _ = env_logger::try_init(); - log::debug!("MQTT plugin {}", LONG_VERSION.as_str()); + log::debug!("MQTT plugin {}", MqttPlugin::PLUGIN_LONG_VERSION); log::debug!("MQTT plugin {:?}", config); // init Zenoh Session with provided Runtime @@ -142,7 +119,7 @@ async fn run(runtime: Runtime, config: Config) { // Start MQTT Server task let config = Arc::new(config); - ntex::rt::System::new(MqttPlugin::STATIC_NAME) + ntex::rt::System::new(MqttPlugin::DEFAULT_NAME) .block_on(async move { ntex::server::Server::build() .bind("mqtt", config.port.clone(), move |_| { @@ -217,7 +194,10 @@ fn treat_admin_query(query: Query, admin_keyexpr_prefix: &keyexpr, config: &Conf let mut kvs: Vec<(&keyexpr, Value)> = Vec::with_capacity(sub_kes.len()); for sub_ke in sub_kes { if sub_ke.intersects(&ADMIN_SPACE_KE_VERSION) { - kvs.push((&ADMIN_SPACE_KE_VERSION, Value::String(LONG_VERSION.clone()))); + kvs.push(( + &ADMIN_SPACE_KE_VERSION, + Value::String(MqttPlugin::PLUGIN_LONG_VERSION.to_string()), + )); } if sub_ke.intersects(&ADMIN_SPACE_KE_CONFIG) { kvs.push(( diff --git a/zenoh-plugin-mqtt/src/mqtt_helpers.rs b/zenoh-plugin-mqtt/src/mqtt_helpers.rs index 92c21a4..9bd52e1 100644 --- a/zenoh-plugin-mqtt/src/mqtt_helpers.rs +++ b/zenoh-plugin-mqtt/src/mqtt_helpers.rs @@ -15,8 +15,8 @@ use ntex::util::{ByteString, Bytes}; use ntex_mqtt::{error::SendPacketError, v3, v5}; use std::convert::TryInto; -use zenoh::plugins::ZResult; use zenoh::prelude::*; +use zenoh::Result as ZResult; use crate::config::Config; diff --git a/zenoh-plugin-mqtt/src/mqtt_session_state.rs b/zenoh-plugin-mqtt/src/mqtt_session_state.rs index e9c844a..710c140 100644 --- a/zenoh-plugin-mqtt/src/mqtt_session_state.rs +++ b/zenoh-plugin-mqtt/src/mqtt_session_state.rs @@ -19,9 +19,9 @@ use lazy_static::__Deref; use ntex::util::{ByteString, Bytes}; use std::convert::TryInto; use std::{collections::HashMap, sync::Arc}; -use zenoh::plugins::ZResult; use zenoh::prelude::r#async::*; use zenoh::subscriber::Subscriber; +use zenoh::Result as ZResult; #[derive(Debug)] pub(crate) struct MqttSessionState<'a> {