Skip to content

Commit 73bdb7f

Browse files
Fix and Improve liveliness doc (#1195)
1 parent fc18f90 commit 73bdb7f

File tree

2 files changed

+97
-14
lines changed

2 files changed

+97
-14
lines changed

zenoh/src/api/liveliness.rs

+45-14
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,12 @@ use super::{
4040
/// A [`LivelinessToken`](LivelinessToken) is a token which liveliness is tied
4141
/// to the Zenoh [`Session`](Session) and can be monitored by remote applications.
4242
///
43-
/// A [`LivelinessToken`](LivelinessToken) with key `key/expression` can be
44-
/// queried or subscribed to on key `@/liveliness/key/expression`.
45-
///
4643
/// The `Liveliness` structure can be obtained with the
4744
/// [`Session::liveliness()`](Session::liveliness) function
4845
/// of the [`Session`] struct.
4946
///
5047
/// # Examples
48+
/// ### Declaring a token
5149
/// ```
5250
/// # #[tokio::main]
5351
/// # async fn main() {
@@ -61,6 +59,39 @@ use super::{
6159
/// .unwrap();
6260
/// # }
6361
/// ```
62+
///
63+
/// ### Querying tokens
64+
/// ```
65+
/// # #[tokio::main]
66+
/// # async fn main() {
67+
/// use zenoh::prelude::*;
68+
///
69+
/// let session = zenoh::open(zenoh::config::peer()).await.unwrap();
70+
/// let replies = session.liveliness().get("key/**").await.unwrap();
71+
/// while let Ok(reply) = replies.recv_async().await {
72+
/// if let Ok(sample) = reply.result() {
73+
/// println!(">> Liveliness token {}", sample.key_expr());
74+
/// }
75+
/// }
76+
/// # }
77+
/// ```
78+
///
79+
/// ### Subscribing to liveliness changes
80+
/// ```no_run
81+
/// # #[tokio::main]
82+
/// # async fn main() {
83+
/// use zenoh::{prelude::*, sample::SampleKind};
84+
///
85+
/// let session = zenoh::open(zenoh::config::peer()).await.unwrap();
86+
/// let subscriber = session.liveliness().declare_subscriber("key/**").await.unwrap();
87+
/// while let Ok(sample) = subscriber.recv_async().await {
88+
/// match sample.kind() {
89+
/// SampleKind::Put => println!("New liveliness: {}", sample.key_expr()),
90+
/// SampleKind::Delete => println!("Lost liveliness: {}", sample.key_expr()),
91+
/// }
92+
/// }
93+
/// # }
94+
/// ```
6495
#[zenoh_macros::unstable]
6596
pub struct Liveliness<'a> {
6697
pub(crate) session: SessionRef<'a>,
@@ -250,9 +281,6 @@ pub(crate) struct LivelinessTokenState {
250281
/// A token whose liveliness is tied to the Zenoh [`Session`](Session)
251282
/// and can be monitored by remote applications.
252283
///
253-
/// A `LivelinessToken` with key `key/expression` can be queried or subscribed
254-
/// to on key `@/liveliness/key/expression`.
255-
///
256284
/// A declared liveliness token will be seen as alive by any other Zenoh
257285
/// application in the system that monitors it while the liveliness token
258286
/// is not undeclared or dropped, while the Zenoh application that declared
@@ -388,7 +416,7 @@ impl Drop for LivelinessToken<'_> {
388416
}
389417
}
390418

391-
/// A builder for initializing a [`FlumeSubscriber`](FlumeSubscriber).
419+
/// A builder for initializing a liveliness [`FlumeSubscriber`](FlumeSubscriber).
392420
///
393421
/// # Examples
394422
/// ```
@@ -398,8 +426,8 @@ impl Drop for LivelinessToken<'_> {
398426
///
399427
/// let session = zenoh::open(zenoh::config::peer()).await.unwrap();
400428
/// let subscriber = session
429+
/// .liveliness()
401430
/// .declare_subscriber("key/expression")
402-
/// .best_effort()
403431
/// .await
404432
/// .unwrap();
405433
/// # }
@@ -415,7 +443,7 @@ pub struct LivelinessSubscriberBuilder<'a, 'b, Handler> {
415443

416444
#[zenoh_macros::unstable]
417445
impl<'a, 'b> LivelinessSubscriberBuilder<'a, 'b, DefaultHandler> {
418-
/// Receive the samples for this subscription with a callback.
446+
/// Receive the samples for this liveliness subscription with a callback.
419447
///
420448
/// # Examples
421449
/// ```
@@ -425,6 +453,7 @@ impl<'a, 'b> LivelinessSubscriberBuilder<'a, 'b, DefaultHandler> {
425453
///
426454
/// let session = zenoh::open(zenoh::config::peer()).await.unwrap();
427455
/// let subscriber = session
456+
/// .liveliness()
428457
/// .declare_subscriber("key/expression")
429458
/// .callback(|sample| { println!("Received: {} {:?}", sample.key_expr(), sample.payload()); })
430459
/// .await
@@ -452,10 +481,10 @@ impl<'a, 'b> LivelinessSubscriberBuilder<'a, 'b, DefaultHandler> {
452481
}
453482
}
454483

455-
/// Receive the samples for this subscription with a mutable callback.
484+
/// Receive the samples for this liveliness subscription with a mutable callback.
456485
///
457486
/// Using this guarantees that your callback will never be called concurrently.
458-
/// If your callback is also accepted by the [`callback`](SubscriberBuilder::callback) method, we suggest you use it instead of `callback_mut`
487+
/// If your callback is also accepted by the [`callback`](LivelinessSubscriberBuilder::callback) method, we suggest you use it instead of `callback_mut`
459488
///
460489
/// # Examples
461490
/// ```
@@ -466,6 +495,7 @@ impl<'a, 'b> LivelinessSubscriberBuilder<'a, 'b, DefaultHandler> {
466495
/// let session = zenoh::open(zenoh::config::peer()).await.unwrap();
467496
/// let mut n = 0;
468497
/// let subscriber = session
498+
/// .liveliness()
469499
/// .declare_subscriber("key/expression")
470500
/// .callback_mut(move |_sample| { n += 1; })
471501
/// .await
@@ -484,7 +514,7 @@ impl<'a, 'b> LivelinessSubscriberBuilder<'a, 'b, DefaultHandler> {
484514
self.callback(locked(callback))
485515
}
486516

487-
/// Receive the samples for this subscription with a [`Handler`](crate::prelude::IntoHandler).
517+
/// Receive the samples for this liveliness subscription with a [`Handler`](crate::prelude::IntoHandler).
488518
///
489519
/// # Examples
490520
/// ```no_run
@@ -494,6 +524,7 @@ impl<'a, 'b> LivelinessSubscriberBuilder<'a, 'b, DefaultHandler> {
494524
///
495525
/// let session = zenoh::open(zenoh::config::peer()).await.unwrap();
496526
/// let subscriber = session
527+
/// .liveliness()
497528
/// .declare_subscriber("key/expression")
498529
/// .with(flume::bounded(32))
499530
/// .await
@@ -642,7 +673,7 @@ impl<'a, 'b> LivelinessGetBuilder<'a, 'b, DefaultHandler> {
642673
}
643674
}
644675

645-
/// Receive the replies for this query with a mutable callback.
676+
/// Receive the replies for this liveliness query with a mutable callback.
646677
///
647678
/// Using this guarantees that your callback will never be called concurrently.
648679
/// If your callback is also accepted by the [`callback`](LivelinessGetBuilder::callback) method, we suggest you use it instead of `callback_mut`
@@ -674,7 +705,7 @@ impl<'a, 'b> LivelinessGetBuilder<'a, 'b, DefaultHandler> {
674705
self.callback(locked(callback))
675706
}
676707

677-
/// Receive the replies for this query with a [`Handler`](crate::prelude::IntoHandler).
708+
/// Receive the replies for this liveliness query with a [`Handler`](crate::prelude::IntoHandler).
678709
///
679710
/// # Examples
680711
/// ```

zenoh/src/lib.rs

+52
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,58 @@ pub mod scouting {
326326
}
327327

328328
/// Liveliness primitives
329+
///
330+
/// A [`LivelinessToken`](liveliness::LivelinessToken) is a token which liveliness is tied
331+
/// to the Zenoh [`Session`](Session) and can be monitored by remote applications.
332+
///
333+
/// # Examples
334+
/// ### Declaring a token
335+
/// ```
336+
/// # #[tokio::main]
337+
/// # async fn main() {
338+
/// use zenoh::prelude::*;
339+
///
340+
/// let session = zenoh::open(zenoh::config::peer()).await.unwrap();
341+
/// let liveliness = session
342+
/// .liveliness()
343+
/// .declare_token("key/expression")
344+
/// .await
345+
/// .unwrap();
346+
/// # }
347+
/// ```
348+
///
349+
/// ### Querying tokens
350+
/// ```
351+
/// # #[tokio::main]
352+
/// # async fn main() {
353+
/// use zenoh::prelude::*;
354+
///
355+
/// let session = zenoh::open(zenoh::config::peer()).await.unwrap();
356+
/// let replies = session.liveliness().get("key/**").await.unwrap();
357+
/// while let Ok(reply) = replies.recv_async().await {
358+
/// if let Ok(sample) = reply.result() {
359+
/// println!(">> Liveliness token {}", sample.key_expr());
360+
/// }
361+
/// }
362+
/// # }
363+
/// ```
364+
///
365+
/// ### Subscribing to liveliness changes
366+
/// ```no_run
367+
/// # #[tokio::main]
368+
/// # async fn main() {
369+
/// use zenoh::{prelude::*, sample::SampleKind};
370+
///
371+
/// let session = zenoh::open(zenoh::config::peer()).await.unwrap();
372+
/// let subscriber = session.liveliness().declare_subscriber("key/**").await.unwrap();
373+
/// while let Ok(sample) = subscriber.recv_async().await {
374+
/// match sample.kind() {
375+
/// SampleKind::Put => println!("New liveliness: {}", sample.key_expr()),
376+
/// SampleKind::Delete => println!("Lost liveliness: {}", sample.key_expr()),
377+
/// }
378+
/// }
379+
/// # }
380+
/// ```
329381
#[zenoh_macros::unstable]
330382
pub mod liveliness {
331383
pub use crate::api::liveliness::{

0 commit comments

Comments
 (0)