class EventMachine::MQTT::Connection

Attributes

last_received[R]
last_sent[R]
state[R]

Public Instance Methods

connected?() click to toggle source

Checks whether a connection is full established

# File lib/em/mqtt/connection.rb, line 17
def connected?
  state == :connected
end
post_init() click to toggle source
# File lib/em/mqtt/connection.rb, line 8
def post_init
  @state = :connecting
  @last_sent = 0
  @last_received = 0
  @packet = nil
  @data = ''
end
process_packet(packet) click to toggle source

The function needs to be sub-classed

# File lib/em/mqtt/connection.rb, line 44
def process_packet(packet)
end
receive_data(data) click to toggle source
# File lib/em/mqtt/connection.rb, line 21
def receive_data(data)
  @data << data

  # FIXME: limit maximum data / packet size

  # Are we at the start of a new packet?
  if @packet.nil? and @data.length >= 2
    @packet = MQTT::Packet.parse_header(@data)
  end

  # Do we have the the full packet body now?
  if @packet and @data.length >= @packet.body_length
    @packet.parse_body(
      @data.slice!(0...@packet.body_length)
    )
    @last_received = Time.now
    process_packet(@packet)
    @packet = nil
    receive_data ''
  end
end
send_packet(packet) click to toggle source
# File lib/em/mqtt/connection.rb, line 47
def send_packet(packet)
  # FIXME: Throw exception if we aren't connected?
  #unless packet.class == MQTT::Packet::Connect
  #  raise MQTT::NotConnectedException if not connected?
  #end

  send_data(packet.to_s)
  @last_sent = Time.now
end