Class: Protocol::MQTT::Packet::Publish

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

Overview

PUBLISH (§3.3) — application message.

Fixed header flags (low nibble of byte 0): DUP(3) | QoS(2..1) | RETAIN(0).

Variable header:

* topic_name (UTF-8); may be empty in v5 when topic_alias is set
* packet_id (u16) — only when qos > 0
* v5 property block

Payload: remaining bytes (application data, opaque BINARY).

Constant Summary collapse

TYPE_ID =
PUBLISH

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, register

Constructor Details

#initialize(topic:, payload:, qos: 0, retain: false, dup: false, packet_id: nil, properties: {}) ⇒ Publish

Returns a new instance of Publish.

Raises:

  • (ArgumentError)


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

def initialize(topic:, payload:, qos: 0, retain: false, dup: false, packet_id: nil, properties: {})
  raise ArgumentError, "qos must be 0..2" unless (0..2).include?(qos)
  raise ArgumentError, "packet_id required for qos > 0" if qos > 0 && packet_id.nil?
  raise ArgumentError, "packet_id forbidden for qos 0" if qos == 0 && !packet_id.nil?
  @topic = topic
  @payload = payload.b
  @qos = qos
  @retain = retain
  @dup = dup
  @packet_id = packet_id
  @properties = properties
end

Instance Attribute Details

#dupObject (readonly)

Returns the value of attribute dup.



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

def dup
  @dup
end

#packet_idObject (readonly)

Returns the value of attribute packet_id.



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

def packet_id
  @packet_id
end

#payloadObject (readonly)

Returns the value of attribute payload.



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

def payload
  @payload
end

#propertiesObject (readonly)

Returns the value of attribute properties.



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

def properties
  @properties
end

#qosObject (readonly)

Returns the value of attribute qos.



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

def qos
  @qos
end

#retainObject (readonly)

Returns the value of attribute retain.



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

def retain
  @retain
end

#topicObject (readonly)

Returns the value of attribute topic.



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

def topic
  @topic
end

Class Method Details

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

Raises:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/protocol/mqtt/packet/publish.rb', line 57

def self.decode_body(reader, flags:, version:)
  dup    = (flags & 0b1000) != 0
  qos    = (flags >> 1) & 0b11
  retain = (flags & 0b0001) != 0
  raise MalformedPacket, "invalid QoS 3 in PUBLISH" if qos == 3
  raise MalformedPacket, "DUP must be 0 when QoS is 0" if dup && qos == 0

  topic = reader.read_utf8
  packet_id = qos > 0 ? reader.read_u16 : nil
  properties = version == 5 ? Property.decode(reader) : {}
  payload = reader.read_rest
  new(
    topic: topic, payload: payload, qos: qos, retain: retain, dup: dup,
    packet_id: packet_id, properties: properties,
  )
end

Instance Method Details

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



75
76
77
78
79
80
81
82
83
84
# File 'lib/protocol/mqtt/packet/publish.rb', line 75

def ==(other)
  other.is_a?(Publish) &&
    other.topic == @topic &&
    other.payload == @payload &&
    other.qos == @qos &&
    other.retain == @retain &&
    other.dup == @dup &&
    other.packet_id == @packet_id &&
    other.properties == @properties
end

#encode_body(version) ⇒ Object



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

def encode_body(version)
  w = Codec::Writer.new
  w.write_utf8(@topic)
  w.write_u16(@packet_id) if @qos > 0
  w.write(Property.encode(@properties)) if version == 5
  w.write(@payload)
  w.bytes
end

#flags_nibble(_version) ⇒ Object



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

def flags_nibble(_version)
  n = 0
  n |= 0b1000 if @dup
  n |= (@qos & 0b11) << 1
  n |= 0b0001 if @retain
  n
end

#hashObject



88
89
90
# File 'lib/protocol/mqtt/packet/publish.rb', line 88

def hash
  [self.class, @topic, @payload, @qos, @retain, @dup, @packet_id, @properties].hash
end