/
dispatcher.rb
124 lines (97 loc) · 2.21 KB
/
dispatcher.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#--
# Copyleft meh. [http://meh.paranoid.pk | meh@paranoici.org]
#
# This file is part of failirc.
#
# failirc is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# failirc is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with failirc. If not, see <http://www.gnu.org/licenses/>.
#++
require 'failirc/server/dispatcher/server'
require 'failirc/server/dispatcher/client'
module IRC; class Server
class Dispatcher
attr_reader :server, :listens_on
def initialize (server)
@server = server
@listens_on = []
@timers = []
end
def reset!
@clients = nil
end
def clients
@clients ||= @listens_on.reduce([]) {|result, server|
result.concat(server.clients)
}
end
def running?; @running; end
def start
@running = true
@listens_on.each {|server|
server.start
}
end
def stop
@listens_on.each {|server|
server.stop
}
@timers.each {|timer|
EM.cancel_timer(timer)
}
end
def data_available
return if @working
@working = true
EM.next_tick {
more = false
clients.each {|client|
if client.handle
more = true
end
}
@working = false
data_available if more
}
end
def listen (options)
server = Server.new(@server, options)
@listens_on.push server
IRC.debug "Starting listening on #{server.host}:#{server.port}#{' (SSL)' if server.ssl?}"
if running?
EM.schedule {
server.start
}
end
end
def set_timeout (*args, &block)
EM.schedule {
EM.add_timer(*args, &block).tap {|timer|
@timers.push(timer)
}
}
end
def set_interval (*args, &block)
EM.schedule {
EM.add_periodic_timer(*args, &block).tap {|timer|
@timers.push(timer)
}
}
end
def clear_timeout (what)
EM.schedule {
EM.cancel_timer(what)
}
end
alias clear_interval clear_timeout
end
end; end