Class: EventMachine::MQTTSN::ServerConnection

Inherits:
EventMachine::MQTT::Connection
  • Object
show all
Defined in:
lib/em/mqtt-sn/server_connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gateway_handler, client_address, client_port) ⇒ ServerConnection

Returns a new instance of ServerConnection.



9
10
11
12
13
14
15
16
# 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

Instance Attribute Details

#client_addressObject

Returns the value of attribute client_address.



4
5
6
# File 'lib/em/mqtt-sn/server_connection.rb', line 4

def client_address
  @client_address
end

#client_idObject

Returns the value of attribute client_id.



6
7
8
# File 'lib/em/mqtt-sn/server_connection.rb', line 6

def client_id
  @client_id
end

#client_portObject

Returns the value of attribute client_port.



5
6
7
# File 'lib/em/mqtt-sn/server_connection.rb', line 5

def client_port
  @client_port
end

#gateway_handlerObject

Returns the value of attribute gateway_handler.



3
4
5
# File 'lib/em/mqtt-sn/server_connection.rb', line 3

def gateway_handler
  @gateway_handler
end

#pending_requestsObject

Returns the value of attribute pending_requests.



7
8
9
# File 'lib/em/mqtt-sn/server_connection.rb', line 7

def pending_requests
  @pending_requests
end

Instance Method Details

#add_to_pending(packet) ⇒ Object

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



55
56
57
58
59
60
# 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

#disconnectObject

Politely close the connection to the MQTT server



72
73
74
75
76
# 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) ⇒ Object

Get the topic ID for a topic name



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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) ⇒ Object

Get the topic name for a topic ID



50
51
52
# File 'lib/em/mqtt-sn/server_connection.rb', line 50

def get_topic_name(id)
  @topic_map[id]
end

#process_packet(packet) ⇒ Object

Incoming packet from server has been received



24
25
26
27
28
29
30
# 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) ⇒ Object

Remove a packet that we have now received a reply to



63
64
65
66
67
68
69
# 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

#unbindObject

TCP connection to server has closed



19
20
21
# File 'lib/em/mqtt-sn/server_connection.rb', line 19

def unbind
  @gateway_handler.disconnect(self)
end