From cc23d3cb40d35f082758a568f05956bf00b18bdd 2020-02-05 17:54:35 From: Christopher Esterhuyse Date: 2020-02-05 17:54:35 Subject: [PATCH] natives working --- diff --git a/src/runtime/connector.rs b/src/runtime/connector.rs index 52acf3b112aceaac4c5b9aaf725cfdc56c5dd26d..9ccde0df5d4ac99b2743ce0b7ed05d2bd35c598d 100644 --- a/src/runtime/connector.rs +++ b/src/runtime/connector.rs @@ -21,7 +21,7 @@ impl Default for Connector { } impl Connector { /// Configure the Connector with the given Pdl description. - pub fn configure(&mut self, pdl: &[u8]) -> Result<(), ConfigErr> { + pub fn configure(&mut self, pdl: &[u8], main_component: &[u8]) -> Result<(), ConfigErr> { use ConfigErr::*; let controller_id = match self { Connector::Configured(_) => return Err(AlreadyConfigured), @@ -29,8 +29,13 @@ impl Connector { Connector::Unconfigured(Unconfigured { controller_id }) => *controller_id, }; let protocol_description = Arc::new(ProtocolD::parse(pdl).map_err(ParseErr)?); - let configured = - Configured { controller_id, protocol_description, bindings: Default::default() }; + let polarities = protocol_description.component_polarities(main_component)?; + let configured = Configured { + controller_id, + protocol_description, + bindings: Default::default(), + polarities, + }; *self = Connector::Configured(configured); Ok(()) } @@ -46,6 +51,9 @@ impl Connector { Connector::Unconfigured { .. } => Err(NotConfigured), Connector::Connected(_) => Err(AlreadyConnected), Connector::Configured(configured) => { + if configured.polarities.len() <= proto_port_index { + return Err(IndexOutOfBounds); + } configured.bindings.insert(proto_port_index, binding); Ok(()) } @@ -60,14 +68,7 @@ impl Connector { Connector::Configured(configured) => configured, }; // 1. Unwrap bindings or err - let mut bindings_vec = Vec::with_capacity(configured.bindings.len()); - for native_index in 0..configured.bindings.len() { - let binding = - configured.bindings.get(&native_index).ok_or(PortNotBound { native_index })?; - bindings_vec.push(*binding); - } - let bound_proto_interface: Vec<(_, _)> = (0..num_bindings) - .map(|i| configured.bindings.get()) + let bound_proto_interface: Vec<(_, _)> = configured .proto_maybe_bindings .iter() .copied() diff --git a/src/runtime/errors.rs b/src/runtime/errors.rs index 474d535f152ef2bd8af1b0ad974a06d4071df254..3c3c228f73efad91deaba0cccfb9b30f7b64fe2c 100644 --- a/src/runtime/errors.rs +++ b/src/runtime/errors.rs @@ -28,6 +28,8 @@ pub enum ConfigErr { AlreadyConnected, ParseErr(String), AlreadyConfigured, + NoSuchComponent, + NonPortTypeParameters, } #[derive(Debug, Clone)] pub enum ConnectErr { @@ -79,3 +81,13 @@ pub enum MessengerRecvErr { PollingFailed, EndpointErr(EndpointErr), } +impl From for ConfigErr { + fn from(e: MainComponentErr) -> Self { + use ConfigErr as C; + use MainComponentErr as M; + match e { + M::NoSuchComponent => C::NoSuchComponent, + M::NonPortTypeParameters => C::NonPortTypeParameters, + } + } +} diff --git a/src/runtime/ffi.rs b/src/runtime/ffi.rs index 39dd2eb5dfbac74e050db218eaa4be8e52d6ab78..0bb643d3c31ba952769cadbf55b33d1376adc816 100644 --- a/src/runtime/ffi.rs +++ b/src/runtime/ffi.rs @@ -110,14 +110,20 @@ pub extern "C" fn connector_with_controller_id(controller_id: ControllerId) -> * /// # Safety /// TODO #[no_mangle] -pub unsafe extern "C" fn connector_configure(connector: *mut Connector, pdl: *mut c_char) -> c_int { +pub unsafe extern "C" fn connector_configure( + connector: *mut Connector, + pdl: *mut c_char, + main: *mut c_char, +) -> c_int { let mut b = Box::from_raw(connector); // unsafe! - let ret = as_rust_bytes(pdl, |pdl_bytes| match b.configure(pdl_bytes) { - Ok(()) => 0, - Err(e) => { - overwrite_last_error(format!("{:?}", e).as_bytes()); - -1 - } + let ret = as_rust_bytes(pdl, |pdl_bytes| { + as_rust_bytes(main, |main_bytes| match b.configure(pdl_bytes, main_bytes) { + Ok(()) => 0, + Err(e) => { + overwrite_last_error(format!("{:?}", e).as_bytes()); + -1 + } + }) }); Box::into_raw(b); // don't drop! ret diff --git a/src/runtime/mod.rs b/src/runtime/mod.rs index ba3908e2bf87d684844ef0044ec1374b6eec2f34..31931fcd10ed9e83743ac4710783bd80251d8d45 100644 --- a/src/runtime/mod.rs +++ b/src/runtime/mod.rs @@ -51,6 +51,7 @@ pub struct Unconfigured { #[derive(Debug)] pub struct Configured { controller_id: ControllerId, + polarities: Vec, bindings: HashMap, protocol_description: Arc, } diff --git a/src/test/connector.rs b/src/test/connector.rs index c328ec417f8cbaa41aba411d0a466cd41c9136aa..27dfb30d9c2452278900d666875116f08cbd5b24 100644 --- a/src/test/connector.rs +++ b/src/test/connector.rs @@ -23,6 +23,7 @@ fn next_addr() -> SocketAddr { fn incremental() { let timeout = Duration::from_millis(1_500); let addrs = [next_addr(), next_addr()]; + static PDL: &[u8] = b""; let handles = vec![ thread::spawn(move || { let controller_id = 0; @@ -34,6 +35,7 @@ fn incremental() { put(a, m); } }", + b"main", ) .unwrap(); x.bind_port(0, Passive(addrs[0])).unwrap(); @@ -51,6 +53,7 @@ fn incremental() { get(a); } }", + b"main", ) .unwrap(); x.bind_port(0, Active(addrs[0])).unwrap(); @@ -85,6 +88,7 @@ fn duo_positive() { put(b, m); } }", + b"main", ) .unwrap(); x.bind_port(0, Passive(addrs[0])).unwrap(); @@ -114,6 +118,7 @@ fn duo_positive() { } } }", + b"main", ) .unwrap(); x.bind_port(0, Active(addrs[0])).unwrap(); @@ -144,6 +149,7 @@ fn duo_negative() { put(a, m); // fires a on second round } }", + b"main", ) .unwrap(); x.bind_port(0, Passive(addrs[0])).unwrap(); @@ -175,6 +181,7 @@ fn duo_negative() { } } }", + b"main", ) .unwrap(); x.bind_port(0, Active(addrs[0])).unwrap(); @@ -202,14 +209,14 @@ fn connect_natives() { let addrs = [next_addr()]; do_all(&[ &|x| { - x.configure(CHAIN).unwrap(); + x.configure(CHAIN, b"main").unwrap(); x.bind_port(0, Native).unwrap(); x.bind_port(1, Passive(addrs[0])).unwrap(); x.connect(timeout).unwrap(); assert_eq!(0, x.sync(timeout).unwrap()); }, &|x| { - x.configure(CHAIN).unwrap(); + x.configure(CHAIN, b"main").unwrap(); x.bind_port(0, Active(addrs[0])).unwrap(); x.bind_port(1, Native).unwrap(); x.connect(timeout).unwrap(); @@ -231,7 +238,7 @@ fn forward() { do_all(&[ // &|x| { - x.configure(FORWARD).unwrap(); + x.configure(FORWARD, b"main").unwrap(); x.bind_port(0, Native).unwrap(); x.bind_port(1, Passive(addrs[0])).unwrap(); x.connect(timeout).unwrap(); @@ -241,7 +248,7 @@ fn forward() { assert_eq!(0, x.sync(timeout).unwrap()); }, &|x| { - x.configure(FORWARD).unwrap(); + x.configure(FORWARD, b"main").unwrap(); x.bind_port(0, Active(addrs[0])).unwrap(); x.bind_port(1, Native).unwrap(); x.connect(timeout).unwrap();