This commit is contained in:
Daniel Løvbrøtte Olsen 2020-03-26 01:39:22 +01:00
parent a4ab569437
commit 416bdaecee
2 changed files with 68 additions and 48 deletions

View File

@ -12,6 +12,7 @@ use std::sync::Mutex;
mod workunit; mod workunit;
use workunit::WUnit; use workunit::WUnit;
use workunit::EStatus;
const VERSION: &str = "0.1.0"; const VERSION: &str = "0.1.0";
@ -31,40 +32,44 @@ fn version() -> &'static str {
} }
#[get("/get_jobs")] #[get("/get_jobs")]
fn getJobs(shared: State<SharedState>) -> Json<Value> { fn get_jobs(shared: State<SharedState>) -> Json<Value> {
// let shared_data: &SharedState = shared.inner();
let list = shared.list.lock().unwrap(); let list = shared.list.lock().unwrap();
println!("get jobs blah");
// println!("{:#?}", Json(list));
Json(serde_json::to_value(&list[..]).unwrap()) Json(serde_json::to_value(&list[..]).unwrap())
} }
#[get("/get_job/<id>")] #[get("/request_job")]
fn getJob(id: usize, shared: State<SharedState>) -> Result<String, NotFound<String>> { fn request_job(shared: State<SharedState>) -> Result<Json<Value>, NotFound<String>> {
let shared_data: &SharedState = shared.inner(); let mut list = shared.list.lock().unwrap().clone();
let list = shared_data.list.lock().unwrap();
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/<id>")]
fn get_job(id: u32, shared: State<SharedState>) -> Result<Json<Value>, NotFound<String>> {
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 { match job {
Ok(j) => Ok(format!("{:#?}", j)), Ok(j) => Ok(Json(serde_json::to_value(&j).unwrap())),
Err(e) => Err(e) Err(e) => Err(e)
} }
} }
#[get("/add_job")] #[post("/add_job")]
fn addJob(shared: State<SharedState>) -> Result<String, std::io::Error> { fn add_job(shared: State<SharedState>) -> Result<String, std::io::Error> {
let shared_data: &SharedState = shared.inner(); shared.list.lock().unwrap().push(WUnit::new(2, "iduno", None, 10, workunit::EOptions::default()));
Ok(format!("{:#?}", shared))
shared_data.list.lock().unwrap().push(WUnit::default());
Ok(format!("{:#?}", shared_data))
} }
fn main() { fn main() {
rocket::ignite() rocket::ignite()
.manage(SharedState::default()) .manage(SharedState::default())
.mount("/", routes![index, version, getJobs, getJob, addJob]) .mount("/", routes![index, version, get_jobs, get_job, request_job, add_job])
.launch(); .launch();
} }

View File

@ -1,26 +1,39 @@
use std::path::PathBuf; use std::path::PathBuf;
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
#[derive(Default, Debug, Serialize, Deserialize)] #[derive(Default, Debug, Serialize, Deserialize, Clone)]
pub struct WUnit { pub struct WUnit {
file_name: PathBuf, pub id: u32,
priority: u16, pub file_url: String,
length: u32, pub priority: u16,
options: EOptions, pub length: u32,
status: EStatus pub options: EOptions,
pub status: EStatus
}
impl WUnit {
pub fn new(id: u32, file_url: &str, priority: Option<u16>, 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)] #[derive(Debug, Serialize, Deserialize, Clone)]
struct EOptions { pub struct EOptions {
mode: EMode, pub mode: EMode,
resolution: Resolution, pub resolution: Resolution,
color_depth: EColorDepth, pub color_depth: EColorDepth,
enable_fwd_keyframe: bool, pub enable_fwd_keyframe: bool,
kf_min_dist: Option<u16>, pub kf_min_dist: Option<u16>,
kf_max_dist: Option<u16>, pub kf_max_dist: Option<u16>,
two_pass: bool, pub two_pass: bool,
speed: u8 pub speed: u8
} }
impl Default for EOptions { impl Default for EOptions {
fn default() -> Self { fn default() -> Self {
@ -37,8 +50,8 @@ impl Default for EOptions {
} }
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
enum EMode { pub enum EMode {
// Quality (CRF), Constrained Quality, Variable Bitrate, Constant Bitrate // Quality (CRF), Constrained Quality, Variable Bitrate, Constant Bitrate
Q(u8), Q(u8),
CQ(u8), CQ(u8),
@ -51,14 +64,14 @@ impl Default for EMode {
} }
} }
#[derive(Default, Debug, Serialize, Deserialize)] #[derive(Default, Debug, Serialize, Deserialize, Clone)]
struct Resolution { pub struct Resolution {
width: Option<u16>, pub width: Option<u16>,
height: Option<u16> pub height: Option<u16>
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
enum EColorDepth { pub enum EColorDepth {
Eight = 8, Eight = 8,
Ten = 10, Ten = 10,
Twelve = 12 Twelve = 12
@ -69,14 +82,16 @@ impl Default for EColorDepth {
} }
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
enum EStatus { pub enum EStatus {
Queued, Queued,
Reserved, Reserved(Client),
Completed Completed
} }
impl Default for EStatus { impl Default for EStatus {
fn default() -> Self { fn default() -> Self {
EStatus::Queued EStatus::Queued
} }
} }
type Client = String;