Class: EventMachine::MQTT::Server

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = []) ⇒ Server

Returns a new instance of Server.



8
9
10
11
12
13
14
15
# File 'lib/em-mqtt/server.rb', line 8

def initialize(args=[])
  # Set defaults
  self.address = "0.0.0.0"
  self.port = MQTT::DEFAULT_PORT
  self.logger = Logger.new(STDOUT)
  self.logger.level = Logger::INFO
  parse(args) unless args.empty?
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



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

def address
  @address
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#portObject

Returns the value of attribute port.



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

def port
  @port
end

Instance Method Details

#parse(args) ⇒ Object



17
18
19
20
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
# File 'lib/em-mqtt/server.rb', line 17

def parse(args)
  OptionParser.new("", 24, '  ') 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: #{address})") do |address|
      self.address = address
    end

    opts.on("-p", "--port [PORT]", "port number to run on (default: #{port})") do |port|
      self.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::MQTT::VERSION
      exit
    end

    opts.parse!(args)
  end
end

#runObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/em-mqtt/server.rb', line 50

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

    logger.info("Starting MQTT server on #{address}:#{port}")
    EventMachine.start_server(address, port, EventMachine::MQTT::ServerConnection, logger)
  end
end