summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2021-08-21 12:10:52 +0200
committerMatthias Beyer <mail@beyermatthias.de>2021-08-21 12:10:52 +0200
commit8677b5379e0c389c9f78a69d48c396b309936258 (patch)
tree07ed8cae71257c08b401791f6d47c0fa20723a5d
parentdf1e0b1ed4e8b0261a936598446783747efa7a4c (diff)
Add simulation-task-communication via unbounded channelsbackend
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r--src/backend/landscape.rs28
-rw-r--r--src/backend/sim.rs42
2 files changed, 50 insertions, 20 deletions
diff --git a/src/backend/landscape.rs b/src/backend/landscape.rs
index 4916f75..aab77e9 100644
--- a/src/backend/landscape.rs
+++ b/src/backend/landscape.rs
@@ -53,28 +53,30 @@ impl Landscape {
while let Some(mut window) = windows.next() {
// we know from the ThreeElemWindowMut impl that 'window' always has three elements
- let el = window[0].element().clone();
- window[1].set_left_neighbor(Some(el));
+ let el = window[0].0.element().clone();
+ window[1].0.set_left_neighbor(el, window[0].1.clone());
- let el = window[1].element().clone();
- window[0].set_right_neighbor(Some(el));
+ let el = window[1].0.element().clone();
+ window[0].0.set_right_neighbor(el, window[1].1.clone());
- let el = window[2].element().clone();
- window[1].set_right_neighbor(Some(el));
+ let el = window[2].0.element().clone();
+ window[1].0.set_right_neighbor(el, window[2].1.clone());
- let el = window[1].element().clone();
- window[2].set_left_neighbor(Some(el));
+ let el = window[1].0.element().clone();
+ window[2].0.set_left_neighbor(el, window[1].1.clone());
}
} else if simulation_elements.len() == 2 {
log::debug!("Having only two landscape elements in the simulation, pairing them now");
- let el = simulation_elements[1].element().clone();
- simulation_elements[0].set_right_neighbor(Some(el));
+ let el = simulation_elements[1].0.element().clone();
+ let sender = simulation_elements[1].1.clone();
+ simulation_elements[0].0.set_right_neighbor(el, sender);
- let el = simulation_elements[0].element().clone();
- simulation_elements[1].set_left_neighbor(Some(el));
+ let el = simulation_elements[0].0.element().clone();
+ let sender = simulation_elements[0].1.clone();
+ simulation_elements[1].0.set_left_neighbor(el, sender);
}
- Ok(simulation_elements)
+ Ok(simulation_elements.into_iter().map(|tpl| tpl.0).collect())
}
}
diff --git a/src/backend/sim.rs b/src/backend/sim.rs
index 256cf62..1e5f797 100644
--- a/src/backend/sim.rs
+++ b/src/backend/sim.rs
@@ -6,24 +6,46 @@ use resiter::Map;
use crate::backend::landscape::LandscapeElement;
use crate::backend::rain::*;
+pub type Receiver = tokio::sync::mpsc::UnboundedReceiver<SimulationMessage>;
+pub type Sender = tokio::sync::mpsc::UnboundedSender<SimulationMessage>;
+
+
#[derive(getset::Getters, getset::Setters)]
pub struct ElementRainingSimulation {
#[getset(get = "pub")]
element: Arc<RwLock<LandscapeElement>>,
+ receiver: Receiver,
- #[getset(set = "pub")]
- left_neighbor: Option<Arc<RwLock<LandscapeElement>>>,
- #[getset(set = "pub")]
- right_neighbor: Option<Arc<RwLock<LandscapeElement>>>,
+ left_neighbor: Option<Neighbor>,
+ right_neighbor: Option<Neighbor>,
+}
+
+/// Helper struct for grouping an element and the communication channel to the simulation for the
+/// element
+struct Neighbor {
+ element: Arc<RwLock<LandscapeElement>>,
+ sender: Sender,
}
impl ElementRainingSimulation {
- pub fn new(element: Arc<RwLock<LandscapeElement>>) -> Self {
- ElementRainingSimulation {
+ pub fn new(element: Arc<RwLock<LandscapeElement>>) -> (Self, Sender) {
+ let (sender, receiver) = tokio::sync::mpsc::unbounded_channel();
+ let rsim = ElementRainingSimulation {
element,
+ receiver,
left_neighbor: None,
right_neighbor: None,
- }
+ };
+
+ (rsim, sender)
+ }
+
+ pub fn set_left_neighbor(&mut self, element: Arc<RwLock<LandscapeElement>>, sender: Sender) {
+ self.left_neighbor = Some(Neighbor { element, sender })
+ }
+
+ pub fn set_right_neighbor(&mut self, element: Arc<RwLock<LandscapeElement>>, sender: Sender) {
+ self.right_neighbor = Some(Neighbor { element, sender })
}
pub fn into_landscape_element(self) -> Arc<RwLock<LandscapeElement>> {
@@ -36,3 +58,9 @@ impl ElementRainingSimulation {
unimplemented!()
}
}
+
+/// A message for exchanging data between simulation elements
+#[derive(Debug)]
+pub enum SimulationMessage {
+}
+