Class: PahoMqtt::Packet::Publish

Inherits:
Base
  • Object
show all
Defined in:
lib/paho_mqtt/packet/publish.rb

Constant Summary collapse

ATTR_DEFAULTS =

Default attribute values

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

Instance Attribute Summary collapse

Attributes inherited from Base

#body_length, #flags, #id, #version

Instance Method Summary collapse

Methods inherited from Base

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



28
29
30
# File 'lib/paho_mqtt/packet/publish.rb', line 28

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

Instance Attribute Details

#duplicateObject

Duplicate delivery flag



7
8
9
# File 'lib/paho_mqtt/packet/publish.rb', line 7

def duplicate
  @duplicate
end

#payloadObject

The data to be published



19
20
21
# File 'lib/paho_mqtt/packet/publish.rb', line 19

def payload
  @payload
end

#qosObject

Quality of Service level (0, 1, 2)



13
14
15
# File 'lib/paho_mqtt/packet/publish.rb', line 13

def qos
  @qos
end

#retainObject

Retain flag



10
11
12
# File 'lib/paho_mqtt/packet/publish.rb', line 10

def retain
  @retain
end

#topicObject

The topic name to publish to



16
17
18
# File 'lib/paho_mqtt/packet/publish.rb', line 16

def topic
  @topic
end

Instance Method Details

#encode_bodyObject

Get serialisation of packet’s body



74
75
76
77
78
79
80
81
82
83
# File 'lib/paho_mqtt/packet/publish.rb', line 74

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



105
106
107
108
109
110
111
112
113
# File 'lib/paho_mqtt/packet/publish.rb', line 105

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



86
87
88
89
90
91
# File 'lib/paho_mqtt/packet/publish.rb', line 86

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



95
96
97
98
99
100
101
102
# File 'lib/paho_mqtt/packet/publish.rb', line 95

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