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

Config defaults #384

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions DEFAULT_CONFIG.json5
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
// "<proto>/<address>"
Expand All @@ -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: [
// "<proto>/<address>"
],
endpoints: { router: ["tcp/[::]:7447"], peer: ["tcp/[::]:0"], },
},
/// Configure the scouting mechanisms and their behaviours
scouting: {
Expand Down
22 changes: 22 additions & 0 deletions commons/zenoh-config/src/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
93 changes: 87 additions & 6 deletions commons/zenoh-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use std::{
collections::HashMap,
fmt,
io::Read,
iter::FromIterator,
marker::PhantomData,
net::SocketAddr,
path::Path,
Expand Down Expand Up @@ -74,8 +75,8 @@ pub fn client<I: IntoIterator<Item = T>, T: Into<EndPoint>>(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
}

Expand Down Expand Up @@ -111,15 +112,20 @@ validated_struct::validator! {
id: ZenohId,
/// The node's mode ("router" (default value in `zenohd`), "peer" or "client").
mode: Option<whatami::WhatAmI>,
/// 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<EndPoint>,
pub endpoints: Option<ModeDependentValue<Vec<EndPoint>>>,
},
/// 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<EndPoint>,
pub endpoints: Option<ModeDependentValue<Vec<EndPoint>>>,
},
pub scouting: #[derive(Default)]
ScoutingConf {
Expand Down Expand Up @@ -1143,6 +1149,15 @@ pub enum ModeDependentValue<T> {
Dependent(ModeValues<T>),
}

impl<T> Default for ModeDependentValue<T>
where
T: Default,
{
fn default() -> Self {
Self::Unique(T::default())
}
}

impl<T> ModeDependent<T> for ModeDependentValue<T> {
#[inline]
fn router(&self) -> Option<&T> {
Expand All @@ -1169,6 +1184,21 @@ impl<T> ModeDependent<T> for ModeDependentValue<T> {
}
}

impl<T> From<T> for ModeDependentValue<T> {
fn from(v: T) -> Self {
Self::Unique(v)
}
}

impl<T> FromIterator<T> for ModeDependentValue<Vec<T>> {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = T>,
{
Self::from(iter.into_iter().collect::<Vec<T>>())
}
}

impl<T> serde::Serialize for ModeDependentValue<T>
where
T: Serialize,
Expand Down Expand Up @@ -1251,6 +1281,43 @@ impl<'a> serde::Deserialize<'a> for ModeDependentValue<WhatAmIMatcher> {
}
}

impl<'a> serde::Deserialize<'a> for ModeDependentValue<Vec<EndPoint>> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'a>,
{
struct UniqueOrDependent<U>(PhantomData<fn() -> U>);

impl<'de> Visitor<'de> for UniqueOrDependent<ModeDependentValue<Vec<EndPoint>>> {
type Value = ModeDependentValue<Vec<EndPoint>>;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("Vec<EndPoint> or mode dependent Vec<EndPoint>")
}

fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
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<M>(self, map: M) -> Result<Self::Value, M::Error>
where
M: MapAccess<'de>,
{
ModeValues::deserialize(de::value::MapAccessDeserializer::new(map))
.map(ModeDependentValue::Dependent)
}
}
deserializer.deserialize_any(UniqueOrDependent(PhantomData))
}
}

impl<T> ModeDependent<T> for Option<ModeDependentValue<T>> {
#[inline]
fn router(&self) -> Option<&T> {
Expand Down Expand Up @@ -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()
}
};
}
6 changes: 6 additions & 0 deletions commons/zenoh-protocol-core/src/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, Self::Error> {
Self::from_str(s)
}
}
8 changes: 4 additions & 4 deletions examples/examples/z_delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions examples/examples/z_forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions examples/examples/z_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions examples/examples/z_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions examples/examples/z_ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions examples/examples/z_pong.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions examples/examples/z_pub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions examples/examples/z_pub_thr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions examples/examples/z_pull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions examples/examples/z_put.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading