Module: PacketGen::Headerable
- Included in:
- PacketGen::Header::ASN1Base, PacketGen::Header::Base
- Defined in:
- lib/packetgen/headerable.rb
Overview
This mixin module defines minimal API for a class to act as a header in Packet.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.included(klass) ⇒ void
private
Extend
klass
with ClassMethods.
Instance Method Summary collapse
- #added_to_packet(packet) ⇒ void abstract
-
#method_name ⇒ String
return header method name.
-
#packet ⇒ Packet?
Reference on packet which owns this header.
-
#packet=(packet) ⇒ Packet
private
Set packet to which this header belongs.
-
#parse? ⇒ Boolean
abstract
Called by Packet#parse when guessing first header to check if header is correct.
-
#protocol_name ⇒ String
Return header protocol name.
-
#read(str) ⇒ self
abstract
Populate headerable object from a binary string.
Class Method Details
.included(klass) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Extend klass
with ClassMethods.
35 36 37 |
# File 'lib/packetgen/headerable.rb', line 35 def self.included(klass) klass.extend ClassMethods end |
Instance Method Details
#added_to_packet(packet) ⇒ void
This method is called when a header is added to a packet. This base method does nothing but may be overriden by subclasses.
This method returns an undefined value.
81 |
# File 'lib/packetgen/headerable.rb', line 81 def added_to_packet(packet) end |
#method_name ⇒ String
return header method name
47 48 49 50 51 |
# File 'lib/packetgen/headerable.rb', line 47 def method_name return @method_name if defined? @method_name @method_name = protocol_name.downcase.gsub('::', '_') end |
#packet ⇒ Packet?
Reference on packet which owns this header
63 64 65 |
# File 'lib/packetgen/headerable.rb', line 63 def packet @packet ||= nil end |
#packet=(packet) ⇒ Packet
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Set packet to which this header belongs
71 72 73 74 75 |
# File 'lib/packetgen/headerable.rb', line 71 def packet=(packet) @packet = packet added_to_packet(packet) @packet end |
#parse? ⇒ Boolean
Should be redefined by subclasses. This method should check invariant fields from header.
Called by Packet#parse when guessing first header to check if header is correct
57 58 59 |
# File 'lib/packetgen/headerable.rb', line 57 def parse? true end |
#protocol_name ⇒ String
Return header protocol name
41 42 43 |
# File 'lib/packetgen/headerable.rb', line 41 def protocol_name self.class.protocol_name end |
#read(str) ⇒ self
This method MUST be redefined by subclasses.
Populate headerable object from a binary string.
88 89 90 91 92 93 |
# File 'lib/packetgen/headerable.rb', line 88 def read(str) # Do not call super and rescue NoMethodError: too slow raise NotImplementedError, "#{self.class} should implement #read" if method(:read).super_method.nil? super end |