]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/timer.rb
try again
[user/henk/code/ruby/rbot.git] / lib / rbot / timer.rb
1 module Timer
2
3   # timer event, something to do and when/how often to do it
4   class Action
5     
6     # when this action is due next (updated by tick())
7     attr_reader :in
8     
9     # is this action blocked? if so it won't be run
10     attr_accessor :blocked
11
12     # period:: how often (seconds) to run the action
13     # data::   optional data to pass to the proc
14     # once::   optional, if true, this action will be run once then removed
15     # func::   associate a block to be called to perform the action
16     # 
17     # create a new action
18     def initialize(period, data=nil, once=false, &func)
19       @blocked = false
20       @period = period
21       @in = period
22       @func = func
23       @data = data
24       @once = once
25       @last_tick = Time.new
26     end
27
28     def tick
29       diff = Time.new - @last_tick
30       @in -= diff
31       @last_tick = Time.new
32     end
33
34     def inspect 
35       "#<#{self.class}:#{@period}s:#{@once ? 'once' : 'repeat'}>"
36     end
37
38     def due?
39       @in <= 0
40     end
41
42     # run the action by calling its proc
43     def run
44       @in += @period
45       # really short duration timers can overrun and leave @in negative,
46       # for these we set @in to @period
47       @in = @period if @in <= 0
48       if(@data)
49         @func.call(@data)
50       else
51         @func.call
52       end
53       return @once
54     end
55   end
56   
57   # timer handler, manage multiple Action objects, calling them when required.
58   # The timer must be ticked by whatever controls it, i.e. regular calls to
59   # tick() at whatever granularity suits your application's needs.
60   # 
61   # Alternatively you can call run(), and the timer will spawn a thread and
62   # tick itself, intelligently shutting down the thread if there are no
63   # pending actions.
64   class Timer
65     def initialize(granularity = 0.1)
66       @granularity = granularity
67       @timers = Hash.new
68       @handle = 0
69       @lasttime = 0
70       @should_be_running = false
71       @thread = false
72       @next_action_time = 0
73     end
74     
75     # period:: how often (seconds) to run the action
76     # data::   optional data to pass to the action's proc
77     # func::   associate a block with add() to perform the action
78     # 
79     # add an action to the timer
80     def add(period, data=nil, &func)
81       @handle += 1
82       @timers[@handle] = Action.new(period, data, &func)
83       start_on_add
84       return @handle
85     end
86
87     # period:: how long (seconds) until the action is run
88     # data::   optional data to pass to the action's proc
89     # func::   associate a block with add() to perform the action
90     # 
91     # add an action to the timer which will be run just once, after +period+
92     def add_once(period, data=nil, &func)
93       @handle += 1
94       @timers[@handle] = Action.new(period, data, true, &func)
95       start_on_add
96       return @handle
97     end
98
99     # remove action with handle +handle+ from the timer
100     def remove(handle)
101       @timers.delete(handle)
102     end
103     
104     # block action with handle +handle+
105     def block(handle)
106       @timers[handle].blocked = true
107     end
108
109     # unblock action with handle +handle+
110     def unblock(handle)
111       @timers[handle].blocked = false
112     end
113
114     # you can call this when you know you're idle, or you can split off a
115     # thread and call the run() method to do it for you.
116     def tick 
117       if(@lasttime == 0)
118         # don't do anything on the first tick
119         @lasttime = Time.now
120         return
121       end
122       @next_action_time = 0
123       diff = (Time.now - @lasttime).to_f
124       @lasttime = Time.now
125       @timers.each { |key,timer|
126         timer.tick
127         next if timer.blocked
128         if(timer.due?)
129           if(timer.run)
130             # run once
131             @timers.delete(key)
132           end
133         end
134         if @next_action_time == 0 || timer.in < @next_action_time
135           @next_action_time = timer.in
136         end
137       }
138     end
139
140     # for backwards compat - this is a bit primitive
141     def run(granularity=0.1)
142       while(true)
143         sleep(granularity)
144         tick
145       end
146     end
147
148     def running?
149       @thread && @thread.alive?
150     end
151
152     # return the number of seconds until the next action is due, or 0 if
153     # none are outstanding - will only be accurate immediately after a
154     # tick()
155     def next_action_time
156       @next_action_time
157     end
158
159     # start the timer, it spawns a thread to tick the timer, intelligently
160     # shutting down if no events remain and starting again when needed.
161     def start
162       return if running?
163       @should_be_running = true
164       start_thread unless @timers.empty?
165     end
166
167     # stop the timer from ticking
168     def stop
169       @should_be_running = false
170       stop_thread
171     end
172     
173     private
174     
175     def start_on_add
176       if running?
177         stop_thread
178         start_thread
179       elsif @should_be_running
180         start_thread
181       end
182     end
183     
184     def stop_thread
185       return unless running?
186       @thread.kill
187     end
188     
189     def start_thread
190       return if running?
191       @thread = Thread.new do
192         while(true)
193           tick
194           exit if @timers.empty?
195           sleep(@next_action_time)
196         end
197       end
198     end
199
200   end
201 end