zenoh 1.9.0

Zenoh: The Zero Overhead Pub/Sub/Query Protocol.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
//
// Copyright (c) 2023 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
//   ZettaScale Zenoh Team, <zenoh@zettascale.tech>
//

//! Tools to access information about the current zenoh [`Session`](crate::Session).

use zenoh_config::{wrappers::ZenohId, WhatAmI};
#[cfg(feature = "unstable")]
use zenoh_core::{Resolve, ResolveClosure};
use zenoh_link::LinkAuthId;
use zenoh_protocol::core::{Locator, Reliability};
use zenoh_transport::TransportPeer;

#[cfg(feature = "unstable")]
use crate::api::builders::info_links::{LinkEventsListenerBuilder, LinksBuilder};
#[cfg(feature = "unstable")]
use crate::api::builders::info_transport::{TransportEventsListenerBuilder, TransportsBuilder};
#[cfg(feature = "unstable")]
use crate::api::handlers::DefaultHandler;
use crate::api::{
    builders::info::{PeersZenohIdBuilder, RoutersZenohIdBuilder, ZenohIdBuilder},
    handlers::CallbackParameter,
    sample::SampleKind,
    session::WeakSession,
};

/// Struct returned by [`Session::info()`](crate::Session::info) that allows
/// access to information about the current zenoh [`Session`](crate::Session).
///
/// This information includes the [`ZenohId`](crate::session::ZenohId) identifier of the current session,
/// and the identifiers of the connected routers and peers (see also [`WhatAmI`](crate::config::WhatAmI)
/// for more information about peers and routers).
///
/// # Examples
/// ```
/// # #[tokio::main]
/// # async fn main() {
/// let session = zenoh::open(zenoh::Config::default()).await.unwrap();
/// let info = session.info();
/// let zid = info.zid().await;
/// # }
/// ```
pub struct SessionInfo {
    pub(crate) session: WeakSession,
}

