Class: Protocol::MQTT::Packet::Pubrec

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

Overview

PUBREC (§3.5) — QoS 2 first acknowledgement. Same body shape as PUBACK.

Constant Summary collapse

TYPE_ID =
PUBREC

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(packet_id:, reason_code: ReasonCodes::SUCCESS, properties: {}) ⇒ Pubrec

Returns a new instance of Pubrec.



17
18
19
20
21
# File 'lib/protocol/mqtt/packet/pubrec.rb', line 17

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

Instance Attribute Details

#packet_idObject (readonly)

Returns the value of attribute packet_id.



14
15
16
# File 'lib/protocol/mqtt/packet/pubrec.rb', line 14

def packet_id
  @packet_id
end

#propertiesObject (readonly)

Returns the value of attribute properties.



14
15
16
# File 'lib/protocol/mqtt/packet/pubrec.rb', line 14

def properties
  @properties
end

#reason_codeObject (readonly)

Returns the value of attribute reason_code.



14
15
16
# File 'lib/protocol/mqtt/packet/pubrec.rb', line 14

def reason_code
  @reason_code
end

Class Method Details

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



35
36
37
38
39
40
41
42
43
44
# File 'lib/protocol/mqtt/packet/pubrec.rb', line 35

def self.decode_body(reader, flags:, version:)
  packet_id = reader.read_u16
  if version == 5 && !reader.eof?
    reason_code = reader.read_u8
    properties = reader.eof? ? {} : Property.decode(reader)
    new(packet_id: packet_id, reason_code: reason_code, properties: properties)
  else
    new(packet_id: packet_id)
  end
end

Instance Method Details

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



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

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

#encode_body(version) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/protocol/mqtt/packet/pubrec.rb', line 24

def encode_body(version)
  w = Codec::Writer.new
  w.write_u16(@packet_id)
  if version == 5 && !(@properties.empty? && @reason_code == ReasonCodes::SUCCESS)
    w.write_u8(@reason_code)
    w.write(Property.encode(@properties)) unless @properties.empty?
  end
  w.bytes
end

#hashObject



56
57
58
# File 'lib/protocol/mqtt/packet/pubrec.rb', line 56

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