class MqttRails::Packet::Connack

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 MqttRails::Packet::Base::new
# 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

Public Instance Methods

encode_body() click to toggle source

Get serialisation of packet's body

# 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
inspect() click to toggle source

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

# File lib/mqtt_rails/packet/connack.rb, line 97
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 MqttRails::Packet::Base#parse_body
# 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_msg() click to toggle source

Get a string message corresponding to a return code

# 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
session_present=(arg) click to toggle source

Set the Session Present flag

# File lib/mqtt_rails/packet/connack.rb, line 45
def session_present=(arg)
  if arg.kind_of?(Integer)
    @connack_flags[0] = (arg == 0x1)
  else
    @connack_flags[0] = arg
  end
end