class MQTT::Packet::Publish

Class representing an MQTT Publish message

Constants

ATTR_DEFAULTS

Default attribute values

Attributes

payload[RW]

The data to be published

topic[RW]

The topic name to publish to

Public Class Methods

new(args = {}) click to toggle source

Create a new Publish packet

Calls superclass method MQTT::Packet::new
# File lib/mqtt/packet.rb, line 306
def initialize(args = {})
  super(ATTR_DEFAULTS.merge(args))
end

Public Instance Methods

duplicate() click to toggle source

Duplicate delivery flag

# File lib/mqtt/packet.rb, line 311
def duplicate
  @flags[3]
end
duplicate=(arg) click to toggle source

Set the DUP flag (true/false)

# File lib/mqtt/packet.rb, line 316
def duplicate=(arg)
  @flags[3] = arg.is_a?(Integer) ? (arg == 0x1) : arg
end
encode_body() click to toggle source

Get serialisation of packet's body

# File lib/mqtt/packet.rb, line 345
def encode_body
  body = ''
  raise 'Invalid topic name when serialising packet' if @topic.nil? || @topic.to_s.empty?

  body += encode_string(@topic)
  body += encode_short(@id) unless qos.zero?
  body += payload.to_s.dup.force_encoding('ASCII-8BIT')
  body
end
inspect() click to toggle source

Returns a human readable string, summarising the properties of the packet

# File lib/mqtt/packet.rb, line 371
def inspect
  "\#<#{self.class}: " \
    "d#{duplicate ? '1' : '0'}, " \
    "q#{qos}, " \
    "r#{retain ? '1' : '0'}, " \
    "m#{id}, " \
    "'#{topic}', " \
    "#{inspect_payload}>"
end
parse_body(buffer) click to toggle source

Parse the body (variable header and payload) of a Publish packet

Calls superclass method MQTT::Packet#parse_body
# File lib/mqtt/packet.rb, line 356
def parse_body(buffer)
  super(buffer)
  @topic = shift_string(buffer)
  @id = shift_short(buffer) unless qos.zero?
  @payload = buffer
end
qos() click to toggle source

Quality of Service level (0, 1, 2)

# File lib/mqtt/packet.rb, line 331
def qos
  (@flags[1] ? 0x01 : 0x00) | (@flags[2] ? 0x02 : 0x00)
end
qos=(arg) click to toggle source

Set the Quality of Service level (0/1/2)

# File lib/mqtt/packet.rb, line 336
def qos=(arg)
  @qos = arg.to_i
  raise "Invalid QoS value: #{@qos}" if @qos < 0 || @qos > 2

  @flags[1] = (arg & 0x01 == 0x01)
  @flags[2] = (arg & 0x02 == 0x02)
end
retain() click to toggle source

Retain flag

# File lib/mqtt/packet.rb, line 321
def retain
  @flags[0]
end
retain=(arg) click to toggle source

Set the retain flag (true/false)

# File lib/mqtt/packet.rb, line 326
def retain=(arg)
  @flags[0] = arg.is_a?(Integer) ? (arg == 0x1) : arg
end
validate_flags() click to toggle source

Check that fixed header flags are valid for this packet type @private

# File lib/mqtt/packet.rb, line 365
def validate_flags
  raise ProtocolException, 'Invalid packet: QoS value of 3 is not allowed' if qos == 3
  raise ProtocolException, 'Invalid packet: DUP cannot be set for QoS 0' if qos.zero? && duplicate
end

Protected Instance Methods

inspect_payload() click to toggle source
# File lib/mqtt/packet.rb, line 383
def inspect_payload
  str = payload.to_s
  if str.bytesize < 16 && str =~ /^[ -~]*$/
    "'#{str}'"
  else
    "... (#{str.bytesize} bytes)"
  end
end