open_dis_rust/simulation_management/
comment_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 crate::common::{
8    dis_error::DISError,
9    entity_id::EntityId,
10    pdu::Pdu,
11    pdu_header::{PduHeader, PduType, ProtocolFamily},
12};
13use bytes::{Buf, BufMut, BytesMut};
14use std::any::Any;
15
16#[derive(Copy, Clone, Debug)]
17/// Implemented according to IEEE 1278.1-2012 ยง7.5.13
18pub struct CommentPdu {
19    pub pdu_header: PduHeader,
20    pub originating_entity_id: EntityId,
21    pub receiving_entity_id: EntityId,
22    pub number_of_fixed_datum_records: u32,
23    pub number_of_variable_datum_records: u32,
24    pub fixed_datum_records: u64,
25    pub variable_datum_records: u64,
26}
27
28impl Default for CommentPdu {
29    fn default() -> Self {
30        CommentPdu {
31            pdu_header: PduHeader::default(
32                PduType::Comment,
33                ProtocolFamily::SimulationManagement,
34                56,
35            ),
36            originating_entity_id: EntityId::default(1),
37            receiving_entity_id: EntityId::default(2),
38            number_of_fixed_datum_records: 0,
39            number_of_variable_datum_records: 0,
40            fixed_datum_records: 0,
41            variable_datum_records: 0,
42        }
43    }
44}
45
46impl Pdu for CommentPdu {
47    fn serialize(&mut self, buf: &mut BytesMut) {
48        self.pdu_header.length = u16::try_from(std::mem::size_of_val(self))
49            .expect("The length of the PDU should fit in a u16.");
50        self.pdu_header.serialize(buf);
51        self.originating_entity_id.serialize(buf);
52        self.receiving_entity_id.serialize(buf);
53        buf.put_u32(self.number_of_fixed_datum_records);
54        buf.put_u32(self.number_of_variable_datum_records);
55        buf.put_u64(self.fixed_datum_records);
56        buf.put_u64(self.variable_datum_records);
57    }
58
59    fn deserialize(mut buffer: BytesMut) -> Result<Self, DISError>
60    where
61        Self: Sized,
62    {
63        let pdu_header = PduHeader::decode(&mut buffer);
64        if pdu_header.pdu_type == PduType::Comment {
65            let originating_entity_id = EntityId::decode(&mut buffer);
66            let receiving_entity_id = EntityId::decode(&mut buffer);
67            let number_of_fixed_datum_records = buffer.get_u32();
68            let number_of_variable_datum_records = buffer.get_u32();
69            let mut fixed_datum_records: u64 = 0;
70            for _record in 0..number_of_fixed_datum_records as usize {
71                fixed_datum_records += buffer.get_u64();
72            }
73            let mut variable_datum_records: u64 = 0;
74            for _record in 0..number_of_variable_datum_records as usize {
75                variable_datum_records += buffer.get_u64();
76            }
77
78            Ok(CommentPdu {
79                pdu_header,
80                originating_entity_id,
81                receiving_entity_id,
82                number_of_fixed_datum_records,
83                number_of_variable_datum_records,
84                fixed_datum_records,
85                variable_datum_records,
86            })
87        } else {
88            Err(DISError::InvalidDISHeader)
89        }
90    }
91
92    fn as_any(&self) -> &dyn Any {
93        self
94    }
95
96    fn deserialize_without_header(
97        mut buffer: BytesMut,
98        pdu_header: PduHeader,
99    ) -> Result<Self, DISError>
100    where
101        Self: Sized,
102    {
103        let originating_entity_id = EntityId::decode(&mut buffer);
104        let receiving_entity_id = EntityId::decode(&mut buffer);
105        let number_of_fixed_datum_records = buffer.get_u32();
106        let number_of_variable_datum_records = buffer.get_u32();
107        let mut fixed_datum_records: u64 = 0;
108        for _record in 0..number_of_fixed_datum_records as usize {
109            fixed_datum_records += buffer.get_u64();
110        }
111        let mut variable_datum_records: u64 = 0;
112        for _record in 0..number_of_variable_datum_records as usize {
113            variable_datum_records += buffer.get_u64();
114        }
115
116        Ok(CommentPdu {
117            pdu_header,
118            originating_entity_id,
119            receiving_entity_id,
120            number_of_fixed_datum_records,
121            number_of_variable_datum_records,
122            fixed_datum_records,
123            variable_datum_records,
124        })
125    }
126}
127
128#[cfg(test)]
129mod tests {
130    use super::CommentPdu;
131    use crate::common::pdu_header::{PduHeader, PduType, ProtocolFamily};
132
133    #[test]
134    fn create_header() {
135        let comment_pdu = CommentPdu::default();
136        let pdu_header = PduHeader::default(
137            PduType::Comment,
138            ProtocolFamily::SimulationManagement,
139            448 / 8,
140        );
141
142        assert_eq!(
143            pdu_header.protocol_version,
144            comment_pdu.pdu_header.protocol_version
145        );
146        assert_eq!(pdu_header.exercise_id, comment_pdu.pdu_header.exercise_id);
147        assert_eq!(pdu_header.pdu_type, comment_pdu.pdu_header.pdu_type);
148        assert_eq!(
149            pdu_header.protocol_family,
150            comment_pdu.pdu_header.protocol_family
151        );
152        assert_eq!(pdu_header.length, comment_pdu.pdu_header.length);
153        assert_eq!(pdu_header.padding, comment_pdu.pdu_header.padding);
154    }
155}