Skip to content

Commit

Permalink
Extended tests with receiver states
Browse files Browse the repository at this point in the history
  • Loading branch information
Klapeyron committed Dec 6, 2020
1 parent 14f6525 commit dda1d3f
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 27 deletions.
104 changes: 77 additions & 27 deletions src/messages/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,29 @@ pub struct Receiver {
pub timestamp: Time_t,
}

impl Receiver {
pub fn new(locator_kind: LocatorKind_t) -> Self {
Receiver {
source_version: ProtocolVersion_t::PROTOCOLVERSION,
source_vendor_id: VendorId_t::VENDOR_UNKNOWN,
source_guid_prefix: GuidPrefix_t::GUIDPREFIX_UNKNOWN,
dest_guid_prefix: GuidPrefix_t::GUIDPREFIX_UNKNOWN,
unicast_reply_locator_list: vec![Locator_t {
kind: locator_kind,
address: Locator_t::LOCATOR_ADDRESS_INVALID,
port: Locator_t::LOCATOR_PORT_INVALID,
}],
multicast_reply_locator_list: vec![Locator_t {
kind: locator_kind,
address: Locator_t::LOCATOR_ADDRESS_INVALID,
port: Locator_t::LOCATOR_PORT_INVALID,
}],
have_timestamp: false,
timestamp: Time_t::TIME_INVALID,
}
}
}

enum DeserializationState {
ReadingHeader,
ReadingSubmessage,
Expand All @@ -45,24 +68,7 @@ pub struct MessageReceiver {
impl MessageReceiver {
pub fn new(locator_kind: LocatorKind_t) -> Self {
MessageReceiver {
receiver: Receiver {
source_version: ProtocolVersion_t::PROTOCOLVERSION,
source_vendor_id: VendorId_t::VENDOR_UNKNOWN,
source_guid_prefix: GuidPrefix_t::GUIDPREFIX_UNKNOWN,
dest_guid_prefix: GuidPrefix_t::GUIDPREFIX_UNKNOWN,
unicast_reply_locator_list: vec![Locator_t {
kind: locator_kind,
address: Locator_t::LOCATOR_ADDRESS_INVALID,
port: Locator_t::LOCATOR_PORT_INVALID,
}],
multicast_reply_locator_list: vec![Locator_t {
kind: locator_kind,
address: Locator_t::LOCATOR_ADDRESS_INVALID,
port: Locator_t::LOCATOR_PORT_INVALID,
}],
have_timestamp: false,
timestamp: Time_t::TIME_INVALID,
},
receiver: Receiver::new(locator_kind),
state: DeserializationState::ReadingHeader,
}
}
Expand Down Expand Up @@ -200,6 +206,7 @@ impl Decoder for MessageReceiver {
}
SubmessageKind::INFO_REPLAY => {
let mut bytes = bytes.split_to(submessage_header.submessage_length.into());
dbg!(&bytes);
let (unicast_locator_list, read_bytes) =
LocatorList_t::read_with_length_from_buffer_with_ctx(
submessage_header.flags.endianness_flag(),
Expand All @@ -208,7 +215,9 @@ impl Decoder for MessageReceiver {
self.receiver.unicast_reply_locator_list = unicast_locator_list?;

use crate::bytes::Buf;
let mut bytes = bytes.split_to(read_bytes);
let mut bytes = bytes.split_off(read_bytes);

dbg!(&bytes);

self.receiver.multicast_reply_locator_list =
if submessage_header.flags.is_flag_set(0x02) {
Expand All @@ -223,6 +232,7 @@ impl Decoder for MessageReceiver {
vec![]
};

dbg!(&bytes);
Ok(None)
}
SubmessageKind::INFO_TS => {
Expand Down Expand Up @@ -356,8 +366,7 @@ mod tests {
// we do not care what happends after that :)
.take(expected_notifications.len());
assert!(decoder_output.eq(expected_notifications));

$(assert_eq!($receiver, message_receiver.receiver);)?
$(pretty_assertions::assert_eq!(message_receiver.receiver, $receiver);)?
}
}
}
Expand Down Expand Up @@ -395,7 +404,12 @@ mod tests {
count: Count_t::from(1)
},
SubmessageFlag { flags: 0b0000_0000 }
))]
))],
receiver_state = Receiver {
have_timestamp: true,
timestamp: Time_t::TIME_INFINITE,
..Receiver::new(LocatorKind_t::LOCATOR_KIND_INVALID)
}
);

