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