messaginjg library

This commit is contained in:
Able 2022-12-05 10:31:18 -06:00
parent 4ee4681ab7
commit a2f3a453dd
3 changed files with 61 additions and 0 deletions

1
Cargo.lock generated
View file

@ -163,6 +163,7 @@ dependencies = [
name = "messaging"
version = "0.1.0"
dependencies = [
"cryptography",
"process",
"versioning",
]

View file

@ -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" }

View file

@ -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<u8>,
}
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)
}