ctrl c listener for producer

This commit is contained in:
2024-10-19 16:01:27 -07:00
parent f47512010f
commit c9bf33ab6c
3 changed files with 21 additions and 2 deletions

10
Cargo.lock generated
View File

@@ -882,6 +882,15 @@ version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "signal-hook-registry"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "simple_producer" name = "simple_producer"
version = "0.0.0" version = "0.0.0"
@@ -966,6 +975,7 @@ dependencies = [
"libc", "libc",
"mio", "mio",
"pin-project-lite", "pin-project-lite",
"signal-hook-registry",
"socket2", "socket2",
"tokio-macros", "tokio-macros",
"windows-sys 0.52.0", "windows-sys 0.52.0",

View File

@@ -6,7 +6,7 @@ edition = "2021"
[dependencies] [dependencies]
server = { path = "../../server" } server = { path = "../../server" }
tonic = "0.12.3" tonic = "0.12.3"
tokio = { version = "1.40.0", features = ["rt-multi-thread"] } tokio = { version = "1.40.0", features = ["rt-multi-thread", "signal"] }
chrono = "0.4.38" chrono = "0.4.38"
tokio-util = "0.7.12" tokio-util = "0.7.12"
num-traits = "0.2.19" num-traits = "0.2.19"

View File

@@ -123,9 +123,18 @@ async fn main() -> Result<(), Box<dyn Error>> {
let sin_tlm_handle = tlm.register("simple_producer/sin".into(), TelemetryDataType::Float32).await?; let sin_tlm_handle = tlm.register("simple_producer/sin".into(), TelemetryDataType::Float32).await?;
let cos_tlm_handle = tlm.register("simple_producer/cos".into(), TelemetryDataType::Float64).await?; let cos_tlm_handle = tlm.register("simple_producer/cos".into(), TelemetryDataType::Float64).await?;
let cancellation_token = CancellationToken::new();
{
let cancellation_token = cancellation_token.clone();
tokio::spawn(async move {
let _ = tokio::signal::ctrl_c().await;
cancellation_token.cancel();
});
}
let mut next_time = Instant::now(); let mut next_time = Instant::now();
let mut index = 0; let mut index = 0;
for _ in 0..100 { while !cancellation_token.is_cancelled() {
next_time += Duration::from_millis(100); next_time += Duration::from_millis(100);
index += 10; index += 10;
tokio::time::sleep_until(next_time).await; tokio::time::sleep_until(next_time).await;