Class: MqttRails::Packet::Connack

Inherits:
Base
  • Object
show all
Defined in:
lib/mqtt_rails/packet/connack.rb

Constant Summary collapse

ATTR_DEFAULTS =

Default attribute values

{ :return_code => 0x00 }

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, #validate_flags

Constructor Details

#initialize(args = {}) ⇒ Connack

Create a new Client Connect packet



33
34
35
36
37
# File 'lib/mqtt_rails/packet/connack.rb', line 33

def initialize(args={})
  # We must set flags before other attributes
  @connack_flags = [false, false, false, false, false, false, false, false]
  super(ATTR_DEFAULTS.merge(args))
end

Instance Attribute Details

#return_codeObject

The return code (defaults to 0 for connection accepted)



27
28
29
# File 'lib/mqtt_rails/packet/connack.rb', line 27

def return_code
  @return_code
end

#session_presentObject

Get the Session Present flag



24
25
26
# File 'lib/mqtt_rails/packet/connack.rb', line 24

def session_present
  @session_present
end

Instance Method Details

#encode_bodyObject

Get serialisation of packet’s body



74
75
76
77
78
79
# File 'lib/mqtt_rails/packet/connack.rb', line 74

def encode_body
  body = ''
  body += encode_bits(@connack_flags)
  body += encode_bytes(@return_code.to_i)
  body
end

#inspectObject

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



97
98
99
# File 'lib/mqtt_rails/packet/connack.rb', line 97

def inspect
  "\#<#{self.class}: 0x%2.2X>" % return_code
end

#parse_body(buffer) ⇒ Object

Parse the body (variable header and payload) of a Connect Acknowledgment packet



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/mqtt_rails/packet/connack.rb', line 82

def parse_body(buffer)
  super(buffer)
  @connack_flags = shift_bits(buffer)
  unless @connack_flags[1, 7] == [false, false, false, false, false, false, false]
    raise PacketFormatException.new(
            "Invalid flags in Connack variable header")
  end
  @return_code = shift_byte(buffer)
  unless buffer.empty?
    raise PacketFormatException.new(
            "Extra bytes at end of Connect Acknowledgment packet")
  end
end

#return_msgObject

Get a string message corresponding to a return code



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mqtt_rails/packet/connack.rb', line 54

def return_msg
  case return_code
  when 0x00
    "Connection accepted"
  when 0x01
    raise LowVersionException
  when 0x02
    "Connection refused: client identifier rejected"
  when 0x03
    "Connection refused: server unavailable"
  when 0x04
    "Connection refused: bad user name or password"
  when 0x05
    "Connection refused: not authorised"
  else
    "Connection refused: error code #{return_code}"
  end
end