class EventMachine::MQTTSN::ServerConnection

Attributes

client_address[RW]
client_id[RW]
client_port[RW]
gateway_handler[RW]
pending_requests[RW]

Public Class Methods

new(gateway_handler, client_address, client_port) click to toggle source
# File lib/em/mqtt-sn/server_connection.rb, line 9
def initialize(gateway_handler, client_address, client_port)
  @client_address = client_address
  @client_port = client_port
  @gateway_handler = gateway_handler
  @topic_id = 0
  @topic_map = {}
  @pending_requests = {}
end

Public Instance Methods

add_to_pending(packet) click to toggle source

Add a packet to a list of messages that we are expecting a reply to

# File lib/em/mqtt-sn/server_connection.rb, line 55
def add_to_pending(packet)
  @pending_requests[packet.id] = {
    :packet => packet,
    :time => Time.now
  }
end
disconnect() click to toggle source

Politely close the connection to the MQTT server

# File lib/em/mqtt-sn/server_connection.rb, line 72
def disconnect
  send_packet(MQTT::Packet::Disconnect.new)
  @state = :disconnected
  close_connection_after_writing
end
get_topic_id(name) click to toggle source

Get the topic ID for a topic name

# File lib/em/mqtt-sn/server_connection.rb, line 33
def get_topic_id(name)
  if name.length == 2
    return :short, name
  else
    # FIXME: improve this
    @topic_map.each_pair do |key,value|
      if value == name
        return :normal, key
      end
    end
    @topic_id += 1
    @topic_map[@topic_id] = name
    return :normal, @topic_id
  end
end
get_topic_name(id) click to toggle source

Get the topic name for a topic ID

# File lib/em/mqtt-sn/server_connection.rb, line 50
def get_topic_name(id)
  @topic_map[id]
end
process_packet(packet) click to toggle source

Incoming packet from server has been received

# File lib/em/mqtt-sn/server_connection.rb, line 24
def process_packet(packet)
  if packet.class == MQTT::Packet::Connack and packet.return_code == 0
    @state = :connected
  end

  @gateway_handler.relay_from_server(self, packet)
end
remove_from_pending(id) click to toggle source

Remove a packet that we have now received a reply to

# File lib/em/mqtt-sn/server_connection.rb, line 63
def remove_from_pending(id)
  if @pending_requests.has_key?(id)
    request = @pending_requests[id]
    @pending_requests.delete(id)
    return request[:packet]
  end
end
unbind() click to toggle source

TCP connection to server has closed

# File lib/em/mqtt-sn/server_connection.rb, line 19
def unbind
  @gateway_handler.disconnect(self)
end