SimCore
Shared transport-layer plumbing for the ADR-029 Train/IL/CTC RBC simulators
Loading...
Searching...
No Matches
simcore.rbc_messages.MessageCatalog Class Reference

Public Member Functions

 __init__ (self, catalog_path, ertms_codec=None, ertms_encoders=None, ertms_flatteners=None)
 message_names (self)
 is_ertms (self, message)
 build_ertms_envelope_raw (self, ertms_codec_bytes, train_id)
 packet_names (self)
 packets_for (self, message)
 fields_for (self, message, packet=None)
 build_envelope (self, message, train_id, fields)
 decode_to_message (self, data)

Protected Attributes

 _path = path
 _messages = messages
 _packets = packets_catalog
 _ertms_codec = ertms_codec
 _ertms_encoders = ertms_encoders
 _ertms_flatteners = ertms_flatteners
 _ertms_wrapper_kind = ertms_wrapper_kind
dict _name_by_kind = {spec["kind"]: name for name, spec in messages.items() if spec["wire_format"] == "flat"}
dict _name_by_nid_message_ertms

Detailed Description

Definition at line 91 of file rbc_messages.py.

Constructor & Destructor Documentation

◆ __init__()

simcore.rbc_messages.MessageCatalog.__init__ ( self,
catalog_path,
ertms_codec = None,
ertms_encoders = None,
ertms_flatteners = None )
@param catalog_path: path to this sim's own message_catalog.json.
Callers should pass an absolute path built from their own
__file__ (e.g. os.path.join(os.path.dirname(os.path.abspath(
__file__)), "message_catalog.json")) rather than a bare relative
name, since a relative path here would otherwise depend on the
process's own current working directory at startup, not on
where the catalog file actually lives on disk.
@param ertms_codec, ertms_encoders, ertms_flatteners: only needed
if this catalog declares any "wireFormat": "ertms" message - see
this module's own header doc for what each one is. Omitted
(None/{}) for a catalog with no ertms messages (raises at load
time below if that combination is wrong either way).

Definition at line 92 of file rbc_messages.py.

Member Function Documentation

◆ build_envelope()

simcore.rbc_messages.MessageCatalog.build_envelope ( self,
message,
train_id,
fields )
@return the raw wire datagram for @message/@train_id/@fields (a
dict of field name -> int, may omit fields to leave them 0).
Raises UnknownMessageError/WrongDirectionError/UnknownFieldError/
InvalidFieldValueError on a bad request - a typo'd name, a
message this sim does not originate, or an out-of-catalog value
(e.g. msg_type=99, not 24 or 15) must fail loudly here, not be
silently sent onto the wire as garbage.

