-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove reading from stdin, align example implementations
- Loading branch information
Showing
13 changed files
with
43 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,10 +11,8 @@ | |
// Contributors: | ||
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
use async_std::prelude::FutureExt; | ||
use async_std::task::sleep; | ||
use clap::Parser; | ||
use futures::prelude::*; | ||
use std::time::Duration; | ||
use zenoh::config::Config; | ||
use zenoh::prelude::r#async::*; | ||
|
@@ -35,40 +33,24 @@ async fn main() { | |
let subscriber = session | ||
.declare_subscriber(&key_expr) | ||
.pull_mode() | ||
.res() | ||
.await | ||
.unwrap(); | ||
|
||
println!("Press <enter> to pull data..."); | ||
|
||
// Define the future to handle incoming samples of the subscription. | ||
let subs = async { | ||
while let Ok(sample) = subscriber.recv_async().await { | ||
.callback(|sample| { | ||
println!( | ||
">> [Subscriber] Received {} ('{}': '{}')", | ||
sample.kind, | ||
sample.key_expr.as_str(), | ||
sample.value, | ||
); | ||
} | ||
}; | ||
|
||
// Define the future to handle keyboard's input. | ||
let keyb = async { | ||
let mut stdin = async_std::io::stdin(); | ||
let mut input = [0_u8]; | ||
loop { | ||
stdin.read_exact(&mut input).await.unwrap(); | ||
match input[0] { | ||
b'q' => break, | ||
0 => sleep(Duration::from_secs(1)).await, | ||
_ => subscriber.pull().res().await.unwrap(), | ||
} | ||
} | ||
}; | ||
}) | ||
.res() | ||
.await | ||
.unwrap(); | ||
|
||
// Execute both futures concurrently until one of them returns. | ||
subs.race(keyb).await; | ||
println!("Press CTRL-C to quit..."); | ||
for idx in 0..u32::MAX { | ||
sleep(Duration::from_secs(1)).await; | ||
println!("[{idx:4}] Pulling..."); | ||
subscriber.pull().res().await.unwrap(); | ||
} | ||
} | ||
|
||
#[derive(clap::Parser, Clone, PartialEq, Eq, Hash, Debug)] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,12 +11,8 @@ | |
// Contributors: | ||
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
use async_std::task::sleep; | ||
use clap::Parser; | ||
use futures::prelude::*; | ||
use futures::select; | ||
use std::sync::atomic::Ordering::Relaxed; | ||
use std::time::Duration; | ||
use zenoh::config::Config; | ||
use zenoh::prelude::r#async::*; | ||
use zenoh_examples::CommonArgs; | ||
|
@@ -27,7 +23,6 @@ async fn main() { | |
env_logger::init(); | ||
|
||
let (config, key_expr, value, complete) = parse_args(); | ||
let send_errors = std::sync::atomic::AtomicBool::new(false); | ||
|
||
println!("Opening session..."); | ||
let session = zenoh::open(config).res().await.unwrap(); | ||
|
@@ -40,9 +35,7 @@ async fn main() { | |
.await | ||
.unwrap(); | ||
|
||
println!("Enter 'q' to quit, 'e' to reply an error to next query..."); | ||
let mut stdin = async_std::io::stdin(); | ||
let mut input = [0_u8]; | ||
println!("Press CTRL-C to quit..."); | ||
loop { | ||
select!( | ||
query = queryable.recv_async() => { | ||
|
@@ -51,34 +44,17 @@ async fn main() { | |
None => println!(">> [Queryable ] Received Query '{}'", query.selector()), | ||
Some(value) => println!(">> [Queryable ] Received Query '{}' with value '{}'", query.selector(), value), | ||
} | ||
let reply = if send_errors.swap(false, Relaxed) { | ||
println!( | ||
">> [Queryable ] Replying (ERROR: '{}')", | ||
value, | ||
); | ||
Err(value.clone().into()) | ||
} else { | ||
println!( | ||
">> [Queryable ] Responding ('{}': '{}')", | ||
key_expr.as_str(), | ||
value, | ||
); | ||
Ok(Sample::new(key_expr.clone(), value.clone())) | ||
}; | ||
println!( | ||
">> [Queryable ] Responding ('{}': '{}')", | ||
key_expr.as_str(), | ||
value, | ||
); | ||
let reply = Ok(Sample::new(key_expr.clone(), value.clone())); | ||
query | ||
.reply(reply) | ||
.res() | ||
.await | ||
.unwrap_or_else(|e| println!(">> [Queryable ] Error sending reply: {e}")); | ||
}, | ||
|
||
_ = stdin.read_exact(&mut input).fuse() => { | ||
match input[0] { | ||
b'q' => break, | ||
0 => sleep(Duration::from_secs(1)).await, | ||
b'e' => send_errors.store(true, Relaxed), | ||
_ => (), | ||
} | ||
} | ||
); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,11 +11,8 @@ | |
// Contributors: | ||
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
use async_std::task::sleep; | ||
use clap::Parser; | ||
use futures::prelude::*; | ||
use futures::select; | ||
use std::time::Duration; | ||
use zenoh::config::Config; | ||
use zenoh::prelude::r#async::*; | ||
use zenoh_examples::CommonArgs; | ||
|
@@ -39,23 +36,13 @@ async fn main() { | |
|
||
let subscriber = session.declare_subscriber(&key_expr).res().await.unwrap(); | ||
|
||
println!("Enter 'q' to quit..."); | ||
let mut stdin = async_std::io::stdin(); | ||
let mut input = [0_u8]; | ||
println!("Press CTRL-C to quit..."); | ||
loop { | ||
select!( | ||
sample = subscriber.recv_async() => { | ||
let sample = sample.unwrap(); | ||
println!(">> [Subscriber] Received {} ('{}': '{}')", | ||
sample.kind, sample.key_expr.as_str(), sample.value); | ||
}, | ||
|
||
_ = stdin.read_exact(&mut input).fuse() => { | ||
match input[0] { | ||
b'q' => break, | ||
0 => sleep(Duration::from_secs(1)).await, | ||
_ => (), | ||
} | ||
} | ||
); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,11 +11,8 @@ | |
// Contributors: | ||
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
use async_std::task::sleep; | ||
use clap::Parser; | ||
use futures::prelude::*; | ||
use futures::select; | ||
use std::time::Duration; | ||
use zenoh::config::Config; | ||
use zenoh::prelude::r#async::*; | ||
use zenoh_examples::CommonArgs; | ||
|
@@ -39,9 +36,7 @@ async fn main() { | |
.await | ||
.unwrap(); | ||
|
||
println!("Enter 'q' to quit..."); | ||
let mut stdin = async_std::io::stdin(); | ||
let mut input = [0_u8]; | ||
println!("Press CTRL-C to quit..."); | ||
loop { | ||
select!( | ||
sample = subscriber.recv_async() => { | ||
|
@@ -54,14 +49,6 @@ async fn main() { | |
">> [LivelinessSubscriber] Dropped token ('{}')", | ||
sample.key_expr.as_str()), | ||
} | ||
}, | ||
|
||
_ = stdin.read_exact(&mut input).fuse() => { | ||
match input[0] { | ||
b'q' => break, | ||
0 => sleep(Duration::from_secs(1)).await, | ||
_ => (), | ||
} | ||
} | ||
); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,8 +12,8 @@ | |
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
use clap::Parser; | ||
use std::io::{stdin, Read}; | ||
use std::time::Instant; | ||
use std::thread::sleep; | ||
use std::time::{Duration, Instant}; | ||
use zenoh::config::Config; | ||
use zenoh::prelude::sync::*; | ||
use zenoh_examples::CommonArgs; | ||
|
@@ -95,11 +95,9 @@ fn main() { | |
.res() | ||
.unwrap(); | ||
|
||
for byte in stdin().bytes() { | ||
match byte { | ||
Ok(b'q') => break, | ||
_ => std::thread::yield_now(), | ||
} | ||
println!("Press CTRL-C to quit..."); | ||
loop { | ||
sleep(Duration::from_secs(1)); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.