diff --git a/src/common.rs b/src/common.rs index 810d1d29f9a313a7005336f337ad784916cfee61..633372524b6c50ed005f0ddf0263f79bf8b427cd 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1,5 +1,8 @@ ///////////////////// PRELUDE ///////////////////// +pub use crate::protocol::{ComponentState, ProtocolDescription}; +pub use crate::runtime::{NonsyncContext, SyncContext}; + pub use core::{ cmp::Ordering, fmt::{Debug, Formatter}, @@ -16,6 +19,7 @@ pub use mio::{ pub use std::{ collections::{hash_map::Entry, BTreeMap, HashMap, HashSet}, convert::TryInto, + io::{Read, Write}, net::SocketAddr, sync::Arc, time::Instant, @@ -25,98 +29,53 @@ pub use Polarity::*; ///////////////////// DEFS ///////////////////// pub type ControllerId = u32; -pub type ChannelIndex = u32; +pub type PortSuffix = u32; -#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, Hash, PartialOrd)] +// globally unique +#[derive( + Copy, Clone, Eq, PartialEq, Ord, Hash, PartialOrd, serde::Serialize, serde::Deserialize, +)] pub struct PortId { pub(crate) controller_id: ControllerId, - pub(crate) port_index: u32, + pub(crate) port_index: PortSuffix, } #[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)] pub struct Payload(Arc>); -/// This is a unique identifier for a channel (i.e., port). -#[derive(Debug, Eq, PartialEq, Clone, Hash, Copy, Ord, PartialOrd)] -pub struct ChannelId { - pub(crate) controller_id: ControllerId, - pub(crate) channel_index: ChannelIndex, -} - -#[derive(Debug, Eq, PartialEq, Clone, Hash, Copy, Ord, PartialOrd)] +#[derive( + Debug, Eq, PartialEq, Clone, Hash, Copy, Ord, PartialOrd, serde::Serialize, serde::Deserialize, +)] pub enum Polarity { Putter, // output port (from the perspective of the component) Getter, // input port (from the perspective of the component) } -#[derive( - Eq, PartialEq, Ord, PartialOrd, Hash, Copy, Clone, serde::Serialize, serde::Deserialize, -)] -#[repr(C)] -pub struct Port(pub u32); // ports are COPY - #[derive(Eq, PartialEq, Copy, Clone, Debug)] -pub enum MainComponentErr { +pub enum AddComponentError { NoSuchComponent, NonPortTypeParameters, - CannotMovePort(Port), + CannotMovePort(PortId), WrongNumberOfParamaters { expected: usize }, - UnknownPort(Port), - WrongPortPolarity { param_index: usize, port: Port }, - DuplicateMovedPort(Port), -} -pub trait ProtocolDescription: Sized { - type S: ComponentState; - - fn parse(pdl: &[u8]) -> Result; - fn component_polarities(&self, identifier: &[u8]) -> Result, MainComponentErr>; - fn new_main_component(&self, identifier: &[u8], ports: &[Port]) -> Self::S; -} - -pub trait ComponentState: Sized + Clone { - type D: ProtocolDescription; - fn pre_sync_run>( - &mut self, - runtime_ctx: &mut C, - protocol_description: &Self::D, - ) -> MonoBlocker; - - fn sync_run>( - &mut self, - runtime_ctx: &mut C, - protocol_description: &Self::D, - ) -> PolyBlocker; + UnknownPort(PortId), + WrongPortPolarity { port: PortId, expected_polarity: Polarity }, + DuplicateMovedPort(PortId), } #[derive(Debug, Clone)] -pub enum MonoBlocker { +pub enum NonsyncBlocker { Inconsistent, ComponentExit, SyncBlockStart, } #[derive(Debug, Clone)] -pub enum PolyBlocker { +pub enum SyncBlocker { Inconsistent, SyncBlockEnd, - CouldntReadMsg(Port), - CouldntCheckFiring(Port), - PutMsg(Port, Payload), -} - -pub trait MonoContext { - type D: ProtocolDescription; - type S: ComponentState; - - fn new_component(&mut self, moved_ports: HashSet, init_state: Self::S); - fn new_channel(&mut self) -> [Port; 2]; - fn new_random(&mut self) -> u64; -} -pub trait PolyContext { - type D: ProtocolDescription; - - fn is_firing(&mut self, port: Port) -> Option; - fn read_msg(&mut self, port: Port) -> Option<&Payload>; + CouldntReadMsg(PortId), + CouldntCheckFiring(PortId), + PutMsg(PortId, Payload), } ///////////////////// IMPL ///////////////////// @@ -176,22 +135,8 @@ impl From> for Payload { Self(s.into()) } } -impl Debug for Port { +impl Debug for PortId { fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { - write!(f, "Port({})", self.0) - } -} -impl Port { - pub fn from_raw(raw: u32) -> Self { - Self(raw) - } - pub fn to_raw(self) -> u32 { - self.0 - } - pub fn to_token(self) -> mio::Token { - mio::Token(self.0.try_into().unwrap()) - } - pub fn from_token(t: mio::Token) -> Self { - Self(t.0.try_into().unwrap()) + write!(f, "PortId({},{})", self.controller_id, self.port_index) } }