fabaccess-bffh/src/initiator.rs

131 lines
3.5 KiB
Rust
Raw Normal View History

2020-12-02 16:20:50 +01:00
use std::pin::Pin;
use std::task::{Poll, Context};
2020-12-01 10:21:39 +01:00
use std::future::Future;
use std::collections::HashMap;
2020-12-02 16:20:50 +01:00
use smol::{Task, Timer};
2020-12-01 09:44:18 +01:00
2020-12-02 16:20:50 +01:00
use futures::FutureExt;
use futures::future::BoxFuture;
use genawaiter::{sync::{Gen, GenBoxed, Co}, GeneratorState};
2020-12-07 12:11:07 +01:00
use futures_signals::signal::{Signal, Mutable, MutableSignalCloned};
2020-12-02 16:20:50 +01:00
use crate::machine::{Machine, ReturnToken};
use crate::db::machine::MachineState;
use crate::db::user::{User, UserId, UserData};
2020-12-01 16:06:39 +01:00
use crate::network::InitMap;
2020-12-02 17:12:25 +01:00
2020-12-01 10:21:39 +01:00
use crate::error::Result;
2020-12-09 10:51:47 +01:00
pub trait Sensor {
fn run_sensor(&mut self) -> BoxFuture<'static, (Option<User>, MachineState)>;
}
type BoxSensor = Box<dyn Sensor + Send>;
pub struct Initiator {
2020-12-02 16:20:50 +01:00
signal: MutableSignalCloned<Option<Machine>>,
machine: Option<Machine>,
2020-12-02 17:15:25 +01:00
future: Option<BoxFuture<'static, (Option<User>, MachineState)>>,
2020-12-02 16:20:50 +01:00
token: Option<ReturnToken>,
sensor: BoxSensor,
2020-12-01 10:21:39 +01:00
}
impl Initiator {
pub fn new(sensor: BoxSensor, signal: MutableSignalCloned<Option<Machine>>) -> Self {
2020-12-02 16:20:50 +01:00
Self {
signal: signal,
machine: None,
future: None,
token: None,
2020-12-02 17:12:25 +01:00
sensor: sensor,
2020-12-02 16:20:50 +01:00
}
}
2020-12-07 12:11:07 +01:00
pub fn wrap(sensor: BoxSensor) -> (Mutable<Option<Machine>>, Self) {
2020-12-07 12:11:07 +01:00
let m = Mutable::new(None);
let s = m.signal_cloned();
(m, Self::new(sensor, s))
}
2020-12-02 16:20:50 +01:00
}
impl Future for Initiator {
2020-12-02 16:20:50 +01:00
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
let mut this = &mut *self;
// First of course, see what machine we should work with.
match Signal::poll_change(Pin::new(&mut this.signal), cx) {
Poll::Pending => { }
Poll::Ready(None) => return Poll::Ready(()),
// Keep in mind this is actually an Option<Machine>
Poll::Ready(Some(machine)) => this.machine = machine,
}
// Do as much work as we can:
loop {
// If there is a future, poll it
match this.future.as_mut().map(|future| Future::poll(Pin::new(future), cx)) {
None => {
2020-12-02 17:15:25 +01:00
this.future = Some(this.sensor.run_sensor());
2020-12-02 16:20:50 +01:00
},
2020-12-02 17:15:25 +01:00
Some(Poll::Ready((user, state))) => {
2020-12-02 16:20:50 +01:00
this.future.take();
this.machine.as_mut().map(|machine| machine.request_state_change(user.as_ref(), state));
}
Some(Poll::Pending) => return Poll::Pending,
}
}
2020-12-01 10:21:39 +01:00
}
2020-12-01 09:44:18 +01:00
}
pub fn load() -> Result<(InitMap, Vec<Initiator>)> {
2020-12-07 12:11:07 +01:00
let d = Box::new(Dummy::new());
let (m, i) = Initiator::wrap(d);
let mut map = HashMap::new();
map.insert("Dummy".to_string(), m);
Ok((map, vec![i]))
2020-12-01 09:44:18 +01:00
}
2020-12-02 17:12:25 +01:00
2020-12-02 17:15:25 +01:00
pub struct Dummy {
step: bool
}
2020-12-02 17:12:25 +01:00
2020-12-02 17:15:25 +01:00
impl Dummy {
pub fn new() -> Self {
Self { step: false }
}
}
2020-12-02 17:12:25 +01:00
2020-12-02 17:15:25 +01:00
impl Sensor for Dummy {
fn run_sensor(&mut self)
-> BoxFuture<'static, (Option<User>, MachineState)>
2020-12-02 17:12:25 +01:00
{
2020-12-02 17:15:25 +01:00
let step = self.step;
2020-12-09 11:14:17 +01:00
self.step = !self.step;
2020-12-02 17:15:25 +01:00
2020-12-02 17:12:25 +01:00
let f = async move {
Timer::after(std::time::Duration::from_secs(1)).await;
if step {
2020-12-02 17:15:25 +01:00
return (None, MachineState::free());
2020-12-02 17:12:25 +01:00
} else {
let user = User::new(
UserId::new("test".to_string(), None, None),
UserData::new(vec![], 0),
);
let p = user.data.priority;
let id = user.id.clone();
2020-12-02 17:15:25 +01:00
return (Some(user), MachineState::used(id, p));
2020-12-02 17:12:25 +01:00
}
};
Box::pin(f)
}
}