Class: MQTT::Packet::Connack

Inherits:
MQTT::Packet show all
Defined in:
lib/mqtt/packet.rb

Overview

Class representing an MQTT Connect Acknowledgment Packet

Constant Summary collapse

ATTR_DEFAULTS =

Default attribute values

{ :return_code => 0x00 }

Instance Attribute Summary collapse

Attributes inherited from MQTT::Packet

#body_length, #flags, #id, #version

Instance Method Summary collapse

Methods inherited from MQTT::Packet

create_from_header, #message_id, #message_id=, parse, parse_header, read, read_byte, #to_s, #type_id, #type_name, #update_attributes, #validate_flags

Constructor Details

#initialize(args = {}) ⇒ Connack

Create a new Client Connect packet



566
567
568
569
570
# File 'lib/mqtt/packet.rb', line 566

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)



560
561
562
# File 'lib/mqtt/packet.rb', line 560

def return_code
  @return_code
end

#session_presentObject

Get the Session Present flag



557
558
559
# File 'lib/mqtt/packet.rb', line 557

def session_present
  @session_present
end

Instance Method Details

#encode_bodyObject

Get serialisation of packet’s body



603
604
605
606
607
608
# File 'lib/mqtt/packet.rb', line 603

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



624
625
626
# File 'lib/mqtt/packet.rb', line 624

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

Raises:



611
612
613
614
615
616
617
618
619
620
621
# File 'lib/mqtt/packet.rb', line 611

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_msgObject

Get a string message corresponding to a return code



583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
# File 'lib/mqtt/packet.rb', line 583

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