👕 refactor

main
_ 2023-09-09 04:24:50 -05:00
parent fdec833fd0
commit 1acd125fdd
1 changed files with 92 additions and 60 deletions

View File

@ -6,16 +6,18 @@ use std::{
}; };
use rayon::ThreadPool; use rayon::ThreadPool;
use thiserror::Error;
use crate::controller::*; use crate::controller::*;
mod controller; mod controller;
fn main() fn main() -> Result <(), TaskError>
{ {
let pool = rayon::ThreadPoolBuilder::new ().build ().unwrap (); let pool = rayon::ThreadPoolBuilder::new ().build ().unwrap ();
let mut driver = Driver::new (&pool); let mut driver = Driver::new (&pool);
driver.main (); driver.main ()?;
Ok (())
} }
struct Driver <'a> struct Driver <'a>
@ -26,9 +28,9 @@ struct Driver <'a>
ctl: Controller, ctl: Controller,
capture: Option <Capture>, capture: Task <Capture, ()>,
encoder: EncoderHandle, encoder: Task <Encoder, EncoderTaskMetadata>,
transmitter: Option <Transmitter>, transmitter: Task <Transmitter, ()>,
} }
impl <'a> Driver <'_> impl <'a> Driver <'_>
@ -45,13 +47,13 @@ impl <'a> Driver <'_>
ctl: Default::default (), ctl: Default::default (),
capture: Some (Default::default ()), capture: Capture::default ().into (),
encoder: EncoderHandle::Stopped (Default::default ()), encoder: Encoder::default ().into (),
transmitter: Some (Default::default ()), transmitter: Transmitter::default ().into (),
} }
} }
fn main (&mut self) fn main (&mut self) -> Result <(), TaskError>
{ {
for _ in 0..10 for _ in 0..10
{ {
@ -60,11 +62,11 @@ impl <'a> Driver <'_>
if let Some (event) = ev.tx if let Some (event) = ev.tx
{ {
dbg! (&event); dbg! (&event);
self.handle_tx_event (event); self.handle_tx_event (event)?;
match self.recv.try_recv () match self.recv.try_recv ()
{ {
Ok (output) => self.handle_task_output (output), Ok (output) => self.handle_task_output (output)?,
Err (_) => (), Err (_) => (),
} }
} }
@ -72,52 +74,41 @@ impl <'a> Driver <'_>
{ {
match self.recv.recv () match self.recv.recv ()
{ {
Ok (output) => self.handle_task_output (output), Ok (output) => self.handle_task_output (output)?,
Err (_) => (), Err (_) => (),
} }
} }
} }
Ok (())
} }
fn handle_task_output (&mut self, output: TaskOutput) fn handle_task_output (&mut self, output: TaskOutput) -> Result <(), TaskError>
{ {
dbg! (&output); dbg! (&output);
match output match output
{ {
TaskOutput::TxCapture ((cap, buf_raw)) => TaskOutput::TxCapture ((cap, buf_raw)) =>
{ {
let _old = match std::mem::replace (&mut self.capture, Some (cap)) self.capture.stop (cap)?;
{
None => (),
_ => panic! ("tried to finish an already finished capture"),
};
self.ctl.handle_capture_frame (buf_raw).unwrap (); self.ctl.handle_capture_frame (buf_raw).unwrap ();
}, },
TaskOutput::TxEncode (enc) => TaskOutput::TxEncode (enc) =>
{ {
let _metadata = match std::mem::replace (&mut self.encoder, EncoderHandle::Stopped (enc)) let _metadata = self.encoder.stop (enc)?;
{
EncoderHandle::Running (_) => (),
_ => panic! ("tried to finish an already finished encode"),
};
self.ctl.handle_encoder_finished (); self.ctl.handle_encoder_finished ();
}, },
TaskOutput::TxTransmit ((tx, busy)) => TaskOutput::TxTransmit ((tx, busy)) =>
{ {
let _old = match std::mem::replace (&mut self.transmitter, Some (tx)) self.transmitter.stop (tx)?;
{
None => (),
_ => panic! ("tried to finish an already finished transmit"),
};
self.ctl.handle_network_busy (busy); self.ctl.handle_network_busy (busy);
} }
} }
Ok (())
} }
fn handle_tx_event (&mut self, event: TxPipelineEvent) fn handle_tx_event (&mut self, event: TxPipelineEvent) -> Result <(), TaskError>
{ {
let dur_capture = Duration::from_millis (30); let dur_capture = Duration::from_millis (30);
let dur_encode = Duration::from_millis (20); let dur_encode = Duration::from_millis (20);
@ -128,14 +119,7 @@ impl <'a> Driver <'_>
match event match event
{ {
TxPipelineEvent::Capture => { TxPipelineEvent::Capture => {
let cap = match std::mem::replace (&mut self.capture, None) let cap = self.capture.start (())?;
{
Some (x) => x,
_ => {
dbg! ("capture is already running");
return;
},
};
self.pool.spawn (move || self.pool.spawn (move ||
{ {
@ -149,11 +133,7 @@ impl <'a> Driver <'_>
}, },
TxPipelineEvent::PollEncoder => TxPipelineEvent::PollEncoder =>
{ {
let encoder = match &mut self.encoder let encoder = self.encoder.try_inner_mut ()?;
{
EncoderHandle::Running (_) => panic! ("tried to poll encoder while it was running"),
EncoderHandle::Stopped (x) => x,
};
let mut buf = vec! []; let mut buf = vec! [];
@ -169,12 +149,7 @@ impl <'a> Driver <'_>
} }
TxPipelineEvent::Encode (buf_raw) => TxPipelineEvent::Encode (buf_raw) =>
{ {
let metadata = EncoderTaskMetadata {}; let mut encoder = self.encoder.start (EncoderTaskMetadata {})?;
let mut encoder = match std::mem::replace (&mut self.encoder, EncoderHandle::Running (metadata))
{
EncoderHandle::Stopped (x) => x,
_ => panic! ("tried to run the same encoder twice"),
};
self.pool.spawn (move || self.pool.spawn (move ||
{ {
@ -184,12 +159,7 @@ impl <'a> Driver <'_>
}); });
}, },
TxPipelineEvent::Transmit (_buf_enc) => { TxPipelineEvent::Transmit (_buf_enc) => {
let tx = match std::mem::replace (&mut self.transmitter, None) let tx = self.transmitter.start (())?;
{
Some (x) => x,
_ => panic! ("tried to run the same transmitter twice"),
};
self.pool.spawn (move || self.pool.spawn (move ||
{ {
thread::sleep (dur_transmit); thread::sleep (dur_transmit);
@ -197,13 +167,75 @@ impl <'a> Driver <'_>
}); });
}, },
} }
Ok (())
} }
} }
enum EncoderHandle /// This is probably just a fancy Cell or something.
enum Task <S, R>
{ {
Stopped (Encoder), Stopped (S),
Running (EncoderTaskMetadata), Running (R),
}
#[derive (Debug, Error)]
enum TaskError
{
#[error ("tried to start already-running task")]
AlreadyRunning,
#[error ("tried to stop already-stopped task")]
AlreadyStopped,
#[error ("cannot access a task's inner object while it's running")]
CantAccessWhileRunning,
}
impl <S, R> From <S> for Task <S, R>
{
fn from (x: S) -> Self
{
Self::Stopped (x)
}
}
impl <S, R> Task <S, R>
{
fn try_inner (&self) -> Result <&S, TaskError>
{
match self
{
Self::Running (_) => Err (TaskError::CantAccessWhileRunning),
Self::Stopped (x) => Ok (x),
}
}
fn try_inner_mut (&mut self) -> Result <&mut S, TaskError>
{
match self
{
Self::Running (_) => Err (TaskError::CantAccessWhileRunning),
Self::Stopped (x) => Ok (x),
}
}
fn start (&mut self, x: R) -> Result <S, TaskError>
{
match std::mem::replace (self, Self::Running (x))
{
Self::Running (_) => Err (TaskError::AlreadyRunning),
Self::Stopped (x) => Ok (x),
}
}
fn stop (&mut self, x: S) -> Result <R, TaskError>
{
match std::mem::replace (self, Self::Stopped (x))
{
Self::Stopped (_) => Err (TaskError::AlreadyStopped),
Self::Running (x) => Ok (x),
}
}
} }
struct EncoderTaskMetadata struct EncoderTaskMetadata