dis_rust/common/event_id_record.rs
1// dis-rust - A rust implementation of the DIS simulation protocol.
2// Copyright (C) 2022 Thomas Mann
3//
4// This software is dual-licensed. It is available under the conditions of
5// the GNU Affero General Public License (see the LICENSE file included)
6// or under a commercial license (email contact@coffeebreakdevs.com for
7// details).
8
9use super::simulation_address_record::SimulationAddressRecord;
10
11use bytes::{BytesMut, BufMut, Buf};
12
13#[derive(Copy, Clone, Debug, Default)]
14/// Event ID Record as defined in IEEE 1278.1 standard. Used to communicate the ID of an event during the simulation.
15pub struct EventIDRecord {
16 pub simulation_address_record: SimulationAddressRecord,
17 pub event_identifier_field: u16
18}
19
20impl EventIDRecord {
21 /// Provides a function to create a new EventIDRecord.
22 ///
23 /// # Examples
24 ///
25 /// Creating a new EventIDRecord at site 1, on application 1, with event ID 1:
26 ///
27 /// ```
28 /// let event_id_record = EventIDRecord::new{
29 /// site_identifier_field: 1,
30 /// application_identifier_field: 1
31 /// event_identifier_field: 1
32 /// };
33 /// ```
34 ///
35 pub fn new(site_identifier_field: u16, application_identifier_field: u16, event_identifier_field: u16) -> Self {
36 EventIDRecord {
37 simulation_address_record: SimulationAddressRecord::new(site_identifier_field, application_identifier_field),
38 event_identifier_field
39 }
40 }
41
42 /// Provides a function to create a default EventIDRecord. Uses the default SimulationAddressRecord.
43 ///
44 /// # Examples
45 ///
46 /// Creating a default EventIDRecord with event ID 2:
47 ///
48 /// ```
49 /// let event_id_record = EventIDRecord::default(2);
50 /// ```
51 ///
52 pub fn default(event_identifier: u16, ) -> Self {
53 EventIDRecord {
54 simulation_address_record: SimulationAddressRecord::default(),
55 event_identifier_field: event_identifier
56 }
57 }
58
59 /// Fills a BytesMut struct with a EventIDRecord serialised into binary. This buffer is then ready to be sent via
60 /// UDP to the simluation network.
61 pub fn serialize(&self, buf: &mut BytesMut) {
62 self.simulation_address_record.serialize(buf);
63 buf.put_u16(self.event_identifier_field);
64 }
65
66 pub fn decode(buf: &mut BytesMut) -> EventIDRecord {
67 EventIDRecord {
68 simulation_address_record: SimulationAddressRecord::decode(buf),
69 event_identifier_field: buf.get_u16(),
70 }
71 }
72}