dis_rust/common/entity_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;
10use bytes::{BytesMut, BufMut, Buf};
11use serde::{Serialize, Deserialize};
12
13#[derive(Copy, Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
14/// Entity ID Record as defined in IEEE 1278.1 standard. Used to communicate the ID of an entity during the simulation.
15pub struct EntityIDRecord {
16 pub simulation_address_record: SimulationAddressRecord,
17 pub entity_identifier_field: u16
18}
19
20impl EntityIDRecord {
21 /// Provides a function to create a new EntityIDRecord. Enforces all entity IDs must be non-zero.
22 ///
23 /// # Examples
24 ///
25 /// Creating a new EntityIDRecord at site 1, on application 1, with entity ID 1:
26 ///
27 /// ```
28 /// let entity_id_record = EntityIDRecord::new{
29 /// site_identifier_field: 1,
30 /// application_identifier_field: 1
31 /// entity_identifier_field: 1
32 /// };
33 /// ```
34 ///
35 pub fn new(site_identifier_field: u16, application_identifier_field: u16, entity_identifier_field: u16) -> Self {
36 if entity_identifier_field == 0 {
37 println!("Invalid entity identifier field! - cannot be 0"); // TODO: Make this a log!
38 }
39 EntityIDRecord {
40 simulation_address_record: SimulationAddressRecord::new(site_identifier_field, application_identifier_field),
41 entity_identifier_field
42 }
43 }
44
45 /// Provides a function to create a default EntityIDRecord. Uses the default SimulationAddressRecord.
46 /// Enforces all entity IDs must be non-zero.
47 ///
48 /// # Examples
49 ///
50 /// Creating a default EntityIDRecord with event ID 2:
51 ///
52 /// ```
53 /// let entity_id_record = EntityIDRecord::default(2);
54 /// ```
55 ///
56 pub fn default(entity_identifier: u16) -> Self {
57 if entity_identifier == 0 {
58 println!("Invalid entity identifier field! - cannot be 0"); // TODO: Make this a log!
59 }
60 EntityIDRecord {
61 simulation_address_record: SimulationAddressRecord::default(),
62 entity_identifier_field: entity_identifier
63 }
64 }
65
66 /// Fills a BytesMut struct with a EntityIDRecord serialised into binary. This buffer is then ready to be sent via
67 /// UDP to the simluation network.
68 pub fn serialize(&self, buf: &mut BytesMut) {
69 self.simulation_address_record.serialize(buf);
70 buf.put_u16(self.entity_identifier_field);
71 }
72
73 pub fn decode(buf: &mut BytesMut) -> EntityIDRecord {
74 EntityIDRecord {
75 simulation_address_record: EntityIDRecord::decode_simulation_address(buf),
76 entity_identifier_field: buf.get_u16()
77 }
78 }
79
80 fn decode_simulation_address(buf: &mut BytesMut) -> SimulationAddressRecord {
81 SimulationAddressRecord {
82 site_identifier_field: buf.get_u16(),
83 application_identifier_field: buf.get_u16()
84 }
85 }
86}