Skip to content

Commit

Permalink
status method removed from trait
Browse files Browse the repository at this point in the history
  • Loading branch information
milyin committed Nov 19, 2023
1 parent d1b4af8 commit d1e4b29
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 36 deletions.
4 changes: 2 additions & 2 deletions plugins/zenoh-backend-traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ use zenoh::time::Timestamp;
use zenoh::value::Value;
pub use zenoh::Result as ZResult;
use zenoh_plugin_trait::{
concat_enabled_features, PluginControl, PluginInstance, PluginStatus, PluginStructVersion,
concat_enabled_features, PluginControl, PluginInstance, PluginStatusRec, PluginStructVersion,
};

pub mod config;
Expand Down Expand Up @@ -235,7 +235,7 @@ impl PluginStructVersion for VolumePlugin {
}

impl PluginControl for VolumePlugin {
fn plugins_status(&self, _names: &zenoh::prelude::keyexpr) -> Vec<(String, PluginStatus)> {
fn plugins_status(&self, _names: &zenoh::prelude::keyexpr) -> Vec<(String, 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 @@ -40,7 +40,7 @@ use zenoh_core::zlock;
use zenoh_plugin_trait::Plugin;
use zenoh_plugin_trait::PluginControl;
use zenoh_plugin_trait::PluginReport;
use zenoh_plugin_trait::PluginStatus;
use zenoh_plugin_trait::PluginStatusRec;
use zenoh_result::ZResult;
use zenoh_util::LibLoader;

Expand Down Expand Up @@ -247,7 +247,7 @@ impl PluginControl for StorageRuntime {
fn report(&self) -> PluginReport {
PluginReport::default()
}
fn plugins_status(&self, names: &keyexpr) -> Vec<(String, PluginStatus)> {
fn plugins_status(&self, names: &keyexpr) -> Vec<(String, PluginStatusRec)> {
let guard = self.0.lock().unwrap();
guard
.plugins_manager
Expand Down
2 changes: 1 addition & 1 deletion plugins/zenoh-plugin-trait/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mod vtable;
pub use manager::{DeclaredPlugin, LoadedPlugin, PluginsManager, StartedPlugin};
pub use plugin::{
Plugin, PluginConditionSetter, PluginControl, PluginInstance, PluginReport, PluginStartArgs,
PluginState, PluginStatus, PluginStatusGetter, PluginStructVersion,
PluginState, PluginStatus, PluginStatusRec, PluginStructVersion,
};
pub use vtable::{Compatibility, PluginLoaderVersion, PluginVTable, PLUGIN_LOADER_VERSION};

Expand Down
24 changes: 14 additions & 10 deletions plugins/zenoh-plugin-trait/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,21 @@ use self::{
static_plugin::StaticPlugin,
};

pub trait DeclaredPlugin<StartArgs, Instance>: PluginStatusGetter {
pub trait DeclaredPlugin<StartArgs, Instance>: PluginStatus {
fn as_status(&self) -> &dyn PluginStatus;
fn load(&mut self) -> ZResult<&mut dyn LoadedPlugin<StartArgs, Instance>>;
fn loaded(&self) -> Option<&dyn LoadedPlugin<StartArgs, Instance>>;
fn loaded_mut(&mut self) -> Option<&mut dyn LoadedPlugin<StartArgs, Instance>>;
}
pub trait LoadedPlugin<StartArgs, Instance>: PluginStatusGetter {
pub trait LoadedPlugin<StartArgs, Instance>: PluginStatus {
fn as_status(&self) -> &dyn PluginStatus;
fn start(&mut self, args: &StartArgs) -> ZResult<&mut dyn StartedPlugin<StartArgs, Instance>>;
fn started(&self) -> Option<&dyn StartedPlugin<StartArgs, Instance>>;
fn started_mut(&mut self) -> Option<&mut dyn StartedPlugin<StartArgs, Instance>>;
}

pub trait StartedPlugin<StartArgs, Instance>: PluginStatusGetter {
pub trait StartedPlugin<StartArgs, Instance>: PluginStatus {
fn as_status(&self) -> &dyn PluginStatus;
fn stop(&mut self);
fn instance(&self) -> &Instance;
fn instance_mut(&mut self) -> &mut Instance;
Expand All @@ -50,7 +53,7 @@ impl<StartArgs: PluginStartArgs, Instance: PluginInstance> PluginRecord<StartArg
}
}

impl<StartArgs: PluginStartArgs, Instance: PluginInstance> PluginStatusGetter
impl<StartArgs: PluginStartArgs, Instance: PluginInstance> PluginStatus
for PluginRecord<StartArgs, Instance>
{
fn name(&self) -> &str {
Expand All @@ -68,14 +71,14 @@ impl<StartArgs: PluginStartArgs, Instance: PluginInstance> PluginStatusGetter
fn report(&self) -> PluginReport {
self.0.report()
}
fn status(&self) -> PluginStatus {
self.0.status()
}
}

impl<StartArgs: PluginStartArgs, Instance: PluginInstance> DeclaredPlugin<StartArgs, Instance>
for PluginRecord<StartArgs, Instance>
{
fn as_status(&self) -> &dyn PluginStatus {
self
}
fn load(&mut self) -> ZResult<&mut dyn LoadedPlugin<StartArgs, Instance>> {
self.0.load()
}
Expand Down Expand Up @@ -260,17 +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, PluginStatus)> {
fn plugins_status(&self, names: &keyexpr) -> Vec<(String, PluginStatusRec)> {
log::debug!(
"Plugin manager with prefix `{}` : requested plugins_status {:?}",
self.default_lib_prefix,
names
);
let mut plugins: Vec<(String, PluginStatus)> = Vec::new();
let mut plugins: Vec<(String, PluginStatusRec)> = Vec::new();
for plugin in self.declared_plugins() {
let name = unsafe { keyexpr::from_str_unchecked(plugin.name()) };
if names.includes(name) {
plugins.push((plugin.name().to_string(), plugin.status()));
let status = PluginStatusRec::new(plugin.as_status());
plugins.push((plugin.name().to_string(), status));
}
// for running plugins append their subplugins prepended with the running plugin name
if let Some(plugin) = plugin.loaded() {
Expand Down
11 changes: 10 additions & 1 deletion plugins/zenoh-plugin-trait/src/manager/dynamic_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl<StartArgs, Instance> DynamicPlugin<StartArgs, Instance> {
}
}

impl<StartArgs: PluginStartArgs, Instance: PluginInstance> PluginStatusGetter
impl<StartArgs: PluginStartArgs, Instance: PluginInstance> PluginStatus
for DynamicPlugin<StartArgs, Instance>
{
fn name(&self) -> &str {
Expand Down Expand Up @@ -161,6 +161,9 @@ impl<StartArgs: PluginStartArgs, Instance: PluginInstance> PluginStatusGetter
impl<StartArgs: PluginStartArgs, Instance: PluginInstance> DeclaredPlugin<StartArgs, Instance>
for DynamicPlugin<StartArgs, Instance>
{
fn as_status(&self) -> &dyn PluginStatus {
self
}
fn load(&mut self) -> ZResult<&mut dyn LoadedPlugin<StartArgs, Instance>> {
if self.starter.is_none() {
let (lib, path) = self.source.load().add_error(&mut self.report)?;
Expand Down Expand Up @@ -191,6 +194,9 @@ impl<StartArgs: PluginStartArgs, Instance: PluginInstance> DeclaredPlugin<StartA
impl<StartArgs: PluginStartArgs, Instance: PluginInstance> LoadedPlugin<StartArgs, Instance>
for DynamicPlugin<StartArgs, Instance>
{
fn as_status(&self) -> &dyn PluginStatus {
self
}
fn start(&mut self, args: &StartArgs) -> ZResult<&mut dyn StartedPlugin<StartArgs, Instance>> {
let starter = self
.starter
Expand Down Expand Up @@ -228,6 +234,9 @@ impl<StartArgs: PluginStartArgs, Instance: PluginInstance> LoadedPlugin<StartArg
impl<StartArgs: PluginStartArgs, Instance: PluginInstance> StartedPlugin<StartArgs, Instance>
for DynamicPlugin<StartArgs, Instance>
{
fn as_status(&self) -> &dyn PluginStatus {
self
}
fn stop(&mut self) {
log::debug!("Plugin `{}` stopped", self.name);
self.report.clear();
Expand Down
12 changes: 10 additions & 2 deletions plugins/zenoh-plugin-trait/src/manager/static_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ where
}
}

impl<StartArgs, Instance: PluginInstance, P> PluginStatusGetter
for StaticPlugin<StartArgs, Instance, P>
impl<StartArgs, Instance: PluginInstance, P> PluginStatus for StaticPlugin<StartArgs, Instance, P>
where
P: Plugin<StartArgs = StartArgs, Instance = Instance>,
{
Expand Down Expand Up @@ -68,6 +67,9 @@ impl<StartArgs, Instance: PluginInstance, P> DeclaredPlugin<StartArgs, Instance>
where
P: Plugin<StartArgs = StartArgs, Instance = Instance>,
{
fn as_status(&self) -> &dyn PluginStatus {
self
}
fn load(&mut self) -> ZResult<&mut dyn LoadedPlugin<StartArgs, Instance>> {
Ok(self)
}
Expand All @@ -84,6 +86,9 @@ impl<StartArgs, Instance: PluginInstance, P> LoadedPlugin<StartArgs, Instance>
where
P: Plugin<StartArgs = StartArgs, Instance = Instance>,
{
fn as_status(&self) -> &dyn PluginStatus {
self
}
fn start(&mut self, args: &StartArgs) -> ZResult<&mut dyn StartedPlugin<StartArgs, Instance>> {
if self.instance.is_none() {
log::debug!("Plugin `{}` started", self.name());
Expand Down Expand Up @@ -114,6 +119,9 @@ impl<StartArgs, Instance: PluginInstance, P> StartedPlugin<StartArgs, Instance>
where
P: Plugin<StartArgs = StartArgs, Instance = Instance>,
{
fn as_status(&self) -> &dyn PluginStatus {
self
}
fn stop(&mut self) {
log::debug!("Plugin `{}` stopped", self.name());
self.instance = None;
Expand Down
33 changes: 17 additions & 16 deletions plugins/zenoh-plugin-trait/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub struct PluginReport {

/// The status of a plugin contains all information about the plugin in single cloneable structure
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct PluginStatus<'a> {
pub struct PluginStatusRec<'a> {
pub name: Cow<'a, str>,
#[serde(skip_serializing_if = "Option::is_none")]
pub version: Option<Cow<'a, str>>,
Expand All @@ -68,9 +68,20 @@ pub struct PluginStatus<'a> {
pub report: PluginReport,
}

impl PluginStatus<'_> {
pub fn into_owned(self) -> PluginStatus<'static> {
PluginStatus {
impl<'a> PluginStatusRec<'a> {
/// Construst status fructure from the getter interface
pub fn new<T: PluginStatus + ?Sized>(plugin: &'a T) -> Self {
Self {
name: Cow::Borrowed(plugin.name()),
version: plugin.version().map(Cow::Borrowed),
path: Cow::Borrowed(plugin.path()),
state: plugin.state(),
report: plugin.report(),
}
}
/// Convert status structure to owned version
pub fn into_owned(self) -> PluginStatusRec<'static> {
PluginStatusRec {
name: Cow::Owned(self.name.into_owned()),
version: self.version.map(|v| Cow::Owned(v.into_owned())),
path: Cow::Owned(self.path.into_owned()),
Expand All @@ -80,7 +91,7 @@ impl PluginStatus<'_> {
}
}

pub trait PluginStatusGetter {
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)
Expand All @@ -92,24 +103,14 @@ pub trait PluginStatusGetter {
/// 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;
/// Returns all the information about the plugin in signed structure
fn status(&self) -> PluginStatus {
PluginStatus {
name: Cow::Borrowed(self.name()),
version: self.version().map(Cow::Borrowed),
path: Cow::Borrowed(self.path()),
state: self.state(),
report: self.report(),
}
}
}

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, PluginStatus)> {
fn plugins_status(&self, _names: &keyexpr) -> Vec<(String, PluginStatusRec)> {
Vec::new()
}
}
Expand Down
4 changes: 2 additions & 2 deletions zenoh/src/plugins/sealed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use zenoh_plugin_trait::Plugin;
use zenoh_plugin_trait::PluginControl;
use zenoh_plugin_trait::PluginInstance;
use zenoh_plugin_trait::PluginReport;
use zenoh_plugin_trait::PluginStatus;
use zenoh_plugin_trait::PluginStatusRec;
use zenoh_plugin_trait::PluginStructVersion;
use zenoh_protocol::core::key_expr::keyexpr;

Expand Down Expand Up @@ -53,7 +53,7 @@ impl PluginControl for RunningPlugin {
self.as_ref().report()
}

fn plugins_status(&self, names: &keyexpr) -> Vec<(String, PluginStatus)> {
fn plugins_status(&self, names: &keyexpr) -> Vec<(String, PluginStatusRec)> {
self.as_ref().plugins_status(names)
}
}
Expand Down

0 comments on commit d1e4b29

Please sign in to comment.