Class: EasyUpnp::HttpListener

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_upnp/events/http_listener.rb

Defined Under Namespace

Classes: Options

Instance Method Summary collapse

Constructor Details

#initialize(o = {}, &block) ⇒ HttpListener

Returns a new instance of HttpListener.



33
34
35
# File 'lib/easy_upnp/events/http_listener.rb', line 33

def initialize(o = {}, &block)
  @options = Options.new(o, &block)
end

Instance Method Details

#listenObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/easy_upnp/events/http_listener.rb', line 37

def listen
  if !@listen_thread
    @server = WEBrick::HTTPServer.new(
      Port: @options.listen_port,
      AccessLog: [],
      BindAddress: @options.bind_address
    )
    @server.mount('/', NotifyServlet, @options.callback, @options.event_parser.new)
  end

  @listen_thread ||= Thread.new do
    @server.start
  end

  url
end

#shutdownObject

Raises:

  • (RuntimeError)


65
66
67
68
69
70
# File 'lib/easy_upnp/events/http_listener.rb', line 65

def shutdown
  raise RuntimeError, "Illegal state: server is not started" if @listen_thread.nil?

  @listen_thread.kill
  @listen_thread = nil
end

#urlObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/easy_upnp/events/http_listener.rb', line 54

def url
  if !@listen_thread or !@server
    raise RuntimeError, 'Server not started'
  end

  addr = Socket.ip_address_list.detect{|intf| intf.ipv4_private?}
  port = @server.config[:Port]

  "http://#{addr.ip_address}:#{port}"
end