Skip to content

Commit e6994ff

Browse files
Add LivlinessSubscriber history option (#1355)
1 parent 7387e28 commit e6994ff

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

examples/examples/z_sub_liveliness.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async fn main() {
2020
// Initiate logging
2121
zenoh::try_init_log_from_env();
2222

23-
let (config, key_expr) = parse_args();
23+
let (config, key_expr, history) = parse_args();
2424

2525
println!("Opening session...");
2626
let session = zenoh::open(config).await.unwrap();
@@ -30,6 +30,7 @@ async fn main() {
3030
let subscriber = session
3131
.liveliness()
3232
.declare_subscriber(&key_expr)
33+
.history(history)
3334
.await
3435
.unwrap();
3536

@@ -51,13 +52,16 @@ async fn main() {
5152
#[derive(clap::Parser, Clone, PartialEq, Eq, Hash, Debug)]
5253
struct Args {
5354
#[arg(short, long, default_value = "group1/**")]
54-
/// The key expression to write to.
55+
/// The key expression to subscribe to.
5556
key: KeyExpr<'static>,
57+
#[arg(long)]
58+
/// Get historical liveliness tokens.
59+
history: bool,
5660
#[command(flatten)]
5761
common: CommonArgs,
5862
}
5963

60-
fn parse_args() -> (Config, KeyExpr<'static>) {
64+
fn parse_args() -> (Config, KeyExpr<'static>, bool) {
6165
let args = Args::parse();
62-
(args.common.into(), args.key)
66+
(args.common.into(), args.key, args.history)
6367
}

zenoh/src/api/liveliness.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ impl<'a> Liveliness<'a> {
169169
session: self.session.clone(),
170170
key_expr: TryIntoKeyExpr::try_into(key_expr).map_err(Into::into),
171171
handler: DefaultHandler::default(),
172+
history: false,
172173
}
173174
}
174175

@@ -439,6 +440,7 @@ pub struct LivelinessSubscriberBuilder<'a, 'b, Handler> {
439440
pub session: SessionRef<'a>,
440441
pub key_expr: ZResult<KeyExpr<'b>>,
441442
pub handler: Handler,
443+
pub history: bool,
442444
}
443445

444446
#[zenoh_macros::unstable]
@@ -473,11 +475,13 @@ impl<'a, 'b> LivelinessSubscriberBuilder<'a, 'b, DefaultHandler> {
473475
session,
474476
key_expr,
475477
handler: _,
478+
history,
476479
} = self;
477480
LivelinessSubscriberBuilder {
478481
session,
479482
key_expr,
480483
handler: callback,
484+
history,
481485
}
482486
}
483487

@@ -544,11 +548,33 @@ impl<'a, 'b> LivelinessSubscriberBuilder<'a, 'b, DefaultHandler> {
544548
session,
545549
key_expr,
546550
handler: _,
551+
history,
547552
} = self;
548553
LivelinessSubscriberBuilder {
549554
session,
550555
key_expr,
551556
handler,
557+
history,
558+
}
559+
}
560+
}
561+
562+
#[zenoh_macros::unstable]
563+
impl<Handler> LivelinessSubscriberBuilder<'_, '_, Handler> {
564+
#[inline]
565+
#[zenoh_macros::unstable]
566+
pub fn history(self, history: bool) -> Self {
567+
let LivelinessSubscriberBuilder {
568+
session,
569+
key_expr,
570+
handler,
571+
history: _,
572+
} = self;
573+
LivelinessSubscriberBuilder {
574+
session,
575+
key_expr,
576+
handler,
577+
history,
552578
}
553579
}
554580
}
@@ -576,7 +602,12 @@ where
576602
let session = self.session;
577603
let (callback, handler) = self.handler.into_handler();
578604
session
579-
.declare_liveliness_subscriber_inner(&key_expr, Locality::default(), callback)
605+
.declare_liveliness_subscriber_inner(
606+
&key_expr,
607+
Locality::default(),
608+
self.history,
609+
callback,
610+
)
580611
.map(|sub_state| Subscriber {
581612
subscriber: SubscriberInner {
582613
session,

zenoh/src/api/session.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1428,6 +1428,7 @@ impl Session {
14281428
&self,
14291429
key_expr: &KeyExpr,
14301430
origin: Locality,
1431+
history: bool,
14311432
callback: Callback<'static, Sample>,
14321433
) -> ZResult<Arc<SubscriberState>> {
14331434
let mut state = zwrite!(self.state);
@@ -1475,7 +1476,11 @@ impl Session {
14751476

14761477
primitives.send_interest(Interest {
14771478
id,
1478-
mode: InterestMode::Future,
1479+
mode: if history {
1480+
InterestMode::CurrentFuture
1481+
} else {
1482+
InterestMode::Future
1483+
},
14791484
options: InterestOptions::KEYEXPRS + InterestOptions::TOKENS,
14801485
wire_expr: Some(key_expr.to_wire(self).to_owned()),
14811486
ext_qos: declare::ext::QoSType::DECLARE,

0 commit comments

Comments
 (0)