From b8092e9090a5e2d6d0fd1a3b36c800854eac2320 Mon Sep 17 00:00:00 2001 From: Nadja Reitzenstein Date: Wed, 18 Jan 2023 15:58:32 +0100 Subject: [PATCH] Implement first draft of raw write serverside --- bffhd/capnp/machine.rs | 12 ++++++++---- bffhd/resources/mod.rs | 22 ++++++++++++++++++++-- bffhd/resources/modules/fabaccess.rs | 4 +--- bffhd/resources/state/mod.rs | 21 +++++++++++++++------ 4 files changed, 44 insertions(+), 15 deletions(-) diff --git a/bffhd/capnp/machine.rs b/bffhd/capnp/machine.rs index d05d4f2..8f60dee 100644 --- a/bffhd/capnp/machine.rs +++ b/bffhd/capnp/machine.rs @@ -173,12 +173,16 @@ impl InUseServer for Machine { fn send_raw_data( &mut self, - _: inuse::SendRawDataParams, + mut params: inuse::SendRawDataParams, _: inuse::SendRawDataResults, ) -> Promise<(), ::capnp::Error> { - Promise::err(::capnp::Error::unimplemented( - "method not implemented".to_string(), - )) + let data: Vec = pry!(pry!(params.get()).get_data()).to_vec(); + let resource = self.resource.clone(); + let session = self.session.clone(); + Promise::from_future(async move { + resource.send_raw(data).await; + Ok(()) + }) } } diff --git a/bffhd/resources/mod.rs b/bffhd/resources/mod.rs index 369efe3..3621e1f 100644 --- a/bffhd/resources/mod.rs +++ b/bffhd/resources/mod.rs @@ -9,7 +9,7 @@ use crate::config::MachineDescription; use crate::db::ArchivedValue; use crate::resources::modules::fabaccess::{ArchivedStatus, MachineState, Status}; use crate::resources::state::db::StateDB; -use crate::resources::state::State; +use crate::resources::state::{ArchivedState, State}; use crate::session::SessionHandle; use crate::users::UserRef; use rkyv::option::ArchivedOption; @@ -161,7 +161,7 @@ impl Resource { fn set_state(&self, state: MachineState) { let mut serializer = AllocSerializer::<1024>::default(); - serializer.serialize_value(&state); + let _ = serializer.serialize_value(&State::new(state)); let archived = ArchivedValue::new(serializer.into_serializer().into_inner()); self.inner.set_state(archived) } @@ -177,6 +177,24 @@ impl Resource { self.set_state(new); } + pub async fn send_raw(&self, data: Vec) { + let mut serializer = AllocSerializer::<1024>::default(); + let old_state_ref = self.inner.get_state_ref(); + let old_state: &ArchivedState = old_state_ref.as_ref(); + let m_state: MachineState = + match Deserialize::::deserialize(&old_state.inner, &mut serializer) { + Ok(s) => s, + Err(error) => { + tracing::error!(?error, "failed to deserialize stored state!"); + return; + } + }; + + let _ = serializer.serialize_value(&State::with_raw(m_state, data)); + let archived = ArchivedValue::new(serializer.into_serializer().into_inner()); + self.inner.set_state(archived); + } + pub async fn try_update(&self, session: SessionHandle, new: Status) { let old = self.get_state(); let old: &Archived = old.as_ref(); diff --git a/bffhd/resources/modules/fabaccess.rs b/bffhd/resources/modules/fabaccess.rs index b50e9b6..5682cc8 100644 --- a/bffhd/resources/modules/fabaccess.rs +++ b/bffhd/resources/modules/fabaccess.rs @@ -88,9 +88,7 @@ impl MachineState { Deserialize::deserialize(state, &mut Infallible).unwrap() } pub fn to_state(&self) -> State { - State { - inner: self.clone(), - } + State::new(self.clone()) } pub fn free(previous: Option) -> Self { diff --git a/bffhd/resources/state/mod.rs b/bffhd/resources/state/mod.rs index 84e3587..1c0c4aa 100644 --- a/bffhd/resources/state/mod.rs +++ b/bffhd/resources/state/mod.rs @@ -1,5 +1,4 @@ -use std::fmt::{Debug, Display, Formatter}; -use std::{fmt, hash::Hasher}; +use std::fmt; use std::ops::Deref; @@ -20,6 +19,16 @@ pub mod value; #[archive_attr(derive(Debug))] pub struct State { pub inner: MachineState, + pub raw: Vec, +} +impl State { + pub fn new(inner: MachineState) -> Self { + Self::with_raw(inner, Vec::new()) + } + + pub fn with_raw(inner: MachineState, raw: Vec) -> Self { + Self { inner, raw } + } } impl fmt::Debug for State { @@ -34,8 +43,8 @@ impl fmt::Debug for State { } impl fmt::Display for ArchivedState { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - Display::fmt(&self.inner, f) + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(&self.inner, f) } } @@ -62,7 +71,7 @@ struct StateVisitor; impl<'de> serde::de::Visitor<'de> for StateVisitor { type Value = State; - fn expecting(&self, formatter: &mut Formatter) -> fmt::Result { + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { write!(formatter, "a map from OIDs to value objects") } @@ -75,7 +84,7 @@ impl<'de> serde::de::Visitor<'de> for StateVisitor { )); } let val: MachineState = map.next_value()?; - Ok(State { inner: val }) + Ok(State { inner: val, raw: Vec::new() }) } }