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;
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<SharedState>) -> Json<Value> {
// let shared_data: &SharedState = shared.inner();
fn get_jobs(shared: State<SharedState>) -> Json<Value> {
let list = shared.list.lock().unwrap();
println!("get jobs blah");
// println!("{:#?}", Json(list));
Json(serde_json::to_value(&list[..]).unwrap())
}
#[get("/get_job/<id>")]
fn getJob(id: usize, shared: State<SharedState>) -> Result<String, NotFound<String>> {
let shared_data: &SharedState = shared.inner();
let list = shared_data.list.lock().unwrap();
#[get("/request_job")]
fn request_job(shared: State<SharedState>) -> Result<Json<Value>, NotFound<String>> {
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/<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 {
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<SharedState>) -> Result<String, std::io::Error> {
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<SharedState>) -> Result<String, std::io::Error> {
shared.list.lock().unwrap().push(WUnit::new(2, "iduno", None, 10, workunit::EOptions::default()));
Ok(format!("{:#?}", shared))
}
fn main() {
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();
}

View File

@ -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<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)]
struct EOptions {
mode: EMode,
resolution: Resolution,
color_depth: EColorDepth,
enable_fwd_keyframe: bool,
kf_min_dist: Option<u16>,
kf_max_dist: Option<u16>,
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<u16>,
pub kf_max_dist: Option<u16>,
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<u16>,
height: Option<u16>
#[derive(Default, Debug, Serialize, Deserialize, Clone)]
pub struct Resolution {
pub width: Option<u16>,
pub height: Option<u16>
}
#[derive(Debug, Serialize, Deserialize)]
enum EColorDepth {
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub enum EColorDepth {
Eight = 8,
Ten = 10,
Twelve = 12
@ -69,10 +82,10 @@ 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 {
@ -80,3 +93,5 @@ impl Default for EStatus {
EStatus::Queued
}
}
type Client = String;