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 listener;
23mod multicast;
24pub mod tcp;
25#[cfg(feature = "tls")]
26pub mod tls;
27mod unicast;
28
29use alloc::{borrow::ToOwned, boxed::Box, string::String, vec, vec::Vec};
30use core::{cmp::PartialEq, fmt, hash::Hash};
31
32use async_trait::async_trait;
33pub use listener::*;
34pub use multicast::*;
35use serde::Serialize;
36pub use unicast::*;
37use zenoh_protocol::{
38    core::{Locator, Metadata, PriorityRange, Reliability},
39    transport::BatchSize,
40};
41use zenoh_result::ZResult;
42
43/*************************************/
44/*            GENERAL                */
45/*************************************/
46
47pub const BIND_INTERFACE: &str = "iface";
48pub const TCP_SO_SND_BUF: &str = "so_sndbuf";
49pub const TCP_SO_RCV_BUF: &str = "so_rcvbuf";
50
51#[derive(Clone, Debug, Serialize, Hash, PartialEq, Eq)]
52pub struct Link {
53    pub src: Locator,
54    pub dst: Locator,
55    pub group: Option<Locator>,
56    pub mtu: BatchSize,
57    pub is_streamed: bool,
58    pub interfaces: Vec<String>,
59    pub auth_identifier: LinkAuthId,
60    pub priorities: Option<PriorityRange>,
61    pub reliability: Option<Reliability>,
62}
63
64#[async_trait]
65pub trait LocatorInspector: Default {
66    fn protocol(&self) -> &str;
67    async fn is_multicast(&self, locator: &Locator) -> ZResult<bool>;
68    fn is_reliable(&self, locator: &Locator) -> ZResult<bool>;
69}
70
71pub trait ConfigurationInspector<C>: Default {
72    fn inspect_config(&self, configuration: &C) -> ZResult<String>;
73}
74
75impl fmt::Display for Link {
76    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77        write!(f, "{} => {}", &self.src, &self.dst)
78    }
79}
80
81impl Link {
82    pub fn new_unicast(
83        link: &LinkUnicast,
84        priorities: Option<PriorityRange>,
85        reliability: Option<Reliability>,
86    ) -> Self {
87        Link {
88            src: Self::to_patched_locator(link.get_src(), priorities.as_ref(), reliability),
89            dst: Self::to_patched_locator(link.get_dst(), priorities.as_ref(), reliability),
90            group: None,
91            mtu: link.get_mtu(),
92            is_streamed: link.is_streamed(),
93            interfaces: link.get_interface_names(),
94            auth_identifier: link.get_auth_id().clone(),
95            priorities,
96            reliability,
97        }
98    }
99
100    pub fn new_multicast(link: &LinkMulticast) -> Self {
101        Link {
102            src: link.get_src().to_owned(),
103            dst: link.get_dst().to_owned(),
104            group: Some(link.get_dst().to_owned()),
105            mtu: link.get_mtu(),
106            is_streamed: false,
107            interfaces: vec![],
108            auth_identifier: link.get_auth_id().clone(),
109            priorities: None,
110            reliability: None,
111        }
112    }
113
114    /// Updates the metadata of the `locator` with `priorities` and `reliability`.
115    fn to_patched_locator(
116        locator: &Locator,
117        priorities: Option<&PriorityRange>,
118        reliability: Option<Reliability>,
119    ) -> Locator {
120        let mut locator = locator.clone();
121        let mut metadata = locator.metadata_mut();
122        reliability
123            .map(|r| metadata.insert(Metadata::RELIABILITY, r.to_string()))
124            .transpose()
125            .expect("adding `reliability` to Locator metadata should not fail");
126        priorities
127            .map(|ps| metadata.insert(Metadata::PRIORITIES, ps.to_string()))
128            .transpose()
129            .expect("adding `priorities` to Locator metadata should not fail");
130        locator
131    }
132}
133
134impl PartialEq<LinkMulticast> for Link {
135    fn eq(&self, other: &LinkMulticast) -> bool {
136        self.src == *other.get_src() && self.dst == *other.get_dst()
137    }
138}