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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
## timer.rb --- timer implementations for the event loop
# Copyright (C) 2005, 2006 Daniel Brockman
# This program is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation;
# either version 2 of the License, or (at your option) any
# later version.
# This file 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 General Public License for more details.
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
require "event-loop"
class EventLoop
def every (interval, options={}, &body)
options[:event_loop] ||= self
PeriodicTimer.new(interval, options, &body).start
end
def after (interval, options={}, &body)
options[:event_loop] ||= self
SporadicTimer.new(interval, options, &body).start
end
def repeat (&body)
every(0, &body)
end
def later (&body)
after(0, &body)
end
end
class EventLoop::Timer
include SignalEmitter
DEFAULT_TOLERANCE = 0.001
define_opposite_readers :stopped? => :running?
define_readers :interval, :tolerance, :event_loop
define_signal :alarm
def initialize (interval, options={}, &alarm_handler)
EventLoop::Utilities.validate_keyword_arguments options.keys,
[:tolerance, :event_loop]
@running = false
@start_time = nil
@interval = interval
@event_loop = options[:event_loop] || EventLoop.current
@alarm_handler = alarm_handler and
replace_alarm_handler(&@alarm_handler)
if options[:tolerance]
@tolerance = options[:tolerance].to_f
elsif DEFAULT_TOLERANCE < @interval
@tolerance = DEFAULT_TOLERANCE
else
@tolerance = 0.0
end
end
def start_time ; @start_time or
fail "the timer has not been started" end
def end_time ; start_time + @interval end
def time_left ; end_time - Time.now end
def ready? ; time_left <= @tolerance end
def interval= (new_interval)
old_interval = @interval
@interval = new_interval
if running? and new_interval < old_interval
@event_loop.check_timer(self)
end
end
def end_time= (new_end_time)
self.interval = new_end_time - start_time end
def time_left= (new_time_left)
self.end_time = Time.now + new_time_left end
def replace_alarm_handler (&block)
remove_signal_handler(:alarm, @alarm_handler) if @alarm_handler
add_signal_handler(:alarm, &block)
@alarm_handler = block
end
def restart (&block)
running? or raise "the timer is not running"
replace_alarm_handler(&block) if block_given?
returning self do
@start_time = Time.now
end
end
def start (&block)
replace_alarm_handler(&block) if block_given?
returning self do
@start_time = Time.now
@event_loop.monitor_timer(self)
@running = true
end
end
def stop
returning self do
@event_loop.ignore_timer(self)
@running = false
end
end
class << self
define_hard_alias :regular_new => :new
def new (*a, &b)
warn "event-loop: As of event-loop 0.3, `EventLoop::Timer.new' " +
"is deprecated in favor of `EventLoop#every' and " +
"`EventLoop#after'; see the documentation for more information."
new!(*a, &b)
end
def new! (options={}, &body)
if options.kind_of? Numeric
interval = options
options = {}
elsif options.include? :interval
interval = options[:interval].to_f
options.delete(:interval)
else
interval = 0.0
end
EventLoop::Utilities.validate_keyword_arguments options.keys,
[:interval, :tolerance, :start?, :event_loop]
if options.include? :start?
start = options.delete(:start?)
else
start = block_given?
end
timer = EventLoop::PeriodicTimer.new(interval, options)
timer.on_alarm(&body) if block_given?
timer.start if start
return timer
end
end
end
class EventLoop::PeriodicTimer < EventLoop::Timer
class << self
define_soft_alias :new => :regular_new end
def sound_alarm
signal :alarm ; restart if running? end
end
class EventLoop::SporadicTimer < EventLoop::Timer
class << self
define_soft_alias :new => :regular_new end
def sound_alarm
stop ; signal :alarm end
end
class Numeric
def nanoseconds ; self / 1_000_000_000.0 end
def microseconds ; self / 1_000_000.0 end
def milliseconds ; self / 1_000.0 end
def seconds ; self end
def minutes ; self * 60.seconds end
def hours ; self * 60.minutes end
def days ; self * 24.hours end
def weeks ; self * 7.days end
def years ; self * 365.24.days end
define_hard_aliases \
:nanosecond => :nanoseconds,
:microsecond => :microseconds,
:millisecond => :milliseconds,
:second => :seconds,
:minute => :minutes,
:hour => :hours,
:day => :days,
:week => :weeks,
:year => :years
define_hard_aliases \
:ns => :nanoseconds,
:ms => :milliseconds
def half ; self / 2.0 end
def quarter ; self / 4.0 end
def from_now (&block)
EventLoop.after(self, &block)
end
def from_now_and_repeat (&block)
EventLoop.every(self, &block)
end
end
class Integer
# It turns out whole numbers of years are
# always whole numbers of seconds.
def years ; super.to_i end
define_hard_alias :year => :years
end
def Time.measure
t0 = now ; yield ; now - t0
end
if __FILE__ == $0
require "test/unit"
class TimerTest < Test::Unit::TestCase
def setup
EventLoop.current = EventLoop.new
@timer = EventLoop::Timer.new!(:interval => 1.ms)
end
def test_monitor_unstarted_timer
assert_raise RuntimeError do
EventLoop.monitor_timer(@timer)
EventLoop.run
end
end
def test_start_monitoring_timer_while_running
EventLoop.later { 1.ms.from_now { EventLoop.quit } }
1.second.from_now { EventLoop.quit }
assert Time.measure { EventLoop.run } < 1.half.second
end
def test_start_monitoring_timer_while_running_deprecated
@timer.start { EventLoop::Timer.new!(1.ms) { EventLoop.quit } }
EventLoop::Timer.new!(1.second) { EventLoop.quit }
assert Time.measure { EventLoop.run } < 1.half.second
end
def test_timer_tolerance
timer = EventLoop::SporadicTimer.new(10.milliseconds) do
puts "[#{timer.time_left * 1000} milliseconds left on alarm]"
EventLoop.quit end
8.times do
dt = Time.measure { timer.start ; EventLoop.run }
assert(dt > timer.interval - timer.tolerance)
end
end
end
class SporadicTimerTest < Test::Unit::TestCase
def setup
EventLoop.current = EventLoop.new
end
def test_sporadicity
counter = 0
1.nanosecond.from_now { counter += 1 }
5.times { EventLoop.iterate(10.milliseconds) }
assert counter == 1
end
end
class PeriodicTimerTest < Test::Unit::TestCase
def setup
EventLoop.current = EventLoop.new
end
def test_periodicity
counter = 0
EventLoop.every(1.nanosecond) { counter += 1 }
5.times { EventLoop.iterate(10.milliseconds) }
assert counter == 5
end
end
end
## timer.rb ends here.
|