Class: Arf::Wire::BaseFrame

Inherits:
Object
  • Object
show all
Defined in:
lib/arf/wire/frames/base_frame.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(frame = nil) {|_self| ... } ⇒ BaseFrame

Returns a new instance of BaseFrame.

Yields:

  • (_self)

Yield Parameters:



48
49
50
51
52
53
54
55
56
57
# File 'lib/arf/wire/frames/base_frame.rb', line 48

def initialize(frame = nil)
  if frame
    frame.validate_kind(frame_kind, wants_stream_id?)
    frame.validate_size(value_size) unless value_size.nil?
    @stream_id = frame.stream_id if wants_stream_id?
    decode_flags(frame)
    from_frame(frame)
  end
  yield self if block_given?
end

Class Method Details

.define_flag(name, offset) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/arf/wire/frames/base_frame.rb', line 32

def self.define_flag(name, offset)
  @flags ||= {}
  @flags[name] = offset
  define_method("#{name}!") do
    instance_variable_set("@#{name}", true)
  end

  define_method("#{name}?") do
    instance_variable_get("@#{name}") || false
  end
end

.flagsObject



44
45
46
# File 'lib/arf/wire/frames/base_frame.rb', line 44

def self.flags
  @flags || {}
end

.frame_by_kind(kind) ⇒ Object



11
# File 'lib/arf/wire/frames/base_frame.rb', line 11

def self.frame_by_kind(kind) = @frames[kind]

.frame_kind(name = nil) ⇒ Object



13
14
15
16
# File 'lib/arf/wire/frames/base_frame.rb', line 13

def self.frame_kind(name = nil)
  BaseFrame.register_frame(name, self) if name
  @frame_kind ||= name
end

.register_frame(kind, cls) ⇒ Object



6
7
8
9
# File 'lib/arf/wire/frames/base_frame.rb', line 6

def self.register_frame(kind, cls)
  @frames ||= {}
  @frames[kind] = cls
end

.value_size(size = nil) ⇒ Object



18
19
20
# File 'lib/arf/wire/frames/base_frame.rb', line 18

def self.value_size(size = nil)
  @value_size ||= size
end

.wants_stream_id!Object



22
23
24
25
26
# File 'lib/arf/wire/frames/base_frame.rb', line 22

def self.wants_stream_id!
  attr_accessor :stream_id

  @wants_stream_id = true
end

.wants_stream_id?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/arf/wire/frames/base_frame.rb', line 28

def self.wants_stream_id?
  @wants_stream_id || false
end

Instance Method Details

#decode_flags(frame) ⇒ Object



89
90
91
92
93
# File 'lib/arf/wire/frames/base_frame.rb', line 89

def decode_flags(frame)
  flags.each_pair do |name, offset|
    send("#{name}!") if frame.flags.anybits?((0x01 << offset))
  end
end

#decode_uint16Object



78
# File 'lib/arf/wire/frames/base_frame.rb', line 78

def decode_uint16(*) = Wire.decode_uint16(*)

#decode_uint32Object



79
# File 'lib/arf/wire/frames/base_frame.rb', line 79

def decode_uint32(*) = Wire.decode_uint32(*)

#encode_flagsObject



81
82
83
84
85
86
87
# File 'lib/arf/wire/frames/base_frame.rb', line 81

def encode_flags
  value = 0x00
  flags.each_pair do |name, offset|
    value |= (0x01 << offset) if send("#{name}?")
  end
  value
end

#encode_payloadObject



63
# File 'lib/arf/wire/frames/base_frame.rb', line 63

def encode_payload = nil

#encode_uint16Object

:nocov:



76
# File 'lib/arf/wire/frames/base_frame.rb', line 76

def encode_uint16(*) = Wire.encode_uint16(*)

#encode_uint32Object



77
# File 'lib/arf/wire/frames/base_frame.rb', line 77

def encode_uint32(*) = Wire.encode_uint32(*)

#flagsObject



64
# File 'lib/arf/wire/frames/base_frame.rb', line 64

def flags = self.class.flags

#frame_kindObject



59
# File 'lib/arf/wire/frames/base_frame.rb', line 59

def frame_kind = self.class.frame_kind

#from_frame(_frame) ⇒ Object



62
# File 'lib/arf/wire/frames/base_frame.rb', line 62

def from_frame(_frame) = nil

#inspect_flagsObject

:nocov:



68
69
70
71
72
# File 'lib/arf/wire/frames/base_frame.rb', line 68

def inspect_flags
  flags.keys.to_h do |k|
    [k, instance_variable_get("@#{k}")]
  end
end

#to_frameObject



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/arf/wire/frames/base_frame.rb', line 95

def to_frame
  Frame.new.tap do |f|
    f.stream_id = stream_id if wants_stream_id?
    f.frame_kind = frame_kind
    f.flags = encode_flags
    payload = encode_payload
    payload = StringIO.new(payload) if payload && !payload.is_a?(StringIO)
    f.length = (payload || "").length
    f.payload = payload
  end
end

#value_sizeObject



61
# File 'lib/arf/wire/frames/base_frame.rb', line 61

def value_size = self.class.value_size

#wants_stream_id?Boolean

Returns:

  • (Boolean)


60
# File 'lib/arf/wire/frames/base_frame.rb', line 60

def wants_stream_id? = self.class.wants_stream_id?