Class: Protocol::MQTT::Packet::Auth

Inherits:
Protocol::MQTT::Packet show all
Defined in:
lib/protocol/mqtt/packet/auth.rb

Overview

AUTH (§3.15) — v5 only. Enhanced authentication exchange.

Body: reason code (u8) + optional property block. Empty body is equivalent to reason=SUCCESS with no props.

Constant Summary collapse

TYPE_ID =
AUTH

Constants inherited from Protocol::MQTT::Packet

AUTH, CONNACK, CONNECT, DISCONNECT, PINGREQ, PINGRESP, PUBACK, PUBCOMP, PUBLISH, PUBREC, PUBREL, SUBACK, SUBSCRIBE, UNSUBACK, UNSUBSCRIBE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Protocol::MQTT::Packet

decode, decode_from_body, #encode, encode_packet, #flags_nibble, register

Constructor Details

#initialize(reason_code: ReasonCodes::SUCCESS, properties: {}) ⇒ Auth

Returns a new instance of Auth.



20
21
22
23
# File 'lib/protocol/mqtt/packet/auth.rb', line 20

def initialize(reason_code: ReasonCodes::SUCCESS, properties: {})
  @reason_code = reason_code
  @properties = properties
end

Instance Attribute Details

#propertiesObject (readonly)

Returns the value of attribute properties.



17
18
19
# File 'lib/protocol/mqtt/packet/auth.rb', line 17

def properties
  @properties
end

#reason_codeObject (readonly)

Returns the value of attribute reason_code.



17
18
19
# File 'lib/protocol/mqtt/packet/auth.rb', line 17

def reason_code
  @reason_code
end

Class Method Details

.decode_body(reader, flags:, version:) ⇒ Object

Raises:



38
39
40
41
42
43
44
# File 'lib/protocol/mqtt/packet/auth.rb', line 38

def self.decode_body(reader, flags:, version:)
  raise ProtocolError, "AUTH is v5-only" if version != 5
  return new if reader.eof?
  reason_code = reader.read_u8
  properties = reader.eof? ? {} : Property.decode(reader)
  new(reason_code: reason_code, properties: properties)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



47
48
49
50
51
# File 'lib/protocol/mqtt/packet/auth.rb', line 47

def ==(other)
  other.is_a?(Auth) &&
    other.reason_code == @reason_code &&
    other.properties == @properties
end

#encode_body(version) ⇒ Object

Raises:



26
27
28
29
30
31
32
33
34
35
# File 'lib/protocol/mqtt/packet/auth.rb', line 26

def encode_body(version)
  raise ProtocolError, "AUTH is v5-only" if version != 5
  if @properties.empty? && @reason_code == ReasonCodes::SUCCESS
    return "".b.freeze
  end
  w = Codec::Writer.new
  w.write_u8(@reason_code)
  w.write(Property.encode(@properties))
  w.bytes
end

#hashObject



55
56
57
# File 'lib/protocol/mqtt/packet/auth.rb', line 55

def hash
  [self.class, @reason_code, @properties].hash
end