I think that download queuing is working

This commit is contained in:
quexeky
2024-10-28 19:23:41 +11:00
parent c9d9d2e94f
commit 1ab61c86b1
6 changed files with 117 additions and 56 deletions

View File

@ -1,17 +1,17 @@
<template> <template>
<button <button
class="w-full rounded-md p-4 bg-blue-600 text-white" class="w-full rounded-md p-4 bg-blue-600 text-white"
@click="requestGameWrapper" @click="queueGameWrapper"
> >
Load Data Queue Game Download
</button> </button>
<input placeholder="GAME ID" v-model="gameId" /> <input placeholder="GAME ID" v-model="gameId" />
<input placeholder="VERSION NAME" v-model="versionName" /> <input placeholder="VERSION NAME" v-model="versionName" />
<button <button
class="w-full rounded-md p-4 bg-blue-600 text-white" class="w-full rounded-md p-4 bg-blue-600 text-white"
@click="requestGameWrapper" @click="startGameDownloads"
> >
Download Game Start Game Downloads
</button> </button>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
@ -20,20 +20,31 @@ import { invoke } from "@tauri-apps/api/core";
const gameId = ref(""); const gameId = ref("");
const versionName = ref(""); const versionName = ref("");
async function requestGame() { async function queueGame() {
await invoke("start_game_download", { await invoke("queue_game_download", {
gameId: gameId.value, gameId: gameId.value,
gameVersion: versionName.value, gameVersion: versionName.value,
maxThreads: 12, maxThreads: 12,
}); });
console.log("Requested game from FE"); console.log("Requested game from FE");
} }
function requestGameWrapper() { function queueGameWrapper() {
console.log("Wrapper started"); console.log("Wrapper started");
requestGame() queueGame()
.then(() => {}) .then(() => {})
.catch((e) => { .catch((e) => {
console.log(e); console.log(e);
}); });
} }
async function startGameDownloads() {
console.log("Downloading Games");
await invoke("start_game_downloads", { maxThreads: 4 })
}
function startGameDownloadsWrapper() {
startGameDownloads()
.then(() => {})
.catch((e) => {
console.log(e)
})
}
</script> </script>

View File

@ -14,8 +14,8 @@ use std::sync::atomic::AtomicUsize;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
pub struct GameDownloadAgent { pub struct GameDownloadAgent {
id: String, pub id: String,
version: String, pub version: String,
state: Mutex<GameDownloadState>, state: Mutex<GameDownloadState>,
contexts: Mutex<Vec<DropDownloadContext>>, contexts: Mutex<Vec<DropDownloadContext>>,
progress: ProgressChecker<DropDownloadContext>, progress: ProgressChecker<DropDownloadContext>,
@ -126,6 +126,10 @@ impl GameDownloadAgent {
let mut lock = self.state.lock().unwrap(); let mut lock = self.state.lock().unwrap();
*lock = state; *lock = state;
} }
pub fn get_state(&self) -> GameDownloadState {
let lock = self.state.lock().unwrap();
lock.clone()
}
pub fn generate_job_contexts( pub fn generate_job_contexts(
&self, &self,
@ -174,27 +178,3 @@ impl GameDownloadAgent {
} }
} }
#[tauri::command]
pub async fn start_game_download(
game_id: String,
game_version: String,
max_threads: usize,
state: tauri::State<'_, Mutex<AppState>>,
) -> Result<(), GameDownloadError> {
info!("Triggered Game Download");
let download_agent = GameDownloadAgent::new(game_id.clone(), game_version.clone());
download_agent.ensure_manifest_exists().await?;
let local_manifest = {
let manifest = download_agent.manifest.lock().unwrap();
(*manifest).clone().unwrap()
};
download_agent.generate_job_contexts(&local_manifest, game_version.clone(), game_id).unwrap();
download_agent.begin_download(max_threads)?;
Ok(())
}

View File

@ -0,0 +1,76 @@
use std::sync::{Arc, Mutex};
use log::info;
use crate::{downloads::download_agent::GameDownloadAgent, AppState};
use super::download_agent::{GameDownloadError, GameDownloadState};
#[tauri::command]
pub async fn queue_game_download(
game_id: String,
game_version: String,
state: tauri::State<'_, Mutex<AppState>>,
) -> Result<(), GameDownloadError> {
info!("Queuing Game Download");
let download_agent = Arc::new(GameDownloadAgent::new(game_id.clone(), game_version.clone()));
download_agent.queue().await?;
let mut queue = state.lock().unwrap();
queue.game_downloads.insert(game_id, download_agent);
Ok(())
}
#[tauri::command]
pub async fn start_game_downloads(
max_threads: usize,
state: tauri::State<'_, Mutex<AppState>>,
) -> Result<(), GameDownloadError> {
info!("Downloading Games");
loop {
let mut current_id = String::new();
let mut download_agent = None;
{
let lock = state.lock().unwrap();
for (id, agent) in &lock.game_downloads {
if agent.get_state() == GameDownloadState::Queued {
download_agent = Some(agent.clone());
current_id = id.clone();
info!("Got queued game to download");
break;
}
}
if download_agent.is_none() {
info!("No more games left to download");
return Ok(())
}
};
info!("Downloading game");
start_game_download(max_threads, download_agent.unwrap()).await?;
{
let mut lock = state.lock().unwrap();
lock.game_downloads.remove_entry(&current_id);
}
}
}
pub async fn start_game_download(
max_threads: usize,
download_agent: Arc<GameDownloadAgent>
) -> Result<(), GameDownloadError> {
info!("Triggered Game Download");
download_agent.ensure_manifest_exists().await?;
let local_manifest = {
let manifest = download_agent.manifest.lock().unwrap();
(*manifest).clone().unwrap()
};
download_agent.generate_job_contexts(&local_manifest, download_agent.version.clone(), download_agent.id.clone()).unwrap();
download_agent.begin_download(max_threads)?;
Ok(())
}

