Skip to content

Commit

Permalink
made runtime state fields private
Browse files Browse the repository at this point in the history
  • Loading branch information
milyin committed Oct 9, 2023
1 parent 1f935b1 commit 0164dd8
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 27 deletions.
2 changes: 1 addition & 1 deletion plugins/example-plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Plugin for ExamplePlugin {

// The first operation called by zenohd on the plugin
fn start(name: &str, runtime: &Self::StartArgs) -> ZResult<Self::RunningPlugin> {
let config = runtime.config.lock();
let config = runtime.config().lock();
let self_cfg = config.plugin(name).unwrap().as_object().unwrap();
// get the plugin's config details from self_cfg Map (here the "storage-selector" property)
let selector: KeyExpr = match self_cfg.get("storage-selector") {
Expand Down
4 changes: 2 additions & 2 deletions plugins/zenoh-plugin-rest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ impl Plugin for RestPlugin {
let _ = env_logger::try_init();
log::debug!("REST plugin {}", LONG_VERSION.as_str());

let runtime_conf = runtime.config.lock();
let runtime_conf = runtime.config().lock();
let plugin_conf = runtime_conf
.plugin(name)
.ok_or_else(|| zerror!("Plugin `{}`: missing config", name))?;
Expand Down Expand Up @@ -476,7 +476,7 @@ pub async fn run(runtime: Runtime, conf: Config) -> ZResult<()> {
// But cannot be done twice in case of static link.
let _ = env_logger::try_init();

let zid = runtime.zid.to_string();
let zid = runtime.zid().to_string();
let session = zenoh::init(runtime).res().await.unwrap();

let mut app = Server::with_state((Arc::new(session), zid));
Expand Down
5 changes: 3 additions & 2 deletions plugins/zenoh-plugin-storage-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Plugin for StoragesPlugin {
std::mem::drop(env_logger::try_init());
log::debug!("StorageManager plugin {}", LONG_VERSION.as_str());
let config =
{ PluginConfig::try_from((name, runtime.config.lock().plugin(name).unwrap())) }?;
{ PluginConfig::try_from((name, runtime.config().lock().plugin(name).unwrap())) }?;
Ok(Box::new(StorageRuntime::from(StorageRuntimeInner::new(
runtime.clone(),
config,
Expand All @@ -85,7 +85,8 @@ impl StorageRuntimeInner {
fn status_key(&self) -> String {
format!(
"@/router/{}/status/plugins/{}",
&self.runtime.zid, &self.name
&self.runtime.zid(),
&self.name
)
}
fn new(runtime: Runtime, config: PluginConfig) -> ZResult<Self> {
Expand Down
2 changes: 1 addition & 1 deletion zenoh/src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl<'a> Resolvable for ZidBuilder<'a> {

impl<'a> SyncResolve for ZidBuilder<'a> {
fn res_sync(self) -> Self::To {
self.session.runtime.zid
self.session.runtime.zid()
}
}

Expand Down
7 changes: 5 additions & 2 deletions zenoh/src/liveliness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,14 @@ impl<'a> Liveliness<'a> {
<TryIntoKeyExpr as TryInto<KeyExpr<'b>>>::Error: Into<zenoh_result::Error>,
{
let key_expr = key_expr.try_into().map_err(Into::into);
let conf = self.session.runtime.config.lock();
let timeout = {
let conf = self.session.runtime.config().lock();
Duration::from_millis(unwrap_or_default!(conf.queries_default_timeout()))
};
LivelinessGetBuilder {
session: &self.session,
key_expr,
timeout: Duration::from_millis(unwrap_or_default!(conf.queries_default_timeout())),
timeout,
handler: DefaultHandler,
}
}
Expand Down
2 changes: 1 addition & 1 deletion zenoh/src/net/routing/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl Network {
log::debug!("{} Add node (self) {}", name, zid);
let idx = graph.add_node(Node {
zid,
whatami: Some(runtime.whatami),
whatami: Some(runtime.whatami()),
locators: None,
sn: 1,
links: vec![],
Expand Down
1 change: 1 addition & 0 deletions zenoh/src/net/routing/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ impl Resource {
}
}

#[allow(dead_code)]
pub fn print_tree(from: &Arc<Resource>) -> String {
let mut result = from.expr();
result.push('\n');
Expand Down
1 change: 1 addition & 0 deletions zenoh/src/net/routing/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ impl Tables {
&self.root_res
}

#[allow(dead_code)]
pub fn print(&self) -> String {
Resource::print_tree(&self.root_res)
}
Expand Down
40 changes: 30 additions & 10 deletions zenoh/src/net/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ use zenoh_transport::{
};

pub struct RuntimeState {
pub zid: ZenohId,
pub whatami: WhatAmI,
pub metadata: serde_json::Value,
pub router: Arc<Router>,
pub config: Notifier<Config>,
pub manager: TransportManager,
pub transport_handlers: std::sync::RwLock<Vec<Arc<dyn TransportEventHandler>>>,
pub(crate) locators: std::sync::RwLock<Vec<Locator>>,
pub hlc: Option<Arc<HLC>>,
pub(crate) stop_source: std::sync::RwLock<Option<StopSource>>,
zid: ZenohId,
whatami: WhatAmI,
metadata: serde_json::Value,
router: Arc<Router>,
config: Notifier<Config>,
manager: TransportManager,
transport_handlers: std::sync::RwLock<Vec<Arc<dyn TransportEventHandler>>>,
locators: std::sync::RwLock<Vec<Locator>>,
hlc: Option<Arc<HLC>>,
stop_source: std::sync::RwLock<Option<StopSource>>,
}

#[derive(Clone)]
Expand Down Expand Up @@ -213,6 +213,26 @@ impl Runtime {
.as_ref()
.map(|source| async_std::task::spawn(future.timeout_at(source.token())))
}

pub(crate) fn router(&self) -> Arc<Router> {
self.router.clone()
}

pub fn config(&self) -> &Notifier<Config> {
&self.config
}

pub fn hlc(&self) -> Option<&HLC> {
self.hlc.as_ref().map(Arc::as_ref)
}

pub fn zid(&self) -> ZenohId {
self.zid
}

pub fn whatami(&self) -> WhatAmI {
self.whatami
}
}

struct RuntimeTransportEventHandler {
Expand Down
17 changes: 10 additions & 7 deletions zenoh/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ impl Session {
aggregated_publishers: Vec<OwnedKeyExpr>,
) -> impl Resolve<Session> {
ResolveClosure::new(move || {
let router = runtime.router.clone();
let router = runtime.router();
let state = Arc::new(RwLock::new(SessionState::new(
aggregated_subscribers,
aggregated_publishers,
Expand Down Expand Up @@ -426,7 +426,7 @@ impl Session {
}

pub fn hlc(&self) -> Option<&HLC> {
self.runtime.hlc.as_ref().map(Arc::as_ref)
self.runtime.hlc()
}

/// Close the zenoh [`Session`](Session).
Expand Down Expand Up @@ -491,7 +491,7 @@ impl Session {
/// # })
/// ```
pub fn config(&self) -> &Notifier<Config> {
&self.runtime.config
&self.runtime.config()

Check failure on line 494 in zenoh/src/session.rs

View workflow job for this annotation

GitHub Actions / Run checks on ubuntu-latest

this expression creates a reference which is immediately dereferenced by the compiler

Check failure on line 494 in zenoh/src/session.rs

View workflow job for this annotation

GitHub Actions / Run checks on macOS-latest

this expression creates a reference which is immediately dereferenced by the compiler

Check failure on line 494 in zenoh/src/session.rs

View workflow job for this annotation

GitHub Actions / Run checks on windows-latest

this expression creates a reference which is immediately dereferenced by the compiler
}

/// Get informations about the zenoh [`Session`](Session).
Expand Down Expand Up @@ -780,15 +780,18 @@ impl Session {
<IntoSelector as TryInto<Selector<'b>>>::Error: Into<zenoh_result::Error>,
{
let selector = selector.try_into().map_err(Into::into);
let conf = self.runtime.config.lock();
let timeout = {
let conf = self.runtime.config().lock();
Duration::from_millis(unwrap_or_default!(conf.queries_default_timeout()))
};
GetBuilder {
session: self,
selector,
scope: Ok(None),
target: QueryTarget::default(),
consolidation: QueryConsolidation::default(),
destination: Locality::default(),
timeout: Duration::from_millis(unwrap_or_default!(conf.queries_default_timeout())),
timeout,
value: None,
handler: DefaultHandler,
}
Expand Down Expand Up @@ -1590,7 +1593,7 @@ impl Session {
};
task::spawn({
let state = self.state.clone();
let zid = self.runtime.zid;
let zid = self.runtime.zid();
async move {
task::sleep(timeout).await;
let mut state = zwrite!(state);
Expand Down Expand Up @@ -1731,7 +1734,7 @@ impl Session {

let parameters = parameters.to_owned();

let zid = self.runtime.zid; // @TODO build/use prebuilt specific zid
let zid = self.runtime.zid(); // @TODO build/use prebuilt specific zid

let query = Query {
inner: Arc::new(QueryInner {
Expand Down
2 changes: 1 addition & 1 deletion zenohd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ clap::Arg::new("adminspace-permissions").long("adminspace-permissions").value_na
log::info!("Finished loading plugins");

{
let mut config_guard = runtime.config.lock();
let mut config_guard = runtime.config().lock();
for (name, (_, plugin)) in plugins.running_plugins() {
let hook = plugin.config_checker();
config_guard.add_plugin_validator(name, hook)
Expand Down

0 comments on commit 0164dd8

Please sign in to comment.