messaginjg library
This commit is contained in:
parent
4ee4681ab7
commit
a2f3a453dd
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -163,6 +163,7 @@ dependencies = [
|
||||||
name = "messaging"
|
name = "messaging"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"cryptography",
|
||||||
"process",
|
"process",
|
||||||
"versioning",
|
"versioning",
|
||||||
]
|
]
|
||||||
|
|
11
libraries/messaging/Cargo.toml
Normal file
11
libraries/messaging/Cargo.toml
Normal 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" }
|
49
libraries/messaging/src/lib.rs
Normal file
49
libraries/messaging/src/lib.rs
Normal 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)
|
||||||
|
}
|
Loading…
Reference in a new issue