Messaging api done

master
Able 2022-04-12 18:17:48 -05:00
parent a1dad085df
commit ba20be6ff6
Signed by untrusted user: able
GPG Key ID: D164AF5F5700BE51
3 changed files with 129 additions and 1 deletions

View File

@ -1,2 +1,3 @@
pub const CONSOLE: &str = "\u{E795}";
pub const POWER_SIGN: &str = "\u{23FB}";
pub const LAMBDA: &str = "λ";

View File

@ -3,6 +3,7 @@
#![deny(missing_docs)]
#![no_std]
#![feature(prelude_import)]
#![feature(arbitrary_enum_discriminant)]
pub mod device_interface;
pub mod messaging;

View File

@ -1,7 +1,22 @@
//! Interprocess communication.
use alloc::vec::Vec;
use crate::{proccess::PID, time::Time};
extern crate alloc;
/// 131070 * 128
pub const TINY_MESSAGE_COUNT: usize = 131070;
/// 16384 * 1024
pub const SMALL_MESSAGE_COUNT: usize = 16384;
/// 65536 * 256
pub const MEDIUM_MESSAGE_COUNT: usize = 256;
/// 262144 * 16
pub const LARGE_MESSAGE_COUNT: usize = 16;
/// 16777216 * 4
pub const HUGE_MESSAGE_COUNT: usize = 4;
/// 128 Bytes
pub type Tiny = [u8; 128];
@ -54,7 +69,18 @@ pub struct ProcessMessage {
/// The time the message was received
pub receiver_time: Time,
}
impl ProcessMessage {
/// Return the size of the message
pub fn size(&self) -> usize {
match &self.message {
Message::Tiny(_) => 128,
Message::Small(_) => 1024,
Message::Medium(_) => 65536,
Message::Large(_) => 1048576,
Message::Huge(_) => 16777216,
}
}
}
#[derive(Debug)]
/// An enum of all possible errors that can occur when sending a message
pub enum MessagingError {
@ -65,3 +91,103 @@ pub enum MessagingError {
/// The message Queue is full
TooManyMessages,
}
/// A mailbox that holds messages and PipeState
pub struct Mailbox {
/// The messages in the mailbox
pub messages: Vec<ProcessMessage>,
/// The count of messages in the mailbox
pub message_count: MessageCount,
}
impl Mailbox {
/// append a message to the mailbox
pub fn append(&mut self, message: ProcessMessage) -> Result<(), MessagingError> {
let msg_size = message.size();
if self.message_count.total() > 147730 {
return Err(MessagingError::TooManyMessages);
}
match msg_size {
TINY_MESSAGE_COUNT => {
if self.message_count.tiny < TINY_MESSAGE_COUNT {
self.messages.push(message);
self.message_count.tiny += 1;
Ok(())
} else {
Err(MessagingError::TooManyMessages)
}
}
SMALL_MESSAGE_COUNT => {
if self.message_count.small < SMALL_MESSAGE_COUNT {
self.messages.push(message);
self.message_count.small += 1;
Ok(())
} else {
Err(MessagingError::TooManyMessages)
}
}
MEDIUM_MESSAGE_COUNT => {
if self.message_count.medium < MEDIUM_MESSAGE_COUNT {
self.messages.push(message);
self.message_count.medium += 1;
Ok(())
} else {
Err(MessagingError::TooManyMessages)
}
}
LARGE_MESSAGE_COUNT => {
if self.message_count.large < LARGE_MESSAGE_COUNT {
self.messages.push(message);
self.message_count.large += 1;
Ok(())
} else {
Err(MessagingError::TooManyMessages)
}
}
HUGE_MESSAGE_COUNT => {
if self.message_count.huge < HUGE_MESSAGE_COUNT {
self.messages.push(message);
self.message_count.huge += 1;
Ok(())
} else {
return Err(MessagingError::TooManyMessages);
}
}
_ => Err(MessagingError::MessageTooLarge),
}
}
}
/// A proper struct to list the number of messages in the mailbox
pub struct MessageCount {
/// The number of tiny messages in the mailbox
pub tiny: usize,
/// The number of small messages in the mailbox
pub small: usize,
/// The number of medium messages in the mailbox
pub medium: usize,
/// The number of large messages in the mailbox
pub large: usize,
/// The number of huge messages in the mailbox
pub huge: usize,
}
impl MessageCount {
/// Return the total number of messages in the mailbox
pub fn total(&self) -> usize {
self.tiny + self.small + self.medium + self.large + self.huge
}
}
impl Default for MessageCount {
fn default() -> Self {
MessageCount {
tiny: 0,
small: 0,
medium: 0,
large: 0,
huge: 0,
}
}
}