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::deserialize(&mut buffer);
64        if pdu_header.pdu_type == PduType::Comment {
65            let originating_entity_id = EntityId::deserialize(&mut buffer);
66            let receiving_entity_id = EntityId::deserialize(&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::invalid_header(
89                format!("Expected PDU type Comment, got {:?}", pdu_header.pdu_type),
90                None,
91            ))
92        }
93    }
94
95    fn as_any(&self) -> &dyn Any {
96        self
97    }
98
99    fn deserialize_without_header(
100        mut buffer: BytesMut,
101        pdu_header: PduHeader,
102    ) -> Result<Self, DISError>
103    where
104        Self: Sized,
105    {
106        let originating_entity_id = EntityId::deserialize(&mut buffer);
107        let receiving_entity_id = EntityId::deserialize(&mut buffer);
108        let number_of_fixed_datum_records = buffer.get_u32();
109        let number_of_variable_datum_records = buffer.get_u32();
110        let mut fixed_datum_records: u64 = 0;
111        for _record in 0..number_of_fixed_datum_records as usize {
112            fixed_datum_records += buffer.get_u64();
113        }
114        let mut variable_datum_records: u64 = 0;
115        for _record in 0..number_of_variable_datum_records as usize {
116            variable_datum_records += buffer.get_u64();
117        }
118
119        Ok(CommentPdu {
120            pdu_header,
121            originating_entity_id,
122            receiving_entity_id,
123            number_of_fixed_datum_records,
124            number_of_variable_datum_records,
125            fixed_datum_records,
126            variable_datum_records,
127        })
128    }
129}
130
131#[cfg(test)]
132mod tests {
133    use super::CommentPdu;
134    use crate::common::pdu_header::{PduHeader, PduType, ProtocolFamily};
135
136    #[test]
137    fn create_header() {
138        let comment_pdu = CommentPdu::default();
139        let pdu_header = PduHeader::default(
140            PduType::Comment,
141            ProtocolFamily::SimulationManagement,
142            448 / 8,
143        );
144
145        assert_eq!(
146            pdu_header.protocol_version,
147            comment_pdu.pdu_header.protocol_version
148        );
149        assert_eq!(pdu_header.exercise_id, comment_pdu.pdu_header.exercise_id);
150        assert_eq!(pdu_header.pdu_type, comment_pdu.pdu_header.pdu_type);
151        assert_eq!(
152            pdu_header.protocol_family,
153            comment_pdu.pdu_header.protocol_family
154        );
155        assert_eq!(pdu_header.length, comment_pdu.pdu_header.length);
156        assert_eq!(
157            pdu_header.status_record,
158            comment_pdu.pdu_header.status_record
159        );
160    }
161}