Skip to content

Commit

Permalink
removed tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
milyin committed Nov 19, 2023
1 parent d1e4b29 commit ce3f585
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 29 deletions.
2 changes: 1 addition & 1 deletion plugins/zenoh-backend-traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PluginStatusRec> {
Vec::new()
}
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/zenoh-plugin-storage-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PluginStatusRec> {
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()
}
}
Expand Down
10 changes: 5 additions & 5 deletions plugins/zenoh-plugin-trait/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,18 +263,18 @@ impl<StartArgs: PluginStartArgs + 'static, Instance: PluginInstance + 'static>
impl<StartArgs: PluginStartArgs + 'static, Instance: PluginInstance + 'static> PluginControl
for PluginsManager<StartArgs, Instance>
{
fn plugins_status(&self, names: &keyexpr) -> Vec<(String, PluginStatusRec)> {
fn plugins_status(&self, names: &keyexpr) -> Vec<PluginStatusRec> {
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() {
Expand All @@ -284,8 +284,8 @@ impl<StartArgs: PluginStartArgs + 'static, Instance: PluginInstance + 'static> 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(),
);
}
Expand Down
57 changes: 41 additions & 16 deletions plugins/zenoh-plugin-trait/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,22 @@ pub struct PluginReport {
messages: Vec<Cow<'static, str>>,
}

/// 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>,
Expand All @@ -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<T: PluginStatus + ?Sized>(plugin: &'a T) -> Self {
Expand All @@ -89,28 +122,20 @@ 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 {
fn report(&self) -> PluginReport {
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<PluginStatusRec> {
Vec::new()
}
}
Expand Down
8 changes: 4 additions & 4 deletions zenoh/src/net/runtime/adminspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion zenoh/src/plugins/sealed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PluginStatusRec> {
self.as_ref().plugins_status(names)
}
}
Expand Down

0 comments on commit ce3f585

Please sign in to comment.