impl SessionInfo {
    /// Return the [`ZenohId`](crate::session::ZenohId) of the current zenoh [`Session`](crate::Session).
    ///
    /// # Examples
    /// ```
    /// # #[tokio::main]
    /// # async fn main() {
    /// let session = zenoh::open(zenoh::Config::default()).await.unwrap();
    /// let zid = session.info().zid().await;
    /// # }
    /// ```
    pub fn zid(&self) -> ZenohIdBuilder<'_> {
        ZenohIdBuilder::new(self.session.runtime())
    }

    /// Return the [`ZenohId`](crate::session::ZenohId) of the zenoh routers this process is currently connected to,
    /// or the [`ZenohId`](crate::session::ZenohId) of the current router if this code is run from a router (plugin).
    ///
    /// # Examples
    /// ```
    /// # #[tokio::main]
    /// # async fn main() {
    ///
    /// let session = zenoh::open(zenoh::Config::default()).await.unwrap();
    /// let mut routers_zid = session.info().routers_zid().await;
    /// while let Some(router_zid) = routers_zid.next() {}
    /// # }
    /// ```
    pub fn routers_zid(&self) -> RoutersZenohIdBuilder<'_> {
        RoutersZenohIdBuilder::new(self.session.runtime())
    }

    /// Return the [`ZenohId`](crate::session::ZenohId) of the zenoh peers this process is currently connected to.
    ///
    /// # Examples
    /// ```
    /// # #[tokio::main]
    /// # async fn main() {
    /// let session = zenoh::open(zenoh::Config::default()).await.unwrap();
    /// let mut peers_zid = session.info().peers_zid().await;
    /// while let Some(peer_zid) = peers_zid.next() {}
    /// # }
    /// ```
    pub fn peers_zid(&self) -> PeersZenohIdBuilder<'_> {
        PeersZenohIdBuilder::new(self.session.runtime())
    }

    /// Return the locators on which the current zenoh [`Session`](crate::Session) is listening to.
    ///
    /// # Examples
    /// ```
    /// # #[tokio::main]
    /// # async fn main() {
    ///
    /// let session = zenoh::open(zenoh::Config::default()).await.unwrap();
    /// println!("{:?}", session.info().locators().await);
    /// // print ["tcp/127.0.0.1:7447"]
    /// # }
    /// ```
    #[zenoh_macros::unstable]
    pub fn locators(&self) -> impl Resolve<Vec<Locator>> + '_ {
        ResolveClosure::new(|| self.session.runtime().get_locators())
    }

    /// Return information about currently opened transport sessions. Transport session is a connection to another zenoh node.
    ///
    /// # Examples
    /// ```no_run
    /// # #[tokio::main]
    /// # async fn main() {
    /// let session = zenoh::open(zenoh::Config::default()).await.unwrap();
    /// let transports = session.info().transports().await;
    /// for transport in transports {
    ///     println!("Transport: zid={}, whatami={:?}", transport.zid(), transport.whatami());
    /// }
    /// # }
    /// ```
    #[zenoh_macros::unstable]
    pub fn transports(&self) -> TransportsBuilder<'_> {
        TransportsBuilder::new(self.session.runtime())
    }

    /// Return information about links across all transports.
    ///
    /// # Examples
    /// ```no_run
    /// # #[tokio::main]
    /// # async fn main() {
    /// let session = zenoh::open(zenoh::Config::default()).await.unwrap();
    /// let links = session.info().links().await;
    /// for link in links {
    ///     println!("Link: {} -> {}", link.src(), link.dst());
    /// }
    /// # }
    /// ```
    #[zenoh_macros::unstable]
    pub fn links(&self) -> LinksBuilder<'_> {
        LinksBuilder::new(&self.session)
    }

    /// Subscribe to transport lifecycle events.
    ///
    /// # Examples
    /// ```no_run
    /// # #[tokio::main]
    /// # async fn main() {
    /// use zenoh::sample::SampleKind;
    ///
    /// let session = zenoh::open(zenoh::Config::default()).await.unwrap();
    /// let events = session.info()
    ///     .transport_events_listener()
    ///     .history(true)
    ///     .with(flume::bounded(32))
    ///     .await
    ///     .expect("Failed to declare transport events listener");
    ///
    /// while let Ok(event) = events.recv_async().await {
    ///     match event.kind() {
    ///         SampleKind::Put => println!("Transport opened: {}", event.transport().zid()),
    ///         SampleKind::Delete => println!("Transport closed"),
    ///     }
    /// }
    /// # }
    /// ```
    #[zenoh_macros::unstable]
    pub fn transport_events_listener(&self) -> TransportEventsListenerBuilder<'_, DefaultHandler> {
        TransportEventsListenerBuilder::new(&self.session)
    }

    /// Subscribe to link lifecycle events.
    ///
    /// # Examples
    /// ```no_run
    /// # #[tokio::main]
    /// # async fn main() {
    /// use zenoh::sample::SampleKind;
    ///
    /// let session = zenoh::open(zenoh::Config::default()).await.unwrap();
    /// let listener = session.info()
    ///     .link_events_listener()
    ///     .history(true)
    ///     .with(flume::bounded(32))
    ///     .await
    ///     .expect("Failed to declare link events listener");
    ///
    /// while let Ok(event) = listener.recv_async().await {
    ///     match event.kind() {
    ///         SampleKind::Put => println!("Link added: {} -> {}",
    ///             event.link().src(), event.link().dst()),
    ///         SampleKind::Delete => println!("Link removed"),
    ///     }
    /// }
    /// # }
    /// ```
    #[zenoh_macros::unstable]
    pub fn link_events_listener(&self) -> LinkEventsListenerBuilder<'_, DefaultHandler> {
        LinkEventsListenerBuilder::new(&self.session)
    }
}

/// Transport is a connection established to zenoh peer node.
/// Multiple transports to the same peer can exist. At this moment it's possible
/// to have both a unicast and a multicast transport to the same peer.
///
/// Each transport can have multiple corresponding [`Link`](crate::session::Link)s which represent
/// actual established data links with various protocols.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Transport {
    pub(crate) zid: ZenohId,
    pub(crate) whatami: WhatAmI,
    pub(crate) is_qos: bool,
    pub(crate) is_multicast: bool,
    #[cfg(feature = "shared-memory")]
    pub(crate) is_shm: bool,
}

impl Transport {
    pub(crate) fn new(peer: &TransportPeer, is_multicast: bool) -> Self {
        Transport {
            zid: peer.zid.into(),
            whatami: peer.whatami,
            is_qos: peer.is_qos,
            is_multicast,
            #[cfg(feature = "shared-memory")]
            is_shm: peer.is_shm,
        }
    }

    /// Constructs an uninitialized empty Transport.
    #[zenoh_macros::internal]
    pub fn empty() -> Self {
        Transport {
            zid: ZenohId::default(),
            whatami: WhatAmI::default(),
            is_qos: false,
            is_multicast: false,
            #[cfg(feature = "shared-memory")]
            is_shm: false,
        }
    }

