Finished getting the project to work.

Just needs clean up and commenting/documenting.
This commit is contained in:
Myrddin Dundragon 2025-05-09 12:24:48 -04:00
parent 6a9bc09be3
commit 43a093afd1
2 changed files with 36 additions and 6 deletions

Binary file not shown.

View File

@ -4,10 +4,12 @@ mod commands;
use std::io::Write; use std::io::Write;
use std::pin::Pin;
use std::future::Future;
use tokio::io::AsyncWriteExt;
use tokio::sync::{mpsc, watch}; use tokio::sync::{mpsc, watch};
use tokio::time::{Duration, Interval}; use tokio::time::{Duration, Interval};
use tokio::io::AsyncWriteExt;
use crate::commands::Command; use crate::commands::Command;
@ -79,7 +81,8 @@ fn read_user_input(command_sender: mpsc::Sender<Command>,
else else
{ {
let larger_seconds: u64 = seconds as u64; 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 }) match command_sender.try_send(Command::Propulsion { delay: delay_duration })
{ {
Ok(_) => Ok(_) =>
@ -106,10 +109,22 @@ fn read_user_input(command_sender: mpsc::Sender<Command>,
Ok(()) 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>, async fn process_commands(mut command_receiver: mpsc::Receiver<Command>,
mut term_receiver: watch::Receiver<bool>) mut term_receiver: watch::Receiver<bool>)
-> IoResult<()> -> IoResult<()>
{ {
let mut propulsion_interval: Option<Interval> = None;
let mut stdout = tokio::io::stdout(); let mut stdout = tokio::io::stdout();
let mut running: bool = true; 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.write_all(b"Received: Cancel\n").await?;
stdout.flush().await?; stdout.flush().await?;
propulsion_interval = None;
} }
Command::Propulsion { delay } => Command::Propulsion { delay } =>
@ -132,13 +148,27 @@ async fn process_commands(mut command_receiver: mpsc::Receiver<Command>,
writeln!(&mut buffer, "Received: {:?} delay", delay); writeln!(&mut buffer, "Received: {:?} delay", delay);
stdout.write_all(&buffer).await?; stdout.write_all(&buffer).await?;
stdout.flush().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.write_all(b"Communication task received shutdown message.").await?;
stdout.flush().await?; stdout.flush().await?;
running = *term_receiver.borrow_and_update(); running = *term_receiver.borrow_and_update();