View File

@ -5,33 +5,28 @@ use crate::DB;
use gxhash::{gxhash128, GxHasher}; use gxhash::{gxhash128, GxHasher};
use log::info; use log::info;
use md5::{Context, Digest}; use md5::{Context, Digest};
use std::{fs::{File, OpenOptions}, hash::Hasher, io::{Seek, SeekFrom, Write}, path::PathBuf}; use std::{fs::{File, OpenOptions}, hash::Hasher, io::{self, Seek, SeekFrom, Write}, path::PathBuf};
use urlencoding::encode; use urlencoding::encode;
pub struct FileWriter { pub struct FileWriter {
file: File, file: File,
hasher: Context, hasher: Context,
data: Vec<u8>
} }
impl FileWriter { impl FileWriter {
fn new(path: PathBuf) -> Self { fn new(path: PathBuf) -> Self {
Self { Self {
file: OpenOptions::new().write(true).open(path).unwrap(), file: OpenOptions::new().write(true).open(path).unwrap(),
hasher: Context::new(), hasher: Context::new(),
data: Vec::new()
} }
} }
fn finish(mut self) -> Digest { fn finish(mut self) -> io::Result<Digest> {
self.flush(); self.flush().unwrap();
let res = self.hasher.compute(); Ok(self.hasher.compute())
info!("Final calculated value hash: {:?}", hex::encode(res.0));
res
} }
} }
impl Write for FileWriter { impl Write for FileWriter {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.hasher.write(buf); self.hasher.write_all(buf).unwrap();
self.data.extend_from_slice(buf);
self.file.write(buf) self.file.write(buf)
} }
@ -82,13 +77,9 @@ pub fn download_game_chunk(ctx: DropDownloadContext) {
response.copy_to(&mut file).unwrap(); response.copy_to(&mut file).unwrap();
let res = hex::encode(file.finish().0); let res = hex::encode(file.finish().unwrap().0);
if res == ctx.checksum { if res != ctx.checksum {
info!("Matched Checksum {}", res);
}
else {
info!("Checksum failed. Original: {}, Calculated: {} for {}", ctx.checksum, res, ctx.file_name); info!("Checksum failed. Original: {}, Calculated: {} for {}", ctx.checksum, res, ctx.file_name);
//info!("Other Checksum: {}", hex::encode(checksum));
} }
// stream.flush().unwrap(); // stream.flush().unwrap();

View File

@ -2,3 +2,4 @@ mod manifest;
pub mod progress; pub mod progress;
pub mod download_agent; pub mod download_agent;
mod download_logic; mod download_logic;
pub mod download_commands;

View File

@ -10,6 +10,7 @@ mod tests;
use auth::{auth_initiate, generate_authorization_header, recieve_handshake}; use auth::{auth_initiate, generate_authorization_header, recieve_handshake};
use db::{DatabaseInterface, DATA_ROOT_DIR}; use db::{DatabaseInterface, DATA_ROOT_DIR};
use downloads::download_commands::{queue_game_download, start_game_downloads};
use env_logger::Env; use env_logger::Env;
use http::{header::*, response::Builder as ResponseBuilder}; use http::{header::*, response::Builder as ResponseBuilder};
use library::{fetch_game, fetch_library, Game}; use library::{fetch_game, fetch_library, Game};
@ -22,7 +23,7 @@ use std::{
use std::sync::Arc; use std::sync::Arc;
use tauri_plugin_deep_link::DeepLinkExt; use tauri_plugin_deep_link::DeepLinkExt;
use crate::db::DatabaseImpls; use crate::db::DatabaseImpls;
use crate::downloads::download_agent::{start_game_download, GameDownloadAgent}; use crate::downloads::download_agent::{GameDownloadAgent};
#[derive(Clone, Copy, Serialize)] #[derive(Clone, Copy, Serialize)]
pub enum AppStatus { pub enum AppStatus {
@ -49,7 +50,7 @@ pub struct AppState {
games: HashMap<String, Game>, games: HashMap<String, Game>,
#[serde(skip_serializing)] #[serde(skip_serializing)]
game_downloads: Vec<Arc<GameDownloadAgent>> game_downloads: HashMap<String, Arc<GameDownloadAgent>>
} }
#[tauri::command] #[tauri::command]
@ -69,7 +70,7 @@ fn setup() -> AppState {
status: AppStatus::NotConfigured, status: AppStatus::NotConfigured,
user: None, user: None,
games: HashMap::new(), games: HashMap::new(),
game_downloads: vec![], game_downloads: HashMap::new(),
}; };
} }
@ -78,7 +79,7 @@ fn setup() -> AppState {
status: auth_result.0, status: auth_result.0,
user: auth_result.1, user: auth_result.1,
games: HashMap::new(), games: HashMap::new(),
game_downloads: vec![], game_downloads: HashMap::new(),
} }
} }
@ -113,7 +114,8 @@ pub fn run() {
fetch_library, fetch_library,
fetch_game, fetch_game,
// Downloads // Downloads
start_game_download queue_game_download,
start_game_downloads
]) ])
.plugin(tauri_plugin_shell::init()) .plugin(tauri_plugin_shell::init())
.setup(|app| { .setup(|app| {