Class: Net::Reactor::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/net/reactor.rb

Instance Method Summary collapse

Constructor Details

#initialize(port, handler, host = '0.0.0.0', interval = 0.20) ⇒ Server

Returns a new instance of Server.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/net/reactor.rb', line 29

def initialize(port, handler, host='0.0.0.0', interval=0.20)
    @socket = TCPServer::new(host, port)
    @handler = handler
    @port = port
    @pollInterval = interval
    @reactor = IO::Reactor.new
    @doShutdown = false
    @connections = []
    
    @reactor.register @socket, :read, &method(:handlePollEvent)
end

Instance Method Details

#handleAccept(socket) ⇒ Object



51
52
53
54
55
56
# File 'lib/net/reactor.rb', line 51

def handleAccept socket
    sock = socket.accept
    hand = Connection.new(sock, @handler, @reactor)
    @reactor.register sock, :read, &hand.method(:handleIOEvent)
    @connections << hand
end

#handlePollEvent(socket, event) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/net/reactor.rb', line 41

def handlePollEvent(socket, event)
    case event
    when :error
        # TODO: log this error
        shutdown
    when :read
        handleAccept(socket)
    end
end

#runObject



58
59
60
61
62
63
64
65
66
# File 'lib/net/reactor.rb', line 58

def run
    until @doShutdown
        @reactor.poll(@pollInterval)
    end
    @connections.each do |connection|
        @reactor.unregister connection.socket
        connection.disconnected
    end
end

#shutdownObject



68
69
70
# File 'lib/net/reactor.rb', line 68

def shutdown
    @doShutdown = true
end