style(downloads): Fixing some references to "id" vs "game_id"

Signed-off-by: quexeky <git@quexeky.dev>
This commit is contained in:
quexeky
2024-11-13 21:55:28 +11:00
parent b8cf44c0b2
commit 27e5a8e31c
2 changed files with 39 additions and 32 deletions

View File

@ -19,7 +19,7 @@ use super::{
pub struct DownloadManager { pub struct DownloadManager {
download_agent_registry: HashMap<String, Arc<GameDownloadAgent>>, download_agent_registry: HashMap<String, Arc<GameDownloadAgent>>,
download_queue: Arc<Mutex<VecDeque<String>>>, download_queue: Arc<Mutex<VecDeque<String>>>,
receiver: Receiver<DownloadManagerSignal>, command_receiver: Receiver<DownloadManagerSignal>,
sender: Sender<DownloadManagerSignal>, sender: Sender<DownloadManagerSignal>,
progress: Arc<Mutex<Option<ProgressObject>>>, progress: Arc<Mutex<Option<ProgressObject>>>,
@ -37,27 +37,27 @@ pub enum DownloadManagerSignal {
impl DownloadManager { impl DownloadManager {
pub fn generate() -> DownloadManagerInterface { pub fn generate() -> DownloadManagerInterface {
let queue = Arc::new(Mutex::new(VecDeque::new())); let queue = Arc::new(Mutex::new(VecDeque::new()));
let (sender, receiver) = channel(); let (command_sender, command_receiver) = channel();
let active_progress = Arc::new(Mutex::new(None)); let active_progress = Arc::new(Mutex::new(None));
let manager = Self { let manager = Self {
download_agent_registry: HashMap::new(), download_agent_registry: HashMap::new(),
download_queue: queue.clone(), download_queue: queue.clone(),
receiver, command_receiver,
current_game_id: None, current_game_id: None,
active_control_flag: None, active_control_flag: None,
sender: sender.clone(), sender: command_sender.clone(),
progress: active_progress.clone(), progress: active_progress.clone(),
}; };
let terminator = spawn(|| manager.manage_queue()); let terminator = spawn(|| manager.manage_queue());
DownloadManagerInterface::new(terminator, queue, active_progress, sender) DownloadManagerInterface::new(terminator, queue, active_progress, command_sender)
} }
fn manage_queue(mut self) -> Result<(), ()> { fn manage_queue(mut self) -> Result<(), ()> {
loop { loop {
let signal = match self.receiver.recv() { let signal = match self.command_receiver.recv() {
Ok(signal) => signal, Ok(signal) => signal,
Err(e) => return Err(()), Err(e) => return Err(()),
}; };
@ -109,19 +109,19 @@ impl DownloadManager {
fn manage_queue_signal( fn manage_queue_signal(
&mut self, &mut self,
game_id: String, id: String,
version: String, version: String,
target_download_dir: usize, target_download_dir: usize,
) { ) {
info!("Got signal Queue"); info!("Got signal Queue");
let download_agent = Arc::new(GameDownloadAgent::new( let download_agent = Arc::new(GameDownloadAgent::new(
game_id.clone(), id.clone(),
version, version,
target_download_dir, target_download_dir,
)); ));
self.download_agent_registry self.download_agent_registry
.insert(game_id.clone(), download_agent); .insert(id.clone(), download_agent);
self.download_queue.lock().unwrap().push_back(game_id); self.download_queue.lock().unwrap().push_back(id);
} }
fn manage_go_signal(&mut self) { fn manage_go_signal(&mut self) {

View File

@ -1,21 +1,29 @@
use std::{ use std::{
collections::VecDeque, any::Any, collections::VecDeque, sync::{
sync::{
mpsc::{SendError, Sender}, mpsc::{SendError, Sender},
Arc, Mutex, MutexGuard, Arc, Mutex, MutexGuard,
}, }, thread::JoinHandle
thread::JoinHandle,
}; };
use log::info; use log::info;
use super::{download_manager::DownloadManagerSignal, progress_object::ProgressObject}; use super::{download_manager::DownloadManagerSignal, progress_object::ProgressObject};
/// Accessible front-end for the DownloadManager
///
/// The system works entirely through signals, both internally and externally,
/// all of which are accessible through the DownloadManagerSignal type, but
/// should not be used directly. Rather, signals are abstracted through this
/// interface.
///
/// The actual download queue may be accessed through the .edit() function,
/// which provides raw access to the underlying queue.
/// THIS EDITING IS BLOCKING!!!
pub struct DownloadManagerInterface { pub struct DownloadManagerInterface {
terminator: JoinHandle<Result<(), ()>>, terminator: JoinHandle<Result<(), ()>>,
download_queue: Arc<Mutex<VecDeque<String>>>, download_queue: Arc<Mutex<VecDeque<String>>>,
progress: Arc<Mutex<Option<ProgressObject>>>, progress: Arc<Mutex<Option<ProgressObject>>>,
sender: Sender<DownloadManagerSignal>, command_sender: Sender<DownloadManagerSignal>,
} }
impl DownloadManagerInterface { impl DownloadManagerInterface {
@ -23,29 +31,29 @@ impl DownloadManagerInterface {
terminator: JoinHandle<Result<(), ()>>, terminator: JoinHandle<Result<(), ()>>,
download_queue: Arc<Mutex<VecDeque<String>>>, download_queue: Arc<Mutex<VecDeque<String>>>,
progress: Arc<Mutex<Option<ProgressObject>>>, progress: Arc<Mutex<Option<ProgressObject>>>,
sender: Sender<DownloadManagerSignal>, command_sender: Sender<DownloadManagerSignal>,
) -> Self { ) -> Self {
Self { Self {
terminator, terminator,
download_queue, download_queue,
progress, progress,
sender, command_sender,
} }
} }
pub fn queue_game( pub fn queue_game(
&self, &self,
game_id: String, id: String,
version: String, version: String,
target_download_dir: usize, target_download_dir: usize,
) -> Result<(), SendError<DownloadManagerSignal>> { ) -> Result<(), SendError<DownloadManagerSignal>> {
info!("Adding game id {}", game_id); info!("Adding game id {}", id);
self.sender.send(DownloadManagerSignal::Queue( self.command_sender.send(DownloadManagerSignal::Queue(
game_id, id,
version, version,
target_download_dir, target_download_dir,
))?; ))?;
self.sender.send(DownloadManagerSignal::Go) self.command_sender.send(DownloadManagerSignal::Go)
} }
pub fn edit(&self) -> MutexGuard<'_, VecDeque<String>> { pub fn edit(&self) -> MutexGuard<'_, VecDeque<String>> {
self.download_queue.lock().unwrap() self.download_queue.lock().unwrap()
@ -68,26 +76,25 @@ impl DownloadManagerInterface {
pub fn remove_from_queue(&self, index: usize) { pub fn remove_from_queue(&self, index: usize) {
self.edit().remove(index); self.edit().remove(index);
} }
pub fn remove_from_queue_string(&self, game_id: String) { pub fn remove_from_queue_string(&self, id: String) {
let mut queue = self.edit(); let mut queue = self.edit();
let current_index = get_index_from_id(&mut queue, game_id).unwrap(); let current_index = get_index_from_id(&mut queue, id).unwrap();
queue.remove(current_index); queue.remove(current_index);
} }
pub fn pause_downloads(&self) -> Result<(), SendError<DownloadManagerSignal>> { pub fn pause_downloads(&self) -> Result<(), SendError<DownloadManagerSignal>> {
self.sender.send(DownloadManagerSignal::Stop) self.command_sender.send(DownloadManagerSignal::Stop)
} }
pub fn resume_downloads(&self) -> Result<(), SendError<DownloadManagerSignal>> { pub fn resume_downloads(&self) -> Result<(), SendError<DownloadManagerSignal>> {
self.sender.send(DownloadManagerSignal::Go) self.command_sender.send(DownloadManagerSignal::Go)
} }
pub fn ensure_terminated(self) -> Result<(), ()> { pub fn ensure_terminated(self) -> Result<Result<(),()>, Box<dyn Any + Send>> {
self.sender.send(DownloadManagerSignal::Finish).unwrap(); self.command_sender.send(DownloadManagerSignal::Finish).unwrap();
match self.terminator.join() { self.terminator.join()
Ok(o) => o,
Err(_) => Err(()),
}
} }
} }
/// Takes in the locked value from .edit() and attempts to
/// get the index of whatever game_id is passed in
fn get_index_from_id(queue: &mut MutexGuard<'_, VecDeque<String>>, id: String) -> Option<usize> { fn get_index_from_id(queue: &mut MutexGuard<'_, VecDeque<String>>, id: String) -> Option<usize> {
queue queue
.iter() .iter()