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