Finished getting the project to work.
Just needs clean up and commenting/documenting.
This commit is contained in:
parent
6a9bc09be3
commit
43a093afd1
BIN
src/.main.rs.swp
BIN
src/.main.rs.swp
Binary file not shown.
38
src/main.rs
38
src/main.rs
@ -4,10 +4,12 @@ mod commands;
|
||||
|
||||
|
||||
use std::io::Write;
|
||||
use std::pin::Pin;
|
||||
use std::future::Future;
|
||||
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tokio::sync::{mpsc, watch};
|
||||
use tokio::time::{Duration, Interval};
|
||||
use tokio::io::AsyncWriteExt;
|
||||
|
||||
use crate::commands::Command;
|
||||
|
||||
@ -79,7 +81,8 @@ fn read_user_input(command_sender: mpsc::Sender<Command>,
|
||||
else
|
||||
{
|
||||
let larger_seconds: u64 = seconds as u64;
|
||||
let delay_duration: Duration = Duration::from_secs(larger_seconds);
|
||||
let delay_duration: Duration =
|
||||
Duration::from_secs(larger_seconds);
|
||||
match command_sender.try_send(Command::Propulsion { delay: delay_duration })
|
||||
{
|
||||
Ok(_) =>
|
||||
@ -106,10 +109,22 @@ fn read_user_input(command_sender: mpsc::Sender<Command>,
|
||||
Ok(())
|
||||
}
|
||||
|
||||
///
|
||||
fn maybe_tick<'a>(interval: Option<&'a mut Interval>)
|
||||
-> Pin<Box<dyn Future<Output = ()> + Send + 'a>>
|
||||
{
|
||||
match interval
|
||||
{
|
||||
Some(interval) => Box::pin(async move { interval.tick().await; () }),
|
||||
None => Box::pin(std::future::pending())
|
||||
}
|
||||
}
|
||||
|
||||
async fn process_commands(mut command_receiver: mpsc::Receiver<Command>,
|
||||
mut term_receiver: watch::Receiver<bool>)
|
||||
-> IoResult<()>
|
||||
{
|
||||
let mut propulsion_interval: Option<Interval> = None;
|
||||
let mut stdout = tokio::io::stdout();
|
||||
|
||||
let mut running: bool = true;
|
||||
@ -124,6 +139,7 @@ async fn process_commands(mut command_receiver: mpsc::Receiver<Command>,
|
||||
{
|
||||
stdout.write_all(b"Received: Cancel\n").await?;
|
||||
stdout.flush().await?;
|
||||
propulsion_interval = None;
|
||||
}
|
||||
|
||||
Command::Propulsion { delay } =>
|
||||
@ -132,13 +148,27 @@ async fn process_commands(mut command_receiver: mpsc::Receiver<Command>,
|
||||
writeln!(&mut buffer, "Received: {:?} delay", delay);
|
||||
stdout.write_all(&buffer).await?;
|
||||
stdout.flush().await?;
|
||||
|
||||
propulsion_interval = Some(tokio::time::interval(delay));
|
||||
|
||||
// Skip the first immediate tick
|
||||
if let Some(interval) = propulsion_interval.as_mut()
|
||||
{
|
||||
interval.tick().await; // skip first tick
|
||||
}
|
||||
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_ = term_receiver.changed() => {
|
||||
_ = maybe_tick(propulsion_interval.as_mut()) =>
|
||||
{
|
||||
stdout.write_all(b"firing now!\n").await?;
|
||||
stdout.flush().await?;
|
||||
}
|
||||
|
||||
_ = term_receiver.changed() =>
|
||||
{
|
||||
stdout.write_all(b"Communication task received shutdown message.").await?;
|
||||
stdout.flush().await?;
|
||||
running = *term_receiver.borrow_and_update();
|
||||
|
Loading…
x
Reference in New Issue
Block a user