use zenoh_config::{
unwrap_or_default, AutoConnectStrategy, Config, ModeDependent, TargetDependentValue,
};
use zenoh_protocol::core::{WhatAmI, WhatAmIMatcher, ZenohIdProto};
#[derive(Clone, Copy)]
pub(crate) struct AutoConnect {
zid: ZenohIdProto,
matcher: WhatAmIMatcher,
strategy: TargetDependentValue<AutoConnectStrategy>,
}
impl AutoConnect {
pub(crate) fn multicast(config: &Config, what: WhatAmI, zid: ZenohIdProto) -> Self {
Self {
zid,
matcher: *unwrap_or_default!(config.scouting().multicast().autoconnect().get(what)),
strategy: *unwrap_or_default!(config
.scouting()
.multicast()
.autoconnect_strategy()
.get(what)),
}
}
pub(crate) fn gossip(config: &Config, what: WhatAmI, zid: ZenohIdProto) -> Self {
Self {
zid,
matcher: *unwrap_or_default!(config.scouting().gossip().autoconnect().get(what)),
strategy: *unwrap_or_default!(config
.scouting()
.gossip()
.autoconnect_strategy()
.get(what)),
}
}
pub(crate) fn disabled() -> Self {
Self {
zid: ZenohIdProto::default(),
matcher: WhatAmIMatcher::empty(),
strategy: TargetDependentValue::Unique(AutoConnectStrategy::default()),
}
}
pub(crate) fn matcher(&self) -> WhatAmIMatcher {
self.matcher
}
pub(crate) fn is_enabled(&self) -> bool {
!self.matcher.is_empty()
}
pub(crate) fn should_autoconnect(&self, to: ZenohIdProto, what: WhatAmI) -> bool {
let strategy = || match self.strategy.get(what).copied().unwrap_or_default() {
AutoConnectStrategy::Always => true,
AutoConnectStrategy::GreaterZid => self.zid > to,
};
self.matcher.matches(what) && strategy()
}
}