diff --git a/Cargo.lock b/Cargo.lock index a4bfd74..3665075 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -882,6 +882,15 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "simple_producer" version = "0.0.0" @@ -966,6 +975,7 @@ dependencies = [ "libc", "mio", "pin-project-lite", + "signal-hook-registry", "socket2", "tokio-macros", "windows-sys 0.52.0", diff --git a/examples/simple_producer/Cargo.toml b/examples/simple_producer/Cargo.toml index 936d388..cd58c89 100644 --- a/examples/simple_producer/Cargo.toml +++ b/examples/simple_producer/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" [dependencies] server = { path = "../../server" } 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" tokio-util = "0.7.12" num-traits = "0.2.19" diff --git a/examples/simple_producer/src/main.rs b/examples/simple_producer/src/main.rs index d0e82c2..4eb4e3e 100644 --- a/examples/simple_producer/src/main.rs +++ b/examples/simple_producer/src/main.rs @@ -123,9 +123,18 @@ async fn main() -> Result<(), Box> { 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 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 index = 0; - for _ in 0..100 { + while !cancellation_token.is_cancelled() { next_time += Duration::from_millis(100); index += 10; tokio::time::sleep_until(next_time).await;