fabaccess-bffh/bffhd/capnp/machine.rs

380 lines
12 KiB
Rust
Raw Normal View History

2022-04-28 20:33:46 +02:00
use crate::capnp::user::User;
2022-03-16 19:01:09 +01:00
use crate::resources::modules::fabaccess::{ArchivedStatus, Status};
2022-03-13 17:29:21 +01:00
use crate::resources::Resource;
use crate::session::SessionHandle;
2022-04-28 20:33:46 +02:00
use api::general_capnp::optional;
use api::machine_capnp::machine::{
2022-04-28 20:33:46 +02:00
self, admin, admin::Server as AdminServer, check, check::Server as CheckServer,
in_use as inuse, in_use::Server as InUseServer, info, info::Server as InfoServer, manage,
manage::Server as ManageServer, use_, use_::Server as UseServer, MachineState,
};
2022-03-13 17:29:21 +01:00
use capnp::capability::Promise;
use capnp_rpc::pry;
2022-03-13 17:29:21 +01:00
#[derive(Clone)]
2022-03-12 17:31:53 +01:00
pub struct Machine {
session: SessionHandle,
2022-03-13 17:29:21 +01:00
resource: Resource,
}
impl Machine {
2022-03-16 19:01:09 +01:00
pub fn new(session: SessionHandle, resource: Resource) -> Self {
Self { session, resource }
}
pub fn build_into(self, mut builder: machine::Builder) {
builder.set_id(self.resource.get_id());
builder.set_name(self.resource.get_name());
if let Some(ref desc) = self.resource.get_description().description {
builder.set_description(desc);
}
if let Some(ref wiki) = self.resource.get_description().wiki {
builder.set_wiki(wiki);
}
if let Some(ref category) = self.resource.get_description().category {
builder.set_category(category);
}
2022-04-28 20:33:46 +02:00
builder.set_urn(&format!(
"urn:fabaccess:resource:{}",
self.resource.get_id()
));
2022-03-16 19:01:09 +01:00
{
let user = self.session.get_user_ref();
let state = self.resource.get_state_ref();
let state = state.as_ref();
2022-03-16 19:01:09 +01:00
2022-04-28 20:33:46 +02:00
if self.session.has_write(&self.resource)
&& match &state.inner.state {
ArchivedStatus::Free => true,
ArchivedStatus::Reserved(reserver) if reserver == &user => true,
_ => false,
}
{
builder.set_use(capnp_rpc::new_client(self.clone()));
}
2022-03-16 19:01:09 +01:00
if self.session.has_manage(&self.resource) {
builder.set_manage(capnp_rpc::new_client(self.clone()));
}
2022-03-16 19:01:09 +01:00
// TODO: admin perm
2022-03-16 19:01:09 +01:00
let s = match &state.inner.state {
ArchivedStatus::Free => MachineState::Free,
ArchivedStatus::Disabled => MachineState::Disabled,
ArchivedStatus::Blocked(_) => MachineState::Blocked,
ArchivedStatus::InUse(owner) => {
if owner == &user {
builder.set_inuse(capnp_rpc::new_client(self.clone()));
}
MachineState::InUse
2022-04-28 20:33:46 +02:00
}
ArchivedStatus::Reserved(_) => MachineState::Reserved,
ArchivedStatus::ToCheck(_) => MachineState::ToCheck,
};
if self.session.has_read(&self.resource) {
builder.set_state(s);
2022-03-16 19:01:09 +01:00
}
}
builder.set_info(capnp_rpc::new_client(self));
2022-03-16 19:01:09 +01:00
}
2022-03-13 17:29:21 +01:00
/// Builds a machine into the given builder. Re
pub fn build(session: SessionHandle, resource: Resource, builder: machine::Builder) {
2022-03-16 19:01:09 +01:00
let this = Self::new(session.clone(), resource.clone());
this.build_into(builder)
2022-03-13 17:29:21 +01:00
}
2022-04-28 20:33:46 +02:00
pub fn optional_build(
session: SessionHandle,
resource: Resource,
builder: optional::Builder<machine::Owned>,
) {
let this = Self::new(session.clone(), resource.clone());
if this.resource.visible(&session) || session.has_read(&resource) {
2022-04-26 23:21:43 +02:00
let builder = builder.init_just();
this.build_into(builder);
}
}
2022-03-12 17:31:53 +01:00
}
impl InfoServer for Machine {
fn get_property_list(
&mut self,
_: info::GetPropertyListParams,
_: info::GetPropertyListResults,
2022-03-13 17:29:21 +01:00
) -> Promise<(), ::capnp::Error> {
Promise::err(::capnp::Error::unimplemented(
"method not implemented".to_string(),
))
}
fn get_reservation_list(
&mut self,
_: info::GetReservationListParams,
_: info::GetReservationListResults,
2022-03-13 17:29:21 +01:00
) -> Promise<(), ::capnp::Error> {
Promise::err(::capnp::Error::unimplemented(
"method not implemented".to_string(),
))
}
}
impl UseServer for Machine {
2022-03-13 17:29:21 +01:00
fn use_(&mut self, _: use_::UseParams, _: use_::UseResults) -> Promise<(), ::capnp::Error> {
let resource = self.resource.clone();
let session = self.session.clone();
Promise::from_future(async move {
2022-03-21 00:01:50 +01:00
let user = session.get_user_ref();
2022-03-13 20:11:37 +01:00
resource.try_update(session, Status::InUse(user)).await;
2022-03-13 17:29:21 +01:00
Ok(())
})
}
2022-03-13 17:29:21 +01:00
fn reserve(
&mut self,
_: use_::ReserveParams,
_: use_::ReserveResults,
2022-03-13 17:29:21 +01:00
) -> Promise<(), ::capnp::Error> {
let resource = self.resource.clone();
let session = self.session.clone();
Promise::from_future(async move {
2022-03-21 00:01:50 +01:00
let user = session.get_user_ref();
2022-04-28 20:33:46 +02:00
resource.try_update(session, Status::Reserved(user)).await;
2022-03-13 17:29:21 +01:00
Ok(())
})
}
2022-03-13 17:29:21 +01:00
fn reserveto(
&mut self,
_: use_::ReservetoParams,
_: use_::ReservetoResults,
2022-03-13 17:29:21 +01:00
) -> Promise<(), ::capnp::Error> {
Promise::err(::capnp::Error::unimplemented(
"method not implemented".to_string(),
))
}
}
impl InUseServer for Machine {
fn give_back(
&mut self,
_: inuse::GiveBackParams,
_: inuse::GiveBackResults,
2022-03-13 17:29:21 +01:00
) -> Promise<(), ::capnp::Error> {
let resource = self.resource.clone();
let session = self.session.clone();
Promise::from_future(async move {
resource.give_back(session.clone()).await;
Ok(())
})
}
2022-03-13 17:29:21 +01:00
fn send_raw_data(
&mut self,
mut params: inuse::SendRawDataParams,
_: inuse::SendRawDataResults,
2022-03-13 17:29:21 +01:00
) -> Promise<(), ::capnp::Error> {
let data: Vec<u8> = 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(())
})
}
}
impl CheckServer for Machine {
fn check(
&mut self,
_: check::CheckParams,
_: check::CheckResults,
2022-03-13 17:29:21 +01:00
) -> Promise<(), ::capnp::Error> {
Promise::err(::capnp::Error::unimplemented(
"method not implemented".to_string(),
))
}
2022-03-13 17:29:21 +01:00
fn reject(
&mut self,
_: check::RejectParams,
_: check::RejectResults,
2022-03-13 17:29:21 +01:00
) -> Promise<(), ::capnp::Error> {
Promise::err(::capnp::Error::unimplemented(
"method not implemented".to_string(),
))
}
}
impl ManageServer for Machine {
2022-03-12 02:00:55 +01:00
fn get_machine_info_extended(
&mut self,
_: manage::GetMachineInfoExtendedParams,
mut result: manage::GetMachineInfoExtendedResults,
2022-03-13 17:29:21 +01:00
) -> Promise<(), ::capnp::Error> {
let mut builder = result.get();
2022-03-21 00:01:50 +01:00
let user = User::new_self(self.session.clone());
2022-04-28 20:33:46 +02:00
User::build_optional(
&self.session,
self.resource.get_current_user(),
builder.reborrow().init_current_user(),
);
User::build_optional(
&self.session,
self.resource.get_previous_user(),
builder.init_last_user(),
);
Promise::ok(())
2022-03-12 02:00:55 +01:00
}
fn set_property(
&mut self,
_: manage::SetPropertyParams,
_: manage::SetPropertyResults,
2022-03-13 17:29:21 +01:00
) -> Promise<(), ::capnp::Error> {
Promise::err(::capnp::Error::unimplemented(
"method not implemented".to_string(),
))
}
fn remove_property(
&mut self,
_: manage::RemovePropertyParams,
_: manage::RemovePropertyResults,
2022-03-13 17:29:21 +01:00
) -> Promise<(), ::capnp::Error> {
Promise::err(::capnp::Error::unimplemented(
"method not implemented".to_string(),
))
}
2022-03-13 17:29:21 +01:00
fn force_use(
&mut self,
_: manage::ForceUseParams,
_: manage::ForceUseResults,
2022-03-13 17:29:21 +01:00
) -> Promise<(), ::capnp::Error> {
let resource = self.resource.clone();
let session = self.session.clone();
Promise::from_future(async move {
resource
2022-03-21 00:01:50 +01:00
.force_set(Status::InUse(session.get_user_ref()))
2022-03-13 17:29:21 +01:00
.await;
Ok(())
})
}
2022-03-13 17:29:21 +01:00
fn force_free(
&mut self,
_: manage::ForceFreeParams,
_: manage::ForceFreeResults,
2022-03-13 17:29:21 +01:00
) -> Promise<(), ::capnp::Error> {
let resource = self.resource.clone();
2022-03-15 20:00:43 +01:00
let _session = self.session.clone();
2022-03-13 17:29:21 +01:00
Promise::from_future(async move {
2022-03-13 20:11:37 +01:00
resource.force_set(Status::Free).await;
2022-03-13 17:29:21 +01:00
Ok(())
})
}
fn force_transfer(
&mut self,
_: manage::ForceTransferParams,
_: manage::ForceTransferResults,
2022-03-13 17:29:21 +01:00
) -> Promise<(), ::capnp::Error> {
Promise::err(::capnp::Error::unimplemented(
"method not implemented".to_string(),
))
}
2022-03-13 17:29:21 +01:00
fn block(
&mut self,
_: manage::BlockParams,
_: manage::BlockResults,
2022-03-13 17:29:21 +01:00
) -> Promise<(), ::capnp::Error> {
let resource = self.resource.clone();
let session = self.session.clone();
Promise::from_future(async move {
resource
2022-03-21 00:01:50 +01:00
.force_set(Status::Blocked(session.get_user_ref()))
2022-03-13 17:29:21 +01:00
.await;
Ok(())
})
}
fn disabled(
&mut self,
_: manage::DisabledParams,
_: manage::DisabledResults,
2022-03-13 17:29:21 +01:00
) -> Promise<(), ::capnp::Error> {
2022-03-15 20:00:43 +01:00
let resource = self.resource.clone();
2022-03-13 17:29:21 +01:00
Promise::from_future(async move {
2022-03-13 20:11:37 +01:00
resource.force_set(Status::Disabled).await;
2022-03-13 17:29:21 +01:00
Ok(())
})
}
}
impl AdminServer for Machine {
fn force_set_state(
&mut self,
2022-03-13 17:29:21 +01:00
params: admin::ForceSetStateParams,
_: admin::ForceSetStateResults,
2022-03-13 17:29:21 +01:00
) -> Promise<(), ::capnp::Error> {
use api::schema::machine_capnp::machine::MachineState as APIMState;
2022-03-21 00:01:50 +01:00
let user = self.session.get_user_ref();
2022-03-13 17:29:21 +01:00
let state = match pry!(pry!(params.get()).get_state()) {
2022-03-13 20:11:37 +01:00
APIMState::Free => Status::Free,
APIMState::Blocked => Status::Blocked(user),
APIMState::Disabled => Status::Disabled,
APIMState::InUse => Status::InUse(user),
APIMState::Reserved => Status::Reserved(user),
APIMState::ToCheck => Status::ToCheck(user),
2022-04-28 20:33:46 +02:00
APIMState::Totakeover => {
return Promise::err(::capnp::Error::unimplemented(
2022-03-13 17:29:21 +01:00
"totakeover not implemented".to_string(),
2022-04-28 20:33:46 +02:00
))
}
2022-03-13 17:29:21 +01:00
};
2022-03-13 22:50:37 +01:00
let resource = self.resource.clone();
Promise::from_future(async move {
resource.force_set(state).await;
Ok(())
})
}
2022-03-13 22:50:37 +01:00
fn force_set_user(
&mut self,
_: admin::ForceSetUserParams,
_: admin::ForceSetUserResults,
2022-03-13 17:29:21 +01:00
) -> Promise<(), ::capnp::Error> {
Promise::err(::capnp::Error::unimplemented(
"method not implemented".to_string(),
))
}
2022-03-13 22:50:37 +01:00
fn get_admin_property_list(
&mut self,
_: admin::GetAdminPropertyListParams,
_: admin::GetAdminPropertyListResults,
2022-03-13 17:29:21 +01:00
) -> Promise<(), ::capnp::Error> {
Promise::err(::capnp::Error::unimplemented(
"method not implemented".to_string(),
))
}
fn set_admin_property(
&mut self,
_: admin::SetAdminPropertyParams,
_: admin::SetAdminPropertyResults,
2022-03-13 17:29:21 +01:00
) -> Promise<(), ::capnp::Error> {
Promise::err(::capnp::Error::unimplemented(
"method not implemented".to_string(),
))
}
fn remove_admin_property(
&mut self,
_: admin::RemoveAdminPropertyParams,
_: admin::RemoveAdminPropertyResults,
2022-03-13 17:29:21 +01:00
) -> Promise<(), ::capnp::Error> {
Promise::err(::capnp::Error::unimplemented(
"method not implemented".to_string(),
))
}
}