From 8442a3d29d09b214d4953d70070eb80a0aa38778 Mon Sep 17 00:00:00 2001 From: Gregor Reitzenstein Date: Tue, 10 Nov 2020 14:56:28 +0100 Subject: [PATCH] Slowly getting there --- src/api.rs | 25 ++++++++++++++++++------- src/db/access.rs | 12 ++++++++++++ src/db/machine.rs | 28 +++++++++++++++++++++------- src/db/user.rs | 2 +- src/modules/shelly.rs | 4 ++-- 5 files changed, 54 insertions(+), 17 deletions(-) diff --git a/src/api.rs b/src/api.rs index 5474c09..5d33593 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,5 +1,4 @@ -use smol::net::TcpStream; -use futures_util::FutureExt; +use std::sync::Arc; use slog::Logger; @@ -13,11 +12,22 @@ use capnp_rpc::twoparty::VatNetwork; use capnp_rpc::rpc_twoparty_capnp::Side; use capnp::capability::FromServer; -pub async fn handle_connection(log: Logger, socket: TcpStream) -> Result<()> { - unimplemented!() +use crate::db::machine::Machines; +use crate::db::user::User; + +use uuid::Uuid; + +pub struct MachinesAPI { + log: Logger, + user: User, + machines: Arc, } -pub struct MachinesAPI; +impl MachinesAPI { + pub fn new(log: Logger, user: User, machines: Arc) -> Self { + Self { log, user, machines } + } +} impl api_capnp::machines::Server for MachinesAPI { fn list_machines(&mut self, @@ -25,8 +35,9 @@ impl api_capnp::machines::Server for MachinesAPI { mut results: api_capnp::machines::ListMachinesResults) -> Promise<(), Error> { - let mut l = results.get(); - l.init_machines(0); + let l = results.get(); + let keys: Vec = self.machines.iter().map(|x| x.into()).collect(); + l.set_machines(keys); Promise::ok(()) } diff --git a/src/db/access.rs b/src/db/access.rs index 94608ec..dff3f22 100644 --- a/src/db/access.rs +++ b/src/db/access.rs @@ -187,6 +187,18 @@ fn is_sep_char(c: char) -> bool { c == '.' } +/// A set of privileges to a thing +pub struct PrivilegesBuf { + /// Which permission is required to know about the existance of this thing + disclose: PermissionBuf, + /// Which permission is required to read this thing + read: PermissionBuf, + /// Which permission is required to write parts of this thing + write: PermissionBuf, + /// Which permission is required to manage all parts of this thing + manage: PermissionBuf +} + #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] #[repr(transparent)] /// An owned permission string diff --git a/src/db/machine.rs b/src/db/machine.rs index 8290d1d..b2f9daa 100644 --- a/src/db/machine.rs +++ b/src/db/machine.rs @@ -37,15 +37,20 @@ use internal::Internal; pub type MachineIdentifier = Uuid; /// Status of a Machine -#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)] -#[repr(u8)] +#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] pub enum Status { /// Not currently used by anybody Free, /// Used by somebody - Occupied, + InUse(UserIdentifier), + /// Was used by somebody and now needs to be checked for cleanliness + ToCheck(UserIdentifier), /// Not used by anybody but also can not be used. E.g. down for maintenance - Blocked, + Blocked(UserIdentifier), + /// Disabled for some other reason + Disabled, + /// Reserved + Reserved(UserIdentifier), } #[derive(Clone)] @@ -88,7 +93,7 @@ pub struct Machine { /// The human-readable name of the machine. Does not need to be unique name: String, - /// The required permission to use this machine. + /// The required permissions to use this machine. perm: access::PermIdentifier, /// The state of the machine as bffh thinks the machine *should* be in. @@ -117,7 +122,7 @@ impl Machine { // 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().dedupe()) + Box::pin(self.state.signal_cloned().dedupe_cloned()) } /// Requests to use a machine. Returns `true` if successful. @@ -130,7 +135,7 @@ impl Machine { ) -> Result { if pp.check(who, &self.perm)? { - self.state.set(Status::Occupied); + self.state.set(Status::InUse(who.id.clone())); return Ok(true); } else { return Ok(false); @@ -142,6 +147,15 @@ impl Machine { } } +#[derive(Debug)] +pub struct Machines { + inner: HashMap, +} + +impl Machines { + +} + // TODO split up for non-writable Definition Databases pub trait MachineDB { fn get_machine(&self, machID: &MachineIdentifier) -> Result>; diff --git a/src/db/user.rs b/src/db/user.rs index 16f7fc9..c5a3f23 100644 --- a/src/db/user.rs +++ b/src/db/user.rs @@ -4,7 +4,7 @@ use crate::db::access::RoleIdentifier; use std::collections::HashMap; /// A Person, from the Authorization perspective -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)] pub struct User { /// The identification of this user. pub id: UserIdentifier, diff --git a/src/modules/shelly.rs b/src/modules/shelly.rs index 91ebee6..dcbea34 100644 --- a/src/modules/shelly.rs +++ b/src/modules/shelly.rs @@ -102,8 +102,8 @@ impl Stream for Shelly { info!(unpin.log, "Machine Status changed: {:?}", status); let topic = format!("shellies/{}/relay/0/command", unpin.name); let pl = match status { - Status::Free | Status::Blocked => "off", - Status::Occupied => "on", + Status::InUse(_) => "on", + _ => "off", }; let msg = mqtt::Message::new(topic, pl, 0); let f = unpin.client.publish(msg).map(|_| ());