2021-01-26 15:33:50 +01:00
|
|
|
use std::ops::Deref;
|
2020-12-07 15:58:25 +01:00
|
|
|
use std::iter::FromIterator;
|
2020-11-30 16:12:52 +01:00
|
|
|
use std::sync::Arc;
|
|
|
|
use futures_util::lock::Mutex;
|
2020-11-19 14:53:14 +01:00
|
|
|
use std::path::Path;
|
2020-11-30 14:08:03 +01:00
|
|
|
use std::task::{Poll, Context};
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::future::Future;
|
|
|
|
|
2020-11-19 14:53:14 +01:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::fs;
|
|
|
|
|
2020-11-17 13:40:44 +01:00
|
|
|
use serde::{Serialize, Deserialize};
|
|
|
|
|
2021-02-15 00:05:03 +01:00
|
|
|
use futures::Stream;
|
2021-01-22 16:25:26 +01:00
|
|
|
use futures::future::BoxFuture;
|
2021-02-15 00:05:03 +01:00
|
|
|
use futures::channel::{mpsc, oneshot};
|
2021-01-22 16:25:26 +01:00
|
|
|
|
2020-11-17 12:09:45 +01:00
|
|
|
use futures_signals::signal::Signal;
|
|
|
|
use futures_signals::signal::SignalExt;
|
2020-12-16 12:49:56 +01:00
|
|
|
use futures_signals::signal::{Mutable, ReadOnlyMutable};
|
2021-12-01 15:46:52 +01:00
|
|
|
use slog::Logger;
|
2020-11-17 12:09:45 +01:00
|
|
|
|
2020-11-30 14:08:03 +01:00
|
|
|
use crate::error::{Result, Error};
|
2020-11-17 12:09:45 +01:00
|
|
|
|
2021-12-01 15:46:52 +01:00
|
|
|
use crate::db::{access, Databases, MachineDB};
|
2021-12-05 23:38:05 +01:00
|
|
|
use crate::db::access::AccessControl;
|
2021-03-23 15:24:58 +01:00
|
|
|
use crate::db::machine::{MachineIdentifier, MachineState, Status};
|
2021-12-01 15:46:52 +01:00
|
|
|
use crate::db::user::{User, UserData, UserId};
|
2020-11-17 12:09:45 +01:00
|
|
|
|
2020-12-07 15:58:25 +01:00
|
|
|
use crate::network::MachineMap;
|
2021-09-09 21:50:11 +02:00
|
|
|
use crate::space;
|
|
|
|
|
|
|
|
pub struct Machines {
|
|
|
|
machines: Vec<Machine>
|
|
|
|
}
|
|
|
|
|
2020-11-30 16:12:52 +01:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Index {
|
|
|
|
inner: HashMap<String, Machine>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Index {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
inner: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn insert(&mut self, key: String, value: Machine) -> Option<Machine> {
|
|
|
|
self.inner.insert(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&mut self, key: &String) -> Option<Machine> {
|
|
|
|
self.inner.get(key).map(|m| m.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-09 21:50:11 +02:00
|
|
|
// Access data of one machine efficiently, using getters/setters for data stored in LMDB backed
|
|
|
|
// memory
|
2020-11-30 16:12:52 +01:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Machine {
|
2021-12-05 18:53:02 +01:00
|
|
|
pub id: MachineIdentifier,
|
2021-09-18 17:01:35 +02:00
|
|
|
pub desc: MachineDescription,
|
2021-09-09 21:50:11 +02:00
|
|
|
|
2021-12-05 23:38:05 +01:00
|
|
|
access_control: Arc<AccessControl>,
|
|
|
|
|
2021-01-22 16:25:26 +01:00
|
|
|
inner: Arc<Mutex<Inner>>,
|
2020-11-30 16:12:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Machine {
|
2021-12-05 23:38:05 +01:00
|
|
|
pub fn new(
|
|
|
|
inner: Inner,
|
|
|
|
id: MachineIdentifier,
|
|
|
|
desc: MachineDescription,
|
|
|
|
access_control: Arc<AccessControl>
|
|
|
|
) -> Self
|
|
|
|
{
|
2021-09-09 21:50:11 +02:00
|
|
|
Self {
|
2021-12-05 18:53:02 +01:00
|
|
|
id,
|
2021-09-09 21:50:11 +02:00
|
|
|
inner: Arc::new(Mutex::new(inner)),
|
2021-09-18 17:01:35 +02:00
|
|
|
desc,
|
2021-12-05 23:38:05 +01:00
|
|
|
access_control,
|
2021-09-09 21:50:11 +02:00
|
|
|
}
|
2020-11-30 16:12:52 +01:00
|
|
|
}
|
|
|
|
|
2021-12-01 15:46:52 +01:00
|
|
|
pub fn construct(
|
|
|
|
id: MachineIdentifier,
|
|
|
|
desc: MachineDescription,
|
|
|
|
state: MachineState,
|
|
|
|
db: Arc<MachineDB>,
|
2021-12-05 23:38:05 +01:00
|
|
|
access_control: Arc<AccessControl>,
|
|
|
|
) -> Machine
|
2020-11-30 16:12:52 +01:00
|
|
|
{
|
2021-12-05 23:38:05 +01:00
|
|
|
Self::new(Inner::new(id.clone(), state, db), id, desc, access_control)
|
2020-12-02 16:20:50 +01:00
|
|
|
}
|
2020-12-07 12:27:53 +01:00
|
|
|
|
2021-09-18 17:01:35 +02:00
|
|
|
pub fn do_state_change(&self, new_state: MachineState)
|
2021-09-21 07:48:19 +02:00
|
|
|
-> BoxFuture<'static, Result<()>>
|
2021-09-18 17:01:35 +02:00
|
|
|
{
|
|
|
|
let this = self.clone();
|
|
|
|
|
|
|
|
let f = async move {
|
|
|
|
let mut guard = this.inner.lock().await;
|
|
|
|
guard.do_state_change(new_state);
|
2021-09-21 07:48:19 +02:00
|
|
|
return Ok(())
|
2021-09-18 17:01:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Box::pin(f)
|
|
|
|
}
|
|
|
|
|
2021-12-05 23:38:05 +01:00
|
|
|
pub fn request_state_change(&mut self, user: Option<&User>, new_state: MachineState)
|
|
|
|
-> BoxFuture<'static, Result<ReturnToken>>
|
|
|
|
{
|
|
|
|
let inner = self.inner.clone();
|
|
|
|
Box::pin(async move {
|
|
|
|
Ok(ReturnToken::new(inner))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-23 15:24:58 +01:00
|
|
|
pub async fn get_status(&self) -> Status {
|
|
|
|
let guard = self.inner.lock().await;
|
|
|
|
guard.state.get_cloned().state
|
|
|
|
}
|
|
|
|
|
2020-12-07 12:27:53 +01:00
|
|
|
pub fn signal(&self) -> impl Signal<Item=MachineState> {
|
2021-01-22 16:25:26 +01:00
|
|
|
let guard = self.inner.try_lock().unwrap();
|
2020-12-07 12:27:53 +01:00
|
|
|
guard.signal()
|
|
|
|
}
|
2021-09-18 17:01:35 +02:00
|
|
|
|
|
|
|
pub fn get_inner(&self) -> Arc<Mutex<Inner>> {
|
|
|
|
self.inner.clone()
|
|
|
|
}
|
2020-11-30 16:12:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for Machine {
|
|
|
|
type Target = Mutex<Inner>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.inner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 00:05:03 +01:00
|
|
|
|
2020-11-17 12:09:45 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
/// 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-11-30 16:12:52 +01:00
|
|
|
pub struct Inner {
|
2020-11-19 14:53:14 +01:00
|
|
|
/// Globally unique machine readable identifier
|
2020-11-20 15:43:03 +01:00
|
|
|
pub id: MachineIdentifier,
|
2020-11-19 14:53:14 +01:00
|
|
|
|
2020-11-17 12:09:45 +01: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<MachineState>,
|
2020-11-30 14:08:03 +01:00
|
|
|
reset: Option<MachineState>,
|
2021-12-01 15:46:52 +01:00
|
|
|
|
|
|
|
previous: Option<UserId>,
|
|
|
|
db: Arc<MachineDB>,
|
2020-11-17 12:09:45 +01:00
|
|
|
}
|
|
|
|
|
2020-11-30 16:12:52 +01:00
|
|
|
impl Inner {
|
2021-12-01 15:46:52 +01:00
|
|
|
pub fn new(id: MachineIdentifier, state: MachineState, db: Arc<MachineDB>) -> Inner {
|
2020-11-30 16:12:52 +01:00
|
|
|
Inner {
|
2021-09-18 17:01:35 +02:00
|
|
|
id,
|
2020-11-30 14:08:03 +01:00
|
|
|
state: Mutable::new(state),
|
|
|
|
reset: None,
|
2021-12-01 15:46:52 +01:00
|
|
|
previous: None,
|
|
|
|
db,
|
2020-11-17 12:09:45 +01: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.
|
|
|
|
pub fn signal(&self) -> impl Signal<Item=MachineState> {
|
|
|
|
// 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.
|
|
|
|
Box::pin(self.state.signal_cloned().dedupe_cloned())
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:46:52 +01:00
|
|
|
fn replace_state(&mut self, new_state: MachineState) -> MachineState {
|
|
|
|
self.db.put(&self.id, &new_state);
|
|
|
|
self.state.replace(new_state)
|
|
|
|
}
|
|
|
|
|
2021-02-15 00:05:03 +01:00
|
|
|
pub fn do_state_change(&mut self, new_state: MachineState) {
|
2021-12-05 23:43:36 +01:00
|
|
|
let old_state = self.replace_state(new_state);
|
|
|
|
|
|
|
|
// Set "previous user" if state change warrants it
|
|
|
|
match old_state.state {
|
|
|
|
Status::InUse(ref user) => {
|
|
|
|
self.previous = user.clone();
|
|
|
|
},
|
|
|
|
Status::ToCheck(ref user) => {
|
|
|
|
self.previous = Some(user.clone());
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
self.reset.replace(old_state);
|
2020-11-17 12:09:45 +01:00
|
|
|
}
|
|
|
|
|
2020-12-16 12:49:56 +01:00
|
|
|
pub fn read_state(&self) -> ReadOnlyMutable<MachineState> {
|
|
|
|
self.state.read_only()
|
|
|
|
}
|
|
|
|
|
2020-11-30 14:08:03 +01:00
|
|
|
pub fn get_signal(&self) -> impl Signal {
|
2020-12-14 14:45:16 +01:00
|
|
|
self.state.signal_cloned()
|
2020-11-30 14:08:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reset_state(&mut self) {
|
2021-12-01 15:46:52 +01:00
|
|
|
let previous_state = self.read_state();
|
|
|
|
let state_lock = previous_state.lock_ref();
|
|
|
|
// Only update previous user if state changed from InUse or ToCheck to whatever.
|
|
|
|
match state_lock.state {
|
|
|
|
Status::InUse(ref user) => {
|
|
|
|
self.previous = user.clone();
|
|
|
|
},
|
|
|
|
Status::ToCheck(ref user) => {
|
|
|
|
self.previous = Some(user.clone());
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
drop(state_lock);
|
|
|
|
|
2020-11-30 14:08:03 +01:00
|
|
|
if let Some(state) = self.reset.take() {
|
2021-12-01 15:46:52 +01:00
|
|
|
self.replace_state(state);
|
|
|
|
} else {
|
|
|
|
// Default to Free
|
|
|
|
self.replace_state(MachineState::free());
|
2020-11-30 14:08:03 +01:00
|
|
|
}
|
|
|
|
}
|
2021-12-01 15:46:52 +01:00
|
|
|
|
|
|
|
pub fn get_previous(&self) -> &Option<UserId> {
|
|
|
|
&self.previous
|
|
|
|
}
|
2020-11-30 14:08:03 +01:00
|
|
|
}
|
|
|
|
|
2021-02-15 00:05:03 +01:00
|
|
|
//pub type ReturnToken = futures::channel::oneshot::Sender<()>;
|
|
|
|
pub struct ReturnToken {
|
2021-02-22 17:25:04 +01:00
|
|
|
f: Option<BoxFuture<'static, ()>>,
|
2021-02-15 00:05:03 +01:00
|
|
|
}
|
2020-11-30 14:08:03 +01:00
|
|
|
|
2021-02-15 00:05:03 +01:00
|
|
|
impl ReturnToken {
|
|
|
|
pub fn new(inner: Arc<Mutex<Inner>>) -> Self {
|
2021-02-22 17:25:04 +01:00
|
|
|
let f = async move {
|
|
|
|
let mut guard = inner.lock().await;
|
|
|
|
guard.reset_state();
|
|
|
|
};
|
|
|
|
|
|
|
|
Self { f: Some(Box::pin(f)) }
|
2021-02-15 00:05:03 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-30 14:08:03 +01:00
|
|
|
|
2021-02-15 00:05:03 +01:00
|
|
|
impl Future for ReturnToken {
|
2021-02-22 17:25:04 +01:00
|
|
|
type Output = (); // FIXME: This should probably be a Result<(), Error>
|
2020-11-30 14:08:03 +01:00
|
|
|
|
2021-02-15 00:05:03 +01:00
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
|
|
|
let mut this = &mut *self;
|
2020-11-30 14:08:03 +01:00
|
|
|
|
2021-02-22 17:25:04 +01:00
|
|
|
match this.f.as_mut().map(|f| Future::poll(Pin::new(f), cx)) {
|
|
|
|
None => Poll::Ready(()), // TODO: Is it saner to return Pending here? This can only happen after the future completed
|
|
|
|
Some(Poll::Pending) => Poll::Pending,
|
|
|
|
Some(Poll::Ready(())) => {
|
|
|
|
let _ = this.f.take(); // Remove the future to not poll after completion
|
|
|
|
Poll::Ready(())
|
|
|
|
}
|
|
|
|
}
|
2020-11-30 14:08:03 +01:00
|
|
|
}
|
2020-11-17 12:09:45 +01:00
|
|
|
}
|
2020-11-17 13:40:44 +01:00
|
|
|
|
2020-11-19 14:53:14 +01:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
2020-11-17 13:40:44 +01:00
|
|
|
/// A description of a machine
|
|
|
|
///
|
|
|
|
/// This is the struct that a machine is serialized to/from.
|
|
|
|
/// Combining this with the actual state of the system will return a machine
|
|
|
|
pub struct MachineDescription {
|
|
|
|
/// The name of the machine. Doesn't need to be unique but is what humans will be presented.
|
2020-11-20 15:43:03 +01:00
|
|
|
pub name: String,
|
2020-11-17 13:40:44 +01:00
|
|
|
/// An optional description of the Machine.
|
2020-11-20 15:43:03 +01:00
|
|
|
pub description: Option<String>,
|
2020-11-17 13:40:44 +01:00
|
|
|
|
2021-10-20 09:43:39 +02:00
|
|
|
#[serde(default)]
|
|
|
|
#[serde(flatten)]
|
|
|
|
pub wiki: Option<String>,
|
|
|
|
|
2020-11-17 13:40:44 +01:00
|
|
|
/// The permission required
|
2020-11-19 14:53:14 +01:00
|
|
|
#[serde(flatten)]
|
2020-12-14 12:39:01 +01:00
|
|
|
pub privs: access::PrivilegesBuf,
|
2020-11-17 13:40:44 +01:00
|
|
|
}
|
2020-11-19 14:53:14 +01:00
|
|
|
|
|
|
|
impl MachineDescription {
|
2020-11-20 13:06:55 +01:00
|
|
|
pub fn load_file<P: AsRef<Path>>(path: P) -> Result<HashMap<MachineIdentifier, MachineDescription>> {
|
2020-11-19 14:53:14 +01:00
|
|
|
let content = fs::read(path)?;
|
|
|
|
Ok(toml::from_slice(&content[..])?)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:46:52 +01:00
|
|
|
pub fn load(config: &crate::config::Config, db: Databases, log: &Logger)
|
2021-01-22 16:25:26 +01:00
|
|
|
-> Result<MachineMap>
|
|
|
|
{
|
2020-12-12 13:58:04 +01:00
|
|
|
let mut map = config.machines.clone();
|
2021-12-05 23:38:05 +01:00
|
|
|
let access_control = db.access;
|
2021-12-01 15:46:52 +01:00
|
|
|
let db = db.machine;
|
2020-12-07 12:27:53 +01:00
|
|
|
|
2020-12-07 15:58:25 +01:00
|
|
|
let it = map.drain()
|
2020-12-07 12:27:53 +01:00
|
|
|
.map(|(k,v)| {
|
|
|
|
// TODO: Read state from the state db
|
2021-12-01 15:46:52 +01:00
|
|
|
if let Some(state) = db.get(&k).unwrap() {
|
|
|
|
debug!(log, "Loading old state from db for {}: {:?}", &k, &state);
|
2021-12-05 23:38:05 +01:00
|
|
|
(k.clone(),
|
|
|
|
Machine::construct(
|
|
|
|
k,
|
|
|
|
v,
|
|
|
|
state,
|
|
|
|
db.clone(),
|
|
|
|
access_control.clone()
|
|
|
|
))
|
2021-12-01 15:46:52 +01:00
|
|
|
} else {
|
|
|
|
debug!(log, "No old state found in db for {}, creating new.", &k);
|
2021-12-05 23:38:05 +01:00
|
|
|
(k.clone(),
|
|
|
|
Machine::construct(
|
|
|
|
k,
|
|
|
|
v,
|
|
|
|
MachineState::new(),
|
|
|
|
db.clone(),
|
|
|
|
access_control.clone(),
|
|
|
|
))
|
2021-12-01 15:46:52 +01:00
|
|
|
}
|
2020-12-07 15:58:25 +01:00
|
|
|
});
|
2021-02-15 00:05:03 +01:00
|
|
|
|
|
|
|
|
2020-12-07 15:58:25 +01:00
|
|
|
Ok(HashMap::from_iter(it))
|
2020-12-01 09:44:18 +01:00
|
|
|
}
|
|
|
|
|
2020-12-01 16:06:39 +01:00
|
|
|
#[cfg(test_DISABLED)]
|
2020-11-19 14:53:14 +01:00
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use std::iter::FromIterator;
|
|
|
|
|
|
|
|
use crate::db::access::{PermissionBuf, PrivilegesBuf};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn load_examples_descriptions_test() {
|
2020-11-19 15:10:42 +01:00
|
|
|
let mut machines = MachineDescription::load_file("examples/machines.toml")
|
2020-11-19 14:53:14 +01:00
|
|
|
.expect("Couldn't load the example machine defs. Does `examples/machines.toml` exist?");
|
|
|
|
|
2020-11-19 15:10:42 +01:00
|
|
|
let expected =
|
|
|
|
vec![
|
2020-11-19 14:53:14 +01:00
|
|
|
(Uuid::parse_str("e5408099-d3e5-440b-a92b-3aabf7683d6b").unwrap(),
|
2020-11-19 15:10:42 +01:00
|
|
|
MachineDescription {
|
|
|
|
name: "Somemachine".to_string(),
|
|
|
|
description: None,
|
|
|
|
privs: PrivilegesBuf {
|
|
|
|
disclose: PermissionBuf::from_string("lab.some.disclose".to_string()),
|
|
|
|
read: PermissionBuf::from_string("lab.some.read".to_string()),
|
|
|
|
write: PermissionBuf::from_string("lab.some.write".to_string()),
|
|
|
|
manage: PermissionBuf::from_string("lab.some.admin".to_string()),
|
|
|
|
},
|
|
|
|
}),
|
2020-11-19 14:53:14 +01:00
|
|
|
(Uuid::parse_str("eaabebae-34d1-4a3a-912a-967b495d3d6e").unwrap(),
|
2020-11-19 15:10:42 +01:00
|
|
|
MachineDescription {
|
|
|
|
name: "Testmachine".to_string(),
|
|
|
|
description: Some("An optional description".to_string()),
|
|
|
|
privs: PrivilegesBuf {
|
|
|
|
disclose: PermissionBuf::from_string("lab.test.read".to_string()),
|
|
|
|
read: PermissionBuf::from_string("lab.test.read".to_string()),
|
|
|
|
write: PermissionBuf::from_string("lab.test.write".to_string()),
|
|
|
|
manage: PermissionBuf::from_string("lab.test.admin".to_string()),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (id, machine) in expected.into_iter() {
|
|
|
|
|
|
|
|
assert_eq!(machines.remove(&id).unwrap(), machine);
|
2020-11-19 14:53:14 +01:00
|
|
|
}
|
2020-11-19 15:10:42 +01:00
|
|
|
|
|
|
|
assert!(machines.is_empty());
|
2020-11-19 14:53:14 +01:00
|
|
|
}
|
|
|
|
}
|