class MQTT::MQTT::MQTT::Packet::Publish

Class representing an MQTT Publish message

Constants

ATTR_DEFAULTS

Default attribute values

Attributes

duplicate[RW]

Duplicate delivery flag

payload[RW]

The data to be published

qos[RW]

Quality of Service level (0, 1, 2)

retain[RW]

Retain flag

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
# File lib/mqttbridge/packet.rb, line 321
def initialize(args={})
  super(ATTR_DEFAULTS.merge(args))
end

Public Instance Methods

duplicate=(arg) click to toggle source

Set the DUP flag (true/false)

# File lib/mqttbridge/packet.rb, line 330
def duplicate=(arg)
  if arg.kind_of?(Integer)
    @flags[3] = (arg == 0x1)
  else
    @flags[3] = arg
  end
end
encode_body() click to toggle source

Get serialisation of packet's body

# File lib/mqttbridge/packet.rb, line 367
def encode_body
  body = ''
  if @topic.nil? or @topic.to_s.empty?
    raise "Invalid topic name when serialising packet"
  end
  body += encode_string(@topic)
  body += encode_short(@id) unless qos == 0
  body += payload.to_s.dup.force_encoding('ASCII-8BIT')
  return body
end
inspect() click to toggle source

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

# File lib/mqttbridge/packet.rb, line 398
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
# File lib/mqttbridge/packet.rb, line 379
def parse_body(buffer)
  super(buffer)
  @topic = shift_string(buffer)
  @id = shift_short(buffer) unless qos == 0
  @payload = buffer
end
qos=(arg) click to toggle source

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

# File lib/mqttbridge/packet.rb, line 356
def qos=(arg)
  @qos = arg.to_i
  if @qos < 0 or @qos > 2
    raise "Invalid QoS value: #{@qos}"
  else
    @flags[1] = (arg & 0x01 == 0x01)
    @flags[2] = (arg & 0x02 == 0x02)
  end
end
retain=(arg) click to toggle source

Set the retain flag (true/false)

# File lib/mqttbridge/packet.rb, line 343
def retain=(arg)
  if arg.kind_of?(Integer)
    @flags[0] = (arg == 0x1)
  else
    @flags[0] = arg
  end
end
validate_flags() click to toggle source

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

# File lib/mqttbridge/packet.rb, line 388
def validate_flags
  if qos == 3
    raise ProtocolException.new("Invalid packet: QoS value of 3 is not allowed")
  end
  if qos == 0 and duplicate
    raise ProtocolException.new("Invalid packet: DUP cannot be set for QoS 0")
  end
end

Protected Instance Methods

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