Add EventNetwork manage struct

This commit is contained in:
Gregor Reitzenstein 2020-12-07 14:39:35 +01:00
parent a16712c66f
commit 81ea99405c
2 changed files with 83 additions and 2 deletions

View File

@ -5,10 +5,12 @@ use toml;
use rsasl::SaslError;
// SpawnError is a somewhat ambigous name, `use as` to make it futures::SpawnError instead.
use futures::task as futures;
use futures::task as futures_task;
use paho_mqtt::errors as mqtt;
use crate::network;
#[derive(Debug)]
pub enum Error {
TomlDe(toml::de::Error),
@ -20,11 +22,12 @@ pub enum Error {
LMDB(lmdb::Error),
FlexbuffersDe(flexbuffers::DeserializationError),
FlexbuffersSer(flexbuffers::SerializationError),
FuturesSpawn(futures::SpawnError),
FuturesSpawn(futures_task::SpawnError),
MQTT(mqtt::Error),
Config(config::ConfigError),
BadVersion((u32,u32)),
Argon2(argon2::Error),
EventNetwork(network::Error),
Denied,
}
@ -76,6 +79,9 @@ impl fmt::Display for Error {
Error::Denied => {
write!(f, "You do not have the permission required to do that.")
}
Error::EventNetwork(e) => {
e.fmt(f)
}
}
}
}
@ -152,6 +158,12 @@ impl From<config::ConfigError> for Error {
}
}
impl From<network::Error> for Error {
fn from(e: network::Error) -> Error {
Error::EventNetwork(e)
}
}
impl From<argon2::Error> for Error {
fn from(e: argon2::Error) -> Error {
Error::Argon2(e)

69
src/network.rs Normal file
View File

@ -0,0 +1,69 @@
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;
use crate::actor::Actor;
use crate::initiator::Initiator;
use crate::db::machine::MachineState;
use crate::error::Result;
type MachineMap = HashMap<String, Machine>;
type ActorMap = HashMap<String, mpsc::Sender<Option<MutableSignalCloned<MachineState>>>>;
type InitMap = HashMap<String, Mutable<Option<Machine>>>;
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 {
machines: MachineMap,
actors: ActorMap,
inits: InitMap,
}
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)?;
init.set(machine);
}
pub fn connect_actor(&self, machine_key: &String, actor_key: &String) -> Result<()> {
let machine = self.machines.get(machine_key)
.ok_or(Error::NoSuchMachine)?;
let actor = self.actors.get(actor_key)
.ok_or(Error::NoSuchActor)?;
actor.try_send(Some(machine.signal())).map_err(|_| Error::NoSuchActor.into())
}
}