diff --git a/Cargo.toml b/Cargo.toml index f21df190776bc49095b6203c431a0b262b40f684..f5fcc33d9d5fdd984de14a02734d8a3e029b9f7a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,8 +16,6 @@ derive_more = "0.99.2" bincode = "1.2.1" serde = { version = "1.0.114", features = ["derive"] } getrandom = "0.1.14" # tiny crate. used to guess controller-id -# take_mut = "0.2.2" -indexmap = "1.4.0" # hashsets/hashmaps with efficient arbitrary element removal # network # integer-encoding = "1.1.5" diff --git a/cbindgen.toml b/cbindgen.toml index 23edeea1b5251eccf26a666ddbe35476131891e6..a0b7d22bdec276b7ec5fc74549df2f462dd98baf 100644 --- a/cbindgen.toml +++ b/cbindgen.toml @@ -2,4 +2,6 @@ language = "C" header = "/* CBindgen generated */" include_guard = "REOWOLF_HEADER_DEFINED" [parse] -parse_deps = false \ No newline at end of file +parse_deps = false +[enum] +prefix_with_name = true \ No newline at end of file diff --git a/examples/8_net_ports/amy.c b/examples/8_net_ports/amy.c index 81571438d8f21a32035554364b1c76f15f1daa86..e84cb43bd52576dc19753ef31f427b76c0250e51 100644 --- a/examples/8_net_ports/amy.c +++ b/examples/8_net_ports/amy.c @@ -12,11 +12,11 @@ int main(int argc, char** argv) { PortId putter, getter; char addr_str[] = "127.0.0.1:8000"; connector_add_net_port( - c, &putter, addr_str, sizeof(addr_str)-1, Putter, Active); + c, &putter, addr_str, sizeof(addr_str)-1, Polarity_Putter, EndpointPolarity_Active); printf("Error str `%s`\n", reowolf_error_peek(NULL)); connector_add_net_port( - c, &getter, addr_str, sizeof(addr_str)-1, Getter, Passive); + c, &getter, addr_str, sizeof(addr_str)-1, Polarity_Getter, EndpointPolarity_Passive); printf("Error str `%s`\n", reowolf_error_peek(NULL)); connector_connect(c, 4000); diff --git a/examples/9_net_self_putget/amy.c b/examples/9_net_self_putget/amy.c index 1650e373ac2a352a599ec597109c80465d663a29..ad7bc4af2d7244a8a062660d04399319eb193973 100644 --- a/examples/9_net_self_putget/amy.c +++ b/examples/9_net_self_putget/amy.c @@ -12,10 +12,10 @@ int main(int argc, char** argv) { PortId putter, getter; char addr_str[] = "127.0.0.1:8000"; connector_add_net_port( - c, &putter, addr_str, sizeof(addr_str)-1, Putter, Active); + c, &putter, addr_str, sizeof(addr_str)-1, Polarity_Putter, EndpointPolarity_Active); printf("Error str `%s`\n", reowolf_error_peek(NULL)); connector_add_net_port( - c, &getter, addr_str, sizeof(addr_str)-1, Getter, Passive); + c, &getter, addr_str, sizeof(addr_str)-1, Polarity_Getter, EndpointPolarity_Passive); printf("Error str `%s`\n", reowolf_error_peek(NULL)); connector_connect(c, 4000); diff --git a/examples/a_swap/amy.c b/examples/a_swap/amy.c index 80e26de93f9e6c7854e3abd83c3f3d7aed83a8b2..a409c74f4075c67fe413b6baa78e7831ee181b5c 100644 --- a/examples/a_swap/amy.c +++ b/examples/a_swap/amy.c @@ -18,9 +18,9 @@ int main(int argc, char** argv) { PortId ports[6]; connector_add_port_pair(c, &ports[0], &ports[1]); printf("Error str `%s`\n", reowolf_error_peek(NULL)); - connector_add_net_port(c, &ports[2], argv[1], strlen(argv[1]), Getter, Passive); + connector_add_net_port(c, &ports[2], argv[1], strlen(argv[1]), Polarity_Getter, EndpointPolarity_Passive); printf("Error str `%s`\n", reowolf_error_peek(NULL)); - connector_add_net_port(c, &ports[3], argv[2], strlen(argv[2]), Putter, Active); + connector_add_net_port(c, &ports[3], argv[2], strlen(argv[2]), Polarity_Putter, EndpointPolarity_Active); printf("Error str `%s`\n", reowolf_error_peek(NULL)); connector_add_port_pair(c, &ports[4], &ports[5]); printf("Error str `%s`\n", reowolf_error_peek(NULL)); diff --git a/examples/a_swap/bob.c b/examples/a_swap/bob.c index f3c1981c596c4bc66b01c90cc97543513983efd1..35658d2b917f8ebf9b5471f5068c7d576b627266 100644 --- a/examples/a_swap/bob.c +++ b/examples/a_swap/bob.c @@ -18,9 +18,9 @@ int main(int argc, char** argv) { PortId ports[6]; connector_add_port_pair(c, &ports[0], &ports[1]); printf("Error str `%s`\n", reowolf_error_peek(NULL)); - connector_add_net_port(c, &ports[2], argv[1], strlen(argv[1]), Getter, Passive); + connector_add_net_port(c, &ports[2], argv[1], strlen(argv[1]), Polarity_Getter, EndpointPolarity_Passive); printf("Error str `%s`\n", reowolf_error_peek(NULL)); - connector_add_net_port(c, &ports[3], argv[2], strlen(argv[2]), Putter, Active); + connector_add_net_port(c, &ports[3], argv[2], strlen(argv[2]), Polarity_Putter, EndpointPolarity_Active); printf("Error str `%s`\n", reowolf_error_peek(NULL)); connector_add_port_pair(c, &ports[4], &ports[5]); printf("Error str `%s`\n", reowolf_error_peek(NULL)); diff --git a/reowolf.h b/reowolf.h index 192f2ab1b1bb9f3e888527797bc7cf184a2c57d5..d2a3ecb4c1a8b8289da4b0587179c918206f40a2 100644 --- a/reowolf.h +++ b/reowolf.h @@ -9,13 +9,13 @@ #include typedef enum { - Active, - Passive, + EndpointPolarity_Active, + EndpointPolarity_Passive, } EndpointPolarity; typedef enum { - Putter, - Getter, + Polarity_Putter, + Polarity_Getter, } Polarity; typedef struct Arc_ProtocolDescription Arc_ProtocolDescription; diff --git a/src/common.rs b/src/common.rs index 85346ae37b405be4238dd9d05c14eec6f8b91045..69871d09bb78f4658fcb7651b153150e6d4641cd 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1,36 +1,38 @@ ///////////////////// PRELUDE ///////////////////// pub(crate) use crate::protocol::{ComponentState, ProtocolDescription}; -pub(crate) use crate::runtime::{NonsyncProtoContext, SyncProtoContext}; +pub(crate) use crate::runtime::{error::AddComponentError, NonsyncProtoContext, SyncProtoContext}; -pub use core::{ +pub(crate) use core::{ cmp::Ordering, fmt::{Debug, Formatter}, - hash::{Hash, Hasher}, - ops::{Range, RangeFrom}, + hash::Hash, + ops::Range, time::Duration, }; -pub use indexmap::{IndexMap, IndexSet}; -pub use maplit::{hashmap, hashset}; -pub use mio::{ +// pub(crate) use indexmap::IndexSet; +pub(crate) use maplit::hashmap; +pub(crate) use mio::{ net::{TcpListener, TcpStream}, Events, Interest, Poll, Token, }; -pub use std::{ - collections::{hash_map::Entry, BTreeMap, HashMap, HashSet}, +pub(crate) use std::{ + collections::{BTreeMap, HashMap, HashSet}, convert::TryInto, io::{Read, Write}, net::SocketAddr, sync::Arc, time::Instant, }; -pub use Polarity::*; - -///////////////////// DEFS ///////////////////// +pub(crate) use Polarity::*; pub type ConnectorId = u32; pub type PortSuffix = u32; - +#[derive( + Copy, Clone, Eq, PartialEq, Ord, Hash, PartialOrd, serde::Serialize, serde::Deserialize, +)] +// acquired via error in the Rust API +pub struct ProtoComponentId(Id); #[derive( Copy, Clone, Eq, PartialEq, Ord, Hash, PartialOrd, serde::Serialize, serde::Deserialize, )] @@ -39,31 +41,17 @@ pub struct Id { pub(crate) connector_id: ConnectorId, pub(crate) u32_suffix: PortSuffix, } - #[derive(Debug, Default)] pub struct U32Stream { next: u32, } - -// globally unique #[derive( Copy, Clone, Eq, PartialEq, Ord, Hash, PartialOrd, serde::Serialize, serde::Deserialize, )] #[repr(transparent)] pub struct PortId(Id); -#[derive( - Copy, Clone, Eq, PartialEq, Ord, Hash, PartialOrd, serde::Serialize, serde::Deserialize, -)] -pub struct FiringVar(pub(crate) PortId); -#[derive( - Copy, Clone, Eq, PartialEq, Ord, Hash, PartialOrd, serde::Serialize, serde::Deserialize, -)] -pub struct ProtoComponentId(Id); - -#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)] -// #[repr(C)] +#[derive(Default, Debug, Clone, Eq, PartialEq, Ord, PartialOrd)] pub struct Payload(Arc>); - #[derive( Debug, Eq, PartialEq, Clone, Hash, Copy, Ord, PartialOrd, serde::Serialize, serde::Deserialize, )] @@ -80,27 +68,19 @@ pub enum EndpointPolarity { Active, // calls connect() Passive, // calls bind() listen() accept() } - -#[derive(Eq, PartialEq, Copy, Clone, Debug)] -pub enum AddComponentError { - NoSuchComponent, - NonPortTypeParameters, - CannotMovePort(PortId), - WrongNumberOfParamaters { expected: usize }, - UnknownPort(PortId), - WrongPortPolarity { port: PortId, expected_polarity: Polarity }, - DuplicateMovedPort(PortId), -} +#[derive( + Copy, Clone, Eq, PartialEq, Ord, Hash, PartialOrd, serde::Serialize, serde::Deserialize, +)] +pub(crate) struct FiringVar(pub(crate) PortId); #[derive(Debug, Clone)] -pub enum NonsyncBlocker { +pub(crate) enum NonsyncBlocker { Inconsistent, ComponentExit, SyncBlockStart, } - #[derive(Debug, Clone)] -pub enum SyncBlocker { +pub(crate) enum SyncBlocker { Inconsistent, SyncBlockEnd, CouldntReadMsg(PortId), @@ -110,7 +90,7 @@ pub enum SyncBlocker { ///////////////////// IMPL ///////////////////// impl U32Stream { - pub fn next(&mut self) -> u32 { + pub(crate) fn next(&mut self) -> u32 { if self.next == u32::MAX { panic!("NO NEXT!") } @@ -150,7 +130,7 @@ impl Payload { pub fn as_mut_slice(&mut self) -> &mut [u8] { Arc::make_mut(&mut self.0) as _ } - pub fn concat_with(&mut self, other: &Self) { + pub fn concatenate_with(&mut self, other: &Self) { let bytes = other.as_slice().iter().copied(); let me = Arc::make_mut(&mut self.0); me.extend(bytes); @@ -174,11 +154,6 @@ impl<'de> serde::Deserialize<'de> for Payload { Ok(Self(Arc::new(inner))) } } -impl std::iter::FromIterator for Payload { - fn from_iter>(it: I) -> Self { - Self(Arc::new(it.into_iter().collect())) - } -} impl From> for Payload { fn from(s: Vec) -> Self { Self(s.into()) diff --git a/src/runtime/error.rs b/src/runtime/error.rs index e9392fb9ce6521357cbaab2f40e6936a99a7c9b7..26e5792541c411071f4b4a98e6907138b9d23391 100644 --- a/src/runtime/error.rs +++ b/src/runtime/error.rs @@ -12,6 +12,16 @@ pub enum ConnectError { EndpointSetupError(SocketAddr, EndpointError), SetupAlgMisbehavior, } +#[derive(Eq, PartialEq, Copy, Clone, Debug)] +pub enum AddComponentError { + NoSuchComponent, + NonPortTypeParameters, + CannotMovePort(PortId), + WrongNumberOfParamaters { expected: usize }, + UnknownPort(PortId), + WrongPortPolarity { port: PortId, expected_polarity: Polarity }, + DuplicateMovedPort(PortId), +} //////////////////////// #[derive(Debug, Clone)] pub enum SyncError { diff --git a/src/runtime/mod.rs b/src/runtime/mod.rs index 8dada839b474f0c9d86b257788eeab8e4f63cd5b..bdf623dd3a781958a81d5b155db871e15205197c 100644 --- a/src/runtime/mod.rs +++ b/src/runtime/mod.rs @@ -1,7 +1,11 @@ +/// cbindgen:ignore mod communication; +/// cbindgen:ignore mod endpoints; pub mod error; +/// cbindgen:ignore mod logging; +/// cbindgen:ignore mod setup; #[cfg(feature = "ffi")] @@ -46,6 +50,7 @@ struct RoundOk { batch_index: usize, gotten: HashMap, } +#[derive(Default)] struct VecSet { // invariant: ordered, deduplicated vec: Vec, @@ -153,7 +158,7 @@ struct EndpointManager { // 2. Events is empty poll: Poll, events: Events, - polled_undrained: IndexSet, + polled_undrained: VecSet, delayed_messages: Vec<(usize, Msg)>, undelayed_messages: Vec<(usize, Msg)>, endpoint_exts: Vec, @@ -209,18 +214,21 @@ impl VecSet { fn contains(&self, element: &T) -> bool { self.vec.binary_search(element).is_ok() } - // fn insert(&mut self, element: T) -> bool { - // match self.vec.binary_search(&element) { - // Ok(_) => false, - // Err(index) => { - // self.vec.insert(index, element); - // true - // } - // } - // } + fn insert(&mut self, element: T) -> bool { + match self.vec.binary_search(&element) { + Ok(_) => false, + Err(index) => { + self.vec.insert(index, element); + true + } + } + } fn iter(&self) -> std::slice::Iter { self.vec.iter() } + fn pop(&mut self) -> Option { + self.vec.pop() + } } impl PortInfo { fn firing_var_for(&self, port: PortId) -> FiringVar { diff --git a/src/runtime/setup.rs b/src/runtime/setup.rs index 5850bb4a2cddd79d5c801ad2946d04cffb98b3bd..04f33bc2d60af4ee5dc01ad38c9684e01f929f94 100644 --- a/src/runtime/setup.rs +++ b/src/runtime/setup.rs @@ -152,7 +152,7 @@ fn new_endpoint_manager( let mut waker_continue_signal: Option> = None; let mut poll = Poll::new().map_err(|_| PollInitFailed)?; let mut events = Events::with_capacity(endpoint_setups.len() * 2 + 4); - let mut polled_undrained = IndexSet::default(); + let mut polled_undrained = VecSet::default(); let mut delayed_messages = vec![]; // 2. create a registered (TcpListener/Endpoint) for passive / active respectively