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.



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/net/reactor.rb', line 35

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



57
58
59
60
61
62
# File 'lib/net/reactor.rb', line 57

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



47
48
49
50
51
52
53
54
55
# File 'lib/net/reactor.rb', line 47

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

#runObject



64
65
66
67
68
69
70
71
72
# File 'lib/net/reactor.rb', line 64

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

#shutdownObject



74
75
76
# File 'lib/net/reactor.rb', line 74

def shutdown
    @doShutdown = true
end