Class: RubyHome::HAP::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_home/hap/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ Server

Returns a new instance of Server.



4
5
6
7
8
9
# File 'lib/ruby_home/hap/server.rb', line 4

def initialize(host, port)
  @port = port
  @host = host
  @selector = NIO::Selector.new
  @status = :running
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



11
12
13
# File 'lib/ruby_home/hap/server.rb', line 11

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



11
12
13
# File 'lib/ruby_home/hap/server.rb', line 11

def port
  @port
end

Instance Method Details

#runObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ruby_home/hap/server.rb', line 13

def run
  puts "Listening on #{host}:#{port}"
  @server = TCPServer.new(host, port)

  monitor = @selector.register(@server, :r)
  monitor.value = proc { accept }

  loop do
    if @status == :running
      @selector.select { |monitor| monitor.value.call(monitor) }
    else
      @selector.close
    end
  end
end

#shutdownObject



29
30
31
# File 'lib/ruby_home/hap/server.rb', line 29

def shutdown
  @status = :shutdown
end