    /// Constructs a Transport from individual fields.
    #[zenoh_macros::internal]
    pub fn new_from_fields(
        zid: ZenohId,
        whatami: WhatAmI,
        is_qos: bool,
        is_multicast: bool,
        #[cfg(feature = "shared-memory")] is_shm: bool,
    ) -> Self {
        Transport {
            zid,
            whatami,
            is_qos,
            is_multicast,
            #[cfg(feature = "shared-memory")]
            is_shm,
        }
    }
}

#[cfg(feature = "unstable")]
impl Transport {
    /// Gets the ZenohId of the remote zenoh node.
    #[inline]
    pub fn zid(&self) -> &ZenohId {
        &self.zid
    }

    /// Gets the type of the remote zenoh node (Router, Peer, or Client).
    #[inline]
    pub fn whatami(&self) -> WhatAmI {
        self.whatami
    }

    /// Returns whether this transport supports QoS.
    #[inline]
    pub fn is_qos(&self) -> bool {
        self.is_qos
    }

    /// Returns whether this transport supports shared memory.
    #[cfg(feature = "shared-memory")]
    #[inline]
    pub fn is_shm(&self) -> bool {
        self.is_shm
    }

    /// Returns whether this transport is multicast.
    #[inline]
    pub fn is_multicast(&self) -> bool {
        self.is_multicast
    }
}

/// Describes a concrete link within a [`Transport`](crate::session::Transport).
/// Zenoh can establish multiple links to the same remote zenoh node using different protocols
/// (e.g., TCP, UDP, QUIC, etc.)
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Link {
    pub(crate) zid: ZenohId,
    pub(crate) src: Locator,
    pub(crate) dst: Locator,
    pub(crate) group: Option<Locator>,
    pub(crate) mtu: u16,
    pub(crate) is_streamed: bool,
    pub(crate) interfaces: Vec<String>,
    pub(crate) auth_identifier: Option<String>,
    pub(crate) priorities: Option<(u8, u8)>,
    pub(crate) reliability: Option<Reliability>,
}

impl Link {
    pub(crate) fn new(zid: ZenohId, link: &zenoh_link_commons::Link, is_qos: bool) -> Self {
        let auth_identifier = match &link.auth_identifier {
            LinkAuthId::Tls(Some(s)) | LinkAuthId::Quic(Some(s)) => Some(s.clone()),
            LinkAuthId::Tls(None)
            | LinkAuthId::Quic(None)
            | LinkAuthId::Tcp
            | LinkAuthId::Udp
            | LinkAuthId::Serial
            | LinkAuthId::Unixpipe
            | LinkAuthId::UnixsockStream
            | LinkAuthId::Vsock
            | LinkAuthId::Ws => None, // avoid using _ wildcard to ensure that new protocols are correctly handled
        };
        let priorities = if is_qos {
            link.priorities
                .as_deref()
                .map(|p| (*p.start() as u8, *p.end() as u8))
                .or(Some((
                    // max priority is the lowest numerical value
                    zenoh_protocol::core::Priority::MAX as u8,
                    zenoh_protocol::core::Priority::MIN as u8,
                )))
        } else {
            None
        };
        // Link reliability is always known - it's either specified explicitly in
        // the config or inferred from the protocol
        let reliability = if is_qos {
            Some(link.reliability.unwrap_or_else(|| {
                let inspector = zenoh_link::LocatorInspector::default();
                // TODO: do we need to check both src and dst?
                if inspector.is_reliable(&link.src).unwrap_or(false)
                    && inspector.is_reliable(&link.dst).unwrap_or(false)
                {
                    Reliability::Reliable
                } else {
                    Reliability::BestEffort
                }
            }))
        } else {
            None
        };
        Link {
            zid,
            src: link.src.clone(),
            dst: link.dst.clone(),
            group: link.group.clone(),
            mtu: link.mtu,
            is_streamed: link.is_streamed,
            interfaces: link.interfaces.clone(),
            auth_identifier,
            priorities,
            reliability,
        }
    }

    /// Constructs an uninitialized empty Link.
    #[zenoh_macros::internal]
    pub fn empty() -> Self {
        Link {
            zid: ZenohId::default(),
            src: Locator::empty(),
            dst: Locator::empty(),
            group: None,
            mtu: 0,
            is_streamed: false,
            interfaces: Vec::new(),
            auth_identifier: None,
            priorities: None,
            reliability: None,
        }
    }
}

#[cfg(feature = "unstable")]
impl Link {
    /// Gets the ZenohId of the transport this link belongs to.
    #[inline]
    pub fn zid(&self) -> &ZenohId {
        &self.zid
    }

