Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zenoh plugins statically linked in a zenoh app #89

Closed
JEnoch opened this issue May 3, 2021 · 2 comments
Closed

zenoh plugins statically linked in a zenoh app #89

JEnoch opened this issue May 3, 2021 · 2 comments

Comments

@JEnoch
Copy link
Member

JEnoch commented May 3, 2021

Currently (v0.5.0-beta.8) the zenoh plugins are dynamic libraries that are loaded by zenohd at startup.
They just must implement 2 #[no_mangle] functions:

// return the list of command lines arguments expected by the plugin
fn get_expected_args<'a, 'b>() -> Vec<clap::Arg<'a, 'b>>;

// start the plugin with command line arguments
fn start(runtime: Runtime, args: clap::ArgMatches<'static>);

Now we would like to be able to use the zenoh plugins in any zenoh Rust application, but in a static way (i.e. just declaring the plugins in the application's cargo.toml and calling them explicitly in the main() operation).

Use case:
The zenoh-dds-plugin can be built as a standalone zenoh application named zenoh-bridge-dds.
We would like to administrate it at runtime in the same way as zenohd: via an admin space accessible through the REST API. The REST API is implemented as a zenoh plugin. But we don't want the zenoh-bridge-dds to have the ability to dynamically load any plugin. The solution is to statically link zenoh-bridge-dds with the zenoh-rest plugin.
And as the code of the DDS/zenoh bridge itself should eventually be also a zenoh plugin (to be loadable by zenohd), the zenoh-bridge-dds will finally just by a main program starting the 2 plugins that are linked in static.

@JEnoch
Copy link
Member Author

JEnoch commented May 3, 2021

The current design using 2 #[no_mangle] function cannot work with statically linked plugins: the declaration of those functions by different plugins override each other, leading only 1 plugin to be accessible by the main().

Conditional compilation of #[no_mangle] attribute depending on the crate type (cdylib or rlib) doesn't work either, as long as rust-lang/rust#20267 is not fixed.

The most appropriate solution seems to follow the design suggested in https://michael-f-bryan.github.io/rust-ffi-guide/dynamic_loading.html:

  • define a trait to be implemented by each plugin struct
  • define a macro to be used by each plugin implementation to generate a "standardized" #[no_mangle] function to instantiate the plugin struct
  • the plugin manager in zenohd will call the #[no_mangle] function for each plugin
  • a standalone application which statically links with the plugins will directly instantiate and drive each plugin struct

@JEnoch
Copy link
Member Author

JEnoch commented Sep 3, 2021

Implemented by #114.
Applied in eclipse-zenoh/zenoh-plugin-dds#38.

@JEnoch JEnoch closed this as completed Sep 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant