class MQTT::Packet::Connack

Class representing an MQTT Connect Acknowledgment Packet

Constants

ATTR_DEFAULTS

Default attribute values

Attributes

return_code[RW]

The return code (defaults to 0 for connection accepted)

session_present[RW]

Session Present flag

Public Class Methods

new(args = {}) click to toggle source

Create a new Client Connect packet

Calls superclass method MQTT::Packet::new
# File lib/qubitro-mqtt/packet.rb, line 569
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

Public Instance Methods

encode_body() click to toggle source

Get serialisation of packet's body

# File lib/qubitro-mqtt/packet.rb, line 606
def encode_body
  body = ''
  body += encode_bits(@connack_flags)
  body += encode_bytes(@return_code.to_i)
  body
end
inspect() click to toggle source

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

# File lib/qubitro-mqtt/packet.rb, line 627
def inspect
  "\#<#{self.class}: 0x%2.2X>" % return_code
end
parse_body(buffer) click to toggle source

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

Calls superclass method MQTT::Packet#parse_body
# File lib/qubitro-mqtt/packet.rb, line 614
def parse_body(buffer)
  super(buffer)
  @connack_flags = shift_bits(buffer)
  unless @connack_flags[1, 7] == [false, false, false, false, false, false, false]
    raise ProtocolException, 'Invalid flags in Connack variable header'
  end
  @return_code = shift_byte(buffer)
  
  return if buffer.empty?
  raise ProtocolException, 'Extra bytes at end of Connect Acknowledgment packet'
end
return_msg() click to toggle source

Get a string message corresponding to a return code

# File lib/qubitro-mqtt/packet.rb, line 586
def return_msg
  case return_code
  when 0x00
    'Connection Accepted'
  when 0x01
    'Connection refused: unacceptable protocol version'
  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
session_present=(arg) click to toggle source

Set the Session Present flag

# File lib/qubitro-mqtt/packet.rb, line 581
def session_present=(arg)
  @connack_flags[0] = arg.is_a?(Integer) ? (arg == 0x1) : arg
end