refactor(downloads): Ran cargo clippy & moved DownloadManagerInterface

Created file "download_manager_interface.rs" to contain the DownloadManagerInterface

Signed-off-by: quexeky <git@quexeky.dev>
This commit is contained in:
quexeky
2024-11-13 21:05:25 +11:00
parent a1ada07690
commit 075d6ecf3c
7 changed files with 151 additions and 127 deletions

View File

@ -4,13 +4,11 @@ use crate::downloads::manifest::{DropDownloadContext, DropManifest};
use crate::remote::RemoteAccessError;
use crate::DB;
use log::info;
use rayon::{spawn, ThreadPool, ThreadPoolBuilder};
use rayon::ThreadPoolBuilder;
use std::fmt::{Display, Formatter};
use std::fs::{create_dir_all, File};
use std::path::Path;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::thread::Thread;
use std::sync::Mutex;
use urlencoding::encode;
#[cfg(target_os = "linux")]
@ -137,7 +135,7 @@ impl GameDownloadAgent {
.values()
.map(|chunk| chunk.lengths.len())
.sum();
self.progress.set_max(length.try_into().unwrap());
self.progress.set_max(length);
self.progress.set_size(chunk_count);
if let Ok(mut manifest) = self.manifest.lock() {

View File

@ -1,7 +1,5 @@
use std::sync::{Arc, Mutex};
use std::sync::Mutex;
use log::info;
use rayon::spawn;
use crate::{AppState};
@ -45,10 +43,7 @@ pub fn get_game_download_progress(
Ok(progress.get_progress())
*/
let progress = match state.lock().unwrap().download_manager.get_current_game_download_progress() {
Some(progress) => progress,
None => 0.0
};
let progress = state.lock().unwrap().download_manager.get_current_game_download_progress().unwrap_or(0.0);
Ok(progress)
}

View File

@ -4,7 +4,7 @@ use std::{
use log::info;
use super::{download_agent::GameDownloadAgent, download_thread_control_flag::{DownloadThreadControl, DownloadThreadControlFlag}, progress_object::ProgressObject};
use super::{download_agent::GameDownloadAgent, download_manager_interface::DownloadManagerInterface, download_thread_control_flag::{DownloadThreadControl, DownloadThreadControlFlag}, progress_object::ProgressObject};
pub struct DownloadManager {
download_agent_registry: HashMap<String, Arc<GameDownloadAgent>>,
@ -16,12 +16,6 @@ pub struct DownloadManager {
current_game_id: Option<String>, // Should be the only game download agent in the map with the "Go" flag
active_control_flag: Option<DownloadThreadControl>
}
pub struct DownloadManagerInterface {
terminator: JoinHandle<Result<(),()>>,
download_queue: Arc<Mutex<VecDeque<String>>>,
progress: Arc<Mutex<Option<ProgressObject>>>,
sender: Sender<DownloadManagerSignal>,
}
pub enum DownloadManagerSignal {
Go,
Stop,
@ -29,51 +23,6 @@ pub enum DownloadManagerSignal {
Queue(String, String, usize)
}
impl DownloadManagerInterface {
pub fn queue_game(&self, game_id: String, version: String, target_download_dir: usize) -> Result<(), SendError<DownloadManagerSignal>> {
info!("Adding game id {}", game_id);
self.sender.send(DownloadManagerSignal::Queue(game_id, version, target_download_dir))?;
self.sender.send(DownloadManagerSignal::Go)
}
pub fn edit(&self) -> MutexGuard<'_, VecDeque<String>> {
self.download_queue.lock().unwrap()
}
pub fn get_current_game_download_progress(&self) -> Option<f64> {
let progress_object = (*self.progress.lock().unwrap()).clone()?;
Some(progress_object.get_progress())
}
pub fn rearrange_string(&self, id: String, new_index: usize) {
let mut queue = self.edit();
let current_index = get_index_from_id(&mut queue, id).unwrap();
let to_move = queue.remove(current_index).unwrap();
queue.insert(new_index, to_move);
}
pub fn rearrange(&self, current_index: usize, new_index: usize) {
let mut queue = self.edit();
let to_move = queue.remove(current_index).unwrap();
queue.insert(new_index, to_move);
}
pub fn remove_from_queue(&self, index: usize) {
self.edit().remove(index);
}
pub fn remove_from_queue_string(&self, game_id: String) {
let mut queue = self.edit();
let current_index = get_index_from_id(&mut queue, game_id).unwrap();
queue.remove(current_index);
}
pub fn pause_downloads(&self) -> Result<(), SendError<DownloadManagerSignal>> {
self.sender.send(DownloadManagerSignal::Stop)
}
pub fn resume_downloads(&self) -> Result<(), SendError<DownloadManagerSignal>> {
self.sender.send(DownloadManagerSignal::Go)
}
pub fn ensure_terminated(self) -> Result<(), ()> {
match self.terminator.join() {
Ok(o) => o,
Err(_) => Err(()),
}
}
}
impl DownloadManager {
pub fn generate() -> DownloadManagerInterface {
@ -93,13 +42,12 @@ impl DownloadManager {
let terminator = spawn(|| {manager.manage_queue()});
let interface = DownloadManagerInterface {
DownloadManagerInterface::new(
terminator,
download_queue: queue,
queue,
active_progress,
sender,
progress: active_progress
};
return interface;
)
}
fn manage_queue(mut self) -> Result<(), ()> {
@ -113,70 +61,80 @@ impl DownloadManager {
match signal {
DownloadManagerSignal::Go => {
info!("Got signal 'Go'");
if self.active_control_flag.is_none() && !self.download_agent_registry.is_empty() {
info!("Starting download agent");
let download_agent = {
let lock = self.download_queue.lock().unwrap();
self.download_agent_registry.get(&lock.front().unwrap().clone()).unwrap().clone()
};
self.current_game_id = Some(download_agent.id.clone());
let progress_object = download_agent.progress.clone();
*self.progress.lock().unwrap() = Some(progress_object);
let active_control_flag = download_agent.control_flag.clone();
self.active_control_flag = Some(active_control_flag.clone());
let sender = self.sender.clone();
info!("Spawning download");
spawn(move || {
download_agent.download().unwrap();
sender.send(DownloadManagerSignal::Completed(download_agent.id.clone())).unwrap();
});
info!("Finished spawning Download");
active_control_flag.set(DownloadThreadControlFlag::Go);
}
else if let Some(active_control_flag) = self.active_control_flag.clone() {
info!("Restarting current download");
active_control_flag.set(DownloadThreadControlFlag::Go);
}
else {
info!("Nothing was set");
}
self.manage_go_signal();
},
DownloadManagerSignal::Stop => {
info!("Got signal 'Stop'");
if let Some(active_control_flag) = self.active_control_flag.clone() {
active_control_flag.set(DownloadThreadControlFlag::Stop);
}
self.manage_stop_signal();
},
DownloadManagerSignal::Completed(game_id) => {
info!("Got signal 'Completed'");
if self.current_game_id == Some(game_id.clone()) {
info!("Popping consumed data");
self.download_queue.lock().unwrap().pop_front();
self.download_agent_registry.remove(&game_id);
self.active_control_flag = None;
*self.progress.lock().unwrap() = None;
}
self.sender.send(DownloadManagerSignal::Go).unwrap();
self.manage_completed_signal(game_id);
}
DownloadManagerSignal::Queue(game_id, version, target_download_dir) => {
info!("Got signal Queue");
let download_agent = Arc::new(GameDownloadAgent::new(game_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);
self.manage_queue_signal(game_id, version, target_download_dir);
},
};
}
}
}
pub fn get_index_from_id(queue: &mut MutexGuard<'_, VecDeque<String>>, id: String) -> Option<usize> {
queue.iter().position(|download_agent| {
download_agent == &id
})
}
fn manage_stop_signal(&mut self) {
info!("Got signal 'Stop'");
if let Some(active_control_flag) = self.active_control_flag.clone() {
active_control_flag.set(DownloadThreadControlFlag::Stop);
}
}
fn manage_completed_signal(&mut self, game_id: String) {
info!("Got signal 'Completed'");
if self.current_game_id == Some(game_id.clone()) {
info!("Popping consumed data");
self.download_queue.lock().unwrap().pop_front();
self.download_agent_registry.remove(&game_id);
self.active_control_flag = None;
*self.progress.lock().unwrap() = None;
}
self.sender.send(DownloadManagerSignal::Go).unwrap();
}
fn manage_queue_signal(&mut self, game_id: String, version: String, target_download_dir: usize) {
info!("Got signal Queue");
let download_agent = Arc::new(GameDownloadAgent::new(game_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);
}
fn manage_go_signal(&mut self) {
info!("Got signal 'Go'");
if self.active_control_flag.is_none() && !self.download_agent_registry.is_empty() {
info!("Starting download agent");
let download_agent = {
let lock = self.download_queue.lock().unwrap();
self.download_agent_registry.get(&lock.front().unwrap().clone()).unwrap().clone()
};
self.current_game_id = Some(download_agent.id.clone());
let progress_object = download_agent.progress.clone();
*self.progress.lock().unwrap() = Some(progress_object);
let active_control_flag = download_agent.control_flag.clone();
self.active_control_flag = Some(active_control_flag.clone());
let sender = self.sender.clone();
info!("Spawning download");
spawn(move || {
download_agent.download().unwrap();
sender.send(DownloadManagerSignal::Completed(download_agent.id.clone())).unwrap();
});
info!("Finished spawning Download");
active_control_flag.set(DownloadThreadControlFlag::Go);
}
else if let Some(active_control_flag) = self.active_control_flag.clone() {
info!("Restarting current download");
active_control_flag.set(DownloadThreadControlFlag::Go);
}
else {
info!("Nothing was set");
}
}
}

View File

@ -0,0 +1,73 @@
use std::{collections::VecDeque, sync::{mpsc::{SendError, Sender}, Arc, Mutex, MutexGuard}, thread::JoinHandle};
use log::info;
use super::{download_manager::DownloadManagerSignal, progress_object::ProgressObject};
pub struct DownloadManagerInterface {
terminator: JoinHandle<Result<(),()>>,
download_queue: Arc<Mutex<VecDeque<String>>>,
progress: Arc<Mutex<Option<ProgressObject>>>,
sender: Sender<DownloadManagerSignal>,
}
impl DownloadManagerInterface {
pub fn new(
terminator: JoinHandle<Result<(),()>>,
download_queue: Arc<Mutex<VecDeque<String>>>,
progress: Arc<Mutex<Option<ProgressObject>>>,
sender: Sender<DownloadManagerSignal>) -> Self {
Self { terminator, download_queue, progress, sender }
}
pub fn queue_game(&self, game_id: String, version: String, target_download_dir: usize) -> Result<(), SendError<DownloadManagerSignal>> {
info!("Adding game id {}", game_id);
self.sender.send(DownloadManagerSignal::Queue(game_id, version, target_download_dir))?;
self.sender.send(DownloadManagerSignal::Go)
}
pub fn edit(&self) -> MutexGuard<'_, VecDeque<String>> {
self.download_queue.lock().unwrap()
}
pub fn get_current_game_download_progress(&self) -> Option<f64> {
let progress_object = (*self.progress.lock().unwrap()).clone()?;
Some(progress_object.get_progress())
}
pub fn rearrange_string(&self, id: String, new_index: usize) {
let mut queue = self.edit();
let current_index = get_index_from_id(&mut queue, id).unwrap();
let to_move = queue.remove(current_index).unwrap();
queue.insert(new_index, to_move);
}
pub fn rearrange(&self, current_index: usize, new_index: usize) {
let mut queue = self.edit();
let to_move = queue.remove(current_index).unwrap();
queue.insert(new_index, to_move);
}
pub fn remove_from_queue(&self, index: usize) {
self.edit().remove(index);
}
pub fn remove_from_queue_string(&self, game_id: String) {
let mut queue = self.edit();
let current_index = get_index_from_id(&mut queue, game_id).unwrap();
queue.remove(current_index);
}
pub fn pause_downloads(&self) -> Result<(), SendError<DownloadManagerSignal>> {
self.sender.send(DownloadManagerSignal::Stop)
}
pub fn resume_downloads(&self) -> Result<(), SendError<DownloadManagerSignal>> {
self.sender.send(DownloadManagerSignal::Go)
}
pub fn ensure_terminated(self) -> Result<(), ()> {
match self.terminator.join() {
Ok(o) => o,
Err(_) => Err(()),
}
}
}
pub fn get_index_from_id(queue: &mut MutexGuard<'_, VecDeque<String>>, id: String) -> Option<usize> {
queue.iter().position(|download_agent| {
download_agent == &id
})
}

View File

@ -1,6 +1,7 @@
pub mod download_agent;
pub mod download_commands;
pub mod download_manager;
mod download_manager_interface;
mod download_logic;
mod download_thread_control_flag;
mod manifest;

View File

@ -26,7 +26,7 @@ impl ProgressObject {
.sum()
}
pub fn get_max(&self) -> usize {
self.max.lock().unwrap().clone()
*self.max.lock().unwrap()
}
pub fn set_max(&self, new_max: usize) {
*self.max.lock().unwrap() = new_max

View File

@ -9,7 +9,6 @@ mod settings;
mod tests;
use crate::db::DatabaseImpls;
use crate::downloads::download_agent::GameDownloadAgent;
use auth::{auth_initiate, generate_authorization_header, recieve_handshake};
use db::{add_new_download_dir, DatabaseInterface, DATA_ROOT_DIR};
use downloads::download_commands::*;