diff --git a/DEFAULT_CONFIG.json5 b/DEFAULT_CONFIG.json5 index 7ed4926d26..ce1f396ed7 100644 --- a/DEFAULT_CONFIG.json5 +++ b/DEFAULT_CONFIG.json5 @@ -12,6 +12,7 @@ /// Which endpoints to connect to. E.g. tcp/localhost:7447. /// By configuring the endpoints, it is possible to tell zenoh which router/peer to connect to at startup. + /// Accepts a single value or different values for router, peer and client. connect: { endpoints: [ // "/
" @@ -21,10 +22,9 @@ /// Which endpoints to listen on. E.g. tcp/localhost:7447. /// By configuring the endpoints, it is possible to tell zenoh which are the endpoints that other routers, /// peers, or client can use to establish a zenoh session. + /// Accepts a single value or different values for router, peer and client. listen: { - endpoints: [ - // "/
" - ], + endpoints: { router: ["tcp/[::]:7447"], peer: ["tcp/[::]:0"], }, }, /// Configure the scouting mechanisms and their behaviours scouting: { diff --git a/commons/zenoh-config/src/defaults.rs b/commons/zenoh-config/src/defaults.rs index 6502061acd..d42e3ae9da 100644 --- a/commons/zenoh-config/src/defaults.rs +++ b/commons/zenoh-config/src/defaults.rs @@ -30,6 +30,28 @@ macro_rules! mode_accessor { #[allow(dead_code)] pub const mode: WhatAmI = WhatAmI::Peer; +#[allow(non_upper_case_globals)] +#[allow(dead_code)] +pub mod connect { + pub mod endpoints { + pub const router: &[&str] = &[]; + pub const peer: &[&str] = &[]; + pub const client: &[&str] = &[]; + mode_accessor!([&'static str]); + } +} + +#[allow(non_upper_case_globals)] +#[allow(dead_code)] +pub mod listen { + pub mod endpoints { + pub const router: &[&str] = &["tcp/[::]:7447"]; + pub const peer: &[&str] = &["tcp/[::]:0"]; + pub const client: &[&str] = &[]; + mode_accessor!([&'static str]); + } +} + #[allow(non_upper_case_globals)] #[allow(dead_code)] pub mod scouting { diff --git a/commons/zenoh-config/src/lib.rs b/commons/zenoh-config/src/lib.rs index c7b6bb7df0..f3976abb78 100644 --- a/commons/zenoh-config/src/lib.rs +++ b/commons/zenoh-config/src/lib.rs @@ -24,6 +24,7 @@ use std::{ collections::HashMap, fmt, io::Read, + iter::FromIterator, marker::PhantomData, net::SocketAddr, path::Path, @@ -74,8 +75,8 @@ pub fn client, T: Into>(peers: I) -> Config config.set_mode(Some(WhatAmI::Client)).unwrap(); config .connect - .endpoints - .extend(peers.into_iter().map(|t| t.into())); + .set_endpoints(Some(peers.into_iter().map(|v| v.into()).collect())) + .unwrap(); config } @@ -111,15 +112,20 @@ validated_struct::validator! { id: ZenohId, /// The node's mode ("router" (default value in `zenohd`), "peer" or "client"). mode: Option, - /// Which zenoh nodes to connect to. + /// Which endpoints to connect to. E.g. tcp/localhost:7447. + /// By configuring the endpoints, it is possible to tell zenoh which router/peer to connect to at startup. + /// Accepts a single value or different values for router, peer and client. pub connect: #[derive(Default)] ConnectConfig { - pub endpoints: Vec, + pub endpoints: Option>>, }, - /// Which endpoints to listen on. `zenohd` will add `tcp/[::]:7447` to these locators if left empty. + /// Which endpoints to listen on. E.g. tcp/localhost:7447. + /// By configuring the endpoints, it is possible to tell zenoh which are the endpoints that other routers, + /// peers, or client can use to establish a zenoh session. + /// Accepts a single value or different values for router, peer and client. pub listen: #[derive(Default)] ListenConfig { - pub endpoints: Vec, + pub endpoints: Option>>, }, pub scouting: #[derive(Default)] ScoutingConf { @@ -1143,6 +1149,15 @@ pub enum ModeDependentValue { Dependent(ModeValues), } +impl Default for ModeDependentValue +where + T: Default, +{ + fn default() -> Self { + Self::Unique(T::default()) + } +} + impl ModeDependent for ModeDependentValue { #[inline] fn router(&self) -> Option<&T> { @@ -1169,6 +1184,21 @@ impl ModeDependent for ModeDependentValue { } } +impl From for ModeDependentValue { + fn from(v: T) -> Self { + Self::Unique(v) + } +} + +impl FromIterator for ModeDependentValue> { + fn from_iter(iter: I) -> Self + where + I: IntoIterator, + { + Self::from(iter.into_iter().collect::>()) + } +} + impl serde::Serialize for ModeDependentValue where T: Serialize, @@ -1251,6 +1281,43 @@ impl<'a> serde::Deserialize<'a> for ModeDependentValue { } } +impl<'a> serde::Deserialize<'a> for ModeDependentValue> { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'a>, + { + struct UniqueOrDependent(PhantomData U>); + + impl<'de> Visitor<'de> for UniqueOrDependent>> { + type Value = ModeDependentValue>; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("Vec or mode dependent Vec") + } + + fn visit_seq(self, mut seq: A) -> Result + where + A: serde::de::SeqAccess<'de>, + { + let mut vec = Vec::new(); + while let Some(elem) = seq.next_element()? { + vec.push(elem); + } + Ok(ModeDependentValue::Unique(vec)) + } + + fn visit_map(self, map: M) -> Result + where + M: MapAccess<'de>, + { + ModeValues::deserialize(de::value::MapAccessDeserializer::new(map)) + .map(ModeDependentValue::Dependent) + } + } + deserializer.deserialize_any(UniqueOrDependent(PhantomData)) + } +} + impl ModeDependent for Option> { #[inline] fn router(&self) -> Option<&T> { @@ -1286,3 +1353,17 @@ macro_rules! unwrap_or_default { $val$(.$field($($param)?))*.clone().unwrap_or(zenoh_config::defaults$(::$field$(($param))?)*.into()) }; } + +#[macro_export] +macro_rules! unwrap_or_default_seq { + ($val:ident$(.$field:ident($($param:ident)?))*) => { + if let Some(seq) = $val$(.$field($($param)?))* { + seq.clone() + } else { + zenoh_config::defaults$(::$field$(($param))?)*.iter().filter_map(|e|{ + use std::convert::TryInto; + (*e).try_into().ok() + }).collect() + } + }; +} diff --git a/commons/zenoh-protocol-core/src/endpoints.rs b/commons/zenoh-protocol-core/src/endpoints.rs index dabb392af6..f8b69b4e39 100644 --- a/commons/zenoh-protocol-core/src/endpoints.rs +++ b/commons/zenoh-protocol-core/src/endpoints.rs @@ -123,3 +123,9 @@ impl FromStr for EndPoint { }) } } +impl TryFrom<&str> for EndPoint { + type Error = zenoh_core::Error; + fn try_from(s: &str) -> Result { + Self::from_str(s) + } +} diff --git a/examples/examples/z_delete.rs b/examples/examples/z_delete.rs index 116a5bd607..cd1ecfedc5 100644 --- a/examples/examples/z_delete.rs +++ b/examples/examples/z_delete.rs @@ -68,14 +68,14 @@ fn parse_args() -> (Config, String) { if let Some(values) = args.values_of("connect") { config .connect - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if let Some(values) = args.values_of("listen") { config .listen - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if args.is_present("no-multicast-scouting") { config.scouting.multicast.set_enabled(Some(false)).unwrap(); diff --git a/examples/examples/z_forward.rs b/examples/examples/z_forward.rs index c50bd9cc3a..cab6cde8ff 100644 --- a/examples/examples/z_forward.rs +++ b/examples/examples/z_forward.rs @@ -73,14 +73,14 @@ fn parse_args() -> (Config, String, String) { if let Some(values) = args.values_of("connect") { config .connect - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if let Some(values) = args.values_of("listeners") { config .listen - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if args.is_present("no-multicast-scouting") { config.scouting.multicast.set_enabled(Some(false)).unwrap(); diff --git a/examples/examples/z_get.rs b/examples/examples/z_get.rs index db32771659..cc91fa8e48 100644 --- a/examples/examples/z_get.rs +++ b/examples/examples/z_get.rs @@ -93,14 +93,14 @@ fn parse_args() -> (Config, String, QueryTarget, Duration) { if let Some(values) = args.values_of("connect") { config .connect - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if let Some(values) = args.values_of("listen") { config .listen - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if args.is_present("no-multicast-scouting") { config.scouting.multicast.set_enabled(Some(false)).unwrap(); diff --git a/examples/examples/z_info.rs b/examples/examples/z_info.rs index cc991b7a32..248ac4c26f 100644 --- a/examples/examples/z_info.rs +++ b/examples/examples/z_info.rs @@ -68,14 +68,14 @@ fn parse_args() -> Config { if let Some(values) = args.values_of("connect") { config .connect - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if let Some(values) = args.values_of("listen") { config .listen - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if args.is_present("no-multicast-scouting") { config.scouting.multicast.set_enabled(Some(false)).unwrap(); diff --git a/examples/examples/z_ping.rs b/examples/examples/z_ping.rs index 9ab11b6442..2defce24d7 100644 --- a/examples/examples/z_ping.rs +++ b/examples/examples/z_ping.rs @@ -114,14 +114,14 @@ fn parse_args() -> (Config, Duration, usize, usize) { if let Some(values) = args.values_of("connect") { config .connect - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if let Some(values) = args.values_of("listen") { config .listen - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if args.is_present("no-multicast-scouting") { config.scouting.multicast.set_enabled(Some(false)).unwrap(); diff --git a/examples/examples/z_pong.rs b/examples/examples/z_pong.rs index ecb877ee7a..8504da574e 100644 --- a/examples/examples/z_pong.rs +++ b/examples/examples/z_pong.rs @@ -74,14 +74,14 @@ fn parse_args() -> Config { if let Some(values) = args.values_of("connect") { config .connect - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if let Some(values) = args.values_of("listen") { config .listen - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if args.is_present("no-multicast-scouting") { config.scouting.multicast.set_enabled(Some(false)).unwrap(); diff --git a/examples/examples/z_pub.rs b/examples/examples/z_pub.rs index 3ddc48f9e9..a6e4cef04b 100644 --- a/examples/examples/z_pub.rs +++ b/examples/examples/z_pub.rs @@ -77,14 +77,14 @@ fn parse_args() -> (Config, String, String) { if let Some(values) = args.values_of("connect") { config .connect - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if let Some(values) = args.values_of("listen") { config .listen - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if args.is_present("no-multicast-scouting") { config.scouting.multicast.set_enabled(Some(false)).unwrap(); diff --git a/examples/examples/z_pub_thr.rs b/examples/examples/z_pub_thr.rs index 2d2f2fa578..a8d458ff60 100644 --- a/examples/examples/z_pub_thr.rs +++ b/examples/examples/z_pub_thr.rs @@ -103,14 +103,14 @@ fn parse_args() -> (Config, usize, Priority, bool, usize) { if let Some(values) = args.values_of("connect") { config .connect - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if let Some(values) = args.values_of("listen") { config .listen - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if args.is_present("no-multicast-scouting") { config.scouting.multicast.set_enabled(Some(false)).unwrap(); diff --git a/examples/examples/z_pull.rs b/examples/examples/z_pull.rs index 784abc5904..3673ef1b5f 100644 --- a/examples/examples/z_pull.rs +++ b/examples/examples/z_pull.rs @@ -105,14 +105,14 @@ fn parse_args() -> (Config, String) { if let Some(values) = args.values_of("connect") { config .connect - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if let Some(values) = args.values_of("listen") { config .listen - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if args.is_present("no-multicast-scouting") { config.scouting.multicast.set_enabled(Some(false)).unwrap(); diff --git a/examples/examples/z_put.rs b/examples/examples/z_put.rs index 450c933187..13ce0a2f74 100644 --- a/examples/examples/z_put.rs +++ b/examples/examples/z_put.rs @@ -68,14 +68,14 @@ fn parse_args() -> (Config, String, String) { if let Some(values) = args.values_of("connect") { config .connect - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if let Some(values) = args.values_of("listen") { config .listen - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if args.is_present("no-multicast-scouting") { config.scouting.multicast.set_enabled(Some(false)).unwrap(); diff --git a/examples/examples/z_put_float.rs b/examples/examples/z_put_float.rs index 647b999d16..02acaf1065 100644 --- a/examples/examples/z_put_float.rs +++ b/examples/examples/z_put_float.rs @@ -75,14 +75,14 @@ fn parse_args() -> (Config, String, f64) { if let Some(values) = args.values_of("connect") { config .connect - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if let Some(values) = args.values_of("listen") { config .listen - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if args.is_present("no-multicast-scouting") { config.scouting.multicast.set_enabled(Some(false)).unwrap(); diff --git a/examples/examples/z_queryable.rs b/examples/examples/z_queryable.rs index 65f3c51368..3c452cfb41 100644 --- a/examples/examples/z_queryable.rs +++ b/examples/examples/z_queryable.rs @@ -101,14 +101,14 @@ fn parse_args() -> (Config, String, String) { if let Some(values) = args.values_of("connect") { config .connect - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if let Some(values) = args.values_of("listen") { config .listen - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if args.is_present("no-multicast-scouting") { config.scouting.multicast.set_enabled(Some(false)).unwrap(); diff --git a/examples/examples/z_storage.rs b/examples/examples/z_storage.rs index 909ee91ac0..392b56621b 100644 --- a/examples/examples/z_storage.rs +++ b/examples/examples/z_storage.rs @@ -112,14 +112,14 @@ fn parse_args() -> (Config, String) { if let Some(values) = args.values_of("connect") { config .connect - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if let Some(values) = args.values_of("listen") { config .listen - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if args.is_present("no-multicast-scouting") { config.scouting.multicast.set_enabled(Some(false)).unwrap(); diff --git a/examples/examples/z_sub.rs b/examples/examples/z_sub.rs index 69514bbe0b..f39560ea8b 100644 --- a/examples/examples/z_sub.rs +++ b/examples/examples/z_sub.rs @@ -91,14 +91,14 @@ fn parse_args() -> (Config, KeyExpr<'static>) { if let Some(values) = args.values_of("connect") { config .connect - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if let Some(values) = args.values_of("listen") { config .listen - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if args.is_present("no-multicast-scouting") { config.scouting.multicast.set_enabled(Some(false)).unwrap(); diff --git a/examples/examples/z_sub_thr.rs b/examples/examples/z_sub_thr.rs index c0a088edf7..ba02203c3a 100644 --- a/examples/examples/z_sub_thr.rs +++ b/examples/examples/z_sub_thr.rs @@ -138,14 +138,14 @@ fn parse_args() -> (Config, usize, usize) { if let Some(values) = args.values_of("connect") { config .connect - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if let Some(values) = args.values_of("listen") { config .listen - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if args.is_present("no-multicast-scouting") { config.scouting.multicast.set_enabled(Some(false)).unwrap(); diff --git a/plugins/zenoh-plugin-rest/examples/z_serve_sse.rs b/plugins/zenoh-plugin-rest/examples/z_serve_sse.rs index f4e6eab6de..db027363c4 100644 --- a/plugins/zenoh-plugin-rest/examples/z_serve_sse.rs +++ b/plugins/zenoh-plugin-rest/examples/z_serve_sse.rs @@ -122,14 +122,14 @@ fn parse_args() -> Config { if let Some(values) = args.values_of("connect") { config .connect - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if let Some(values) = args.values_of("listen") { config .listen - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if args.is_present("no-multicast-scouting") { config.scouting.multicast.set_enabled(Some(false)).unwrap(); diff --git a/zenoh-ext/examples/z_pub_cache.rs b/zenoh-ext/examples/z_pub_cache.rs index 160148b84a..e3bbb6719f 100644 --- a/zenoh-ext/examples/z_pub_cache.rs +++ b/zenoh-ext/examples/z_pub_cache.rs @@ -91,14 +91,14 @@ fn parse_args() -> (Config, String, String, usize, Option) { if let Some(values) = args.values_of("connect") { config .connect - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if let Some(values) = args.values_of("listen") { config .listen - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if args.is_present("no-multicast-scouting") { config.scouting.multicast.set_enabled(Some(false)).unwrap(); diff --git a/zenoh-ext/examples/z_query_sub.rs b/zenoh-ext/examples/z_query_sub.rs index d17560af49..0300c39026 100644 --- a/zenoh-ext/examples/z_query_sub.rs +++ b/zenoh-ext/examples/z_query_sub.rs @@ -114,14 +114,14 @@ fn parse_args() -> (Config, String, Option) { if let Some(values) = args.values_of("connect") { config .connect - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if let Some(values) = args.values_of("listen") { config .listen - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if args.is_present("no-multicast-scouting") { config.scouting.multicast.set_enabled(Some(false)).unwrap(); diff --git a/zenoh-ext/examples/z_view_size.rs b/zenoh-ext/examples/z_view_size.rs index 973eab9b33..bb9666192b 100644 --- a/zenoh-ext/examples/z_view_size.rs +++ b/zenoh-ext/examples/z_view_size.rs @@ -90,14 +90,14 @@ fn parse_args() -> (Config, String, Option, usize, u64) { if let Some(values) = args.values_of("connect") { config .connect - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } if let Some(values) = args.values_of("listeners") { config .listen - .endpoints - .extend(values.map(|v| v.parse().unwrap())) + .set_endpoints(Some(values.map(|v| v.parse().unwrap()).collect())) + .unwrap(); } let group = args.value_of("group").unwrap().to_string(); diff --git a/zenoh/src/lib.rs b/zenoh/src/lib.rs index db90cfdb95..e7a62c55d7 100644 --- a/zenoh/src/lib.rs +++ b/zenoh/src/lib.rs @@ -220,7 +220,7 @@ where /// /// let mut config = config::peer(); /// config.set_id(ZenohId::from_str("F000").unwrap()); -/// config.connect.endpoints.extend("tcp/10.10.10.10:7447,tcp/11.11.11.11:7447".split(',').map(|s|s.parse().unwrap())); +/// config.connect.set_endpoints(Some("tcp/10.10.10.10:7447,tcp/11.11.11.11:7447".split(',').map(|s|s.parse().unwrap()).collect())); /// /// let session = zenoh::open(config).res().await.unwrap(); /// # }) diff --git a/zenoh/src/net/runtime/orchestrator.rs b/zenoh/src/net/runtime/orchestrator.rs index 88152dba0c..580b819386 100644 --- a/zenoh/src/net/runtime/orchestrator.rs +++ b/zenoh/src/net/runtime/orchestrator.rs @@ -20,7 +20,7 @@ use std::net::{IpAddr, Ipv6Addr, SocketAddr}; use std::time::Duration; use zenoh_buffers::reader::HasReader; use zenoh_buffers::SplitBuffer; -use zenoh_config::{unwrap_or_default, EndPoint, ModeDependent}; +use zenoh_config::{unwrap_or_default, unwrap_or_default_seq, EndPoint, ModeDependent}; use zenoh_core::Result as ZResult; use zenoh_core::{bail, zerror}; use zenoh_link::Locator; @@ -39,8 +39,6 @@ const CONNECTION_TIMEOUT: Duration = Duration::from_millis(10_000); const CONNECTION_RETRY_INITIAL_PERIOD: Duration = Duration::from_millis(1_000); const CONNECTION_RETRY_MAX_PERIOD: Duration = Duration::from_millis(4_000); const CONNECTION_RETRY_PERIOD_INCREASE_FACTOR: u32 = 2; -const ROUTER_DEFAULT_LISTENER: &str = "tcp/[::]:7447"; -const PEER_DEFAULT_LISTENER: &str = "tcp/[::]:0"; pub enum Loop { Continue, @@ -60,7 +58,7 @@ impl Runtime { let (peers, scouting, addr, ifaces, timeout) = { let guard = self.config.lock(); ( - guard.connect().endpoints().clone(), + unwrap_or_default_seq!(guard.connect().endpoints().client()), unwrap_or_default!(guard.scouting().multicast().enabled()), unwrap_or_default!(guard.scouting().multicast().address()), unwrap_or_default!(guard.scouting().multicast().interface()), @@ -113,14 +111,9 @@ impl Runtime { async fn start_peer(&self) -> ZResult<()> { let (listeners, peers, scouting, listen, autoconnect, addr, ifaces, delay) = { let guard = &self.config.lock(); - let listeners = if guard.listen().endpoints().is_empty() { - vec![PEER_DEFAULT_LISTENER.parse().unwrap()] - } else { - guard.listen().endpoints().clone() - }; ( - listeners, - guard.connect().endpoints().clone(), + unwrap_or_default_seq!(guard.listen().endpoints().peer()), + unwrap_or_default_seq!(guard.connect().endpoints().peer()), unwrap_or_default!(guard.scouting().multicast().enabled()), *unwrap_or_default!(guard.scouting().multicast().listen().peer()), *unwrap_or_default!(guard.scouting().multicast().autoconnect().peer()), @@ -147,14 +140,9 @@ impl Runtime { async fn start_router(&self) -> ZResult<()> { let (listeners, peers, scouting, listen, autoconnect, addr, ifaces) = { let guard = self.config.lock(); - let listeners = if guard.listen().endpoints().is_empty() { - vec![ROUTER_DEFAULT_LISTENER.parse().unwrap()] - } else { - guard.listen().endpoints().clone() - }; ( - listeners, - guard.connect().endpoints().clone(), + unwrap_or_default_seq!(guard.listen().endpoints().router()), + unwrap_or_default_seq!(guard.connect().endpoints().router()), unwrap_or_default!(guard.scouting().multicast().enabled()), *unwrap_or_default!(guard.scouting().multicast().listen().router()), *unwrap_or_default!(guard.scouting().multicast().autoconnect().router()), @@ -221,7 +209,10 @@ impl Runtime { } pub(crate) async fn update_peers(&self) -> ZResult<()> { - let peers = { self.config.lock().connect().endpoints().clone() }; + let peers = { + let guard = self.config.lock(); + unwrap_or_default_seq!(guard.connect().endpoints().router()) + }; let tranports = self.manager().get_transports(); if self.whatami == WhatAmI::Client { @@ -752,7 +743,10 @@ impl Runtime { } _ => { if let Some(endpoint) = &*zread!(session.endpoint) { - let peers = { session.runtime.config.lock().connect().endpoints().clone() }; + let peers = { + let guard = session.runtime.config.lock(); + unwrap_or_default_seq!(guard.connect().endpoints().router()) + }; if peers.contains(endpoint) { let endpoint = endpoint.clone(); let runtime = session.runtime.clone(); diff --git a/zenoh/tests/session.rs b/zenoh/tests/session.rs index 1d99251a47..978e313e1a 100644 --- a/zenoh/tests/session.rs +++ b/zenoh/tests/session.rs @@ -34,19 +34,13 @@ macro_rules! ztimeout { async fn open_session(endpoints: &[&str]) -> (Session, Session) { // Open the sessions let mut config = config::peer(); - config.listen.endpoints = endpoints - .iter() - .map(|e| e.parse().unwrap()) - .collect::>(); + config.listen.endpoints = Some(endpoints.iter().map(|e| e.parse().unwrap()).collect()); config.scouting.multicast.set_enabled(Some(false)).unwrap(); println!("[ ][01a] Opening peer01 session"); let peer01 = ztimeout!(zenoh::open(config).res_async()).unwrap(); let mut config = config::peer(); - config.connect.endpoints = endpoints - .iter() - .map(|e| e.parse().unwrap()) - .collect::>(); + config.connect.endpoints = Some(endpoints.iter().map(|e| e.parse().unwrap()).collect()); config.scouting.multicast.set_enabled(Some(false)).unwrap(); println!("[ ][02a] Opening peer02 session"); let peer02 = ztimeout!(zenoh::open(config).res_async()).unwrap(); diff --git a/zenohd/src/main.rs b/zenohd/src/main.rs index c632da4872..ed2ac11e3b 100644 --- a/zenohd/src/main.rs +++ b/zenohd/src/main.rs @@ -27,8 +27,6 @@ lazy_static::lazy_static!( static ref LONG_VERSION: String = format!("{} built with {}", GIT_VERSION, env!("RUSTC_VERSION")); ); -const DEFAULT_LISTENER: &str = "tcp/[::]:7447"; - fn main() { task::block_on(async { let mut log_builder = @@ -187,7 +185,7 @@ fn config_from_args(args: &ArgMatches) -> Config { if let Some(peers) = args.values_of("connect") { config .connect - .set_endpoints( + .set_endpoints(Some( peers .map(|v| match v.parse::() { Ok(v) => v, @@ -196,13 +194,13 @@ fn config_from_args(args: &ArgMatches) -> Config { } }) .collect(), - ) + )) .unwrap(); } if let Some(listeners) = args.values_of("listen") { config .listen - .set_endpoints( + .set_endpoints(Some( listeners .map(|v| match v.parse::() { Ok(v) => v, @@ -211,15 +209,9 @@ fn config_from_args(args: &ArgMatches) -> Config { } }) .collect(), - ) + )) .unwrap(); } - if config.listen.endpoints.is_empty() { - config - .listen - .endpoints - .push(DEFAULT_LISTENER.parse().unwrap()) - } if args.is_present("no-timestamp") { config .timestamping