Class: PahoMqtt::Packet::Connack

Inherits:
PahoMqtt::Packet show all
Defined in:
lib/paho.mqtt/packet_manager.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 PahoMqtt::Packet

#body_length, #flags, #id, #version

Instance Method Summary collapse

Methods inherited from PahoMqtt::Packet

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



572
573
574
575
576
# File 'lib/paho.mqtt/packet_manager.rb', line 572

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)



566
567
568
# File 'lib/paho.mqtt/packet_manager.rb', line 566

def return_code
  @return_code
end

#session_presentObject

Get the Session Present flag



563
564
565
# File 'lib/paho.mqtt/packet_manager.rb', line 563

def session_present
  @session_present
end

Instance Method Details

#encode_bodyObject

Get serialisation of packet’s body



613
614
615
616
617
618
# File 'lib/paho.mqtt/packet_manager.rb', line 613

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

#inspectObject

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



634
635
636
# File 'lib/paho.mqtt/packet_manager.rb', line 634

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



621
622
623
624
625
626
627
628
629
630
631
# File 'lib/paho.mqtt/packet_manager.rb', line 621

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

#return_msgObject

Get a string message corresponding to a return code



593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
# File 'lib/paho.mqtt/packet_manager.rb', line 593

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