Class: Omnizip::Parity::Models::PacketRegistry

Inherits:
Object
  • Object
show all
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

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

Parameters:

  • type_id (String)

    16-byte type identifier

Returns:

  • (Class, nil)

    Packet class or nil if not registered



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.

Parameters:

  • io (IO)

    Input stream

Returns:

  • (Packet, nil)

    Parsed packet or nil if EOF/unknown type



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

Parameters:

  • type_id (String)

    16-byte type identifier

  • klass (Class)

    Packet class



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

Parameters:

  • type_id (String)

    16-byte type identifier

Returns:

  • (Boolean)

    true 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

.typesArray<String>

Get all registered packet types

Returns:

  • (Array<String>)

    Array of 16-byte type identifiers



47
48
49
# File 'lib/omnizip/parity/models/packet_registry.rb', line 47

def types
  @registry.keys
end