Class: EventMachine::MQTTSN::Gateway

Inherits:
Object
  • Object
show all
Defined in:
lib/em/mqtt-sn/gateway.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = []) ⇒ Gateway

Returns a new instance of Gateway.



10
11
12
13
14
15
16
17
18
19
# File 'lib/em/mqtt-sn/gateway.rb', line 10

def initialize(args=[])
  # Set defaults
  self.local_address = "0.0.0.0"
  self.local_port = EventMachine::MQTTSN::DEFAULT_PORT
  self.server_address = "127.0.0.1"
  self.server_port = MQTT::DEFAULT_PORT
  self.logger = Logger.new(STDOUT)
  self.logger.level = Logger::INFO
  parse(args) unless args.empty?
end

Instance Attribute Details

#local_addressObject

Returns the value of attribute local_address.



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

def local_address
  @local_address
end

#local_portObject

Returns the value of attribute local_port.



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

def local_port
  @local_port
end

#loggerObject

Returns the value of attribute logger.



8
9
10
# File 'lib/em/mqtt-sn/gateway.rb', line 8

def logger
  @logger
end

#server_addressObject

Returns the value of attribute server_address.



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

def server_address
  @server_address
end

#server_portObject

Returns the value of attribute server_port.



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

def server_port
  @server_port
end

Instance Method Details

#parse(args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/em/mqtt-sn/gateway.rb', line 21

def parse(args)
  OptionParser.new("", 28, '  ') do |opts|
    opts.banner = "Usage: #{File.basename $0} [options]"

    opts.separator ""
    opts.separator "Options:"

    opts.on("-D", "--debug", "turn on debug logging") do
      self.logger.level = Logger::DEBUG
    end

    opts.on("-a", "--address [HOST]", "bind to HOST address (default: #{local_address})") do |address|
      self.local_address = address
    end

    opts.on("-p", "--port [PORT]", "UDP port number to run on (default: #{local_port})") do |port|
      self.local_port = port
    end

    opts.on("-A", "--server-address [HOST]", "MQTT server address to connect to (default: #{server_address})") do |address|
      self.server_address = address
    end

    opts.on("-P", "--server-port [PORT]", "MQTT server port to connect to (default: #{server_port})") do |port|
      self.server_port = port
    end

    opts.on_tail("-h", "--help", "show this message") do
      puts opts
      exit
    end

    opts.on_tail("--version", "show version") do
      puts EventMachine::MQTTSN::VERSION
      exit
    end

    opts.parse!(args)
  end
end

#runObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/em/mqtt-sn/gateway.rb', line 62

def run
  EventMachine.run do
    # hit Control + C to stop
    Signal.trap("INT")  { EventMachine.stop }
    Signal.trap("TERM") { EventMachine.stop }

    logger.info("Starting MQTT-SN gateway on UDP #{local_address}:#{local_port}")
    logger.info("MQTT server address #{server_address}:#{server_port}")
    EventMachine.open_datagram_socket(
      local_address,
      local_port,
      EventMachine::MQTTSN::GatewayHandler,
      :logger => logger,
      :server_address => server_address,
      :server_port => server_port
    )
  end
end