statime/datastructures/messages/
delay_resp.rs

1use crate::datastructures::{
2    common::{PortIdentity, WireTimestamp},
3    WireFormat, WireFormatError,
4};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub(crate) struct DelayRespMessage {
8    pub(crate) receive_timestamp: WireTimestamp,
9    pub(crate) requesting_port_identity: PortIdentity,
10}
11
12impl DelayRespMessage {
13    pub(crate) fn content_size(&self) -> usize {
14        20
15    }
16
17    pub(crate) fn serialize_content(&self, buffer: &mut [u8]) -> Result<(), WireFormatError> {
18        self.receive_timestamp.serialize(&mut buffer[0..10])?;
19        self.requesting_port_identity
20            .serialize(&mut buffer[10..20])?;
21
22        Ok(())
23    }
24
25    pub(crate) fn deserialize_content(buffer: &[u8]) -> Result<Self, WireFormatError> {
26        let slice = buffer.get(0..20).ok_or(WireFormatError::BufferTooShort)?;
27        let receive_timestamp = WireTimestamp::deserialize(&slice[0..10])?;
28        let requesting_port_identity = PortIdentity::deserialize(&slice[10..20])?;
29
30        Ok(Self {
31            receive_timestamp,
32            requesting_port_identity,
33        })
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40    use crate::datastructures::common::{ClockIdentity, PortIdentity};
41
42    #[test]
43    fn timestamp_wireformat() {
44        let representations = [(
45            [
46                0x00, 0x00, 0x45, 0xb1, 0x11, 0x5a, 0x0a, 0x64, 0xfa, 0xb0, 0x01, 0x02, 0x03, 0x04,
47                0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
48            ],
49            DelayRespMessage {
50                receive_timestamp: WireTimestamp {
51                    seconds: 1169232218,
52                    nanos: 174389936,
53                },
54                requesting_port_identity: PortIdentity {
55                    clock_identity: ClockIdentity([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]),
56                    port_number: 0x090a,
57                },
58            },
59        )];
60
61        for (byte_representation, object_representation) in representations {
62            // Test the serialization output
63            let mut serialization_buffer = [0; 20];
64            object_representation
65                .serialize_content(&mut serialization_buffer)
66                .unwrap();
67            assert_eq!(serialization_buffer, byte_representation);
68
69            // Test the deserialization output
70            let deserialized_data =
71                DelayRespMessage::deserialize_content(&byte_representation).unwrap();
72            assert_eq!(deserialized_data, object_representation);
73        }
74    }
75}