open_dis_rust/simulation_management_with_reliability/
acknowledge_reliable_pdu.rs

1//     open-dis-rust - Rust implementation of the IEEE 1278.1-2012 Distributed Interactive
2//                     Simulation (DIS) application protocol
3//     Copyright (C) 2023 Cameron Howell
4//
5//     Licensed under the BSD 2-Clause License
6
7use bytes::{Buf, BufMut, BytesMut};
8use std::any::Any;
9
10use crate::common::{
11    dis_error::DISError,
12    entity_id::EntityId,
13    enums::{AcknowledgeFlag, AcknowledgeResponseFlag},
14    pdu::Pdu,
15    pdu_header::{PduHeader, PduType, ProtocolFamily},
16};
17
18#[derive(Copy, Clone, Debug)]
19/// Implemented according to IEEE 1278.1-2012 ยง7.11.6
20pub struct AcknowledgeReliablePdu {
21    pub pdu_header: PduHeader,
22    pub originating_entity_id: EntityId,
23    pub receiving_entity_id: EntityId,
24    pub acknowledge_flag: AcknowledgeFlag,
25    pub response_flag: AcknowledgeResponseFlag,
26    pub request_id: u32,
27}
28
29impl Default for AcknowledgeReliablePdu {
30    /// Creates a default Acknowledge Reliable PDU with arbitrary originating and receiving
31    /// entity IDs
32    ///
33    /// # Examples
34    ///
35    /// Initializing an Acknowledge Reliable PDU:
36    /// ```
37    /// use open_dis_rust::simulation_management_with_reliability::acknowledge_reliable_pdu::AcknowledgeReliablePdu;
38    /// let acknowledge_reliable_pdu = AcknowledgeReliablePdu::default();
39    /// ```
40    ///
41    fn default() -> Self {
42        AcknowledgeReliablePdu {
43            pdu_header: PduHeader::default(
44                PduType::AcknowledgeReliable,
45                ProtocolFamily::SimulationManagementWithReliability,
46                32,
47            ),
48            originating_entity_id: EntityId::default(1),
49            receiving_entity_id: EntityId::default(2),
50            acknowledge_flag: AcknowledgeFlag::default(),
51            response_flag: AcknowledgeResponseFlag::default(),
52            request_id: 0,
53        }
54    }
55}
56
57impl Pdu for AcknowledgeReliablePdu {
58    fn serialize(&mut self, buf: &mut BytesMut) {
59        self.pdu_header.length = u16::try_from(std::mem::size_of_val(self))
60            .expect("The length of the PDU should fit in a u16.");
61        self.pdu_header.serialize(buf);
62        self.originating_entity_id.serialize(buf);
63        self.receiving_entity_id.serialize(buf);
64        buf.put_u16(self.acknowledge_flag as u16);
65        buf.put_u16(self.response_flag as u16);
66        buf.put_u32(self.request_id);
67    }
68
69    fn deserialize(mut buffer: BytesMut) -> Result<Self, DISError>
70    where
71        Self: Sized,
72    {
73        let pdu_header = PduHeader::deserialize(&mut buffer);
74        if pdu_header.pdu_type == PduType::AcknowledgeReliable {
75            let originating_entity_id = EntityId::deserialize(&mut buffer);
76            let receiving_entity_id = EntityId::deserialize(&mut buffer);
77            let acknowledge_flag = AcknowledgeFlag::deserialize(&mut buffer);
78            let response_flag = AcknowledgeResponseFlag::deserialize(&mut buffer);
79            let request_id = buffer.get_u32();
80
81            Ok(AcknowledgeReliablePdu {
82                pdu_header,
83                originating_entity_id,
84                receiving_entity_id,
85                acknowledge_flag,
86                response_flag,
87                request_id,
88            })
89        } else {
90            Err(DISError::invalid_header(
91                format!(
92                    "Expected PDU type AcknowledgeReliable, got {:?}",
93                    pdu_header.pdu_type
94                ),
95                None,
96            ))
97        }
98    }
99
100    fn as_any(&self) -> &dyn Any {
101        self
102    }
103
104    fn deserialize_without_header(
105        mut buffer: BytesMut,
106        pdu_header: PduHeader,
107    ) -> Result<Self, DISError>
108    where
109        Self: Sized,
110    {
111        let originating_entity_id = EntityId::deserialize(&mut buffer);
112        let receiving_entity_id = EntityId::deserialize(&mut buffer);
113        let acknowledge_flag = AcknowledgeFlag::deserialize(&mut buffer);
114        let response_flag = AcknowledgeResponseFlag::deserialize(&mut buffer);
115        let request_id = buffer.get_u32();
116
117        Ok(AcknowledgeReliablePdu {
118            pdu_header,
119            originating_entity_id,
120            receiving_entity_id,
121            acknowledge_flag,
122            response_flag,
123            request_id,
124        })
125    }
126}
127
128#[cfg(test)]
129mod tests {
130    use super::AcknowledgeReliablePdu;
131    use crate::common::{
132        pdu::Pdu,
133        pdu_header::{PduHeader, PduType, ProtocolFamily},
134    };
135    use bytes::BytesMut;
136
137    #[test]
138    fn create_header() {
139        let acknowledge_reliable_pdu = AcknowledgeReliablePdu::default();
140        let pdu_header = PduHeader::default(
141            PduType::AcknowledgeReliable,
142            ProtocolFamily::SimulationManagementWithReliability,
143            32,
144        );
145
146        assert_eq!(
147            pdu_header.protocol_version,
148            acknowledge_reliable_pdu.pdu_header.protocol_version
149        );
150        assert_eq!(
151            pdu_header.exercise_id,
152            acknowledge_reliable_pdu.pdu_header.exercise_id
153        );
154        assert_eq!(
155            pdu_header.pdu_type,
156            acknowledge_reliable_pdu.pdu_header.pdu_type
157        );
158        assert_eq!(
159            pdu_header.protocol_family,
160            acknowledge_reliable_pdu.pdu_header.protocol_family
161        );
162        assert_eq!(
163            pdu_header.length,
164            acknowledge_reliable_pdu.pdu_header.length
165        );
166        assert_eq!(
167            pdu_header.status_record,
168            acknowledge_reliable_pdu.pdu_header.status_record
169        );
170    }
171
172    #[test]
173    fn deserialize_header() {
174        let mut acknowledge_reliable_pdu = AcknowledgeReliablePdu::default();
175        let mut buffer = BytesMut::new();
176        acknowledge_reliable_pdu.serialize(&mut buffer);
177
178        let new_acknowledge_reliable_pdu = AcknowledgeReliablePdu::deserialize(buffer).unwrap();
179        assert_eq!(
180            new_acknowledge_reliable_pdu.pdu_header,
181            acknowledge_reliable_pdu.pdu_header
182        );
183    }
184}