2020-12-07 14:39:35 +01:00
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use smol::Executor;
|
|
|
|
|
|
|
|
use futures::channel::mpsc;
|
|
|
|
use futures_signals::signal::{Signal, MutableSignalCloned, Mutable};
|
|
|
|
|
|
|
|
use crate::machine::Machine;
|
2020-12-07 15:58:25 +01:00
|
|
|
use crate::actor::{Actor, ActorSignal};
|
2020-12-07 14:39:35 +01:00
|
|
|
use crate::initiator::Initiator;
|
|
|
|
use crate::db::machine::MachineState;
|
|
|
|
|
|
|
|
use crate::error::Result;
|
|
|
|
|
2020-12-07 15:58:25 +01:00
|
|
|
pub type MachineMap = HashMap<String, Machine>;
|
|
|
|
pub type ActorMap = HashMap<String, mpsc::Sender<Option<ActorSignal>>>;
|
|
|
|
pub type InitMap = HashMap<String, Mutable<Option<Machine>>>;
|
2020-12-07 14:39:35 +01:00
|
|
|
|
2020-12-07 15:58:25 +01:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2020-12-07 14:39:35 +01:00
|
|
|
pub enum Error {
|
|
|
|
NoSuchInitiator,
|
|
|
|
NoSuchMachine,
|
|
|
|
NoSuchActor,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Error::NoSuchInitiator => write!(f, "No initiator found with that name"),
|
|
|
|
Error::NoSuchActor => write!(f, "No actor found with that name"),
|
|
|
|
Error::NoSuchMachine => write!(f, "No machine found with that name"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Main signal network
|
|
|
|
///
|
|
|
|
/// Network as per FRP, not the one with packages and frames
|
|
|
|
pub struct Network {
|
2020-12-07 15:58:25 +01:00
|
|
|
inits: InitMap,
|
|
|
|
|
|
|
|
// Store connections
|
|
|
|
//miconn: Vec<(String, String)>,
|
|
|
|
|
2020-12-07 14:39:35 +01:00
|
|
|
machines: MachineMap,
|
2020-12-07 15:58:25 +01:00
|
|
|
|
|
|
|
// Store connections
|
|
|
|
//maconn: Vec<(String, String)>,
|
|
|
|
|
2020-12-07 14:39:35 +01:00
|
|
|
actors: ActorMap,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Network {
|
|
|
|
pub fn new(machines: MachineMap, actors: ActorMap, inits: InitMap) -> Self {
|
|
|
|
Self { machines, actors, inits }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn connect_init(&self, init_key: &String, machine_key: &String) -> Result<()> {
|
|
|
|
let init = self.inits.get(init_key)
|
|
|
|
.ok_or(Error::NoSuchInitiator)?;
|
|
|
|
let machine = self.machines.get(machine_key)
|
|
|
|
.ok_or(Error::NoSuchMachine)?;
|
|
|
|
|
2020-12-07 15:58:25 +01:00
|
|
|
init.set(Some(machine.clone()));
|
|
|
|
Ok(())
|
2020-12-07 14:39:35 +01:00
|
|
|
}
|
|
|
|
|
2020-12-07 15:58:25 +01:00
|
|
|
pub fn connect_actor(&mut self, machine_key: &String, actor_key: &String) -> Result<()> {
|
2020-12-07 14:39:35 +01:00
|
|
|
let machine = self.machines.get(machine_key)
|
|
|
|
.ok_or(Error::NoSuchMachine)?;
|
2020-12-07 15:58:25 +01:00
|
|
|
let actor = self.actors.get_mut(actor_key)
|
2020-12-07 14:39:35 +01:00
|
|
|
.ok_or(Error::NoSuchActor)?;
|
|
|
|
|
2020-12-07 15:58:25 +01:00
|
|
|
actor.try_send(Some(Box::new(machine.signal()))).map_err(|_| Error::NoSuchActor.into())
|
2020-12-07 14:39:35 +01:00
|
|
|
}
|
|
|
|
}
|