2020-02-16 16:02:03 +01:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{Read, Write};
|
|
|
|
|
2020-02-17 14:56:43 +01:00
|
|
|
use slog::Logger;
|
|
|
|
|
2020-02-16 16:02:03 +01:00
|
|
|
use serde::{Serialize, Deserialize};
|
|
|
|
|
2020-09-08 09:56:40 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
use smol::lock::RwLock;
|
|
|
|
|
2020-02-16 16:02:03 +01:00
|
|
|
use crate::error::Result;
|
2020-09-15 14:31:10 +02:00
|
|
|
use crate::config::Settings;
|
2020-02-17 14:56:43 +01:00
|
|
|
|
|
|
|
use capnp::Error;
|
|
|
|
|
|
|
|
use uuid::Uuid;
|
2020-02-16 16:02:03 +01:00
|
|
|
|
|
|
|
/// Status of a Machine
|
|
|
|
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize)]
|
|
|
|
pub enum Status {
|
|
|
|
/// Not currently used by anybody
|
|
|
|
Free,
|
|
|
|
/// Used by somebody
|
|
|
|
Occupied,
|
|
|
|
/// Not used by anybody but also can not be used. E.g. down for maintenance
|
|
|
|
Blocked,
|
|
|
|
}
|
|
|
|
|
2020-02-18 16:55:19 +01:00
|
|
|
pub struct MachinesProvider {
|
2020-02-17 14:56:43 +01:00
|
|
|
log: Logger,
|
2020-02-18 16:55:19 +01:00
|
|
|
mdb: MachineDB,
|
2020-02-17 14:56:43 +01:00
|
|
|
}
|
2020-02-17 03:44:02 +01:00
|
|
|
|
2020-02-18 16:55:19 +01:00
|
|
|
impl MachinesProvider {
|
|
|
|
pub fn new(log: Logger, mdb: MachineDB) -> Self {
|
|
|
|
Self { log, mdb }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn use_(&mut self, uuid: &Uuid) -> std::result::Result<(), capnp::Error> {
|
|
|
|
if let Some(m) = self.mdb.get_mut(uuid) {
|
|
|
|
match m.status {
|
|
|
|
Status::Free => {
|
|
|
|
trace!(self.log, "Granted use on machine {}", uuid);
|
|
|
|
|
|
|
|
m.status = Status::Occupied;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
Status::Occupied => {
|
|
|
|
info!(self.log, "Attempted use on an occupied machine {}", uuid);
|
|
|
|
Err(Error::failed("Machine is occupied".to_string()))
|
|
|
|
},
|
|
|
|
Status::Blocked => {
|
|
|
|
info!(self.log, "Attempted use on a blocked machine {}", uuid);
|
|
|
|
Err(Error::failed("Machine is blocked".to_string()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
info!(self.log, "Attempted use on invalid machine {}", uuid);
|
|
|
|
Err(Error::failed("No such machine".to_string()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn give_back(&mut self, uuid: &Uuid) -> std::result::Result<(), capnp::Error> {
|
|
|
|
if let Some(m) = self.mdb.get_mut(uuid) {
|
|
|
|
m.status = Status::Free;
|
|
|
|
} else {
|
|
|
|
warn!(self.log, "A giveback was issued for a unknown machine {}", uuid);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_perm_req(&self, uuid: &Uuid) -> Option<String> {
|
|
|
|
self.mdb.get(uuid).map(|m| m.perm.clone())
|
|
|
|
}
|
2020-02-19 14:50:23 +01:00
|
|
|
|
|
|
|
pub fn set_blocked(&mut self, uuid: &Uuid, blocked: bool) -> std::result::Result<(), capnp::Error> {
|
|
|
|
// If the value can not be found map doesn't run and ok_or changes it into a Err with the
|
|
|
|
// given error value
|
|
|
|
self.mdb.get_mut(uuid).map(|m| m.set_blocked(blocked))
|
|
|
|
.ok_or(capnp::Error::failed("No such machine".to_string()))
|
|
|
|
}
|
2020-02-18 16:55:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Machines {
|
2020-09-08 09:56:40 +02:00
|
|
|
inner: Arc<RwLock<MachinesProvider>>,
|
2020-02-18 16:55:19 +01:00
|
|
|
}
|
2020-02-17 14:56:43 +01:00
|
|
|
impl Machines {
|
2020-09-08 09:56:40 +02:00
|
|
|
pub fn new(inner: Arc<RwLock<MachinesProvider>>) -> Self {
|
2020-05-04 13:22:14 +02:00
|
|
|
Self { inner }
|
2020-02-17 14:56:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-18 16:55:19 +01:00
|
|
|
#[derive(Clone)]
|
2020-02-17 21:07:50 +01:00
|
|
|
pub struct GiveBack {
|
2020-02-18 16:55:19 +01:00
|
|
|
mdb: Arc<RwLock<MachinesProvider>>,
|
2020-02-17 21:07:50 +01:00
|
|
|
uuid: Uuid,
|
|
|
|
}
|
|
|
|
impl GiveBack {
|
2020-02-18 16:55:19 +01:00
|
|
|
pub fn new(mdb: Arc<RwLock<MachinesProvider>>, uuid: Uuid) -> Self {
|
|
|
|
Self { mdb, uuid }
|
2020-02-17 21:07:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-08 09:56:40 +02:00
|
|
|
fn uuid_from_api(uuid: crate::api::api_capnp::u_u_i_d::Reader) -> Uuid {
|
2020-02-17 14:56:43 +01:00
|
|
|
let uuid0 = uuid.get_uuid0() as u128;
|
|
|
|
let uuid1 = uuid.get_uuid1() as u128;
|
|
|
|
let num: u128 = (uuid1 << 64) + uuid0;
|
|
|
|
Uuid::from_u128(num)
|
|
|
|
}
|
2020-09-08 09:56:40 +02:00
|
|
|
fn api_from_uuid(uuid: Uuid, mut wr: crate::api::api_capnp::u_u_i_d::Builder) {
|
2020-02-17 14:56:43 +01:00
|
|
|
let num = uuid.to_u128_le();
|
|
|
|
let uuid0 = num as u64;
|
|
|
|
let uuid1 = (num >> 64) as u64;
|
|
|
|
wr.set_uuid0(uuid0);
|
|
|
|
wr.set_uuid1(uuid1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct MachineManager {
|
2020-02-18 16:55:19 +01:00
|
|
|
mdb: Arc<RwLock<MachinesProvider>>,
|
2020-02-17 14:56:43 +01:00
|
|
|
uuid: Uuid,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MachineManager {
|
2020-02-19 14:50:23 +01:00
|
|
|
pub fn new(uuid: Uuid, mdb: Arc<RwLock<MachinesProvider>>) -> Self {
|
2020-02-17 14:56:43 +01:00
|
|
|
Self { mdb, uuid }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-16 16:02:03 +01:00
|
|
|
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct Machine {
|
2020-02-17 14:56:43 +01:00
|
|
|
pub name: String,
|
2020-02-16 16:02:03 +01:00
|
|
|
pub location: String,
|
|
|
|
pub status: Status,
|
2020-02-17 14:56:43 +01:00
|
|
|
pub perm: String,
|
2020-02-16 16:02:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Machine {
|
2020-02-17 14:56:43 +01:00
|
|
|
pub fn new(name: String, location: String, perm: String) -> Machine {
|
2020-02-16 16:02:03 +01:00
|
|
|
Machine {
|
2020-02-17 14:56:43 +01:00
|
|
|
name: name,
|
2020-02-16 16:02:03 +01:00
|
|
|
location: location,
|
|
|
|
status: Status::Free,
|
2020-02-17 14:56:43 +01:00
|
|
|
perm: perm,
|
2020-02-16 16:02:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 14:56:43 +01:00
|
|
|
pub fn set_blocked(&mut self, blocked: bool) {
|
|
|
|
if blocked {
|
|
|
|
self.status = Status::Blocked;
|
|
|
|
} else {
|
|
|
|
self.status = Status::Free;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-16 16:02:03 +01:00
|
|
|
|
2020-02-17 14:56:43 +01:00
|
|
|
pub type MachineDB = HashMap<Uuid, Machine>;
|
2020-02-16 16:02:03 +01:00
|
|
|
|
2020-09-15 14:31:10 +02:00
|
|
|
pub async fn init(log: Logger, config: &Settings) -> Result<MachinesProvider> {
|
|
|
|
unimplemented!()
|
2020-02-16 16:02:03 +01:00
|
|
|
}
|