Class: Biryani::Frame::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/biryani/frame/settings.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ack, stream_id, setting) ⇒ Settings

Returns a new instance of Settings.

Parameters:

  • ack (Boolean)
  • stream_id (Integer)
  • setting (Hash<Integer, Integer>)

    uint16 uint32



9
10
11
12
13
14
# File 'lib/biryani/frame/settings.rb', line 9

def initialize(ack, stream_id, setting)
  @f_type = FrameType::SETTINGS
  @ack = ack
  @stream_id = stream_id
  @setting = setting
end

Instance Attribute Details

#f_typeObject (readonly)

Returns the value of attribute f_type.



4
5
6
# File 'lib/biryani/frame/settings.rb', line 4

def f_type
  @f_type
end

#settingObject (readonly)

Returns the value of attribute setting.



4
5
6
# File 'lib/biryani/frame/settings.rb', line 4

def setting
  @setting
end

#stream_idObject (readonly)

Returns the value of attribute stream_id.



4
5
6
# File 'lib/biryani/frame/settings.rb', line 4

def stream_id
  @stream_id
end

Class Method Details

.read(s, flags, stream_id) ⇒ Settings

rubocop: disable Metrics/AbcSize rubocop: disable Metrics/CyclomaticComplexity rubocop: disable Metrics/PerceivedComplexity

Parameters:

  • s (String)
  • flags (Integer)
  • stream_id (Integer)

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/biryani/frame/settings.rb', line 43

def self.read(s, flags, stream_id)
  return ConnectionError.new(ErrorCode::FRAME_SIZE_ERROR, 'SETTINGS payload length MUST be a multiple of 6') if s.bytesize % 6 != 0

  ack = Frame.read_ack(flags)
  setting = s.unpack('nN' * (s.bytesize / 6)).each_slice(2).to_h
  return ConnectionError.new(ErrorCode::FRAME_SIZE_ERROR, 'SETTINGS MUST NOT have setting with ack') \
    if ack && setting.any?
  return ConnectionError.new(ErrorCode::PROTOCOL_ERROR, 'invalid SETTINGS_ENABLE_PUSH') \
    if setting.key?(SettingsID::SETTINGS_ENABLE_PUSH) && ![0, 1].include?(setting[SettingsID::SETTINGS_ENABLE_PUSH])
  return ConnectionError.new(ErrorCode::PROTOCOL_ERROR, 'invalid SETTINGS_MAX_FRAME_SIZE') \
    if setting.key?(SettingsID::SETTINGS_MAX_FRAME_SIZE) && setting[SettingsID::SETTINGS_MAX_FRAME_SIZE] < 16_384
  return ConnectionError.new(ErrorCode::PROTOCOL_ERROR, 'invalid SETTINGS_MAX_FRAME_SIZE') \
    if setting.key?(SettingsID::SETTINGS_MAX_FRAME_SIZE) && setting[SettingsID::SETTINGS_MAX_FRAME_SIZE] > 16_777_215
  return ConnectionError.new(ErrorCode::FLOW_CONTROL_ERROR, 'invalid SETTINGS_INITIAL_WINDOW_SIZE') \
    if setting.key?(SettingsID::SETTINGS_INITIAL_WINDOW_SIZE) && setting[SettingsID::SETTINGS_INITIAL_WINDOW_SIZE] > 2_147_483_647

  Settings.new(ack, stream_id, setting)
end

Instance Method Details

#ack?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/biryani/frame/settings.rb', line 17

def ack?
  @ack
end

#lengthInteger

Returns:

  • (Integer)


22
23
24
# File 'lib/biryani/frame/settings.rb', line 22

def length
  @setting.length * 6
end

#to_binary_sString

Returns:

  • (String)


27
28
29
30
31
32
33
# File 'lib/biryani/frame/settings.rb', line 27

def to_binary_s
  payload_length = length
  flags = Frame.to_flags(ack: ack?)
  setting = @setting.map { |x| x.pack('nN') }.join

  Frame.to_binary_s_header(payload_length, @f_type, flags, @stream_id) + setting
end