zenoh_link_commons/
lib.rs

1//
2// Copyright (c) 2023 ZettaScale Technology
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7// which is available at https://www.apache.org/licenses/LICENSE-2.0.
8//
9// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10//
11// Contributors:
12//   ZettaScale Zenoh Team, <zenoh@zettascale.tech>
13//
14
15//! ⚠️ WARNING ⚠️
16//!
17//! This crate is intended for Zenoh's internal use.
18//!
19//! [Click here for Zenoh's documentation](https://docs.rs/zenoh/latest/zenoh)
20extern crate alloc;
21
22mod dscp;
23mod listener;
24mod multicast;
25#[cfg(feature = "quic")]
26pub mod quic;
27pub mod tcp;
28#[cfg(feature = "tls")]
29pub mod tls;
30mod unicast;
31
32use alloc::{borrow::ToOwned, boxed::Box, string::String, vec, vec::Vec};
33use core::{cmp::PartialEq, fmt, hash::Hash};
34
35use async_trait::async_trait;
36pub use dscp::*;
37pub use listener::*;
38pub use multicast::*;
39use serde::Serialize;
40pub use unicast::*;
41use zenoh_protocol::{
42    core::{Locator, Metadata, PriorityRange, Reliability},
43    transport::BatchSize,
44};
45use zenoh_result::ZResult;
46
47/*************************************/
48/*            GENERAL                */
49/*************************************/
50
51pub const BIND_SOCKET: &str = "bind";
52pub const BIND_INTERFACE: &str = "iface";
53pub const TCP_SO_SND_BUF: &str = "so_sndbuf";
54pub const TCP_SO_RCV_BUF: &str = "so_rcvbuf";
55pub const DSCP: &str = "dscp";
56
57#[derive(Clone, Debug, Serialize, Hash, PartialEq, Eq)]
58pub struct Link {
59    pub src: Locator,
60    pub dst: Locator,
61    pub group: Option<Locator>,
62    pub mtu: BatchSize,
63    pub is_streamed: bool,
64    pub interfaces: Vec<String>,
65    pub auth_identifier: LinkAuthId,
66    pub priorities: Option<PriorityRange>,
67    pub reliability: Option<Reliability>,
68}
69
70#[async_trait]
71pub trait LocatorInspector: Default {
72    fn protocol(&self) -> &str;
73    async fn is_multicast(&self, locator: &Locator) -> ZResult<bool>;
74    fn is_reliable(&self, locator: &Locator) -> ZResult<bool>;
75}
76
77pub trait ConfigurationInspector<C>: Default {
78    fn inspect_config(&self, configuration: &C) -> ZResult<String>;
79}
80
81impl fmt::Display for Link {
82    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83        write!(f, "{} => {}", &self.src, &self.dst)
84    }
85}
86
87impl Link {
88    pub fn new_unicast(
89        link: &LinkUnicast,
90        priorities: Option<PriorityRange>,
91        reliability: Option<Reliability>,
92    ) -> Self {
93        Link {
94            src: Self::to_patched_locator(link.get_src(), priorities.as_ref(), reliability),
95            dst: Self::to_patched_locator(link.get_dst(), priorities.as_ref(), reliability),
96            group: None,
97            mtu: link.get_mtu(),
98            is_streamed: link.is_streamed(),
99            interfaces: link.get_interface_names(),
100            auth_identifier: link.get_auth_id().clone(),
101            priorities,
102            reliability,
103        }
104    }
105
106    pub fn new_multicast(link: &LinkMulticast) -> Self {
107        Link {
108            src: link.get_src().to_owned(),
109            dst: link.get_dst().to_owned(),
110            group: Some(link.get_dst().to_owned()),
111            mtu: link.get_mtu(),
112            is_streamed: false,
113            interfaces: vec![],
114            auth_identifier: link.get_auth_id().clone(),
115            priorities: None,
116            reliability: None,
117        }
118    }
119
120    /// Updates the metadata of the `locator` with `priorities` and `reliability`.
121    fn to_patched_locator(
122        locator: &Locator,
123        priorities: Option<&PriorityRange>,
124        reliability: Option<Reliability>,
125    ) -> Locator {
126        let mut locator = locator.clone();
127        let mut metadata = locator.metadata_mut();
128        reliability
129            .map(|r| metadata.insert(Metadata::RELIABILITY, r.to_string()))
130            .transpose()
131            .expect("adding `reliability` to Locator metadata should not fail");
132        priorities
133            .map(|ps| metadata.insert(Metadata::PRIORITIES, ps.to_string()))
134            .transpose()
135            .expect("adding `priorities` to Locator metadata should not fail");
136        locator
137    }
138}
139
140impl PartialEq<LinkMulticast> for Link {
141    fn eq(&self, other: &LinkMulticast) -> bool {
142        self.src == *other.get_src() && self.dst == *other.get_dst()
143    }
144}