From 416bdaeceef41792ec6f07000a4ea1b8a6da2f70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20L=C3=B8vbr=C3=B8tte=20Olsen?= Date: Thu, 26 Mar 2020 01:39:22 +0100 Subject: [PATCH] 3 --- src/main.rs | 45 +++++++++++++++++-------------- src/workunit.rs | 71 ++++++++++++++++++++++++++++++------------------- 2 files changed, 68 insertions(+), 48 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6444966..f60a819 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ use std::sync::Mutex; mod workunit; use workunit::WUnit; +use workunit::EStatus; const VERSION: &str = "0.1.0"; @@ -31,40 +32,44 @@ fn version() -> &'static str { } #[get("/get_jobs")] -fn getJobs(shared: State) -> Json { -// let shared_data: &SharedState = shared.inner(); +fn get_jobs(shared: State) -> Json { let list = shared.list.lock().unwrap(); - - println!("get jobs blah"); - // println!("{:#?}", Json(list)); - Json(serde_json::to_value(&list[..]).unwrap()) } -#[get("/get_job/")] -fn getJob(id: usize, shared: State) -> Result> { - let shared_data: &SharedState = shared.inner(); - let list = shared_data.list.lock().unwrap(); +#[get("/request_job")] +fn request_job(shared: State) -> Result, NotFound> { + let mut list = shared.list.lock().unwrap().clone(); - let job = list.get(id).ok_or(NotFound(format!("Job not Found: {id}", id = id))); + list.retain(|x| x.status == EStatus::Queued); + list.sort_by(|a, b| b.length.cmp(&a.length)); + + let job = list.get(0); + + Ok(Json(serde_json::to_value(&job).unwrap())) +} + +#[get("/get_job/")] +fn get_job(id: u32, shared: State) -> Result, NotFound> { + let list = shared.list.lock().unwrap().clone(); + + let job = list.into_iter().find(|x| x.id == id).ok_or(NotFound(format!("Job not Found: {id}", id = id))); match job { - Ok(j) => Ok(format!("{:#?}", j)), + Ok(j) => Ok(Json(serde_json::to_value(&j).unwrap())), Err(e) => Err(e) } } -#[get("/add_job")] -fn addJob(shared: State) -> Result { - let shared_data: &SharedState = shared.inner(); - - shared_data.list.lock().unwrap().push(WUnit::default()); - Ok(format!("{:#?}", shared_data)) +#[post("/add_job")] +fn add_job(shared: State) -> Result { + shared.list.lock().unwrap().push(WUnit::new(2, "iduno", None, 10, workunit::EOptions::default())); + Ok(format!("{:#?}", shared)) } fn main() { - rocket::ignite() + rocket::ignite() .manage(SharedState::default()) - .mount("/", routes![index, version, getJobs, getJob, addJob]) + .mount("/", routes![index, version, get_jobs, get_job, request_job, add_job]) .launch(); } \ No newline at end of file diff --git a/src/workunit.rs b/src/workunit.rs index b531ce9..139768f 100644 --- a/src/workunit.rs +++ b/src/workunit.rs @@ -1,26 +1,39 @@ use std::path::PathBuf; use serde::{Serialize, Deserialize}; -#[derive(Default, Debug, Serialize, Deserialize)] +#[derive(Default, Debug, Serialize, Deserialize, Clone)] pub struct WUnit { - file_name: PathBuf, - priority: u16, - length: u32, - options: EOptions, - status: EStatus + pub id: u32, + pub file_url: String, + pub priority: u16, + pub length: u32, + pub options: EOptions, + pub status: EStatus +} +impl WUnit { + pub fn new(id: u32, file_url: &str, priority: Option, length: u32, options: EOptions) -> Self { + WUnit { + id: id, + file_url: file_url.to_string(), + priority: priority.unwrap_or(0), + length: length, + options: options, + status: EStatus::default() + } + } } -#[derive(Debug, Serialize, Deserialize)] -struct EOptions { - mode: EMode, - resolution: Resolution, - color_depth: EColorDepth, - enable_fwd_keyframe: bool, - kf_min_dist: Option, - kf_max_dist: Option, - two_pass: bool, - speed: u8 +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct EOptions { + pub mode: EMode, + pub resolution: Resolution, + pub color_depth: EColorDepth, + pub enable_fwd_keyframe: bool, + pub kf_min_dist: Option, + pub kf_max_dist: Option, + pub two_pass: bool, + pub speed: u8 } impl Default for EOptions { fn default() -> Self { @@ -37,8 +50,8 @@ impl Default for EOptions { } } -#[derive(Debug, Serialize, Deserialize)] -enum EMode { +#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)] +pub enum EMode { // Quality (CRF), Constrained Quality, Variable Bitrate, Constant Bitrate Q(u8), CQ(u8), @@ -51,14 +64,14 @@ impl Default for EMode { } } -#[derive(Default, Debug, Serialize, Deserialize)] -struct Resolution { - width: Option, - height: Option +#[derive(Default, Debug, Serialize, Deserialize, Clone)] +pub struct Resolution { + pub width: Option, + pub height: Option } -#[derive(Debug, Serialize, Deserialize)] -enum EColorDepth { +#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)] +pub enum EColorDepth { Eight = 8, Ten = 10, Twelve = 12 @@ -69,14 +82,16 @@ impl Default for EColorDepth { } } -#[derive(Debug, Serialize, Deserialize)] -enum EStatus { +#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)] +pub enum EStatus { Queued, - Reserved, + Reserved(Client), Completed } impl Default for EStatus { fn default() -> Self { EStatus::Queued } -} \ No newline at end of file +} + +type Client = String; \ No newline at end of file