diff --git a/plugins/zenoh-backend-traits/src/lib.rs b/plugins/zenoh-backend-traits/src/lib.rs index 0e1e9ecf74..090a377e9f 100644 --- a/plugins/zenoh-backend-traits/src/lib.rs +++ b/plugins/zenoh-backend-traits/src/lib.rs @@ -235,7 +235,7 @@ impl PluginStructVersion for VolumePlugin { } impl PluginControl for VolumePlugin { - fn plugins_status(&self, _names: &zenoh::prelude::keyexpr) -> Vec<(String, PluginStatusRec)> { + fn plugins_status(&self, _names: &zenoh::prelude::keyexpr) -> Vec { Vec::new() } } diff --git a/plugins/zenoh-plugin-storage-manager/src/lib.rs b/plugins/zenoh-plugin-storage-manager/src/lib.rs index 997b48971a..1767201300 100644 --- a/plugins/zenoh-plugin-storage-manager/src/lib.rs +++ b/plugins/zenoh-plugin-storage-manager/src/lib.rs @@ -247,13 +247,13 @@ impl PluginControl for StorageRuntime { fn report(&self) -> PluginReport { PluginReport::default() } - fn plugins_status(&self, names: &keyexpr) -> Vec<(String, PluginStatusRec)> { + fn plugins_status(&self, names: &keyexpr) -> Vec { let guard = self.0.lock().unwrap(); guard .plugins_manager .plugins_status(names) .into_iter() - .map(|(k, v)| (k, v.into_owned())) + .map(PluginStatusRec::into_owned) .collect() } } diff --git a/plugins/zenoh-plugin-trait/src/manager.rs b/plugins/zenoh-plugin-trait/src/manager.rs index cf5b12d644..ea6b88f671 100644 --- a/plugins/zenoh-plugin-trait/src/manager.rs +++ b/plugins/zenoh-plugin-trait/src/manager.rs @@ -263,18 +263,18 @@ impl impl PluginControl for PluginsManager { - fn plugins_status(&self, names: &keyexpr) -> Vec<(String, PluginStatusRec)> { + fn plugins_status(&self, names: &keyexpr) -> Vec { log::debug!( "Plugin manager with prefix `{}` : requested plugins_status {:?}", self.default_lib_prefix, names ); - let mut plugins: Vec<(String, PluginStatusRec)> = Vec::new(); + let mut plugins = Vec::new(); for plugin in self.declared_plugins() { let name = unsafe { keyexpr::from_str_unchecked(plugin.name()) }; if names.includes(name) { let status = PluginStatusRec::new(plugin.as_status()); - plugins.push((plugin.name().to_string(), status)); + plugins.push(status); } // for running plugins append their subplugins prepended with the running plugin name if let Some(plugin) = plugin.loaded() { @@ -284,8 +284,8 @@ impl P &mut plugin .instance() .plugins_status(names) - .iter() - .map(|(n, s)| (format!("{}/{}", name, n), s.clone())) + .into_iter() + .map(|s| s.prepend_name(name)) .collect(), ); } diff --git a/plugins/zenoh-plugin-trait/src/plugin.rs b/plugins/zenoh-plugin-trait/src/plugin.rs index 9a78434f31..21a978a26c 100644 --- a/plugins/zenoh-plugin-trait/src/plugin.rs +++ b/plugins/zenoh-plugin-trait/src/plugin.rs @@ -57,7 +57,22 @@ pub struct PluginReport { messages: Vec>, } -/// The status of a plugin contains all information about the plugin in single cloneable structure +/// Trait allowing to get all information about the plugin +pub trait PluginStatus { + /// Returns name of the plugin + fn name(&self) -> &str; + /// Returns version of the loaded plugin (usually the version of the plugin's crate) + fn version(&self) -> Option<&str>; + /// Returns path of the loaded plugin + fn path(&self) -> &str; + /// Returns the plugin's state (Declared, Loaded, Started) + fn state(&self) -> PluginState; + /// Returns the plugin's current report: a list of messages and the severity level + /// When status is changed, report is cleared + fn report(&self) -> PluginReport; +} + +/// The structure which contains all information about the plugin status in single cloneable structure #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct PluginStatusRec<'a> { pub name: Cow<'a, str>, @@ -68,6 +83,24 @@ pub struct PluginStatusRec<'a> { pub report: PluginReport, } +impl PluginStatus for PluginStatusRec<'_> { + fn name(&self) -> &str { + &self.name + } + fn version(&self) -> Option<&str> { + self.version.as_deref() + } + fn path(&self) -> &str { + &self.path + } + fn state(&self) -> PluginState { + self.state + } + fn report(&self) -> PluginReport { + self.report.clone() + } +} + impl<'a> PluginStatusRec<'a> { /// Construst status fructure from the getter interface pub fn new(plugin: &'a T) -> Self { @@ -89,20 +122,12 @@ impl<'a> PluginStatusRec<'a> { report: self.report, } } -} - -pub trait PluginStatus { - /// Returns name of the plugin - fn name(&self) -> &str; - /// Returns version of the loaded plugin (usually the version of the plugin's crate) - fn version(&self) -> Option<&str>; - /// Returns path of the loaded plugin - fn path(&self) -> &str; - /// Returns the plugin's state (Declared, Loaded, Started) - fn state(&self) -> PluginState; - /// Returns the plugin's current report: a list of messages and the severity level - /// When status is changed, report is cleared - fn report(&self) -> PluginReport; + pub(crate) fn prepend_name(self, prefix: &str) -> Self { + Self { + name: Cow::Owned(format!("{}/{}", prefix, self.name)), + ..self + } + } } pub trait PluginControl { @@ -110,7 +135,7 @@ pub trait PluginControl { PluginReport::default() } /// Collect information of sub-plugins matching `_names` keyexpr - fn plugins_status(&self, _names: &keyexpr) -> Vec<(String, PluginStatusRec)> { + fn plugins_status(&self, _names: &keyexpr) -> Vec { Vec::new() } } diff --git a/zenoh/src/net/runtime/adminspace.rs b/zenoh/src/net/runtime/adminspace.rs index 40c39535bf..637ec01809 100644 --- a/zenoh/src/net/runtime/adminspace.rs +++ b/zenoh/src/net/runtime/adminspace.rs @@ -28,7 +28,7 @@ use std::sync::Arc; use std::sync::Mutex; use zenoh_buffers::SplitBuffer; use zenoh_config::{ConfigValidator, ValidatedMap}; -use zenoh_plugin_trait::PluginControl; +use zenoh_plugin_trait::{PluginControl, PluginStatus}; use zenoh_protocol::core::key_expr::keyexpr; use zenoh_protocol::{ core::{key_expr::OwnedKeyExpr, ExprId, KnownEncoding, WireExpr, ZenohId, EMPTY_EXPR_ID}, @@ -663,9 +663,9 @@ fn plugins_data(context: &AdminContext, query: Query) { log::debug!("requested plugins status {:?}", query.key_expr()); if let [names, ..] = query.key_expr().strip_prefix(root_key)[..] { let statuses = guard.plugins_status(names); - for (name, status) in statuses { - log::debug!("plugin {} status: {:?}", name, status); - let key = root_key.join(&name).unwrap(); + for status in statuses { + log::debug!("plugin status: {:?}", status); + let key = root_key.join(status.name()).unwrap(); let status = serde_json::to_value(status).unwrap(); if let Err(e) = query.reply(Ok(Sample::new(key, Value::from(status)))).res() { log::error!("Error sending AdminSpace reply: {:?}", e); diff --git a/zenoh/src/plugins/sealed.rs b/zenoh/src/plugins/sealed.rs index 3a4df52b83..f3c31be2ea 100644 --- a/zenoh/src/plugins/sealed.rs +++ b/zenoh/src/plugins/sealed.rs @@ -53,7 +53,7 @@ impl PluginControl for RunningPlugin { self.as_ref().report() } - fn plugins_status(&self, names: &keyexpr) -> Vec<(String, PluginStatusRec)> { + fn plugins_status(&self, names: &keyexpr) -> Vec { self.as_ref().plugins_status(names) } }