Skip to content

Commit

Permalink
Patch Link::src and Link::dst with negotiated metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzzypixelz committed Sep 6, 2024
1 parent c0efe28 commit 8da03bb
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
3 changes: 3 additions & 0 deletions commons/zenoh-protocol/src/core/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ impl fmt::Debug for AddressMut<'_> {
pub struct Metadata<'a>(pub(super) &'a str);

impl<'a> Metadata<'a> {
pub const RELIABILITY: &'static str = "reliability";
pub const PRIORITIES: &'static str = "priorities";

pub fn as_str(&self) -> &'a str {
self.0
}
Expand Down
24 changes: 21 additions & 3 deletions commons/zenoh-protocol/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,12 @@ impl PriorityRange {
}
}

impl ToString for PriorityRange {
fn to_string(&self) -> String {
format!("{}..={}", *self.start() as u8, *self.end() as u8)
}
}

#[derive(Debug)]
pub enum InvalidPriorityRange {
InvalidSyntax { found: String },
Expand Down Expand Up @@ -458,6 +464,16 @@ pub enum Reliability {
impl Reliability {
pub const DEFAULT: Self = Self::BestEffort;

const BEST_EFFORT_STR: &str = "best_effort";
const RELIABLE_STR: &str = "reliable";

pub fn as_str(&self) -> &str {
match self {
Reliability::BestEffort => Reliability::BEST_EFFORT_STR,
Reliability::Reliable => Reliability::RELIABLE_STR,
}
}

#[cfg(feature = "test")]
pub fn rand() -> Self {
use rand::Rng;
Expand Down Expand Up @@ -500,7 +516,9 @@ impl Display for InvalidReliability {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"invalid Reliability string, expected `best_effort` or `reliable` but found {}",
"invalid Reliability string, expected `{}` or `{}` but found {}",
Reliability::BEST_EFFORT_STR,
Reliability::RELIABLE_STR,
self.found
)
}
Expand All @@ -514,8 +532,8 @@ impl FromStr for Reliability {

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"reliable" => Ok(Reliability::Reliable),
"best_effort" => Ok(Reliability::BestEffort),
Reliability::RELIABLE_STR => Ok(Reliability::Reliable),
Reliability::BEST_EFFORT_STR => Ok(Reliability::BestEffort),
other => Err(InvalidReliability {
found: other.to_string(),
}),
Expand Down
25 changes: 21 additions & 4 deletions io/zenoh-link-commons/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub use multicast::*;
use serde::Serialize;
pub use unicast::*;
use zenoh_protocol::{
core::{Locator, PriorityRange, Reliability},
core::{Locator, Metadata, PriorityRange, Reliability},
transport::BatchSize,
};
use zenoh_result::ZResult;
Expand All @@ -45,7 +45,6 @@ use zenoh_result::ZResult;

pub const BIND_INTERFACE: &str = "iface";

// TODO(fuzzypixelz): Patch the Locators to contain negotiated priority and reliability
#[derive(Clone, Debug, Serialize, Hash, PartialEq, Eq)]
pub struct Link {
pub src: Locator,
Expand Down Expand Up @@ -82,8 +81,8 @@ impl Link {
reliability: Reliability,
) -> Self {
Link {
src: link.get_src().to_owned(),
dst: link.get_dst().to_owned(),
src: Self::to_patched_locator(link.get_src(), priorities.as_ref(), reliability),
dst: Self::to_patched_locator(link.get_dst(), priorities.as_ref(), reliability),
group: None,
mtu: link.get_mtu(),
is_streamed: link.is_streamed(),
Expand All @@ -107,6 +106,24 @@ impl Link {
reliability: Reliability::from(link.is_reliable()),
}
}

/// Updates the metdata of the `locator` with `priorities` and `reliability`.

Check warning on line 110 in io/zenoh-link-commons/src/lib.rs

View workflow job for this annotation

GitHub Actions / Typos Check

"metdata" should be "metadata".

Check warning on line 110 in io/zenoh-link-commons/src/lib.rs

View workflow job for this annotation

GitHub Actions / Typos Check

"metdata" should be "metadata".

Check warning on line 110 in io/zenoh-link-commons/src/lib.rs

View workflow job for this annotation

GitHub Actions / Typos Check

"metdata" should be "metadata".
fn to_patched_locator(
locator: &Locator,
priorities: Option<&PriorityRange>,
reliability: Reliability,
) -> Locator {
let mut locator = locator.clone();
let mut metadata = locator.metadata_mut();
metadata
.insert(Metadata::RELIABILITY, reliability.as_str())
.expect("adding `reliability` to Locator metadata should not fail");
priorities
.map(|ps| metadata.insert(Metadata::PRIORITIES, ps.to_string()))
.transpose()
.expect("adding `priorities` to Locator metadata should not fail");
locator
}
}

impl PartialEq<LinkUnicast> for Link {
Expand Down
9 changes: 3 additions & 6 deletions io/zenoh-transport/src/unicast/establishment/ext/qos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use zenoh_codec::{RCodec, WCodec, Zenoh080};
use zenoh_core::zerror;
use zenoh_link::EndPoint;
use zenoh_protocol::{
core::{Priority, PriorityRange, Reliability},
core::{Metadata, Priority, PriorityRange, Reliability},
transport::{init, open},
};
use zenoh_result::{Error as ZError, ZResult};
Expand Down Expand Up @@ -56,18 +56,15 @@ impl State {
if !is_qos {
Ok(State::NoQoS)
} else {
const RELIABILITY_METADATA_KEY: &str = "reliability";
const PRIORITY_METADATA_KEY: &str = "priorities";

let metadata = endpoint.metadata();

let reliability = metadata
.get(RELIABILITY_METADATA_KEY)
.get(Metadata::RELIABILITY)
.map(Reliability::from_str)
.transpose()?;

let priorities = metadata
.get(PRIORITY_METADATA_KEY)
.get(Metadata::PRIORITIES)
.map(PriorityRange::from_str)
.transpose()?;

Expand Down

0 comments on commit 8da03bb

Please sign in to comment.