zenoh/api/builders/
info.rs

1//
2// Copyright (c) 2024 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
15use std::future::{IntoFuture, Ready};
16
17use zenoh_config::wrappers::ZenohId;
18use zenoh_core::{Resolvable, Wait};
19use zenoh_protocol::core::WhatAmI;
20
21use crate::net::runtime::Runtime;
22
23/// A builder returned by [`SessionInfo::zid()`](crate::session::SessionInfo::zid) that allows
24/// to access the [`ZenohId`] of the current zenoh [`Session`](crate::Session).
25///
26/// # Examples
27/// ```
28/// # #[tokio::main]
29/// # async fn main() {
30///
31/// let session = zenoh::open(zenoh::Config::default()).await.unwrap();
32/// let zid = session.info().zid().await;
33/// # }
34/// ```
35#[must_use = "Resolvables do nothing unless you resolve them using `.await` or `zenoh::Wait::wait`"]
36pub struct ZenohIdBuilder<'a> {
37    runtime: &'a Runtime,
38}
39
40impl<'a> ZenohIdBuilder<'a> {
41    pub(crate) fn new(runtime: &'a Runtime) -> Self {
42        Self { runtime }
43    }
44}
45
46impl Resolvable for ZenohIdBuilder<'_> {
47    type To = ZenohId;
48}
49
50impl Wait for ZenohIdBuilder<'_> {
51    fn wait(self) -> Self::To {
52        self.runtime.zid()
53    }
54}
55
56impl IntoFuture for ZenohIdBuilder<'_> {
57    type Output = <Self as Resolvable>::To;
58    type IntoFuture = Ready<<Self as Resolvable>::To>;
59
60    fn into_future(self) -> Self::IntoFuture {
61        std::future::ready(self.wait())
62    }
63}
64
65/// A builder returned by [`SessionInfo::routers_zid()`](crate::session::SessionInfo::routers_zid) that allows
66/// to access the [`ZenohId`] of the zenoh routers this process is currently connected to
67/// or the [`ZenohId`] of the current router if this code is run from a router (plugin).
68///
69/// # Examples
70/// ```
71/// # #[tokio::main]
72/// # async fn main() {
73///
74/// let session = zenoh::open(zenoh::Config::default()).await.unwrap();
75/// let mut routers_zid = session.info().routers_zid().await;
76/// while let Some(router_zid) = routers_zid.next() {}
77/// # }
78/// ```
79#[must_use = "Resolvables do nothing unless you resolve them using `.await` or `zenoh::Wait::wait`"]
80pub struct RoutersZenohIdBuilder<'a> {
81    runtime: &'a Runtime,
82}
83
84impl<'a> RoutersZenohIdBuilder<'a> {
85    pub(crate) fn new(runtime: &'a Runtime) -> Self {
86        Self { runtime }
87    }
88}
89
90impl Resolvable for RoutersZenohIdBuilder<'_> {
91    type To = Box<dyn Iterator<Item = ZenohId> + Send + Sync>;
92}
93
94impl Wait for RoutersZenohIdBuilder<'_> {
95    fn wait(self) -> Self::To {
96        Box::new(
97            zenoh_runtime::ZRuntime::Application
98                .block_in_place(self.runtime.manager().get_transports_unicast())
99                .into_iter()
100                .filter_map(|s| {
101                    s.get_whatami()
102                        .ok()
103                        .and_then(|what| (what == WhatAmI::Router).then_some(()))
104                        .and_then(|_| s.get_zid().map(Into::into).ok())
105                }),
106        )
107    }
108}
109
110impl IntoFuture for RoutersZenohIdBuilder<'_> {
111    type Output = <Self as Resolvable>::To;
112    type IntoFuture = Ready<<Self as Resolvable>::To>;
113
114    fn into_future(self) -> Self::IntoFuture {
115        std::future::ready(self.wait())
116    }
117}
118
119/// A builder returned by [`SessionInfo::peers_zid()`](crate::session::SessionInfo::peers_zid) that allows
120/// to access the [`ZenohId`] of the zenoh peers this process is currently connected to.
121///
122/// # Examples
123/// ```
124/// # #[tokio::main]
125/// # async fn main() {
126///
127/// let session = zenoh::open(zenoh::Config::default()).await.unwrap();
128/// let zid = session.info().zid().await;
129/// let mut peers_zid = session.info().peers_zid().await;
130/// while let Some(peer_zid) = peers_zid.next() {}
131/// # }
132/// ```
133#[must_use = "Resolvables do nothing unless you resolve them using `.await` or `zenoh::Wait::wait`"]
134pub struct PeersZenohIdBuilder<'a> {
135    runtime: &'a Runtime,
136}
137
138impl<'a> PeersZenohIdBuilder<'a> {
139    pub(crate) fn new(runtime: &'a Runtime) -> Self {
140        Self { runtime }
141    }
142}
143
144impl Resolvable for PeersZenohIdBuilder<'_> {
145    type To = Box<dyn Iterator<Item = ZenohId> + Send + Sync>;
146}
147
148impl Wait for PeersZenohIdBuilder<'_> {
149    fn wait(self) -> <Self as Resolvable>::To {
150        Box::new(
151            zenoh_runtime::ZRuntime::Application
152                .block_in_place(self.runtime.manager().get_transports_unicast())
153                .into_iter()
154                .filter_map(|s| {
155                    s.get_whatami()
156                        .ok()
157                        .and_then(|what| (what == WhatAmI::Peer).then_some(()))
158                        .and_then(|_| s.get_zid().map(Into::into).ok())
159                }),
160        )
161    }
162}
163
164impl IntoFuture for PeersZenohIdBuilder<'_> {
165    type Output = <Self as Resolvable>::To;
166    type IntoFuture = Ready<<Self as Resolvable>::To>;
167
168    fn into_future(self) -> Self::IntoFuture {
169        std::future::ready(self.wait())
170    }
171}