From d0005cdd63f1fd5c01b94dceadb1d99815090a62 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 13 Mar 2024 10:36:08 +0100 Subject: [PATCH] this works --- satrs-minisim/Cargo.toml | 3 +++ satrs-minisim/src/acs.rs | 28 ++++++++++++++-------------- satrs-minisim/src/controller.rs | 12 +++++++----- satrs-minisim/src/eps.rs | 12 ++++++------ satrs-minisim/src/lib.rs | 16 +++++++++++++--- satrs-minisim/src/test_helpers.rs | 5 ++++- satrs-minisim/src/udp.rs | 10 +++++----- 7 files changed, 52 insertions(+), 34 deletions(-) diff --git a/satrs-minisim/Cargo.toml b/satrs-minisim/Cargo.toml index cb8e29c..9f8058e 100644 --- a/satrs-minisim/Cargo.toml +++ b/satrs-minisim/Cargo.toml @@ -13,6 +13,9 @@ thiserror = "1" [dependencies.asynchronix] version = "0.2.1" +git = "https://github.com/asynchronics/asynchronix.git" +branch = "main" +features = ["serde"] [dependencies.satrs] path = "../satrs" diff --git a/satrs-minisim/src/acs.rs b/satrs-minisim/src/acs.rs index 141840d..77b8cb0 100644 --- a/satrs-minisim/src/acs.rs +++ b/satrs-minisim/src/acs.rs @@ -189,11 +189,11 @@ pub mod tests { #[test] fn test_basic_mgm_request() { let mut sim_testbench = SimTestbench::new(); - let request = SimRequest::new(MgmRequest::RequestSensorData); + let request = SimRequest::new_with_epoch_time(MgmRequest::RequestSensorData); sim_testbench .send_request(request) .expect("sending MGM request failed"); - sim_testbench.handle_sim_requests(); + sim_testbench.handle_sim_requests_time_agnostic(); sim_testbench.step(); let sim_reply = sim_testbench.try_receive_next_reply(); assert!(sim_reply.is_some()); @@ -212,11 +212,11 @@ pub mod tests { let mut sim_testbench = SimTestbench::new(); switch_device_on(&mut sim_testbench, PcduSwitch::Mgm); - let mut request = SimRequest::new(MgmRequest::RequestSensorData); + let mut request = SimRequest::new_with_epoch_time(MgmRequest::RequestSensorData); sim_testbench .send_request(request) .expect("sending MGM request failed"); - sim_testbench.handle_sim_requests(); + sim_testbench.handle_sim_requests_time_agnostic(); sim_testbench.step(); let mut sim_reply_res = sim_testbench.try_receive_next_reply(); assert!(sim_reply_res.is_some()); @@ -226,11 +226,11 @@ pub mod tests { .expect("failed to deserialize MGM sensor values"); sim_testbench.step_by(Duration::from_millis(50)); - request = SimRequest::new(MgmRequest::RequestSensorData); + request = SimRequest::new_with_epoch_time(MgmRequest::RequestSensorData); sim_testbench .send_request(request) .expect("sending MGM request failed"); - sim_testbench.handle_sim_requests(); + sim_testbench.handle_sim_requests_time_agnostic(); sim_testbench.step(); sim_reply_res = sim_testbench.try_receive_next_reply(); assert!(sim_reply_res.is_some()); @@ -245,11 +245,11 @@ pub mod tests { #[test] fn test_basic_mgt_request_is_off() { let mut sim_testbench = SimTestbench::new(); - let request = SimRequest::new(MgtRequest::RequestHk); + let request = SimRequest::new_with_epoch_time(MgtRequest::RequestHk); sim_testbench .send_request(request) .expect("sending MGM request failed"); - sim_testbench.handle_sim_requests(); + sim_testbench.handle_sim_requests_time_agnostic(); sim_testbench.step(); let sim_reply_res = sim_testbench.try_receive_next_reply(); assert!(sim_reply_res.is_none()); @@ -259,12 +259,12 @@ pub mod tests { fn test_basic_mgt_request_is_on() { let mut sim_testbench = SimTestbench::new(); switch_device_on(&mut sim_testbench, PcduSwitch::Mgt); - let request = SimRequest::new(MgtRequest::RequestHk); + let request = SimRequest::new_with_epoch_time(MgtRequest::RequestHk); sim_testbench .send_request(request) .expect("sending MGM request failed"); - sim_testbench.handle_sim_requests(); + sim_testbench.handle_sim_requests_time_agnostic(); sim_testbench.step(); let sim_reply_res = sim_testbench.try_receive_next_reply(); assert!(sim_reply_res.is_some()); @@ -281,11 +281,11 @@ pub mod tests { } fn check_mgt_hk(sim_testbench: &mut SimTestbench, expected_hk_set: MgtHkSet) { - let request = SimRequest::new(MgtRequest::RequestHk); + let request = SimRequest::new_with_epoch_time(MgtRequest::RequestHk); sim_testbench .send_request(request) .expect("sending MGM request failed"); - sim_testbench.handle_sim_requests(); + sim_testbench.handle_sim_requests_time_agnostic(); sim_testbench.step(); let sim_reply_res = sim_testbench.try_receive_next_reply(); assert!(sim_reply_res.is_some()); @@ -309,14 +309,14 @@ pub mod tests { y: 200, z: 1000, }; - let request = SimRequest::new(MgtRequest::ApplyTorque { + let request = SimRequest::new_with_epoch_time(MgtRequest::ApplyTorque { duration: Duration::from_millis(100), dipole: commanded_dipole, }); sim_testbench .send_request(request) .expect("sending MGM request failed"); - sim_testbench.handle_sim_requests(); + sim_testbench.handle_sim_requests_time_agnostic(); sim_testbench.step_by(Duration::from_millis(5)); check_mgt_hk( diff --git a/satrs-minisim/src/controller.rs b/satrs-minisim/src/controller.rs index ba0fe78..9255932 100644 --- a/satrs-minisim/src/controller.rs +++ b/satrs-minisim/src/controller.rs @@ -56,18 +56,20 @@ impl SimController { // replies lying in the past. t += Duration::from_millis(udp_polling_interval_ms); self.sys_clock.synchronize(t); - self.handle_sim_requests(); + self.handle_sim_requests(t_old); self.simulation .step_until(t) .expect("simulation step failed"); - } } - pub fn handle_sim_requests(&mut self) { + pub fn handle_sim_requests(&mut self, old_timestamp: MonotonicTime) { loop { match self.request_receiver.try_recv() { Ok(request) => { + if request.timestamp < old_timestamp { + log::warn!("stale data with timestamp {:?} received", request.timestamp); + } if let Err(e) = match request.target() { SimTarget::SimCtrl => self.handle_ctrl_request(&request), SimTarget::Mgm => self.handle_mgm_request(&request), @@ -172,11 +174,11 @@ mod tests { #[test] fn test_basic_ping() { let mut sim_testbench = SimTestbench::new(); - let request = SimRequest::new(SimCtrlRequest::Ping); + let request = SimRequest::new_with_epoch_time(SimCtrlRequest::Ping); sim_testbench .send_request(request) .expect("sending sim ctrl request failed"); - sim_testbench.handle_sim_requests(); + sim_testbench.handle_sim_requests_time_agnostic(); sim_testbench.step(); let sim_reply = sim_testbench.try_receive_next_reply(); assert!(sim_reply.is_some()); diff --git a/satrs-minisim/src/eps.rs b/satrs-minisim/src/eps.rs index 2873348..ebbeb4e 100644 --- a/satrs-minisim/src/eps.rs +++ b/satrs-minisim/src/eps.rs @@ -86,14 +86,14 @@ pub(crate) mod tests { switch: PcduSwitch, target: SwitchStateBinary, ) { - let request = SimRequest::new(PcduRequest::SwitchDevice { + let request = SimRequest::new_with_epoch_time(PcduRequest::SwitchDevice { switch, state: target, }); sim_testbench .send_request(request) .expect("sending MGM switch request failed"); - sim_testbench.handle_sim_requests(); + sim_testbench.handle_sim_requests_time_agnostic(); sim_testbench.step(); } @@ -113,11 +113,11 @@ pub(crate) mod tests { } fn check_switch_state(sim_testbench: &mut SimTestbench, expected_switch_map: &SwitchMap) { - let request = SimRequest::new(PcduRequest::RequestSwitchInfo); + let request = SimRequest::new_with_epoch_time(PcduRequest::RequestSwitchInfo); sim_testbench .send_request(request) .expect("sending MGM request failed"); - sim_testbench.handle_sim_requests(); + sim_testbench.handle_sim_requests_time_agnostic(); sim_testbench.step(); let sim_reply = sim_testbench.try_receive_next_reply(); assert!(sim_reply.is_some()); @@ -143,11 +143,11 @@ pub(crate) mod tests { #[test] fn test_pcdu_switcher_request() { let mut sim_testbench = SimTestbench::new(); - let request = SimRequest::new(PcduRequest::RequestSwitchInfo); + let request = SimRequest::new_with_epoch_time(PcduRequest::RequestSwitchInfo); sim_testbench .send_request(request) .expect("sending MGM request failed"); - sim_testbench.handle_sim_requests(); + sim_testbench.handle_sim_requests_time_agnostic(); sim_testbench.step_by(Duration::from_millis(1)); let sim_reply = sim_testbench.try_receive_next_reply(); diff --git a/satrs-minisim/src/lib.rs b/satrs-minisim/src/lib.rs index 23148b7..aa32e4a 100644 --- a/satrs-minisim/src/lib.rs +++ b/satrs-minisim/src/lib.rs @@ -20,7 +20,7 @@ pub struct SimMessage { #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct SimRequest { inner: SimMessage, - timestamp: MonotonicTime + pub timestamp: MonotonicTime, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] @@ -55,12 +55,22 @@ pub trait SimMessageProvider: Serialize + DeserializeOwned + Clone + Sized { } impl SimRequest { - pub fn new>(serializable_request: T) -> Self { + pub fn new_with_epoch_time>( + serializable_request: T, + ) -> Self { + Self::new(serializable_request, MonotonicTime::EPOCH) + } + + pub fn new>( + serializable_request: T, + timestamp: MonotonicTime, + ) -> Self { Self { inner: SimMessage { target: T::TARGET, payload: serde_json::to_string(&serializable_request).unwrap(), }, + timestamp, } } } @@ -365,7 +375,7 @@ pub mod tests { #[test] fn test_basic_request() { - let sim_request = SimRequest::new(DummyRequest::Ping); + let sim_request = SimRequest::new_with_epoch_time(DummyRequest::Ping); assert_eq!(sim_request.target(), SimTarget::SimCtrl); assert_eq!(sim_request.msg_type(), SimMessageType::Request); let dummy_request = diff --git a/satrs-minisim/src/test_helpers.rs b/satrs-minisim/src/test_helpers.rs index a8bf0a1..e3c1412 100644 --- a/satrs-minisim/src/test_helpers.rs +++ b/satrs-minisim/src/test_helpers.rs @@ -26,10 +26,13 @@ impl SimTestbench { request_sender, } } + pub fn handle_sim_requests_time_agnostic(&mut self) { + self.handle_sim_requests(MonotonicTime::EPOCH); + } delegate! { to self.sim_controller { - pub fn handle_sim_requests(&mut self); + pub fn handle_sim_requests(&mut self, old_timestamp: MonotonicTime); } to self.sim_controller.simulation { pub fn step(&mut self); diff --git a/satrs-minisim/src/udp.rs b/satrs-minisim/src/udp.rs index 36800b7..ad50672 100644 --- a/satrs-minisim/src/udp.rs +++ b/satrs-minisim/src/udp.rs @@ -270,7 +270,7 @@ mod tests { UdpTestbench::new(true, Some(SERVER_WAIT_TIME_MS), 10) .expect("could not create testbench"); let server_thread = std::thread::spawn(move || udp_server.run()); - let sim_request = SimRequest::new(PcduRequest::RequestSwitchInfo); + let sim_request = SimRequest::new_with_epoch_time(PcduRequest::RequestSwitchInfo); udp_testbench .send_request(&sim_request) .expect("sending request failed"); @@ -292,7 +292,7 @@ mod tests { .expect("could not create testbench"); let server_thread = std::thread::spawn(move || udp_server.run()); udp_testbench - .send_request(&SimRequest::new(SimCtrlRequest::Ping)) + .send_request(&SimRequest::new_with_epoch_time(SimCtrlRequest::Ping)) .expect("sending request failed"); let sim_reply = SimReply::new(PcduReply::SwitchInfo(get_all_off_switch_map())); @@ -316,7 +316,7 @@ mod tests { // Send a ping so that the server knows the address of the client. // Do not check that the request arrives on the receiver side, is done by other test. udp_testbench - .send_request(&SimRequest::new(SimCtrlRequest::Ping)) + .send_request(&SimRequest::new_with_epoch_time(SimCtrlRequest::Ping)) .expect("sending request failed"); // Send a reply to the server, ensure it gets forwarded to the client. @@ -347,7 +347,7 @@ mod tests { // Connect by sending a ping. udp_testbench - .send_request(&SimRequest::new(SimCtrlRequest::Ping)) + .send_request(&SimRequest::new_with_epoch_time(SimCtrlRequest::Ping)) .expect("sending request failed"); std::thread::sleep(Duration::from_millis(SERVER_WAIT_TIME_MS)); @@ -376,7 +376,7 @@ mod tests { // Connect by sending a ping. udp_testbench - .send_request(&SimRequest::new(SimCtrlRequest::Ping)) + .send_request(&SimRequest::new_with_epoch_time(SimCtrlRequest::Ping)) .expect("sending request failed"); std::thread::sleep(Duration::from_millis(SERVER_WAIT_TIME_MS));