Class: PahoMqtt::Packet::Publish

Inherits:
PahoMqtt::Packet show all
Defined in:
lib/paho.mqtt/packet_manager.rb

Overview

Class representing an MQTT Publish message

Constant Summary collapse

ATTR_DEFAULTS =

Default attribute values

{
  :topic => nil,
  :payload => ''
}

Instance Attribute Summary collapse

Attributes inherited from PahoMqtt::Packet

#body_length, #flags, #id, #version

Instance Method Summary collapse

Methods inherited from PahoMqtt::Packet

create_from_header, parse, parse_header, read, #to_s, #type_id, #type_name, #update_attributes

Constructor Details

#initialize(args = {}) ⇒ Publish

Create a new Publish packet



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

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

Instance Attribute Details

#duplicateObject

Duplicate delivery flag



295
296
297
# File 'lib/paho.mqtt/packet_manager.rb', line 295

def duplicate
  @duplicate
end

#payloadObject

The data to be published



307
308
309
# File 'lib/paho.mqtt/packet_manager.rb', line 307

def payload
  @payload
end

#qosObject

Quality of Service level (0, 1, 2)



301
302
303
# File 'lib/paho.mqtt/packet_manager.rb', line 301

def qos
  @qos
end

#retainObject

Retain flag



298
299
300
# File 'lib/paho.mqtt/packet_manager.rb', line 298

def retain
  @retain
end

#topicObject

The topic name to publish to



304
305
306
# File 'lib/paho.mqtt/packet_manager.rb', line 304

def topic
  @topic
end

Instance Method Details

#encode_bodyObject

Get serialisation of packet’s body



362
363
364
365
366
367
368
369
370
371
# File 'lib/paho.mqtt/packet_manager.rb', line 362

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

#inspectObject

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



393
394
395
396
397
398
399
400
401
# File 'lib/paho.mqtt/packet_manager.rb', line 393

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



374
375
376
377
378
379
# File 'lib/paho.mqtt/packet_manager.rb', line 374

def parse_body(buffer)
  super(buffer)
  @topic = shift_string(buffer)
  @id = shift_short(buffer) unless qos == 0
  @payload = buffer
end

#validate_flagsObject

Check that fixed header flags are valid for this packet type



383
384
385
386
387
388
389
390
# File 'lib/paho.mqtt/packet_manager.rb', line 383

def validate_flags
  if qos == 3
    raise "Invalid packet: QoS value of 3 is not allowed"
  end
  if qos == 0 and duplicate
    raise "Invalid packet: DUP cannot be set for QoS 0"
  end
end