For a "wireFormat": "ertms" message, delegates to this catalog's
own injected ertms_encoders[message] adapter (byte length is
genuinely message-specific, not this method's own concern the
way the flat scheme's l_message computation below is) and wraps
the result with the shared 2-byte _ertmsWrapperKind+train_id
prefix - see this module's own header doc.

Definition at line 315 of file rbc_messages.py.

◆ build_ertms_envelope_raw()

simcore.rbc_messages.MessageCatalog.build_ertms_envelope_raw ( self,
ertms_codec_bytes,
train_id )
@return the wire datagram wrapping already-encoded ertms_codec
bytes with this catalog's own _ertmsWrapperKind+train_id prefix -
the same 2-byte prefix build_envelope() applies for a catalog-
registered "wireFormat": "ertms" message, exposed for a sim's own
internally-driven sends (e.g. a periodic heartbeat) that have no
catalog entry of their own name to look up an encoder by. Raises
if this catalog has no ertms wrapper kind configured at all.

Definition at line 278 of file rbc_messages.py.

◆ decode_to_message()

simcore.rbc_messages.MessageCatalog.decode_to_message ( self,
data )
@return (train_id, message_name, fields_dict) for a raw
received datagram, or None if malformed/unrecognized by THIS
catalog (mirrors rbc_wire.decode()'s own contract -
REQ-RBC-002's Python side) - including a structurally valid
envelope of a kind this sim's own catalog simply does not list
(e.g. a ROUTE_ADD arriving at the Train sim, which should never
happen but is treated the same as any other unrecognized kind,
not a crash). fields_dict holds only the fields THIS message's
own packet defines, not the whole flat envelope.

First checks data[0] against this catalog's own _ertmsWrapperKind
(if any "ertms" messages are declared) and routes to the injected
ertms_codec + ertms_flatteners for that family; falls through to
the original flat-scheme path (_wire.decode()) otherwise - a
single call site works for either family, same as build_envelope().

Definition at line 387 of file rbc_messages.py.

◆ fields_for()

simcore.rbc_messages.MessageCatalog.fields_for ( self,
message,
packet = None )
@return {field_name: spec_dict} for @message. If packet is specified,
returns fields for that specific packet; otherwise returns all fields for the message.

Definition at line 301 of file rbc_messages.py.

◆ is_ertms()

simcore.rbc_messages.MessageCatalog.is_ertms ( self,
message )
@return True if @message is a "wireFormat": "ertms" catalog
entry, False if "flat". Lets a caller decide whether
fields_for(message)'s own field-NAME list is safe to use as a
real, populated-variable-store filter (true for flat messages,
whose "fields" are this project's own snake_case variable names)
or is purely Subset-026 documentation text unrelated to any
variable a caller would actually have stored (true for ertms
messages - see message_catalog.json's own field lists for e.g.
"M129"/"M132": ["NID_MESSAGE", "L_MESSAGE", ..., "Train data
(Packet type 11)"], never matching real stored field names like
"l_train"/"v_maxtrain"). Raises UnknownMessageError if @message
is not in this catalog at all.

Definition at line 261 of file rbc_messages.py.

◆ message_names()

simcore.rbc_messages.MessageCatalog.message_names ( self)
@return every message name in this catalog, sorted.

Definition at line 257 of file rbc_messages.py.

◆ packet_names()

simcore.rbc_messages.MessageCatalog.packet_names ( self)
@return every packet name in this catalog, sorted.

Definition at line 291 of file rbc_messages.py.

◆ packets_for()

simcore.rbc_messages.MessageCatalog.packets_for ( self,
message )
@return dict of packet_name -> packet_spec for @message.

Definition at line 295 of file rbc_messages.py.

Member Data Documentation

◆ _ertms_codec

simcore.rbc_messages.MessageCatalog._ertms_codec = ertms_codec
protected

Definition at line 234 of file rbc_messages.py.

◆ _ertms_encoders

simcore.rbc_messages.MessageCatalog._ertms_encoders = ertms_encoders
protected

Definition at line 235 of file rbc_messages.py.

◆ _ertms_flatteners

simcore.rbc_messages.MessageCatalog._ertms_flatteners = ertms_flatteners
protected

Definition at line 236 of file rbc_messages.py.

◆ _ertms_wrapper_kind

simcore.rbc_messages.MessageCatalog._ertms_wrapper_kind = ertms_wrapper_kind
protected

Definition at line 237 of file rbc_messages.py.

◆ _messages

simcore.rbc_messages.MessageCatalog._messages = messages
protected

Definition at line 232 of file rbc_messages.py.

◆ _name_by_kind

dict simcore.rbc_messages.MessageCatalog._name_by_kind = {spec["kind"]: name for name, spec in messages.items() if spec["wire_format"] == "flat"}
protected

Definition at line 238 of file rbc_messages.py.

◆ _name_by_nid_message_ertms

dict simcore.rbc_messages.MessageCatalog._name_by_nid_message_ertms
protected
Initial value:
= {spec["nid_message"]: name for name, spec in messages.items()
if (spec["wire_format"] == "ertms") and name.startswith("M")}

Definition at line 254 of file rbc_messages.py.

◆ _packets

simcore.rbc_messages.MessageCatalog._packets = packets_catalog
protected

Definition at line 233 of file rbc_messages.py.

◆ _path

simcore.rbc_messages.MessageCatalog._path = path
protected

Definition at line 231 of file rbc_messages.py.


The documentation for this class was generated from the following file: