Default attribute values
Duplicate delivery flag
The data to be published
Quality of Service level (0, 1, 2)
Retain flag
The topic name to publish to
Create a new Publish packet
# File lib/mqtt/packet.rb, line 317 def initialize(args={}) super(ATTR_DEFAULTS.merge(args)) end
Set the DUP flag (true/false)
# File lib/mqtt/packet.rb, line 326 def duplicate=(arg) if arg.kind_of?(Integer) @flags[3] = (arg == 0x1) else @flags[3] = arg end end
Get serialisation of packet's body
# File lib/mqtt/packet.rb, line 363 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
Returns a human readable string, summarising the properties of the packet
# File lib/mqtt/packet.rb, line 394 def inspect "\#<#{self.class}: " + "d#{duplicate ? '1' : '0'}, " + "q#{qos}, " + "r#{retain ? '1' : '0'}, " + "m#{id}, " + "'#{topic}', " + "#{inspect_payload}>" end
Parse the body (variable header and payload) of a Publish packet
# File lib/mqtt/packet.rb, line 375 def parse_body(buffer) super(buffer) @topic = shift_string(buffer) @id = shift_short(buffer) unless qos == 0 @payload = buffer end
Set the Quality of Service level (0/1/2)
# File lib/mqtt/packet.rb, line 352 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
Set the retain flag (true/false)
# File lib/mqtt/packet.rb, line 339 def retain=(arg) if arg.kind_of?(Integer) @flags[0] = (arg == 0x1) else @flags[0] = arg end end
Check that fixed header flags are valid for this packet type @private
# File lib/mqtt/packet.rb, line 384 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
# File lib/mqtt/packet.rb, line 406 def inspect_payload str = payload.to_s if str.bytesize < 16 and str =~ /^[ -~]*$/ "'#{str}'" else "... (#{str.bytesize} bytes)" end end