lib/mqtt/packet.rb in mqtt-0.0.7 vs lib/mqtt/packet.rb in mqtt-0.0.8
- old
+ new
@@ -1,25 +1,32 @@
module MQTT
# Class representing a MQTT Packet
# Performs binary encoding and decoding of headers
class MQTT::Packet
- attr_reader :dup # Duplicate delivery flag
+ attr_reader :duplicate # Duplicate delivery flag
attr_reader :retain # Retain flag
attr_reader :qos # Quality of Service level
attr_reader :body_length # The length of the parsed packet body
- # Deprecate this: Read in a packet from a socket
+ DEFAULTS = {
+ :duplicate => false,
+ :qos => 0,
+ :retain => false,
+ :body_length => nil
+ }
+
+ # Read in a packet from a socket
def self.read(socket)
# Read in the packet header and work out the class
header = read_byte(socket)
type_id = ((header & 0xF0) >> 4)
packet_class = MQTT::PACKET_TYPES[type_id]
# Create a new packet object
packet = packet_class.new(
- :dup => ((header & 0x08) >> 3),
+ :duplicate => ((header & 0x08) >> 3),
:qos => ((header & 0x06) >> 1),
:retain => ((header & 0x01) >> 0)
)
# Read in the packet length
@@ -58,11 +65,11 @@
raise ProtocolException.new("Invalid packet type identifier: #{type_id}")
end
# Create a new packet object
packet = packet_class.new(
- :dup => ((buffer[0] & 0x08) >> 3) == 0x01,
+ :duplicate => ((buffer[0] & 0x08) >> 3) == 0x01,
:qos => ((buffer[0] & 0x06) >> 1),
:retain => ((buffer[0] & 0x01) >> 0) == 0x01
)
# Parse the packet length
@@ -89,16 +96,11 @@
end
# Create a new empty packet
def initialize(args={})
- update_attributes({
- :dup => false,
- :qos => 0,
- :retain => false,
- :body_length => nil
- }.merge(args))
+ update_attributes(DEFAULTS.merge(args))
end
def update_attributes(attr={})
attr.each_pair do |k,v|
send("#{k}=", v)
@@ -113,15 +115,15 @@
end
return index
end
# Set the dup flag (true/false)
- def dup=(arg)
+ def duplicate=(arg)
if arg.kind_of?(Integer)
- @dup = (arg != 0)
+ @duplicate = (arg != 0)
else
- @dup = arg
+ @duplicate = arg
end
end
# Set the retain flag (true/false)
def retain=(arg)
@@ -160,11 +162,11 @@
# Serialise the packet
def to_s
# Encode the fixed header
header = [
((type_id.to_i & 0x0F) << 4) |
- ((dup ? 0x1 : 0x0) << 3) |
+ ((duplicate ? 0x1 : 0x0) << 3) |
((qos.to_i & 0x03) << 1) |
(retain ? 0x1 : 0x0)
]
# Get the packet's variable header and payload
@@ -247,17 +249,19 @@
class Publish < MQTT::Packet
attr_accessor :topic
attr_accessor :message_id
attr_accessor :payload
- # Create a new Publish packet
- def initialize(args={})
- super({
+ DEFAULTS = {
:topic => nil,
:message_id => 0,
:payload => ''
- }.merge(args))
+ }
+
+ # Create a new Publish packet
+ def initialize(args={})
+ super(DEFAULTS.merge(args))
end
# Get serialisation of packet's body
def encode_body
body = ''
@@ -295,25 +299,27 @@
# OLD deprecated clean_start
alias :clean_start :clean_session
alias :clean_start= :clean_session=
+ DEFAULTS = {
+ :protocol_name => 'MQIsdp',
+ :protocol_version => 0x03,
+ :client_id => nil,
+ :clean_session => true,
+ :keep_alive => 15,
+ :will_topic => nil,
+ :will_qos => 0,
+ :will_retain => false,
+ :will_payload => '',
+ :username => nil,
+ :password => nil,
+ }
+
# Create a new Client Connect packet
def initialize(args={})
- super({
- :protocol_name => 'MQIsdp',
- :protocol_version => 0x03,
- :client_id => nil,
- :clean_session => true,
- :keep_alive => 10,
- :will_topic => nil,
- :will_qos => 0,
- :will_retain => false,
- :will_payload => '',
- :username => nil,
- :password => nil,
- }.merge(args))
+ super(DEFAULTS.merge(args))
end
# Get serialisation of packet's body
def encode_body
body = ''
@@ -370,16 +376,15 @@
end
# Class representing an MQTT Connect Acknowledgment Packet
class Connack < MQTT::Packet
attr_accessor :return_code
+ DEFAULTS = {:return_code => 0x00}
# Create a new Client Connect packet
def initialize(args={})
- super({
- :return_code => 0x00
- }.merge(args))
+ super(DEFAULTS.merge(args))
end
# Get a string message corresponding to a return code
def return_msg
case return_code
@@ -420,16 +425,15 @@
end
# Class representing an MQTT Publish Acknowledgment packet
class Puback < MQTT::Packet
attr_accessor :message_id
+ DEFAULTS = {:message_id => 0}
# Create a new Publish Acknowledgment packet
def initialize(args={})
- super({
- :message_id => 0
- }.merge(args))
+ super(DEFAULTS.merge(args))
end
# Get serialisation of packet's body
def encode_body
encode_short(@message_id)
@@ -446,16 +450,15 @@
end
# Class representing an MQTT Publish Received packet
class Pubrec < MQTT::Packet
attr_accessor :message_id
+ DEFAULTS = {:message_id => 0}
# Create a new Publish Recieved packet
def initialize(args={})
- super({
- :message_id => 0
- }.merge(args))
+ super(DEFAULTS.merge(args))
end
# Get serialisation of packet's body
def encode_body
encode_short(@message_id)
@@ -472,16 +475,15 @@
end
# Class representing an MQTT Publish Release packet
class Pubrel < MQTT::Packet
attr_accessor :message_id
+ DEFAULTS = {:message_id => 0}
# Create a new Publish Release packet
def initialize(args={})
- super({
- :message_id => 0
- }.merge(args))
+ super(DEFAULTS.merge(args))
end
# Get serialisation of packet's body
def encode_body
encode_short(@message_id)
@@ -498,16 +500,15 @@
end
# Class representing an MQTT Publish Complete packet
class Pubcomp < MQTT::Packet
attr_accessor :message_id
+ DEFAULTS = {:message_id => 0}
# Create a new Publish Complete packet
def initialize(args={})
- super({
- :message_id => 0
- }.merge(args))
+ super(DEFAULTS.merge(args))
end
# Get serialisation of packet's body
def encode_body
encode_short(@message_id)
@@ -525,18 +526,17 @@
# Class representing an MQTT Client Subscribe packet
class Subscribe < MQTT::Packet
attr_accessor :message_id
attr_reader :topics
+ DEFAULTS = {:message_id => 0}
# Create a new Subscribe packet
def initialize(args={})
- super({
- :topics => [],
- :message_id => 0
- }.merge(args))
- self.qos = 1 # Force a QOS of 1
+ super(DEFAULTS.merge(args))
+ @topics ||= []
+ @qos = 1 # Force a QOS of 1
end
# Set one or more topics for the Subscrible packet
# The topics parameter should be one of the following:
# * String: subscribe to one topic with QOS 0
@@ -607,17 +607,16 @@
# Class representing an MQTT Subscribe Acknowledgment packet
class Suback < MQTT::Packet
attr_accessor :message_id
attr_reader :granted_qos
+ DEFAULTS = {:message_id => 0}
# Create a new Subscribe Acknowledgment packet
def initialize(args={})
- super({
- :granted_qos => [],
- :message_id => 0
- }.merge(args))
+ super(DEFAULTS.merge(args))
+ @granted_qos ||= []
end
def granted_qos=(value)
unless value.is_a?(Array)
raise "granted QOS should be an array of arrays"
@@ -647,18 +646,17 @@
# Class representing an MQTT Client Unsubscribe packet
class Unsubscribe < MQTT::Packet
attr_reader :topics
attr_accessor :message_id
+ DEFAULTS = {:message_id => 0}
# Create a new Unsubscribe packet
def initialize(args={})
- super({
- :topics => [],
- :message_id => 0
- }.merge(args))
- self.qos = 1 # Force a QOS of 1
+ super(DEFAULTS.merge(args))
+ @topics ||= []
+ @qos = 1 # Force a QOS of 1
end
def topics=(value)
if value.is_a?(Array)
@topics = value
@@ -688,15 +686,14 @@
end
# Class representing an MQTT Unsubscribe Acknowledgment packet
class Unsuback < MQTT::Packet
attr_accessor :message_id
+ DEFAULTS = {:message_id => 0}
# Create a new Unsubscribe Acknowledgment packet
def initialize(args={})
- super({
- :message_id => 0
- }.merge(args))
+ super(DEFAULTS.merge(args))
end
# Get serialisation of packet's body
def encode_body
encode_short(@message_id)