    /// Gets the source locator (local endpoint).
    #[inline]
    pub fn src(&self) -> &Locator {
        &self.src
    }

    /// Gets the destination locator (remote endpoint).
    #[inline]
    pub fn dst(&self) -> &Locator {
        &self.dst
    }

    /// Gets the optional group locator (destination if link is for multicast).
    #[inline]
    pub fn group(&self) -> Option<&Locator> {
        self.group.as_ref()
    }

    /// Gets the maximum transmission unit in bytes (MTU) of the link.
    #[inline]
    pub fn mtu(&self) -> u16 {
        self.mtu
    }

    /// Returns whether the link is streamed.
    #[inline]
    pub fn is_streamed(&self) -> bool {
        self.is_streamed
    }

    /// Gets the network interfaces associated with the link.
    #[inline]
    pub fn interfaces(&self) -> &[String] {
        &self.interfaces
    }

    /// Gets the authentication identifier used for the link (if any).
    #[inline]
    pub fn auth_identifier(&self) -> Option<&str> {
        self.auth_identifier.as_deref()
    }

    /// Gets the optional priority range (min, max) associated with the link.
    /// The numeric priority values corresponds [`Priority`](crate::qos::Priority)
    /// but can also contain value 0 (Control) not exposed in the public enum.
    /// Returns None if the transport does not support QoS.
    #[inline]
    pub fn priorities(&self) -> Option<(u8, u8)> {
        self.priorities
    }

    /// Gets the reliability level of the link.
    /// Returns None if the transport does not support QoS.
    #[inline]
    pub fn reliability(&self) -> Option<Reliability> {
        self.reliability
    }
}

/// Event emitted when a transport is opened or closed
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TransportEvent {
    pub(crate) kind: SampleKind, // Put = opened, Delete = closed
    pub(crate) transport: Transport,
}

impl TransportEvent {
    /// Constructs an uninitialized empty TransportEvent.
    #[zenoh_macros::internal]
    pub fn empty() -> Self {
        TransportEvent {
            kind: SampleKind::Put,
            transport: Transport::empty(),
        }
    }
}

#[cfg(feature = "unstable")]
impl TransportEvent {
    /// Returns the kind of event (Put for opened, Delete for closed)
    pub fn kind(&self) -> SampleKind {
        self.kind
    }

    /// Returns a reference to the transport
    pub fn transport(&self) -> &Transport {
        &self.transport
    }

    /// Returns a mutable reference to the transport
    pub fn transport_mut(&mut self) -> &mut Transport {
        &mut self.transport
    }
}

impl CallbackParameter for TransportEvent {
    type Message<'a> = Self;
    fn from_message(msg: Self::Message<'_>) -> Self {
        msg
    }
}

/// Event emitted when a link is added or removed
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LinkEvent {
    pub(crate) kind: SampleKind, // Put = added, Delete = removed
    pub(crate) link: Link,
}

impl LinkEvent {
    /// Constructs an uninitialized empty LinkEvent.
    #[zenoh_macros::internal]
    pub fn empty() -> Self {
        LinkEvent {
            kind: SampleKind::Put,
            link: Link::empty(),
        }
    }
}

#[cfg(feature = "unstable")]
impl LinkEvent {
    /// Returns the kind of event (Put for added, Delete for removed)
    pub fn kind(&self) -> SampleKind {
        self.kind
    }

    /// Returns a reference to the link
    pub fn link(&self) -> &Link {
        &self.link
    }

    /// Returns a mutable reference to the link
    pub fn link_mut(&mut self) -> &mut Link {
        &mut self.link
    }
}

impl CallbackParameter for LinkEvent {
    type Message<'a> = Self;
    fn from_message(msg: Self::Message<'_>) -> Self {
        msg
    }
}

#[cfg(all(test, feature = "internal"))]
mod tests {
    use zenoh_protocol::core::WhatAmI;

    use super::*;

    #[test]
    fn test_new_from_fields_equals_new_from_peer() {
        let peer = TransportPeer {
            zid: ZenohId::default().into(),
            whatami: WhatAmI::Router,
            is_qos: true,
            #[cfg(feature = "shared-memory")]
            is_shm: false,
            links: vec![],
            region_name: None,
        };

        let via_new = Transport::new(&peer, /*is_multicast=*/ false);
        let via_fields = Transport::new_from_fields(
            peer.zid.into(),
            peer.whatami,
            peer.is_qos,
            /*is_multicast=*/ false,
            #[cfg(feature = "shared-memory")]
            peer.is_shm,
        );

        assert_eq!(via_new, via_fields);
    }
}