Class: Timer
Overview
Timer handler, manage multiple Action objects, calling them when required. When the Timer is constructed, a new Thread is created to manage timed delays and run Actions.
XXX: there is no way to stop the timer currently. I’m keeping it this way to weed out old Timer implementation legacy in rbot code. -jsn.
Defined Under Namespace
Classes: Action
Instance Method Summary collapse
-
#add(period, opts = {}, &block) ⇒ Object
Creates and installs a new Action, repeatable by default.
-
#add_once(period, opts = {}, &block) ⇒ Object
Creates and installs a new Action, one-time by default.
-
#block(aid) ⇒ Object
- blocks an existing Action aid
-
Action id, obtained previously from add() or add_once().
-
#configure(aid, opts = {}, &block) ⇒ Object
- Provides for on-the-fly reconfiguration of Actions aid
- Action id, obtained previously from add() or add_once() opts
- see Action#new block
-
(optional) new Action callback code.
-
#initialize ⇒ Timer
constructor
creates a new Timer and starts it.
-
#remove(aid) ⇒ Object
(also: #delete)
- removes an existing blocked Action aid
-
Action id, obtained previously from add() or add_once().
-
#reschedule(aid, period, &block) ⇒ Object
- changes Action period aid
- Action id period
- new period block
-
(optional) new Action callback code.
- #start ⇒ Object
- #stop ⇒ Object
-
#unblock(aid) ⇒ Object
- unblocks an existing blocked Action aid
-
Action id, obtained previously from add() or add_once().
Constructor Details
#initialize ⇒ Timer
creates a new Timer and starts it.
123 124 125 126 127 128 129 130 |
# File 'lib/rbot/timer.rb', line 123 def initialize self.extend(MonitorMixin) @tick = self.new_cond @thread = nil @actions = Hash.new @current = nil self.start end |
Instance Method Details
#add(period, opts = {}, &block) ⇒ Object
Creates and installs a new Action, repeatable by default.
- period
-
Action period
- opts
-
options for Action#new, see there
- block
-
Action callback code
Returns the id of the created Action
138 139 140 141 142 143 144 145 |
# File 'lib/rbot/timer.rb', line 138 def add(period, opts = {}, &block) a = Action.new({:repeat => true, :period => period}.merge(opts), &block) self.synchronize do @actions[a.object_id] = a @tick.signal end return a.object_id end |
#add_once(period, opts = {}, &block) ⇒ Object
Creates and installs a new Action, one-time by default.
- period
-
Action delay
- opts
-
options for Action#new, see there
- block
-
Action callback code
Returns the id of the created Action
153 154 155 |
# File 'lib/rbot/timer.rb', line 153 def add_once(period, opts = {}, &block) self.add(period, {:repeat => false}.merge(opts), &block) end |
#block(aid) ⇒ Object
blocks an existing Action
- aid
-
Action id, obtained previously from add() or add_once()
159 160 161 162 |
# File 'lib/rbot/timer.rb', line 159 def block(aid) debug "blocking #{aid}" self.synchronize { self[aid].block } end |
#configure(aid, opts = {}, &block) ⇒ Object
Provides for on-the-fly reconfiguration of Actions
- aid
-
Action id, obtained previously from add() or add_once()
- opts
-
see Action#new
- block
-
(optional) new Action callback code
188 189 190 191 192 193 |
# File 'lib/rbot/timer.rb', line 188 def configure(aid, opts = {}, &block) self.synchronize do self[aid].configure(opts, &block) @tick.signal end end |
#remove(aid) ⇒ Object Also known as: delete
removes an existing blocked Action
- aid
-
Action id, obtained previously from add() or add_once()
176 177 178 179 180 |
# File 'lib/rbot/timer.rb', line 176 def remove(aid) self.synchronize do @actions.delete(aid) # or raise "nonexistent action #{aid}" end end |
#reschedule(aid, period, &block) ⇒ Object
changes Action period
- aid
-
Action id
- period
-
new period
- block
-
(optional) new Action callback code
199 200 201 |
# File 'lib/rbot/timer.rb', line 199 def reschedule(aid, period, &block) self.configure(aid, :period => period, &block) end |
#start ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/rbot/timer.rb', line 203 def start raise 'already started' if @thread @stopping = false debug "starting timer #{self}" @thread = Thread.new do loop do tmout = self.run_actions break if tmout and tmout < 0 self.synchronize { @tick.wait(tmout) } end end end |
#stop ⇒ Object
216 217 218 219 220 221 222 223 224 |
# File 'lib/rbot/timer.rb', line 216 def stop raise 'already stopped' unless @thread debug "stopping timer #{self}..." @stopping = true self.synchronize { @tick.signal } @thread.join(60) or @thread.kill debug "timer #{self} stopped" @thread = nil end |