zenoh_link/
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)
20use std::collections::HashMap;
21
22use zenoh_config::Config;
23pub use zenoh_link_commons::*;
24#[cfg(feature = "transport_quic")]
25pub use zenoh_link_quic as quic;
26#[cfg(feature = "transport_quic")]
27use zenoh_link_quic::{
28    LinkManagerUnicastQuic, QuicConfigurator, QuicLocatorInspector, QUIC_LOCATOR_PREFIX,
29};
30#[cfg(feature = "transport_serial")]
31pub use zenoh_link_serial as serial;
32#[cfg(feature = "transport_serial")]
33use zenoh_link_serial::{LinkManagerUnicastSerial, SerialLocatorInspector, SERIAL_LOCATOR_PREFIX};
34#[cfg(feature = "transport_tcp")]
35pub use zenoh_link_tcp as tcp;
36#[cfg(feature = "transport_tcp")]
37use zenoh_link_tcp::{
38    LinkManagerUnicastTcp, TcpConfigurator, TcpLocatorInspector, TCP_LOCATOR_PREFIX,
39};
40#[cfg(feature = "transport_tls")]
41pub use zenoh_link_tls as tls;
42#[cfg(feature = "transport_tls")]
43use zenoh_link_tls::{
44    LinkManagerUnicastTls, TlsConfigurator, TlsLocatorInspector, TLS_LOCATOR_PREFIX,
45};
46#[cfg(feature = "transport_udp")]
47pub use zenoh_link_udp as udp;
48#[cfg(feature = "transport_udp")]
49use zenoh_link_udp::{
50    LinkManagerMulticastUdp, LinkManagerUnicastUdp, UdpLocatorInspector, UDP_LOCATOR_PREFIX,
51};
52#[cfg(feature = "transport_unixpipe")]
53pub use zenoh_link_unixpipe as unixpipe;
54#[cfg(feature = "transport_unixpipe")]
55use zenoh_link_unixpipe::{
56    LinkManagerUnicastPipe, UnixPipeConfigurator, UnixPipeLocatorInspector, UNIXPIPE_LOCATOR_PREFIX,
57};
58#[cfg(all(feature = "transport_unixsock-stream", target_family = "unix"))]
59pub use zenoh_link_unixsock_stream as unixsock_stream;
60#[cfg(all(feature = "transport_unixsock-stream", target_family = "unix"))]
61use zenoh_link_unixsock_stream::{
62    LinkManagerUnicastUnixSocketStream, UnixSockStreamLocatorInspector,
63    UNIXSOCKSTREAM_LOCATOR_PREFIX,
64};
65#[cfg(all(feature = "transport_vsock", target_os = "linux"))]
66pub use zenoh_link_vsock as vsock;
67#[cfg(all(feature = "transport_vsock", target_os = "linux"))]
68use zenoh_link_vsock::{LinkManagerUnicastVsock, VsockLocatorInspector, VSOCK_LOCATOR_PREFIX};
69#[cfg(feature = "transport_ws")]
70pub use zenoh_link_ws as ws;
71#[cfg(feature = "transport_ws")]
72use zenoh_link_ws::{LinkManagerUnicastWs, WsLocatorInspector, WS_LOCATOR_PREFIX};
73pub use zenoh_protocol::core::{EndPoint, Locator};
74use zenoh_result::{bail, ZResult};
75
76pub const PROTOCOLS: &[&str] = &[
77    #[cfg(feature = "transport_quic")]
78    quic::QUIC_LOCATOR_PREFIX,
79    #[cfg(feature = "transport_tcp")]
80    tcp::TCP_LOCATOR_PREFIX,
81    #[cfg(feature = "transport_tls")]
82    tls::TLS_LOCATOR_PREFIX,
83    #[cfg(feature = "transport_udp")]
84    udp::UDP_LOCATOR_PREFIX,
85    #[cfg(feature = "transport_ws")]
86    ws::WS_LOCATOR_PREFIX,
87    #[cfg(all(feature = "transport_unixsock-stream", target_family = "unix"))]
88    unixsock_stream::UNIXSOCKSTREAM_LOCATOR_PREFIX,
89    #[cfg(feature = "transport_serial")]
90    serial::SERIAL_LOCATOR_PREFIX,
91    #[cfg(feature = "transport_unixpipe")]
92    unixpipe::UNIXPIPE_LOCATOR_PREFIX,
93    #[cfg(all(feature = "transport_vsock", target_os = "linux"))]
94    vsock::VSOCK_LOCATOR_PREFIX,
95];
96
97#[derive(Default, Clone)]
98pub struct LocatorInspector {
99    #[cfg(feature = "transport_quic")]
100    quic_inspector: QuicLocatorInspector,
101    #[cfg(feature = "transport_tcp")]
102    tcp_inspector: TcpLocatorInspector,
103    #[cfg(feature = "transport_tls")]
104    tls_inspector: TlsLocatorInspector,
105    #[cfg(feature = "transport_udp")]
106    udp_inspector: UdpLocatorInspector,
107    #[cfg(feature = "transport_ws")]
108    ws_inspector: WsLocatorInspector,
109    #[cfg(all(feature = "transport_unixsock-stream", target_family = "unix"))]
110    unixsock_stream_inspector: UnixSockStreamLocatorInspector,
111    #[cfg(feature = "transport_serial")]
112    serial_inspector: SerialLocatorInspector,
113    #[cfg(feature = "transport_unixpipe")]
114    unixpipe_inspector: UnixPipeLocatorInspector,
115    #[cfg(all(feature = "transport_vsock", target_os = "linux"))]
116    vsock_inspector: VsockLocatorInspector,
117}
118impl LocatorInspector {
119    pub fn is_reliable(&self, locator: &Locator) -> ZResult<bool> {
120        #[allow(unused_imports)]
121        use zenoh_link_commons::LocatorInspector;
122        let protocol = locator.protocol();
123        match protocol.as_str() {
124            #[cfg(feature = "transport_tcp")]
125            TCP_LOCATOR_PREFIX => self.tcp_inspector.is_reliable(locator),
126            #[cfg(feature = "transport_udp")]
127            UDP_LOCATOR_PREFIX => self.udp_inspector.is_reliable(locator),
128            #[cfg(feature = "transport_tls")]
129            TLS_LOCATOR_PREFIX => self.tls_inspector.is_reliable(locator),
130            #[cfg(feature = "transport_quic")]
131            QUIC_LOCATOR_PREFIX => self.quic_inspector.is_reliable(locator),
132            #[cfg(all(feature = "transport_unixsock-stream", target_family = "unix"))]
133            UNIXSOCKSTREAM_LOCATOR_PREFIX => self.unixsock_stream_inspector.is_reliable(locator),
134            #[cfg(feature = "transport_ws")]
135            WS_LOCATOR_PREFIX => self.ws_inspector.is_reliable(locator),
136            #[cfg(feature = "transport_serial")]
137            SERIAL_LOCATOR_PREFIX => self.serial_inspector.is_reliable(locator),
138            #[cfg(feature = "transport_unixpipe")]
139            UNIXPIPE_LOCATOR_PREFIX => self.unixpipe_inspector.is_reliable(locator),
140            #[cfg(all(feature = "transport_vsock", target_os = "linux"))]
141            VSOCK_LOCATOR_PREFIX => self.vsock_inspector.is_reliable(locator),
142            _ => bail!("Unsupported protocol: {}.", protocol),
143        }
144    }
145
146    pub async fn is_multicast(&self, locator: &Locator) -> ZResult<bool> {
147        #[allow(unused_imports)]
148        use zenoh_link_commons::LocatorInspector;
149        let protocol = locator.protocol();
150        match protocol.as_str() {
151            #[cfg(feature = "transport_tcp")]
152            TCP_LOCATOR_PREFIX => self.tcp_inspector.is_multicast(locator).await,
153            #[cfg(feature = "transport_udp")]
154            UDP_LOCATOR_PREFIX => self.udp_inspector.is_multicast(locator).await,
155            #[cfg(feature = "transport_tls")]
156            TLS_LOCATOR_PREFIX => self.tls_inspector.is_multicast(locator).await,
157            #[cfg(feature = "transport_quic")]
158            QUIC_LOCATOR_PREFIX => self.quic_inspector.is_multicast(locator).await,
159            #[cfg(all(feature = "transport_unixsock-stream", target_family = "unix"))]
160            UNIXSOCKSTREAM_LOCATOR_PREFIX => {
161                self.unixsock_stream_inspector.is_multicast(locator).await
162            }
163            #[cfg(feature = "transport_ws")]
164            WS_LOCATOR_PREFIX => self.ws_inspector.is_multicast(locator).await,
165            #[cfg(feature = "transport_serial")]
166            SERIAL_LOCATOR_PREFIX => self.serial_inspector.is_multicast(locator).await,
167            #[cfg(feature = "transport_unixpipe")]
168            UNIXPIPE_LOCATOR_PREFIX => self.unixpipe_inspector.is_multicast(locator).await,
169            #[cfg(all(feature = "transport_vsock", target_os = "linux"))]
170            VSOCK_LOCATOR_PREFIX => self.vsock_inspector.is_multicast(locator).await,
171            _ => bail!("Unsupported protocol: {}.", protocol),
172        }
173    }
174}
175#[derive(Default)]
176pub struct LinkConfigurator {
177    #[cfg(feature = "transport_tcp")]
178    tcp_inspector: TcpConfigurator,
179    #[cfg(feature = "transport_quic")]
180    quic_inspector: QuicConfigurator,
181    #[cfg(feature = "transport_tls")]
182    tls_inspector: TlsConfigurator,
183    #[cfg(feature = "transport_unixpipe")]
184    unixpipe_inspector: UnixPipeConfigurator,
185}
186
187impl LinkConfigurator {
188    #[allow(unused_variables, unused_mut)]
189    pub fn configurations(
190        &self,
191        config: &Config,
192    ) -> (
193        HashMap<String, String>,
194        HashMap<String, zenoh_result::Error>,
195    ) {
196        let mut configs = HashMap::new();
197        let mut errors = HashMap::new();
198        let mut insert_config = |proto: String, cfg: ZResult<String>| match cfg {
199            Ok(v) => {
200                configs.insert(proto, v);
201            }
202            Err(e) => {
203                errors.insert(proto, e);
204            }
205        };
206        #[cfg(feature = "transport_tcp")]
207        {
208            insert_config(
209                TCP_LOCATOR_PREFIX.into(),
210                self.tcp_inspector.inspect_config(config),
211            );
212        }
213        #[cfg(feature = "transport_quic")]
214        {
215            insert_config(
216                QUIC_LOCATOR_PREFIX.into(),
217                self.quic_inspector.inspect_config(config),
218            );
219        }
220        #[cfg(feature = "transport_tls")]
221        {
222            insert_config(
223                TLS_LOCATOR_PREFIX.into(),
224                self.tls_inspector.inspect_config(config),
225            );
226        }
227        #[cfg(feature = "transport_unixpipe")]
228        {
229            insert_config(
230                UNIXPIPE_LOCATOR_PREFIX.into(),
231                self.unixpipe_inspector.inspect_config(config),
232            );
233        }
234        (configs, errors)
235    }
236}
237
238/*************************************/
239/*             UNICAST               */
240/*************************************/
241
242pub struct LinkManagerBuilderUnicast;
243
244impl LinkManagerBuilderUnicast {
245    pub fn make(_manager: NewLinkChannelSender, protocol: &str) -> ZResult<LinkManagerUnicast> {
246        match protocol {
247            #[cfg(feature = "transport_tcp")]
248            TCP_LOCATOR_PREFIX => Ok(std::sync::Arc::new(LinkManagerUnicastTcp::new(_manager))),
249            #[cfg(feature = "transport_udp")]
250            UDP_LOCATOR_PREFIX => Ok(std::sync::Arc::new(LinkManagerUnicastUdp::new(_manager))),
251            #[cfg(feature = "transport_tls")]
252            TLS_LOCATOR_PREFIX => Ok(std::sync::Arc::new(LinkManagerUnicastTls::new(_manager))),
253            #[cfg(feature = "transport_quic")]
254            QUIC_LOCATOR_PREFIX => Ok(std::sync::Arc::new(LinkManagerUnicastQuic::new(_manager))),
255            #[cfg(all(feature = "transport_unixsock-stream", target_family = "unix"))]
256            UNIXSOCKSTREAM_LOCATOR_PREFIX => Ok(std::sync::Arc::new(
257                LinkManagerUnicastUnixSocketStream::new(_manager),
258            )),
259            #[cfg(feature = "transport_ws")]
260            WS_LOCATOR_PREFIX => Ok(std::sync::Arc::new(LinkManagerUnicastWs::new(_manager))),
261            #[cfg(feature = "transport_serial")]
262            SERIAL_LOCATOR_PREFIX => {
263                Ok(std::sync::Arc::new(LinkManagerUnicastSerial::new(_manager)))
264            }
265            #[cfg(feature = "transport_unixpipe")]
266            UNIXPIPE_LOCATOR_PREFIX => {
267                Ok(std::sync::Arc::new(LinkManagerUnicastPipe::new(_manager)))
268            }
269            #[cfg(all(feature = "transport_vsock", target_os = "linux"))]
270            VSOCK_LOCATOR_PREFIX => Ok(std::sync::Arc::new(LinkManagerUnicastVsock::new(_manager))),
271            _ => bail!("Unicast not supported for {} protocol", protocol),
272        }
273    }
274}
275
276/*************************************/
277/*            MULTICAST              */
278/*************************************/
279
280pub struct LinkManagerBuilderMulticast;
281
282impl LinkManagerBuilderMulticast {
283    pub fn make(protocol: &str) -> ZResult<LinkManagerMulticast> {
284        match protocol {
285            #[cfg(feature = "transport_udp")]
286            UDP_LOCATOR_PREFIX => Ok(std::sync::Arc::new(LinkManagerMulticastUdp)),
287            _ => bail!("Multicast not supported for {} protocol", protocol),
288        }
289    }
290}