diff --git a/src/runtime/mod.rs b/src/runtime/mod.rs index 77868fab50213f7e4d0a8a045b5154c4cb1c63c2..2110ddbcd370ba2376e749d43862ec156e05962d 100644 --- a/src/runtime/mod.rs +++ b/src/runtime/mod.rs @@ -14,41 +14,70 @@ use crate::common::*; use error::*; #[derive(Debug)] -pub struct RoundOk { +#[repr(C)] +pub struct Connector { + unphased: ConnectorUnphased, + phased: ConnectorPhased, +} +pub trait Logger: Debug { + fn line_writer(&mut self) -> &mut dyn std::io::Write; +} +#[derive(Debug)] +pub struct VecLogger(ConnectorId, Vec); +#[derive(Debug)] +pub struct DummyLogger; +#[derive(Debug)] +pub struct FileLogger(ConnectorId, std::fs::File); +pub(crate) struct NonsyncProtoContext<'a> { + logger: &'a mut dyn Logger, + proto_component_id: ProtoComponentId, + port_info: &'a mut PortInfo, + id_manager: &'a mut IdManager, + proto_component_ports: &'a mut HashSet, + unrun_components: &'a mut Vec<(ProtoComponentId, ProtoComponent)>, +} +pub(crate) struct SyncProtoContext<'a> { + logger: &'a mut dyn Logger, + predicate: &'a Predicate, + port_info: &'a PortInfo, + inbox: &'a HashMap, +} +#[derive(Debug)] +struct RoundOk { batch_index: usize, gotten: HashMap, } -pub struct VecSet { +struct VecSet { // invariant: ordered, deduplicated vec: Vec, } #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)] -pub enum ComponentId { +enum ComponentId { Native, Proto(ProtoComponentId), } #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)] -pub enum Route { +enum Route { LocalComponent(ComponentId), Endpoint { index: usize }, } #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] -pub struct MyPortInfo { +struct MyPortInfo { polarity: Polarity, port: PortId, } #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] -pub enum Decision { +enum Decision { Failure, Success(Predicate), } #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] -pub enum Msg { +enum Msg { SetupMsg(SetupMsg), CommMsg(CommMsg), } #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] -pub enum SetupMsg { +enum SetupMsg { MyPortInfo(MyPortInfo), LeaderWave { wave_leader: ConnectorId }, LeaderAnnounce { tree_leader: ConnectorId }, @@ -56,85 +85,70 @@ pub enum SetupMsg { SessionGather { unoptimized_map: HashMap }, SessionScatter { optimized_map: HashMap }, } -#[derive(Debug, Clone)] -pub(crate) struct SerdeProtocolDescription(Arc); #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] -pub struct SessionInfo { +struct SessionInfo { serde_proto_description: SerdeProtocolDescription, port_info: PortInfo, proto_components: HashMap, } - +#[derive(Debug, Clone)] +struct SerdeProtocolDescription(Arc); #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] -pub struct CommMsg { - pub round_index: usize, - pub contents: CommMsgContents, +struct CommMsg { + round_index: usize, + contents: CommMsgContents, } #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] -pub enum CommMsgContents { +enum CommMsgContents { SendPayload(SendPayloadMsg), Suggest { suggestion: Decision }, // SINKWARD Announce { decision: Decision }, // SINKAWAYS } #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] -pub struct SendPayloadMsg { +struct SendPayloadMsg { predicate: Predicate, payload: Payload, } #[derive(Debug, PartialEq)] -pub enum CommonSatResult { +enum CommonSatResult { FormerNotLatter, LatterNotFormer, Equivalent, New(Predicate), Nonexistant, } -pub struct Endpoint { +struct Endpoint { inbox: Vec, stream: TcpStream, } #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] -pub struct ProtoComponent { +struct ProtoComponent { state: ComponentState, ports: HashSet, } -pub trait Logger: Debug { - fn line_writer(&mut self) -> &mut dyn std::io::Write; -} -#[derive(Debug)] -pub struct VecLogger(ConnectorId, Vec); -#[derive(Debug)] -pub struct DummyLogger; -#[derive(Debug)] -pub struct FileLogger(ConnectorId, std::fs::File); #[derive(Debug, Clone)] -pub struct EndpointSetup { - pub sock_addr: SocketAddr, - pub endpoint_polarity: EndpointPolarity, +struct EndpointSetup { + sock_addr: SocketAddr, + endpoint_polarity: EndpointPolarity, } #[derive(Debug)] -pub struct EndpointExt { +struct EndpointExt { endpoint: Endpoint, getter_for_incoming: PortId, } #[derive(Debug)] -pub struct Neighborhood { +struct Neighborhood { parent: Option, children: VecSet, } #[derive(Debug)] -pub struct MemInMsg { - inp: PortId, - msg: Payload, -} -#[derive(Debug)] -pub struct IdManager { +struct IdManager { connector_id: ConnectorId, port_suffix_stream: U32Stream, proto_component_suffix_stream: U32Stream, } #[derive(Debug)] -pub struct EndpointManager { +struct EndpointManager { // invariants: // 1. endpoint N is registered READ | WRITE with poller // 2. Events is empty @@ -146,28 +160,21 @@ pub struct EndpointManager { endpoint_exts: Vec, } #[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize)] -pub struct PortInfo { +struct PortInfo { polarities: HashMap, peers: HashMap, routes: HashMap, } #[derive(Debug)] -// #[repr(C)] -pub struct Connector { - unphased: ConnectorUnphased, - phased: ConnectorPhased, -} -#[derive(Debug)] -pub struct ConnectorCommunication { +struct ConnectorCommunication { round_index: usize, endpoint_manager: EndpointManager, neighborhood: Neighborhood, - mem_inbox: Vec, native_batches: Vec, round_result: Result, SyncError>, } #[derive(Debug)] -pub struct ConnectorUnphased { +struct ConnectorUnphased { proto_description: Arc, proto_components: HashMap, logger: Box, @@ -176,34 +183,20 @@ pub struct ConnectorUnphased { port_info: PortInfo, } #[derive(Debug)] -pub enum ConnectorPhased { +enum ConnectorPhased { Setup { endpoint_setups: Vec<(PortId, EndpointSetup)>, surplus_sockets: u16 }, Communication(Box), } #[derive(Default, Clone, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)] -pub struct Predicate { - pub assigned: BTreeMap, +struct Predicate { + assigned: BTreeMap, } #[derive(Debug, Default)] -pub struct NativeBatch { +struct NativeBatch { // invariant: putters' and getters' polarities respected to_put: HashMap, to_get: HashSet, } -pub struct NonsyncProtoContext<'a> { - logger: &'a mut dyn Logger, - proto_component_id: ProtoComponentId, - port_info: &'a mut PortInfo, - id_manager: &'a mut IdManager, - proto_component_ports: &'a mut HashSet, - unrun_components: &'a mut Vec<(ProtoComponentId, ProtoComponent)>, -} -pub struct SyncProtoContext<'a> { - logger: &'a mut dyn Logger, - predicate: &'a Predicate, - port_info: &'a PortInfo, - inbox: &'a HashMap, -} //////////////// pub fn would_block(err: &std::io::Error) -> bool { err.kind() == std::io::ErrorKind::WouldBlock @@ -370,7 +363,7 @@ impl Predicate { /// If the resulting predicate is equivlanet to self, other, or both, /// FormerNotLatter, LatterNotFormer and Equivalent are returned respectively. /// otherwise New(N) is returned. - pub fn common_satisfier(&self, other: &Self) -> CommonSatResult { + fn common_satisfier(&self, other: &Self) -> CommonSatResult { use CommonSatResult as Csr; // iterators over assignments of both predicates. Rely on SORTED ordering of BTreeMap's keys. let [mut s_it, mut o_it] = [self.assigned.iter(), other.assigned.iter()];