message_decoding_test!(
Expand Down Expand Up @@ -431,7 +445,16 @@ mod tests {
writer_id: EntityId_t::ENTITYID_P2P_BUILTIN_PARTICIPANT_MESSAGE_WRITER,
gap_start: SequenceNumber_t::from(42),
gap_list: SequenceNumberSet_t::new(SequenceNumber_t::from(0b10110100))
}))]
}))],
receiver_state = Receiver {
dest_guid_prefix: GuidPrefix_t::GUIDPREFIX_UNKNOWN,
source_version: ProtocolVersion_t::PROTOCOLVERSION,
source_vendor_id: VendorId_t::VENDOR_UNKNOWN,
unicast_reply_locator_list: vec![Locator_t::LOCATOR_INVALID],
multicast_reply_locator_list: vec![Locator_t::LOCATOR_INVALID],
have_timestamp: false,
..Receiver::new(LocatorKind_t::LOCATOR_KIND_INVALID)
}
);

message_decoding_test!(
Expand All @@ -444,7 +467,7 @@ mod tests {
flags: SubmessageFlag { flags: 0b0000_0001 },
submessage_length: 12,
},
submessage_entities = [GuidPrefix_t::GUIDPREFIX_UNKNOWN],
submessage_entities = [GuidPrefix_t::from([0x42; 12])],
submessage_header = SubmessageHeader {
submessage_id: SubmessageKind::HEARTBEAT,
flags: SubmessageFlag { flags: 0b0000_0001 },
Expand All @@ -468,7 +491,11 @@ mod tests {
count: Count_t::from(99)
},
SubmessageFlag { flags: 0b0000_0001 }
))]
))],
receiver_state = Receiver {
dest_guid_prefix: GuidPrefix_t::from([0x42; 12]),
..Receiver::new(LocatorKind_t::LOCATOR_KIND_INVALID)
}
);

message_decoding_test!(
Expand Down Expand Up @@ -512,7 +539,19 @@ mod tests {
writer_sn: SequenceNumber_t::from(36),
last_fragment_num: FragmentNumber_t::from(33),
count: Count_t::from(12345)
}))]
}))],
receiver_state = Receiver {
unicast_reply_locator_list: vec![Locator_t::LOCATOR_INVALID],
multicast_reply_locator_list: vec![
Locator_t::from("127.0.0.1:8080".parse::<std::net::SocketAddr>().unwrap()),
Locator_t::from(
"[2001:db8::1]:8080"
.parse::<std::net::SocketAddr>()
.unwrap()
)
],
..Receiver::new(LocatorKind_t::LOCATOR_KIND_INVALID)
}
);

message_decoding_test!(
Expand All @@ -534,6 +573,17 @@ mod tests {
count: Count_t::from(1)
},
SubmessageFlag { flags: 0b0000_0001 }
))]
))],
receiver_state = Receiver {
source_guid_prefix: GuidPrefix_t::from([
0x01, 0x0f, 0xbb, 0x1d, 0xdf, 0x2b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
]),
dest_guid_prefix: GuidPrefix_t::from([
0x01, 0x0f, 0xbb, 0x1d, 0xe6, 0x2b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
]),
source_version: ProtocolVersion_t::PROTOCOLVERSION_2_1,
source_vendor_id: VendorId_t::from([0x01, 0x0F]),
..Receiver::new(LocatorKind_t::LOCATOR_KIND_INVALID)
}
);
}
8 changes: 8 additions & 0 deletions src/messages/vendor_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ impl VendorId_t {
};
}

impl From<[u8; 2]> for VendorId_t {
fn from(vendor_id: [u8; 2]) -> Self {
VendorId_t {
vendorId: vendor_id,
}
}
}

impl Default for VendorId_t {
fn default() -> Self {
VendorId_t::VENDOR_UNKNOWN
Expand Down
8 changes: 8 additions & 0 deletions src/structure/guid_prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ impl Default for GuidPrefix_t {
}
}

impl From<[u8; 12]> for GuidPrefix_t {
fn from(entity_key: [u8; 12]) -> Self {
GuidPrefix_t {
entityKey: entity_key,
}
}
}

impl<'a, C: Context> Readable<'a, C> for GuidPrefix_t {
#[inline]
fn read_from<R: Reader<'a, C>>(reader: &mut R) -> Result<Self, C::Error> {
Expand Down

0 comments on commit dda1d3f

Please sign in to comment.