lib/mqtt/packet.rb in mqtt-0.0.8 vs lib/mqtt/packet.rb in mqtt-0.0.9
- old
+ new
@@ -57,32 +57,32 @@
# Parse the header and create a new packet object of the correct type
# The header is removed from the buffer passed into this function
def self.parse_header(buffer)
# Work out the class
- type_id = ((buffer[0] & 0xF0) >> 4)
+ type_id = ((buffer.unpack("C*")[0] & 0xF0) >> 4)
packet_class = MQTT::PACKET_TYPES[type_id]
if packet_class.nil?
raise ProtocolException.new("Invalid packet type identifier: #{type_id}")
end
# Create a new packet object
packet = packet_class.new(
- :duplicate => ((buffer[0] & 0x08) >> 3) == 0x01,
- :qos => ((buffer[0] & 0x06) >> 1),
- :retain => ((buffer[0] & 0x01) >> 0) == 0x01
+ :duplicate => ((buffer.unpack("C*")[0] & 0x08) >> 3) == 0x01,
+ :qos => ((buffer.unpack("C*")[0] & 0x06) >> 1),
+ :retain => ((buffer.unpack("C*")[0] & 0x01) >> 0) == 0x01
)
# Parse the packet length
body_length = 0
multiplier = 1
pos = 1
begin
if buffer.length <= pos
raise ProtocolException.new("The packet length header is incomplete")
end
- digit = buffer[pos]
+ digit = buffer.unpack("C*")[pos]
body_length += ((digit & 0x7F) * multiplier)
multiplier *= 0x80
pos += 1
end while ((digit & 0x80) != 0x00) and pos <= 4
@@ -615,32 +615,37 @@
def initialize(args={})
super(DEFAULTS.merge(args))
@granted_qos ||= []
end
+ # Set the granted QOS value for each of the topics that were subscribed to
+ # Can either be an integer or an array or integers.
def granted_qos=(value)
- unless value.is_a?(Array)
- raise "granted QOS should be an array of arrays"
+ if value.is_a?(Array)
+ @granted_qos = value
+ elsif value.is_a?(Integer)
+ @granted_qos = [value]
+ else
+ raise "granted QOS should be an integer or an array of QOS levels"
end
- @granted_qos = value
end
# Get serialisation of packet's body
def encode_body
if @granted_qos.empty?
raise "no granted QOS given when serialising packet"
end
body = encode_short(@message_id)
- granted_qos.flatten.each { |qos| body += encode_bytes(qos) }
+ granted_qos.each { |qos| body += encode_bytes(qos) }
return body
end
# Parse the body (variable header and payload) of a packet
def parse_body(buffer)
super(buffer)
@message_id = shift_short(buffer)
while(buffer.length>0)
- @granted_qos << [shift_byte(buffer),shift_byte(buffer)]
+ @granted_qos << shift_byte(buffer)
end
end
end
# Class representing an MQTT Client Unsubscribe packet