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

View File

@ -1,21 +1,29 @@
use std::{
collections::VecDeque,
sync::{
any::Any, collections::VecDeque, sync::{
mpsc::{SendError, Sender},
Arc, Mutex, MutexGuard,
},
thread::JoinHandle,
}, thread::JoinHandle
};
use log::info;
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 {
terminator: JoinHandle<Result<(), ()>>,
download_queue: Arc<Mutex<VecDeque<String>>>,
progress: Arc<Mutex<Option<ProgressObject>>>,
sender: Sender<DownloadManagerSignal>,
command_sender: Sender<DownloadManagerSignal>,
}
impl DownloadManagerInterface {
@ -23,29 +31,29 @@ impl DownloadManagerInterface {
terminator: JoinHandle<Result<(), ()>>,
download_queue: Arc<Mutex<VecDeque<String>>>,
progress: Arc<Mutex<Option<ProgressObject>>>,
sender: Sender<DownloadManagerSignal>,
command_sender: Sender<DownloadManagerSignal>,
) -> Self {
Self {
terminator,
download_queue,
progress,
sender,
command_sender,
}
}
pub fn queue_game(
&self,
game_id: String,
id: String,
version: String,
target_download_dir: usize,
) -> Result<(), SendError<DownloadManagerSignal>> {
info!("Adding game id {}", game_id);
self.sender.send(DownloadManagerSignal::Queue(
game_id,
info!("Adding game id {}", id);
self.command_sender.send(DownloadManagerSignal::Queue(
id,
version,
target_download_dir,
))?;
self.sender.send(DownloadManagerSignal::Go)
self.command_sender.send(DownloadManagerSignal::Go)
}
pub fn edit(&self) -> MutexGuard<'_, VecDeque<String>> {
self.download_queue.lock().unwrap()
@ -68,26 +76,25 @@ impl DownloadManagerInterface {
pub fn remove_from_queue(&self, index: usize) {
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 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);
}
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>> {
self.sender.send(DownloadManagerSignal::Go)
}
pub fn ensure_terminated(self) -> Result<(), ()> {
self.sender.send(DownloadManagerSignal::Finish).unwrap();
match self.terminator.join() {
Ok(o) => o,
Err(_) => Err(()),
self.command_sender.send(DownloadManagerSignal::Go)
}
pub fn ensure_terminated(self) -> Result<Result<(),()>, Box<dyn Any + Send>> {
self.command_sender.send(DownloadManagerSignal::Finish).unwrap();
self.terminator.join()
}
}
/// 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> {
queue
.iter()