summaryrefslogtreecommitdiff
path: root/meap/meap-code/ch5/ch5-message-store
diff options
context:
space:
mode:
Diffstat (limited to 'meap/meap-code/ch5/ch5-message-store')
-rw-r--r--meap/meap-code/ch5/ch5-message-store/Cargo.lock4
-rw-r--r--meap/meap-code/ch5/ch5-message-store/Cargo.toml6
-rw-r--r--meap/meap-code/ch5/ch5-message-store/src/main.rs93
3 files changed, 103 insertions, 0 deletions
diff --git a/meap/meap-code/ch5/ch5-message-store/Cargo.lock b/meap/meap-code/ch5/ch5-message-store/Cargo.lock
new file mode 100644
index 0000000..e5df836
--- /dev/null
+++ b/meap/meap-code/ch5/ch5-message-store/Cargo.lock
@@ -0,0 +1,4 @@
+[root]
+name = "ch5-message-store"
+version = "0.1.0"
+
diff --git a/meap/meap-code/ch5/ch5-message-store/Cargo.toml b/meap/meap-code/ch5/ch5-message-store/Cargo.toml
new file mode 100644
index 0000000..4fb4bc0
--- /dev/null
+++ b/meap/meap-code/ch5/ch5-message-store/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "ch5-message-store"
+version = "0.1.0"
+authors = ["Tim McNamara <code@timmcnamara.co.nz>"]
+
+[dependencies]
diff --git a/meap/meap-code/ch5/ch5-message-store/src/main.rs b/meap/meap-code/ch5/ch5-message-store/src/main.rs
new file mode 100644
index 0000000..e790a57
--- /dev/null
+++ b/meap/meap-code/ch5/ch5-message-store/src/main.rs
@@ -0,0 +1,93 @@
+
+#[derive(Debug)]
+struct CubeSat {
+ id: u64,
+}
+
+#[derive(Debug)]
+enum StatusMessage {
+ Ok,
+}
+
+#[derive(Debug)]
+struct Mailbox {
+ messages: Vec<Message>,
+}
+
+#[derive(Debug)]
+struct Message {
+ to: u64,
+ content: String,
+}
+
+struct GroundStation {}
+
+impl Mailbox {
+ fn post(&mut self, to: &CubeSat, msg: Message) {
+ self.messages.push(msg);
+ }
+
+ fn deliver(&mut self, recipient: &CubeSat) -> Option<Message> {
+ for i in 0..self.messages.len() {
+ if self.messages[i].to == recipient.id {
+ let msg = self.messages.remove(i);
+ return Some(msg);
+ }
+ }
+
+ None
+ }
+}
+
+impl GroundStation {
+ fn connect(&self, sat_id: u64) -> CubeSat {
+ CubeSat {
+ id: sat_id,
+ }
+ }
+
+ fn send(&self, mailbox: &mut Mailbox, to: &CubeSat, msg: Message) {
+ mailbox.post(to, msg);
+ }
+}
+
+impl CubeSat {
+ fn recv(&self, mailbox: &mut Mailbox) -> Option<Message> {
+ mailbox.deliver(&self)
+ }
+}
+
+// fn main() {
+// let base = GroundStation {};
+// let mut sat_a = CubeSat { id: 0, mailbox: Mailbox { messages: vec![] } };
+// let mut sat_b = CubeSat { id: 1, mailbox: Mailbox { messages: vec![] } };
+// let mut sat_c = CubeSat { id: 2, mailbox: Mailbox { messages: vec![] } };
+
+// }
+
+/// Returns a vector of CubeSat IDs
+fn fetch_sat_ids() -> Vec<u64> {
+ vec![1,2,3]
+}
+
+
+fn main() {
+ let mut mail = Mailbox { messages: vec![] };
+
+ let base = GroundStation {};
+
+ let sat_ids = fetch_sat_ids();
+
+ for sat_id in &sat_ids {
+ let sat = base.connect(sat_id);
+ let msg = Message { to: sat_id, content: String::from("hello") };
+ base.send(&mut mail, &sat, msg);
+ }
+
+ for sat_id in &sat_ids {
+ let sat = base.connect(sat_id);
+
+ let msg = sat.recv(&mut mail);
+ println!("{:?}: {:?}", sat, msg);
+ }
+} \ No newline at end of file