Class: PahoMqtt::Packet::Connect

Inherits:
PahoMqtt::Packet show all
Defined in:
lib/paho.mqtt/packet_manager.rb

Overview

Class representing an MQTT Connect Packet

Constant Summary collapse

ATTR_DEFAULTS =

Default attribute values

{
  :client_id => nil,
  :clean_session => true,
  :keep_alive => 15,
  :will_topic => nil,
  :will_qos => 0,
  :will_retain => false,
  :will_payload => '',
  :username => nil,
  :password => nil,
}

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 = {}) ⇒ Connect

Create a new Client Connect packet



464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/paho.mqtt/packet_manager.rb', line 464

def initialize(args={})
  super(ATTR_DEFAULTS.merge(args))

  if version == '3.1.0' or version == '3.1'
    self.protocol_name ||= 'MQIsdp'
    self.protocol_level ||= 0x03
  elsif version == '3.1.1'
    self.protocol_name ||= 'MQTT'
    self.protocol_level ||= 0x04
  else
    raise ArgumentError.new("Unsupported protocol version: #{version}")
  end
end

Instance Attribute Details

#clean_sessionObject

Set to false to keep a persistent session with the server



427
428
429
# File 'lib/paho.mqtt/packet_manager.rb', line 427

def clean_session
  @clean_session
end

#client_idObject

The client identifier string



424
425
426
# File 'lib/paho.mqtt/packet_manager.rb', line 424

def client_id
  @client_id
end

#keep_aliveObject

Period the server should keep connection open for between pings



430
431
432
# File 'lib/paho.mqtt/packet_manager.rb', line 430

def keep_alive
  @keep_alive
end

#passwordObject

The password for authenticating with the server



448
449
450
# File 'lib/paho.mqtt/packet_manager.rb', line 448

def password
  @password
end

#protocol_levelObject

The version number of the protocol



421
422
423
# File 'lib/paho.mqtt/packet_manager.rb', line 421

def protocol_level
  @protocol_level
end

#protocol_nameObject

The name of the protocol



418
419
420
# File 'lib/paho.mqtt/packet_manager.rb', line 418

def protocol_name
  @protocol_name
end

#usernameObject

The username for authenticating with the server



445
446
447
# File 'lib/paho.mqtt/packet_manager.rb', line 445

def username
  @username
end

#will_payloadObject

The payload of the Will message



442
443
444
# File 'lib/paho.mqtt/packet_manager.rb', line 442

def will_payload
  @will_payload
end

#will_qosObject

The QoS level to send the Will message as



436
437
438
# File 'lib/paho.mqtt/packet_manager.rb', line 436

def will_qos
  @will_qos
end

#will_retainObject

Set to true to make the Will message retained



439
440
441
# File 'lib/paho.mqtt/packet_manager.rb', line 439

def will_retain
  @will_retain
end

#will_topicObject

The topic name to send the Will message to



433
434
435
# File 'lib/paho.mqtt/packet_manager.rb', line 433

def will_topic
  @will_topic
end

Instance Method Details

#encode_bodyObject

Get serialisation of packet’s body



479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/paho.mqtt/packet_manager.rb', line 479

def encode_body
  body = ''
  if @version == '3.1.0'
    if @client_id.nil? or @client_id.bytesize < 1
      raise "Client identifier too short while serialising packet"
    elsif @client_id.bytesize > 23
      raise "Client identifier too long when serialising packet"
    end
  end
  body += encode_string(@protocol_name)
  body += encode_bytes(@protocol_level.to_i)
  if @keep_alive < 0
    raise "Invalid keep-alive value: cannot be less than 0"
  end

  # Set the Connect flags
  @connect_flags = 0
  @connect_flags |= 0x02 if @clean_session
  @connect_flags |= 0x04 unless @will_topic.nil?
  @connect_flags |= ((@will_qos & 0x03) << 3)
  @connect_flags |= 0x20 if @will_retain
  @connect_flags |= 0x40 unless @password.nil?
  @connect_flags |= 0x80 unless @username.nil?
  body += encode_bytes(@connect_flags)
  body += encode_short(@keep_alive)
  body += encode_string(@client_id)
  unless will_topic.nil?
    body += encode_string(@will_topic)
    # The MQTT v3.1 specification says that the payload is a UTF-8 string
    body += encode_string(@will_payload)
  end
  body += encode_string(@username) unless @username.nil?
  body += encode_string(@password) unless @password.nil?
  return body
end

#inspectObject

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



549
550
551
552
553
554
555
556
557
# File 'lib/paho.mqtt/packet_manager.rb', line 549

def inspect
  str = "\#<#{self.class}: "
  str += "keep_alive=#{keep_alive}"
  str += ", clean" if clean_session
  str += ", client_id='#{client_id}'"
  str += ", username='#{username}'" unless username.nil?
  str += ", password=..." unless password.nil?
  str += ">"
end

#parse_body(buffer) ⇒ Object

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



516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/paho.mqtt/packet_manager.rb', line 516

def parse_body(buffer)
  super(buffer)
  @protocol_name = shift_string(buffer)
  @protocol_level = shift_byte(buffer).to_i
  if @protocol_name == 'MQIsdp' and @protocol_level == 3
    @version = '3.1.0'
  elsif @protocol_name == 'MQTT' and @protocol_level == 4
    @version = '3.1.1'
  else
    raise "Unsupported protocol: #{@protocol_name}/#{@protocol_level}"
  end

  @connect_flags = shift_byte(buffer)
  @clean_session = ((@connect_flags & 0x02) >> 1) == 0x01
  @keep_alive = shift_short(buffer)
  @client_id = shift_string(buffer)
  if ((@connect_flags & 0x04) >> 2) == 0x01
    # Last Will and Testament
    @will_qos = ((@connect_flags & 0x18) >> 3)
    @will_retain = ((@connect_flags & 0x20) >> 5) == 0x01
    @will_topic = shift_string(buffer)
    # The MQTT v3.1 specification says that the payload is a UTF-8 string
    @will_payload = shift_string(buffer)
  end
  if ((@connect_flags & 0x80) >> 7) == 0x01 and buffer.bytesize > 0
    @username = shift_string(buffer)
  end
  if ((@connect_flags & 0x40) >> 6) == 0x01 and buffer.bytesize > 0
    @password = shift_string(buffer)
  end
end