Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions opendis/dis7.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
DEFireFlags,
WorldCoordinates,
EntityIdentifier,
EntityID,
EventIdentifier,
SimulationAddress,
BeamAntennaPattern,
Expand Down
34 changes: 34 additions & 0 deletions opendis/record/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"Vector3Float",
"WorldCoordinates",
"EntityIdentifier",
"EntityID",
"EulerAngles",
"SimulationAddress",
"EventIdentifier",
Expand Down Expand Up @@ -53,6 +54,39 @@ def parse(self, inputStream: DataInputStream) -> None:
self.simulationAddress.parse(inputStream)
self.entityNumber = inputStream.read_uint16()

# Legacy EntityID field names, kept for backwards compatibility with
# pre-1.1 code and other open-dis implementations. They delegate to the
# fields above and do not change the wire format.
@property
def siteID(self) -> uint16:
return self.simulationAddress.site

@siteID.setter
def siteID(self, value: uint16) -> None:
self.simulationAddress.site = value

@property
def applicationID(self) -> uint16:
return self.simulationAddress.application

@applicationID.setter
def applicationID(self, value: uint16) -> None:
self.simulationAddress.application = value

@property
def entityID(self) -> uint16:
return self.entityNumber

@entityID.setter
def entityID(self, value: uint16) -> None:
self.entityNumber = value


# Legacy name for EntityIdentifier (the standard's own shorthand for the
# §6.2.28 record is "Entity ID"); kept as an alias for backwards
# compatibility.
EntityID = EntityIdentifier


class EulerAngles(base.Record):
"""6.2.32 Euler Angles record
Expand Down
54 changes: 54 additions & 0 deletions tests/test_EntityID_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""Backwards compatibility of the EntityID name and its flat field names.
The type of EntityStatePdu.entityID changed from the flat EntityID class to
EntityIdentifier (see issue #88). EntityID is an alias of EntityIdentifier
and the flat field names delegate to it, restoring pre-1.1 code without
changing the wire format.
"""

import io
import os
import unittest

from opendis.dis7 import EntityID, EntityIdentifier
from opendis.PduFactory import createPduFromFilePath
from opendis.stream import DataOutputStream


class TestEntityIDCompat(unittest.TestCase):

def setUp(self):
testdir = os.path.dirname(os.path.abspath(__file__))
os.chdir(testdir)

def test_alias_identity(self):
self.assertIs(EntityID, EntityIdentifier)

def test_flat_fields_write_through(self):
entityID = EntityID()
entityID.siteID = 17
entityID.applicationID = 23
entityID.entityID = 42
self.assertEqual(17, entityID.simulationAddress.site)
self.assertEqual(23, entityID.simulationAddress.application)
self.assertEqual(42, entityID.entityNumber)

def test_wire_format_unchanged(self):
entityID = EntityID()
entityID.siteID = 17
entityID.applicationID = 23
entityID.entityID = 42
buffer = io.BytesIO()
entityID.serialize(DataOutputStream(buffer))
# site 17, application 23, entity number 42, as three big-endian u16
self.assertEqual(b"\x00\x11\x00\x17\x00\x2a", buffer.getvalue())

def test_flat_reads_on_parsed_pdu(self):
pdu = createPduFromFilePath("EntityStatePdu-26.raw")
self.assertEqual(42, pdu.entityID.siteID)
self.assertEqual(4, pdu.entityID.applicationID)
self.assertEqual(26, pdu.entityID.entityID)


if __name__ == "__main__":
unittest.main()
Loading