2020-09-18 13:14:24 +02:00
|
|
|
use std::str::FromStr;
|
2020-02-16 16:02:03 +01:00
|
|
|
use std::collections::HashMap;
|
2020-09-18 13:14:24 +02:00
|
|
|
use std::fs;
|
2020-02-16 16:02:03 +01:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{Read, Write};
|
2020-09-18 13:14:24 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2020-02-16 16:02:03 +01:00
|
|
|
|
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-09-17 10:51:51 +02:00
|
|
|
use crate::access;
|
2020-02-17 14:56:43 +01:00
|
|
|
|
|
|
|
use capnp::Error;
|
|
|
|
|
|
|
|
use uuid::Uuid;
|
2020-02-16 16:02:03 +01:00
|
|
|
|
2020-09-18 13:14:24 +02:00
|
|
|
use lmdb::{Transaction, RwTransaction, Cursor};
|
2020-09-15 14:48:59 +02:00
|
|
|
|
2020-09-15 16:35:37 +02:00
|
|
|
use smol::channel::{Receiver, Sender};
|
|
|
|
|
|
|
|
use futures_signals::signal::*;
|
|
|
|
|
2020-09-18 12:34:18 +02:00
|
|
|
use crate::registries::StatusSignal;
|
|
|
|
|
2020-09-17 10:18:02 +02:00
|
|
|
pub type ID = Uuid;
|
|
|
|
|
2020-02-16 16:02:03 +01:00
|
|
|
/// Status of a Machine
|
2020-09-15 16:37:50 +02:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)]
|
2020-09-17 10:51:51 +02:00
|
|
|
#[repr(u8)]
|
2020-02-16 16:02:03 +01:00
|
|
|
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
|
|
|
#[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-09-15 16:35:37 +02:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2020-09-15 15:29:55 +02:00
|
|
|
/// Internal machine representation
|
|
|
|
///
|
|
|
|
/// A machine connects an event from a sensor to an actor activating/deactivating a real-world
|
|
|
|
/// machine, checking that the user who wants the machine (de)activated has the required
|
|
|
|
/// permissions.
|
2020-02-16 16:02:03 +01:00
|
|
|
pub struct Machine {
|
2020-09-17 10:51:51 +02:00
|
|
|
/// Computer-readable identifier for this machine
|
|
|
|
// Implicit in database since it's the key.
|
|
|
|
#[serde(skip)]
|
|
|
|
id: ID,
|
|
|
|
|
2020-09-15 16:35:37 +02:00
|
|
|
/// The human-readable name of the machine. Does not need to be unique
|
|
|
|
name: String,
|
|
|
|
|
|
|
|
/// The required permission to use this machine.
|
2020-09-17 10:51:51 +02:00
|
|
|
perm: access::PermIdentifier,
|
2020-09-15 16:35:37 +02:00
|
|
|
|
|
|
|
/// The state of the machine as bffh thinks the machine *should* be in.
|
|
|
|
///
|
|
|
|
/// This is a Signal generator. Subscribers to this signal will be notified of changes. In the
|
|
|
|
/// case of an actor it should then make sure that the real world matches up with the set state
|
|
|
|
state: Mutable<Status>,
|
2020-02-16 16:02:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Machine {
|
2020-09-17 10:51:51 +02:00
|
|
|
pub fn new(id: Uuid, name: String, perm: access::PermIdentifier) -> Machine {
|
2020-02-16 16:02:03 +01:00
|
|
|
Machine {
|
2020-09-17 10:51:51 +02:00
|
|
|
id: id,
|
2020-02-17 14:56:43 +01:00
|
|
|
name: name,
|
|
|
|
perm: perm,
|
2020-09-15 16:35:37 +02:00
|
|
|
state: Mutable::new(Status::Free),
|
2020-02-16 16:02:03 +01:00
|
|
|
}
|
|
|
|
}
|
2020-09-15 16:35:37 +02:00
|
|
|
|
2020-09-15 17:04:13 +02:00
|
|
|
/// Generate a signal from the internal state.
|
|
|
|
///
|
|
|
|
/// A signal is a lossy stream of state changes. Lossy in that if changes happen in quick
|
|
|
|
/// succession intermediary values may be lost. But this isn't really relevant in this case
|
|
|
|
/// since the only relevant state is the latest one.
|
|
|
|
/// dedupe ensures that if state is changed but only changes to the value it had beforehand
|
|
|
|
/// (could for example happen if the machine changes current user but stays activated) no
|
|
|
|
/// update is sent.
|
2020-09-18 12:34:18 +02:00
|
|
|
pub fn signal(&self) -> StatusSignal {
|
|
|
|
Box::pin(self.state.signal().dedupe())
|
2020-09-15 16:35:37 +02:00
|
|
|
}
|
2020-09-17 10:51:51 +02:00
|
|
|
|
|
|
|
/// Requests to use a machine. Returns `true` if successful.
|
|
|
|
///
|
|
|
|
/// This will update the internal state of the machine, notifying connected actors, if any.
|
|
|
|
pub fn request_use<T: Transaction>
|
|
|
|
( &mut self
|
|
|
|
, txn: &T
|
|
|
|
, pp: &access::PermissionsProvider
|
|
|
|
, who: access::UserIdentifier
|
|
|
|
) -> Result<bool>
|
|
|
|
{
|
|
|
|
if pp.check(txn, who, self.perm)? {
|
|
|
|
self.state.set(Status::Occupied);
|
|
|
|
return Ok(true);
|
|
|
|
} else {
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
}
|
2020-09-18 12:34:18 +02:00
|
|
|
|
|
|
|
pub fn set_state(&mut self, state: Status) {
|
|
|
|
self.state.set(state)
|
|
|
|
}
|
2020-02-17 14:56:43 +01:00
|
|
|
}
|
2020-02-16 16:02:03 +01:00
|
|
|
|
2020-09-18 13:14:24 +02:00
|
|
|
pub struct MachinesProvider {
|
|
|
|
log: Logger,
|
2020-09-15 14:41:50 +02:00
|
|
|
db: lmdb::Database,
|
|
|
|
}
|
|
|
|
|
2020-09-18 13:14:24 +02:00
|
|
|
impl MachinesProvider {
|
|
|
|
pub fn new(log: Logger, db: lmdb::Database) -> Self {
|
|
|
|
Self { log, db }
|
2020-09-15 14:41:50 +02:00
|
|
|
}
|
|
|
|
|
2020-09-15 14:48:59 +02:00
|
|
|
pub fn get_machine<T: Transaction>(&self, txn: &T, uuid: Uuid)
|
2020-09-15 14:41:50 +02:00
|
|
|
-> Result<Option<Machine>>
|
|
|
|
{
|
2020-09-15 14:48:59 +02:00
|
|
|
match txn.get(self.db, &uuid.as_bytes()) {
|
2020-09-15 14:41:50 +02:00
|
|
|
Ok(bytes) => {
|
2020-09-17 10:51:51 +02:00
|
|
|
let mut machine: Machine = flexbuffers::from_slice(bytes)?;
|
|
|
|
machine.id = uuid;
|
|
|
|
|
|
|
|
Ok(Some(machine))
|
2020-09-15 14:41:50 +02:00
|
|
|
},
|
|
|
|
Err(lmdb::Error::NotFound) => { Ok(None) },
|
|
|
|
Err(e) => { Err(e.into()) },
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 14:48:59 +02:00
|
|
|
|
|
|
|
pub fn put_machine( &self, txn: &mut RwTransaction, uuid: Uuid, machine: Machine)
|
|
|
|
-> Result<()>
|
|
|
|
{
|
|
|
|
let bytes = flexbuffers::to_vec(machine)?;
|
|
|
|
txn.put(self.db, &uuid.as_bytes(), &bytes, lmdb::WriteFlags::empty())?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-09-18 13:14:24 +02:00
|
|
|
|
|
|
|
pub fn load_db(&mut self, txn: &mut RwTransaction, mut path: PathBuf) -> Result<()> {
|
|
|
|
path.push("machines");
|
|
|
|
for entry in std::fs::read_dir(path)? {
|
|
|
|
let entry = entry?;
|
|
|
|
let path = entry.path();
|
|
|
|
if path.is_file() {
|
|
|
|
// will only ever be none if the path has no file name and then how is it a file?!
|
|
|
|
let machID_str = path
|
|
|
|
.file_stem().expect("Found a file with no filename?")
|
|
|
|
.to_str().expect("Found an OsStr that isn't valid Unicode. Fix your OS!");
|
|
|
|
let machID = match uuid::Uuid::from_str(machID_str) {
|
|
|
|
Ok(i) => i,
|
|
|
|
Err(e) => {
|
|
|
|
warn!(self.log, "File {} had a invalid name. Expected an u64 in [0-9a-z] hex with optional file ending: {}. Skipping!", path.display(), e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let s = match fs::read_to_string(path.as_path()) {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(e) => {
|
|
|
|
warn!(self.log, "Failed to open file {}: {}, skipping!"
|
|
|
|
, path.display()
|
|
|
|
, e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let mach: Machine = match toml::from_str(&s) {
|
|
|
|
Ok(r) => r,
|
|
|
|
Err(e) => {
|
|
|
|
warn!(self.log, "Failed to parse mach at path {}: {}, skipping!"
|
|
|
|
, path.display()
|
|
|
|
, e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
self.put_machine(txn, machID, mach)?;
|
|
|
|
debug!(self.log, "Loaded machine {}", machID);
|
|
|
|
} else {
|
|
|
|
warn!(self.log, "Path {} is not a file, skipping!", path.display());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn dump_db<T: Transaction>(&self, txn: &T, mut path: PathBuf) -> Result<()> {
|
|
|
|
path.push("machines");
|
|
|
|
let mut mach_cursor = txn.open_ro_cursor(self.db)?;
|
|
|
|
for buf in mach_cursor.iter_start() {
|
|
|
|
let (kbuf, vbuf) = buf?;
|
|
|
|
let machID = uuid::Uuid::from_slice(kbuf).unwrap();
|
|
|
|
let mach: Machine = flexbuffers::from_slice(vbuf)?;
|
|
|
|
let filename = format!("{}.yml", machID.to_hyphenated().to_string());
|
|
|
|
path.set_file_name(filename);
|
|
|
|
let mut fp = std::fs::File::create(&path)?;
|
|
|
|
let out = toml::to_vec(&mach)?;
|
|
|
|
fp.write_all(&out)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-09-15 14:41:50 +02:00
|
|
|
}
|
2020-02-16 16:02:03 +01:00
|
|
|
|
2020-09-18 13:14:24 +02:00
|
|
|
pub fn init(log: Logger, config: &Settings, env: &lmdb::Environment) -> Result<MachinesProvider> {
|
|
|
|
let mut flags = lmdb::DatabaseFlags::empty();
|
|
|
|
flags.set(lmdb::DatabaseFlags::INTEGER_KEY, true);
|
|
|
|
let machdb = env.create_db(Some("machines"), flags)?;
|
|
|
|
debug!(&log, "Opened machine db successfully.");
|
|
|
|
|
|
|
|
Ok(MachinesProvider::new(log, machdb))
|
2020-02-16 16:02:03 +01:00
|
|
|
}
|