From a2f3a453dd37f0f1e6a98b88ac413a048c1a1c6b Mon Sep 17 00:00:00 2001 From: Able Date: Mon, 5 Dec 2022 10:31:18 -0600 Subject: [PATCH] messaginjg library --- Cargo.lock | 1 + libraries/messaging/Cargo.toml | 11 ++++++++ libraries/messaging/src/lib.rs | 49 ++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 libraries/messaging/Cargo.toml create mode 100644 libraries/messaging/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index a23dd5b..2a42ba4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -163,6 +163,7 @@ dependencies = [ name = "messaging" version = "0.1.0" dependencies = [ + "cryptography", "process", "versioning", ] diff --git a/libraries/messaging/Cargo.toml b/libraries/messaging/Cargo.toml new file mode 100644 index 0000000..d2225d5 --- /dev/null +++ b/libraries/messaging/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "messaging" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +versioning = { path = "../versioning" } +process = { path = "../process" } +cryptography = { path = "../cryptography" } diff --git a/libraries/messaging/src/lib.rs b/libraries/messaging/src/lib.rs new file mode 100644 index 0000000..98ccdce --- /dev/null +++ b/libraries/messaging/src/lib.rs @@ -0,0 +1,49 @@ +#![no_std] +#[macro_use] +extern crate alloc; + +use alloc::vec::Vec; +use cryptography::hashing::checksum; +use process::PID; +use versioning::Version; + +pub const VERSION: Version = Version::new(0, 1, 0); + +#[repr(C)] +pub struct Message { + src: PID, // bytes 0-3 + dest: PID, // bytes 4-7 + version: Version, // bytes 8-10 + checksum: u64, // bytes 11-19 + length: usize, // bytes 20-23 + timestamp: u64, // bytes 24-31 + + body: Vec, +} +impl Message { + pub fn set_checksum(&mut self) { + self.checksum = self.compute_body_checksum() + } + + fn compute_body_checksum(&mut self) -> u64 { + checksum(&self.body) + } +} + +#[test] +fn test_checksum() { + let mut message = Message { + src: 123, + dest: 456, + version: Version::new(0, 1, 0), + checksum: 0, + timestamp: 0, + length: 5, + body: vec![1, 2], + }; + + message.set_checksum(); + + assert_eq!(message.checksum, 3); + assert_ne!(message.checksum, 1) +}