Class: MQTT::Packet::Publish

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

Overview

Class representing an MQTT Publish message

Constant Summary collapse

ATTR_DEFAULTS =

Default attribute values

{
  topic: nil,
  payload: ''
}.freeze

Instance Attribute Summary collapse

Attributes inherited from MQTT::Packet

#body_length, #flags, #id, #version

Instance Method Summary collapse

Methods inherited from MQTT::Packet

create_from_header, #dup, #message_id, #message_id=, parse, parse_header, read, read_byte, #to_s, #type_id, #type_name, #update_attributes

Constructor Details

#initialize(args = {}) ⇒ Publish

Create a new Publish packet



306
307
308
# File 'lib/mqtt/packet.rb', line 306

def initialize(args = {})
  super(ATTR_DEFAULTS.merge(args))
end

Instance Attribute Details

#payloadObject

The data to be published



297
298
299
# File 'lib/mqtt/packet.rb', line 297

def payload
  @payload
end

#topicObject

The topic name to publish to



294
295
296
# File 'lib/mqtt/packet.rb', line 294

def topic
  @topic
end

Instance Method Details

#duplicateObject

Duplicate delivery flag



311
312
313
# File 'lib/mqtt/packet.rb', line 311

def duplicate
  @flags[3]
end

#duplicate=(arg) ⇒ Object

Set the DUP flag (true/false)



316
317
318
# File 'lib/mqtt/packet.rb', line 316

def duplicate=(arg)
  @flags[3] = arg.is_a?(Integer) ? (arg == 0x1) : arg
end

#encode_bodyObject

Get serialisation of packet’s body



345
346
347
348
349
350
351
352
353
# 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

#inspectObject

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



371
372
373
374
375
376
377
378
379
# 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) ⇒ Object

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



356
357
358
359
360
361
# 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

#qosObject

Quality of Service level (0, 1, 2)



331
332
333
# File 'lib/mqtt/packet.rb', line 331

def qos
  (@flags[1] ? 0x01 : 0x00) | (@flags[2] ? 0x02 : 0x00)
end

#qos=(arg) ⇒ Object

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



336
337
338
339
340
341
342
# 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

#retainObject

Retain flag



321
322
323
# File 'lib/mqtt/packet.rb', line 321

def retain
  @flags[0]
end

#retain=(arg) ⇒ Object

Set the retain flag (true/false)



326
327
328
# File 'lib/mqtt/packet.rb', line 326

def retain=(arg)
  @flags[0] = arg.is_a?(Integer) ? (arg == 0x1) : arg
end

#validate_flagsObject

Check that fixed header flags are valid for this packet type

Raises:



365
366
367
368
# 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