Skip to content

Commit

Permalink
unfinished
Browse files Browse the repository at this point in the history
  • Loading branch information
milyin committed Oct 31, 2023
1 parent 0eaa517 commit be3382d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 11 deletions.
22 changes: 22 additions & 0 deletions plugins/zenoh-plugin-trait/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,28 @@ pub trait CompatibilityVersion {
fn features() -> &'static str;
}

pub enum PluginState {
Declared,
Loaded,
Started,
}

pub enum PluginCondition {
Ok,
Warning(String),
Error(String),
}

pub struct PluginStatus {
pub state: PluginState,
pub condition: PluginCondition,
}

pub trait PluginControl {
fn plugins(&self) -> Vec<&str>;
fn status(&self, name: &str) -> PluginStatus;
}

pub trait Plugin: Sized + 'static {
type StartArgs: CompatibilityVersion;
type RunningPlugin: CompatibilityVersion;
Expand Down
74 changes: 63 additions & 11 deletions plugins/zenoh-plugin-trait/src/loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,17 +349,28 @@ impl<StartArgs: CompatibilityVersion, RunningPlugin: CompatibilityVersion>
}
}

struct

pub struct DynamicPlugin<StartArgs: CompatibilityVersion, RunningPlugin: CompatibilityVersion> {
_lib: Library,
vtable: PluginVTable<StartArgs, RunningPlugin>,
pub name: String,
pub path: PathBuf,
name: String,
lib: Option<Library>,
vtable: Option<PluginVTable<StartArgs, RunningPlugin>>,
path: Option<PathBuf>,
}

impl<StartArgs: CompatibilityVersion, RunningPlugin: CompatibilityVersion>
DynamicPlugin<StartArgs, RunningPlugin>
{
fn new(name: String, lib: Library, path: PathBuf) -> ZResult<Self> {
pub fn new(name: String, lib: Library, path: PathBuf) -> Self {
Self {
name,
lib: None,
vtable: None,
path: None,
}
}

fn get_vtable(path: &PathBuf) -> ZResult<PluginVTable<StartArgs, RunningPlugin>> {
log::debug!("Loading plugin {}", &path.to_str().unwrap(),);
let get_plugin_loader_version =
unsafe { lib.get::<fn() -> PluginLoaderVersion>(b"get_plugin_loader_version")? };
Expand Down Expand Up @@ -389,11 +400,52 @@ impl<StartArgs: CompatibilityVersion, RunningPlugin: CompatibilityVersion>
let load_plugin =
unsafe { lib.get::<fn() -> PluginVTable<StartArgs, RunningPlugin>>(b"load_plugin")? };
let vtable = load_plugin();
Ok(Self {
_lib: lib,
vtable,
name,
path,
})
Ok(vtable)
}

/// Tries to load a plugin with the name `libname` + `.so | .dll | .dylib`
/// in lib_loader's search paths.
/// Returns a tuple of (retval, plugin_record)
/// where `retval`` is true if the plugin was successfully loaded, false if pluginw with this name it was already loaded
fn load_by_libname(
&self,
libloader: &LibLoader,
libname: &str,
) -> ZResult<Library, PathBuf> {
let (lib, p) = unsafe { libloader.search_and_load(libname)? };

let plugin = match Self::load_plugin(name, lib, p.clone()) {
Ok(p) => p,
Err(e) => bail!("After loading `{:?}`: {}", &p, e),
};
self.plugins.push(PluginRecord::new(plugin));
Ok((true, self.plugins.last_mut().unwrap()))
}
/// Tries to load a plugin from the list of path to plugin (absolute or relative to the current working directory)
/// Returns a tuple of (retval, plugin_record)
/// where `retval`` is true if the plugin was successfully loaded, false if pluginw with this name it was already loaded
pub fn load_plugin_by_paths<T: AsRef<str>, P: AsRef<str> + std::fmt::Debug>(
&mut self,
name: T,
paths: &[P],
) -> ZResult<(bool, &mut PluginRecord<StartArgs, RunningPlugin>)> {
let name = name.as_ref();
if let Some(index) = self.get_plugin_index(name) {
return Ok((false, &mut self.plugins[index]));
}
for path in paths {
let path = path.as_ref();
match unsafe { LibLoader::load_file(path) } {
Ok((lib, p)) => {
let plugin = Self::load_plugin(name, lib, p)?;
self.plugins.push(PluginRecord::new(plugin));
return Ok((true, self.plugins.last_mut().unwrap()));
}
Err(e) => log::warn!("Plugin '{}' load fail at {}: {}", &name, path, e),
}
}
bail!("Plugin '{}' not found in {:?}", name, &paths)
}
}

}

Check failure on line 451 in plugins/zenoh-plugin-trait/src/loading.rs

View workflow job for this annotation

GitHub Actions / Run checks on ubuntu-latest

unexpected closing delimiter: `}`

Check failure on line 451 in plugins/zenoh-plugin-trait/src/loading.rs

View workflow job for this annotation

GitHub Actions / Run tests on ubuntu-latest

unexpected closing delimiter: `}`

Check failure on line 451 in plugins/zenoh-plugin-trait/src/loading.rs

View workflow job for this annotation

GitHub Actions / Run checks on macOS-latest

unexpected closing delimiter: `}`

Check failure on line 451 in plugins/zenoh-plugin-trait/src/loading.rs

View workflow job for this annotation

GitHub Actions / Run tests on macOS-latest

unexpected closing delimiter: `}`

Check failure on line 451 in plugins/zenoh-plugin-trait/src/loading.rs

View workflow job for this annotation

GitHub Actions / Run checks on windows-latest

unexpected closing delimiter: `}`

Check failure on line 451 in plugins/zenoh-plugin-trait/src/loading.rs

View workflow job for this annotation

GitHub Actions / Run tests on windows-latest

unexpected closing delimiter: `}`

0 comments on commit be3382d

Please sign in to comment.