2020-11-20 13:06:55 +01:00
|
|
|
use std::sync::Arc;
|
2020-12-16 12:49:56 +01:00
|
|
|
use std::ops::Deref;
|
2020-11-17 14:15:29 +01:00
|
|
|
|
|
|
|
use capnp::capability::Promise;
|
|
|
|
use capnp::Error;
|
|
|
|
|
2021-01-22 15:25:26 +00:00
|
|
|
use futures::FutureExt;
|
|
|
|
|
2020-11-20 15:43:03 +01:00
|
|
|
use crate::schema::api_capnp::State;
|
2020-11-20 13:06:55 +01:00
|
|
|
use crate::schema::api_capnp::machine::*;
|
|
|
|
use crate::connection::Session;
|
|
|
|
use crate::db::Databases;
|
2021-01-20 11:55:15 +00:00
|
|
|
use crate::db::machine::{Status, MachineState};
|
2021-02-09 17:41:05 +00:00
|
|
|
use crate::machine::{Machine as NwMachine, ReturnToken};
|
2020-11-17 14:15:29 +01:00
|
|
|
|
2020-11-20 13:06:55 +01:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Machine {
|
|
|
|
session: Arc<Session>,
|
2020-12-16 11:32:31 +01:00
|
|
|
machine: NwMachine,
|
2020-11-20 13:06:55 +01:00
|
|
|
db: Databases,
|
|
|
|
}
|
2020-11-17 14:15:29 +01:00
|
|
|
|
2020-11-17 14:28:04 +01:00
|
|
|
impl Machine {
|
2020-12-16 11:32:31 +01:00
|
|
|
pub fn new(session: Arc<Session>, machine: NwMachine, db: Databases) -> Self {
|
|
|
|
Machine { session, machine, db }
|
2020-11-17 14:28:04 +01:00
|
|
|
}
|
2020-11-20 15:43:03 +01:00
|
|
|
|
|
|
|
pub fn fill(self: Arc<Self>, builder: &mut Builder) {
|
|
|
|
builder.set_read(capnp_rpc::new_client(Read(self.clone())));
|
2021-02-08 18:28:27 +00:00
|
|
|
builder.set_write(capnp_rpc::new_client(Write(self.clone())));
|
|
|
|
builder.set_manage(capnp_rpc::new_client(Manage(self.clone())));
|
|
|
|
builder.set_admin(capnp_rpc::new_client(Admin(self.clone())));
|
2020-11-20 15:43:03 +01:00
|
|
|
}
|
|
|
|
|
2020-12-16 12:49:56 +01:00
|
|
|
pub async fn fill_info(&self, builder: &mut m_info::Builder<'_>) {
|
2020-12-16 12:42:14 +01:00
|
|
|
let guard = self.machine.lock().await;
|
|
|
|
|
|
|
|
builder.set_name(guard.desc.name.as_ref());
|
|
|
|
|
2020-12-16 12:49:56 +01:00
|
|
|
if let Some(desc) = guard.desc.description.as_ref() {
|
|
|
|
builder.set_description(desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
match guard.read_state().lock_ref().deref().state {
|
2020-12-16 12:42:14 +01:00
|
|
|
Status::Free => {
|
|
|
|
builder.set_state(State::Free);
|
|
|
|
}
|
|
|
|
Status::Disabled => {
|
|
|
|
builder.set_state(State::Disabled);
|
|
|
|
}
|
2021-01-20 11:55:15 +00:00
|
|
|
Status::Blocked(_) => {
|
2020-12-16 12:42:14 +01:00
|
|
|
builder.set_state(State::Blocked);
|
|
|
|
}
|
2021-01-20 11:55:15 +00:00
|
|
|
Status::InUse(_) => {
|
2020-12-16 12:42:14 +01:00
|
|
|
builder.set_state(State::InUse);
|
|
|
|
}
|
2021-01-20 11:55:15 +00:00
|
|
|
Status::ToCheck(_) => {
|
2020-12-16 12:42:14 +01:00
|
|
|
builder.set_state(State::ToCheck);
|
|
|
|
}
|
2021-01-20 11:55:15 +00:00
|
|
|
Status::Reserved(_) => {
|
2020-12-16 12:42:14 +01:00
|
|
|
builder.set_state(State::Reserved);
|
|
|
|
}
|
|
|
|
}
|
2020-11-20 15:43:03 +01:00
|
|
|
}
|
2020-11-17 14:28:04 +01:00
|
|
|
}
|
|
|
|
|
2021-02-08 18:28:27 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Read(Arc<Machine>);
|
|
|
|
|
|
|
|
impl Read {
|
|
|
|
pub fn new(inner: Arc<Machine>) -> Self {
|
|
|
|
Self(inner)
|
|
|
|
}
|
|
|
|
}
|
2020-11-17 14:28:04 +01:00
|
|
|
|
|
|
|
impl read::Server for Read {
|
2020-11-17 14:15:29 +01:00
|
|
|
fn info(&mut self,
|
|
|
|
_params: read::InfoParams,
|
2020-11-20 15:43:03 +01:00
|
|
|
mut results: read::InfoResults)
|
2020-11-17 14:15:29 +01:00
|
|
|
-> Promise<(), Error>
|
|
|
|
{
|
2021-02-08 18:28:27 +00:00
|
|
|
let this = self.clone();
|
|
|
|
let f = async move {
|
|
|
|
let mut b = results.get().init_minfo();
|
|
|
|
|
|
|
|
this.0.fill_info(&mut b).await;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
|
|
|
|
Promise::from_future(f)
|
2020-11-17 14:15:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-20 13:06:55 +01:00
|
|
|
struct Write(Arc<Machine>);
|
2020-11-17 14:28:04 +01:00
|
|
|
|
|
|
|
impl write::Server for Write {
|
2020-11-17 14:15:29 +01:00
|
|
|
fn use_(&mut self,
|
|
|
|
_params: write::UseParams,
|
2021-02-09 17:41:05 +00:00
|
|
|
mut results: write::UseResults)
|
2021-01-20 11:55:15 +00:00
|
|
|
-> Promise<(), Error>
|
|
|
|
{
|
2021-02-09 17:15:31 +00:00
|
|
|
let uid = self.0.session.user.try_lock().unwrap().as_ref().map(|u| u.id.clone());
|
2021-01-20 11:55:15 +00:00
|
|
|
let new_state = MachineState::used(uid.clone());
|
2021-01-22 15:25:26 +00:00
|
|
|
let this = self.0.clone();
|
2021-02-09 17:41:05 +00:00
|
|
|
let f = async move {
|
|
|
|
let res_token = this.machine.request_state_change(
|
|
|
|
this.session.user.try_lock().unwrap().as_ref(),
|
|
|
|
new_state
|
|
|
|
).await;
|
|
|
|
|
|
|
|
match res_token {
|
2021-01-26 14:47:58 +00:00
|
|
|
// TODO: Do something with the token we get returned
|
2021-02-09 17:41:05 +00:00
|
|
|
Ok(tok) => {
|
|
|
|
let gb = GiveBack(Some(tok));
|
|
|
|
results.get().set_ret(capnp_rpc::new_client(gb));
|
|
|
|
|
2021-01-22 15:25:26 +00:00
|
|
|
return Ok(());
|
|
|
|
},
|
2021-02-09 08:51:53 +00:00
|
|
|
Err(e) => Err(capnp::Error::failed(format!("State change request returned {}", e))),
|
2021-02-09 17:41:05 +00:00
|
|
|
}
|
|
|
|
};
|
2021-01-22 15:25:26 +00:00
|
|
|
|
|
|
|
Promise::from_future(f)
|
2021-01-20 11:55:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn reserve(&mut self,
|
|
|
|
_params: write::ReserveParams,
|
|
|
|
_results: write::ReserveResults)
|
2020-11-17 14:15:29 +01:00
|
|
|
-> Promise<(), Error>
|
|
|
|
{
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 17:41:05 +00:00
|
|
|
struct GiveBack(Option<ReturnToken>);
|
|
|
|
|
|
|
|
impl write::give_back::Server for GiveBack {
|
|
|
|
fn ret(&mut self,
|
|
|
|
_params: write::give_back::RetParams,
|
|
|
|
_results: write::give_back::RetResults)
|
|
|
|
-> Promise<(), Error>
|
|
|
|
{
|
2021-02-14 23:05:03 +00:00
|
|
|
println!("I'm doing my part!");
|
2021-02-09 17:41:05 +00:00
|
|
|
if let Some(chan) = self.0.take() {
|
2021-02-14 23:05:03 +00:00
|
|
|
// Err here just means machine was taken from us
|
|
|
|
let _ = chan.send(());
|
2021-02-09 17:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Promise::ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-20 13:06:55 +01:00
|
|
|
struct Manage(Arc<Machine>);
|
2020-11-17 14:28:04 +01:00
|
|
|
|
|
|
|
impl manage::Server for Manage {
|
2020-11-17 14:15:29 +01:00
|
|
|
fn ok(&mut self,
|
|
|
|
_params: manage::OkParams,
|
|
|
|
_results: manage::OkResults)
|
|
|
|
-> Promise<(), Error>
|
|
|
|
{
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-20 13:06:55 +01:00
|
|
|
struct Admin(Arc<Machine>);
|
2020-11-17 14:28:04 +01:00
|
|
|
|
|
|
|
impl admin::Server for Admin {
|
2020-11-17 14:15:29 +01:00
|
|
|
fn force_set_state(&mut self,
|
|
|
|
_params: admin::ForceSetStateParams,
|
|
|
|
_results: admin::ForceSetStateResults)
|
|
|
|
-> Promise<(), Error>
|
|
|
|
{
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn force_set_user(&mut self,
|
|
|
|
_params: admin::ForceSetUserParams,
|
|
|
|
_results: admin::ForceSetUserResults)
|
|
|
|
-> Promise<(), Error>
|
|
|
|
{
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|