open_dis_rust/common/
simulation_address.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 bytes::{Buf, BufMut, BytesMut};
8use serde::{Deserialize, Serialize};
9
10#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
11/// Implemented according to IEEE 1278.1-2012 ยง6.2.80
12pub struct SimulationAddress {
13    /// Identification number representing the site, which may be a facility,
14    /// installation, organizational unit, or a geographical location. Valid
15    /// site ID values range from 1 to 65,534
16    pub site_id: u16,
17    /// Identification number representing the software program that is used to
18    /// generate and process distributed simulation data. Valid application ID
19    /// values range from 1 to 65,534
20    pub application_id: u16,
21}
22
23impl Default for SimulationAddress {
24    fn default() -> Self {
25        SimulationAddress {
26            site_id: 1,
27            application_id: 1,
28        }
29    }
30}
31
32impl SimulationAddress {
33    #[must_use]
34    pub fn new(site_id: u16, application_id: u16) -> Self {
35        SimulationAddress {
36            site_id,
37            application_id,
38        }
39    }
40
41    pub fn serialize(&self, buf: &mut BytesMut) {
42        buf.put_u16(self.site_id);
43        buf.put_u16(self.application_id);
44    }
45
46    pub fn decode(buf: &mut BytesMut) -> SimulationAddress {
47        SimulationAddress {
48            site_id: buf.get_u16(),
49            application_id: buf.get_u16(),
50        }
51    }
52}