Class: Omnizip::Parity::Models::PacketRegistry
- Inherits:
-
Object
- Object
- Omnizip::Parity::Models::PacketRegistry
- Defined in:
- lib/omnizip/parity/models/packet_registry.rb
Overview
Registry for PAR2 packet types
Maps 16-byte packet type identifiers to their corresponding packet class implementations. Enables polymorphic packet parsing based on type field.
Class Method Summary collapse
-
.clear! ⇒ Object
Clear all registrations (primarily for testing).
-
.get(type_id) ⇒ Class?
Get packet class for type identifier.
-
.read_packet(io) ⇒ Packet?
Parse packet from IO and return appropriate subclass.
-
.register(type_id, klass) ⇒ Object
Register a packet type.
-
.registered?(type_id) ⇒ Boolean
Check if type is registered.
-
.types ⇒ Array<String>
Get all registered packet types.
Class Method Details
.clear! ⇒ Object
Clear all registrations (primarily for testing)
88 89 90 |
# File 'lib/omnizip/parity/models/packet_registry.rb', line 88 def clear! @registry.clear end |
.get(type_id) ⇒ Class?
Get packet class for type identifier
32 33 34 |
# File 'lib/omnizip/parity/models/packet_registry.rb', line 32 def get(type_id) @registry[type_id] end |
.read_packet(io) ⇒ Packet?
Parse packet from IO and return appropriate subclass
Reads a packet header, determines the type, and returns an instance of the appropriate packet subclass.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/omnizip/parity/models/packet_registry.rb', line 58 def read_packet(io) # Read base packet packet = Packet.read_from(io) return nil unless packet # Look up packet class for this type packet_class = get(packet.type) # If unknown type, return base Packet return packet unless packet_class # Create specific packet type specific_packet = packet_class.new( magic: packet.magic, length: packet.length, packet_hash: packet.packet_hash, set_id: packet.set_id, type: packet.type, ) # Set body_data directly (not a lutaml attribute) specific_packet.body_data = packet.body_data # Parse body into structured fields specific_packet.parse_body specific_packet end |
.register(type_id, klass) ⇒ Object
Register a packet type
19 20 21 22 23 24 25 26 |
# File 'lib/omnizip/parity/models/packet_registry.rb', line 19 def register(type_id, klass) unless type_id.bytesize == 16 raise ArgumentError, "Type ID must be 16 bytes, got #{type_id.bytesize}" end @registry[type_id] = klass end |
.registered?(type_id) ⇒ Boolean
Check if type is registered
40 41 42 |
# File 'lib/omnizip/parity/models/packet_registry.rb', line 40 def registered?(type_id) @registry.key?(type_id) end |
.types ⇒ Array<String>
Get all registered packet types
47 48 49 |
# File 'lib/omnizip/parity/models/packet_registry.rb', line 47 def types @registry.keys end |