Skip to content

Commit

Permalink
Avoid using zenoh-unstable in the examples (#94)
Browse files Browse the repository at this point in the history
Signed-off-by: ChenYing Kuo <[email protected]>
  • Loading branch information
evshary authored Dec 1, 2024
1 parent f1ae77f commit 94e6c53
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 42 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ async-trait = "0.1"
bytes = "1.6.1"
lazy_static = "1.4.0"
protobuf = { version = "3.3" }
serde_json = "1.0.128"
tokio = { version = "1.35.1", default-features = false }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
Expand Down
74 changes: 32 additions & 42 deletions examples/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
use clap::Parser;
use serde_json::json;
use up_transport_zenoh::zenoh_config;

#[derive(clap::ValueEnum, Clone, Copy, PartialEq, Eq, Hash, Debug)]
Expand All @@ -20,15 +21,16 @@ pub enum WhatAmIType {
Router,
}

#[cfg(not(feature = "zenoh-unstable"))]
#[derive(clap::Parser, Clone, PartialEq, Eq, Hash, Debug)]
pub struct Args {
#[arg(short, long)]
/// A configuration file.
config: Option<String>,
impl WhatAmIType {
const fn to_str(self) -> &'static str {
match self {
Self::Peer => "peer",
Self::Client => "client",
Self::Router => "router",
}
}
}

#[cfg(feature = "zenoh-unstable")]
#[derive(clap::Parser, Clone, PartialEq, Eq, Hash, Debug)]
pub struct Args {
#[arg(short, long)]
Expand All @@ -53,49 +55,37 @@ pub fn get_zenoh_config() -> zenoh_config::Config {
let args = Args::parse();

// Load the config from file path
#[allow(unused_mut)]
let mut zenoh_cfg = match &args.config {
Some(path) => zenoh_config::Config::from_file(path).unwrap(),
None => zenoh_config::Config::default(),
};

#[cfg(feature = "zenoh-unstable")]
{
// You can choose from Router, Peer, Client
match args.mode {
Some(WhatAmIType::Peer) => zenoh_cfg.set_mode(Some(zenoh::config::WhatAmI::Peer)),
Some(WhatAmIType::Client) => zenoh_cfg.set_mode(Some(zenoh::config::WhatAmI::Client)),
Some(WhatAmIType::Router) => zenoh_cfg.set_mode(Some(zenoh::config::WhatAmI::Router)),
None => Ok(None),
}
.unwrap();
// You can choose from Router, Peer, Client
if let Some(mode) = args.mode {
zenoh_cfg
.insert_json5("mode", &json!(mode.to_str()).to_string())
.unwrap();
}

// Set connection address
if !args.connect.is_empty() {
zenoh_cfg
.connect
.endpoints
.set(args.connect.iter().map(|v| v.parse().unwrap()).collect())
.unwrap();
}
// Set connection address
if !args.connect.is_empty() {
zenoh_cfg
.insert_json5("connect/endpoints", &json!(args.connect).to_string())
.unwrap();
}

// Set listener address
if !args.listen.is_empty() {
zenoh_cfg
.listen
.endpoints
.set(args.listen.iter().map(|v| v.parse().unwrap()).collect())
.unwrap();
}
// Set listener address
if !args.listen.is_empty() {
zenoh_cfg
.insert_json5("listen/endpoints", &json!(args.listen).to_string())
.unwrap();
}

// Set multicast configuration
if args.no_multicast_scouting {
zenoh_cfg
.scouting
.multicast
.set_enabled(Some(false))
.unwrap();
}
// Set multicast configuration
if args.no_multicast_scouting {
zenoh_cfg
.insert_json5("scouting/multicast/enabled", &json!(false).to_string())
.unwrap();
}

zenoh_cfg
Expand Down

0 comments on commit 94e6c53

Please sign in to comment.