Skip to content

Commit

Permalink
globals/datagram: Util to convert proto enum to u16 msg type
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrdz committed Oct 2, 2023
1 parent 28aee75 commit 16ac22e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
43 changes: 37 additions & 6 deletions src/datagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ impl Datagram {
&mut self,
to: Vec<globals::Channel>,
from: globals::Channel,
msg_type: u16,
msg_type: globals::MsgType,
) -> globals::DgResult {
// Add recipient(s) count
self.add_u8(to.len().try_into().unwrap())?;
Expand All @@ -262,7 +262,7 @@ impl Datagram {
// Appends a control header, which is very similar to a server header,
// but it always has only one recipient, which is the control channel,
// and does not require a sender (or 'from') channel to be provided.
pub fn add_control_header(&mut self, msg_type: u16) -> globals::DgResult {
pub fn add_control_header(&mut self, msg_type: globals::MsgType) -> globals::DgResult {
self.add_u8(1)?;
self.add_channel(globals::CONTROL_CHANNEL)?;
self.add_u16(msg_type)?;
Expand Down Expand Up @@ -476,11 +476,11 @@ impl DatagramIterator {
+ usize::from(self.read_recipient_count()) * mem::size_of::<globals::Channel>()
+ mem::size_of::<globals::Channel>(); // seek message type

let msg_type: u16 = self.read_u16(); // read message type
let msg_type: globals::MsgType = self.read_u16(); // read message type
self.index = start_index; // do not advance dgi index

for message in globals::Protocol::iter() {
let msg_id: u16 = message as u16;
let msg_id: globals::MsgType = globals::msg_type(message);
if msg_type == msg_id {
return message;
}
Expand All @@ -494,6 +494,7 @@ impl DatagramIterator {
mod unit_testing {
use crate::datagram;
use crate::globals;
use crate::globals::{msg_type, Protocol};

// ----------- Datagram ------------ //
#[test]
Expand Down Expand Up @@ -551,7 +552,7 @@ mod unit_testing {

#[test]
#[rustfmt::skip]
fn add_datagram() {
fn dg_add_datagram() {
let mut dg: datagram::Datagram = datagram::Datagram::default();
let mut dg_2: datagram::Datagram = datagram::Datagram::new();

Expand All @@ -568,7 +569,37 @@ mod unit_testing {
u8::MAX, u8::MAX, u8::MAX, u8::MAX,
3, 0, 0, 125, u8::MAX,
]);
assert_eq!(dg_size, 13);
}

#[test]
#[rustfmt::skip]
fn dg_add_message_headers() {
let mut dg: datagram::Datagram = datagram::Datagram::default();
let mut results: Vec<globals::DgResult> = vec![];

results.push(dg.add_server_header(
vec![globals::CHANNEL_MAX], // recipients
0, // sender
msg_type(Protocol::MDAddChannel), // msg type
));

results.push(dg.add_control_header(msg_type(Protocol::MDAddChannel)));

for dg_res in results {
assert!(dg_res.is_ok());
}
let dg_size: globals::DgSize = dg.size();
let dg_buffer: Vec<u8> = dg.get_data();

assert_eq!(dg_buffer.len() as u16, dg_size);
assert_eq!(dg_buffer, vec![
1, u8::MAX, u8::MAX, u8::MAX, u8::MAX, // recipients
u8::MAX, u8::MAX, u8::MAX, u8::MAX,
0, 0, 0, 0, 0, 0, 0, 0, // sender
40, 35, // message type (9000; 0x2823, or 40, 35)
1, 1, 0, 0, 0, 0, 0, 0, 0, // recipients (control)
40, 35, // message type
]);
}

#[test]
Expand Down
15 changes: 14 additions & 1 deletion src/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use std::result::Result; // not to be confused with std::io::Result
use strum_macros::EnumIter;

// Type Definitions
pub type MsgType = u16;
pub type DgSize = u16;
pub type Channel = u64;
pub type DoId = u32;
Expand Down Expand Up @@ -68,6 +69,11 @@ pub type SqlResult = Result<(), Box<dyn Error>>;
// Hack to reassure the compiler the result type of a future.
pub fn set_future_return_type<T, F: Future<Output = T>>(_arg: &F) {}

// Utility for converting protocol enumerator to u16 (MsgType)
pub fn msg_type(proto_enum: Protocol) -> MsgType {
proto_enum as MsgType
}

#[repr(u16)] // 16-bit alignment
#[derive(Copy, Clone, EnumIter)]
pub enum Protocol {
Expand Down Expand Up @@ -204,7 +210,7 @@ pub enum Protocol {

#[cfg(test)]
mod unit_testing {
use super::set_future_return_type;
use super::{msg_type, set_future_return_type, Protocol};
use std::io::Result;

#[test]
Expand All @@ -217,4 +223,11 @@ mod unit_testing {
// Need this test to have test coverage on this file.
set_future_return_type::<Result<()>, _>(&test_future);
}

#[test]
fn test_protocol_to_u16_util() {
assert_eq!(msg_type(Protocol::MDRemoveChannel), 9001);
assert_eq!(msg_type(Protocol::CAAddInterest), 1200);
assert_eq!(msg_type(Protocol::SSDeleteAIObjects), 2009);
}
}

0 comments on commit 16ac22e

Please sign in to comment.