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

View File

@ -14,8 +14,8 @@ use std::sync::atomic::AtomicUsize;
use std::sync::{Arc, Mutex};
pub struct GameDownloadAgent {
id: String,
version: String,
pub id: String,
pub version: String,
state: Mutex<GameDownloadState>,
contexts: Mutex<Vec<DropDownloadContext>>,
progress: ProgressChecker<DropDownloadContext>,
@ -126,6 +126,10 @@ impl GameDownloadAgent {
let mut lock = self.state.lock().unwrap();
*lock = state;
}
pub fn get_state(&self) -> GameDownloadState {
let lock = self.state.lock().unwrap();
lock.clone()
}
pub fn generate_job_contexts(
&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 log::info;
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;
pub struct FileWriter {
file: File,
hasher: Context,
data: Vec<u8>
}
impl FileWriter {
fn new(path: PathBuf) -> Self {
Self {
file: OpenOptions::new().write(true).open(path).unwrap(),
hasher: Context::new(),
data: Vec::new()
}
}
fn finish(mut self) -> Digest {
self.flush();
let res = self.hasher.compute();
info!("Final calculated value hash: {:?}", hex::encode(res.0));
res
fn finish(mut self) -> io::Result<Digest> {
self.flush().unwrap();
Ok(self.hasher.compute())
}
}
impl Write for FileWriter {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.hasher.write(buf);
self.data.extend_from_slice(buf);
self.hasher.write_all(buf).unwrap();
self.file.write(buf)
}
@ -82,13 +77,9 @@ pub fn download_game_chunk(ctx: DropDownloadContext) {
response.copy_to(&mut file).unwrap();
let res = hex::encode(file.finish().0);
if res == ctx.checksum {
info!("Matched Checksum {}", res);
}
else {
let res = hex::encode(file.finish().unwrap().0);
if res != ctx.checksum {
info!("Checksum failed. Original: {}, Calculated: {} for {}", ctx.checksum, res, ctx.file_name);
//info!("Other Checksum: {}", hex::encode(checksum));
}
// stream.flush().unwrap();

View File

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