Class: EventMachine::MQTTSN::Packet
- Inherits:
-
Object
- Object
- EventMachine::MQTTSN::Packet
- Defined in:
- lib/em/mqtt-sn/packet.rb
Overview
Class representing a MQTTSN Packet Performs binary encoding and decoding of headers
Direct Known Subclasses
Connack, Connect, Disconnect, Pingreq, Pingresp, Publish, Regack, Register, Suback, Subscribe
Defined Under Namespace
Classes: Connack, Connect, Disconnect, Pingreq, Pingresp, Publish, Regack, Register, Suback, Subscribe
Constant Summary collapse
- DEFAULTS =
{}
Instance Attribute Summary collapse
-
#clean_session ⇒ Object
When true, subscriptions are deleted after disconnect.
-
#duplicate ⇒ Object
Duplicate delivery flag.
-
#qos ⇒ Object
Quality of Service level.
-
#request_will ⇒ Object
Request that gateway prompts for Will.
-
#retain ⇒ Object
Retain flag.
-
#topic_id_type ⇒ Object
One of :normal, :predefined or :short.
Class Method Summary collapse
-
.parse(buffer) ⇒ Object
Parse buffer into new packet object.
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ Packet
constructor
Create a new empty packet.
- #parse_body(buffer) ⇒ Object
-
#to_s ⇒ Object
Serialise the packet.
-
#type_id ⇒ Object
Get the identifer for this packet type.
- #update_attributes(attr = {}) ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Packet
Create a new empty packet
41 42 43 |
# File 'lib/em/mqtt-sn/packet.rb', line 41 def initialize(args={}) update_attributes(self.class::DEFAULTS.merge(args)) end |
Instance Attribute Details
#clean_session ⇒ Object
When true, subscriptions are deleted after disconnect
10 11 12 |
# File 'lib/em/mqtt-sn/packet.rb', line 10 def clean_session @clean_session end |
#duplicate ⇒ Object
Duplicate delivery flag
6 7 8 |
# File 'lib/em/mqtt-sn/packet.rb', line 6 def duplicate @duplicate end |
#qos ⇒ Object
Quality of Service level
7 8 9 |
# File 'lib/em/mqtt-sn/packet.rb', line 7 def qos @qos end |
#request_will ⇒ Object
Request that gateway prompts for Will
9 10 11 |
# File 'lib/em/mqtt-sn/packet.rb', line 9 def request_will @request_will end |
#retain ⇒ Object
Retain flag
8 9 10 |
# File 'lib/em/mqtt-sn/packet.rb', line 8 def retain @retain end |
#topic_id_type ⇒ Object
One of :normal, :predefined or :short
11 12 13 |
# File 'lib/em/mqtt-sn/packet.rb', line 11 def topic_id_type @topic_id_type end |
Class Method Details
.parse(buffer) ⇒ Object
Parse buffer into new packet object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/em/mqtt-sn/packet.rb', line 16 def self.parse(buffer) # Parse the fixed header (length and type) length,type_id,body = buffer.unpack('CCa*') if length == 1 length,type_id,body = buffer.unpack('xnCa*') end # Double-check the length if buffer.length != length raise ProtocolException.new("Length of packet is not the same as the length header") end packet_class = 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 packet.parse_body(body) return packet end |
Instance Method Details
#parse_body(buffer) ⇒ Object
75 76 |
# File 'lib/em/mqtt-sn/packet.rb', line 75 def parse_body(buffer) end |
#to_s ⇒ Object
Serialise the packet
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/em/mqtt-sn/packet.rb', line 60 def to_s # Get the packet's variable header and payload body = self.encode_body # Build up the body length field bytes body_length = body.length if body_length > 65531 raise "Packet too big" elsif body_length > 253 [0x01, body_length + 4, type_id].pack('CnC') + body else [body_length + 2, type_id].pack('CC') + body end end |
#type_id ⇒ Object
Get the identifer for this packet type
52 53 54 55 56 57 |
# File 'lib/em/mqtt-sn/packet.rb', line 52 def type_id PACKET_TYPES.each_pair do |key, value| return key if self.class == value end raise "Invalid packet type: #{self.class}" end |
#update_attributes(attr = {}) ⇒ Object
45 46 47 48 49 |
# File 'lib/em/mqtt-sn/packet.rb', line 45 def update_attributes(attr={}) attr.each_pair do |k,v| send("#{k}=", v) end end |