refactor(download manager): Added Downloadable trait and replaced references to GameDownloadAgent

Signed-off-by: quexeky <git@quexeky.dev>
This commit is contained in:
quexeky
2024-12-30 20:30:38 +11:00
parent b6c64e56e5
commit b4d70a35b3
5 changed files with 65 additions and 17 deletions

View File

@ -18,7 +18,7 @@ use crate::{
}, state::GameStatusManager, DB
};
use super::{download_manager::{DownloadManager, DownloadManagerSignal, DownloadManagerStatus, GameDownloadAgentQueueStandin}, download_thread_control_flag::{DownloadThreadControl, DownloadThreadControlFlag}, progress_object::ProgressObject, queue::Queue};
use super::{download_manager::{DownloadManager, DownloadManagerSignal, DownloadManagerStatus, GameDownloadAgentQueueStandin}, download_thread_control_flag::{DownloadThreadControl, DownloadThreadControlFlag}, downloadable::Downloadable, progress_object::ProgressObject, queue::Queue};
/*
@ -59,9 +59,10 @@ Behold, my madness - quexeky
// Refactored to consolidate this type. It's a monster.
pub type CurrentProgressObject = Arc<Mutex<Option<Arc<ProgressObject>>>>;
pub type DownloadAgent = Arc<Mutex<dyn Downloadable + Send + Sync>>;
pub struct DownloadManagerBuilder {
download_agent_registry: HashMap<String, Arc<Mutex<GameDownloadAgent>>>,
download_agent_registry: HashMap<String, DownloadAgent>,
download_queue: Queue,
command_receiver: Receiver<DownloadManagerSignal>,
sender: Sender<DownloadManagerSignal>,
@ -156,7 +157,7 @@ impl DownloadManagerBuilder {
drop(download_thread_lock);
}
fn remove_and_cleanup_front_game(&mut self, game_id: &String) -> Arc<Mutex<GameDownloadAgent>> {
fn remove_and_cleanup_front_game(&mut self, game_id: &String) -> DownloadAgent {
self.download_queue.pop_front();
let download_agent = self.download_agent_registry.remove(game_id).unwrap();
self.cleanup_current_download();
@ -331,13 +332,8 @@ impl DownloadManagerBuilder {
let download_agent = self.remove_and_cleanup_front_game(&game_id);
let download_agent_lock = download_agent.lock().unwrap();
let version = download_agent_lock.version.clone();
let install_dir = download_agent_lock
.stored_manifest
.base_path
.clone()
.to_string_lossy()
.to_string();
let version = download_agent_lock.version();
let install_dir = download_agent_lock.install_dir();
drop(download_agent_lock);
@ -362,7 +358,7 @@ impl DownloadManagerBuilder {
// Should always give us a value
if let Some(download_agent) = self.download_agent_registry.get(&id) {
let download_agent_handle = download_agent.lock().unwrap();
if download_agent_handle.version == version {
if download_agent_handle.version() == version {
info!("game with same version already queued, skipping");
return;
}
@ -429,12 +425,12 @@ impl DownloadManagerBuilder {
// Cloning option should be okay because it only clones the Arc inside, not the AgentInterfaceData
let agent_data = self.current_download_agent.clone().unwrap();
let version_name = download_agent_lock.version.clone();
let version_name = download_agent_lock.version().clone();
let progress_object = download_agent_lock.progress.clone();
let progress_object = download_agent_lock.progress();
*self.progress.lock().unwrap() = Some(progress_object);
let active_control_flag = download_agent_lock.control_flag.clone();
let active_control_flag = download_agent_lock.control_flag();
self.active_control_flag = Some(active_control_flag.clone());
let sender = self.sender.clone();

View File

@ -0,0 +1,18 @@
use std::sync::Arc;
use crate::downloads::download_agent::GameDownloadError;
use super::{
download_thread_control_flag::DownloadThreadControl,
progress_object::ProgressObject,
};
pub trait Downloadable: Sync {
fn get_progress_object(&self) -> Arc<ProgressObject>;
fn version(&self) -> String;
fn id(&self) -> String;
fn download(&mut self) -> Result<(), GameDownloadError>;
fn progress(&self) -> Arc<ProgressObject>;
fn control_flag(&self) -> DownloadThreadControl;
fn install_dir(&self) -> String;
}

View File

@ -3,3 +3,4 @@ pub mod download_manager_builder;
pub mod progress_object;
pub mod queue;
pub mod download_thread_control_flag;
pub mod downloadable;

View File

@ -4,7 +4,7 @@ use std::{
mpsc::Sender,
Arc, Mutex, RwLock,
},
time::{Duration, Instant},
time::Instant,
};
use log::info;

View File

@ -1,7 +1,8 @@
use crate::auth::generate_authorization_header;
use crate::db::DatabaseImpls;
use crate::download_manager::download_manager::DownloadManagerSignal;
use crate::download_manager::download_manager::{DownloadManagerSignal, GameDownloadStatus};
use crate::download_manager::download_thread_control_flag::{DownloadThreadControl, DownloadThreadControlFlag};
use crate::download_manager::downloadable::Downloadable;
use crate::download_manager::progress_object::{ProgressHandle, ProgressObject};
use crate::downloads::manifest::{DropDownloadContext, DropManifest};
use crate::remote::RemoteAccessError;
@ -34,6 +35,7 @@ pub struct GameDownloadAgent {
pub progress: Arc<ProgressObject>,
sender: Sender<DownloadManagerSignal>,
pub stored_manifest: StoredManifest,
status: Mutex<GameDownloadStatus>
}
#[derive(Debug, Clone)]
@ -102,6 +104,7 @@ impl GameDownloadAgent {
progress: Arc::new(ProgressObject::new(0, 0, sender.clone())),
sender,
stored_manifest,
status: Mutex::new(GameDownloadStatus::Queued),
}
}
@ -328,3 +331,33 @@ impl GameDownloadAgent {
Ok(())
}
}
impl Downloadable for GameDownloadAgent {
fn get_progress_object(&self) -> Arc<ProgressObject> {
self.progress.clone()
}
fn id(&self) -> String {
self.id.clone()
}
fn download(&mut self) -> Result<(), GameDownloadError> {
self.download()
}
fn version(&self) -> String {
self.version.clone()
}
fn progress(&self) -> Arc<ProgressObject> {
self.progress.clone()
}
fn control_flag(&self) -> DownloadThreadControl {
self.control_flag.clone()
}
fn install_dir(&self) -> String {
self.stored_manifest.base_path.to_str().unwrap().to_owned()
}
}