class QuartzTorrent::TimerManager

Class used to manage timers.

Public Class Methods

new(logger = nil) click to toggle source
# File lib/quartz_torrent/timermanager.rb, line 31
def initialize(logger = nil)
  @queue = PQueue.new { |a,b| b.expiry <=> a.expiry }
  @mutex = Mutex.new
  @logger = logger
end

Public Instance Methods

add(duration, metainfo = nil, recurring = true, immed = false) click to toggle source

Add a timer. Parameter ‘duration’ specifies the timer duration in seconds, ‘metainfo’ is caller information passed to the handler when the timer expires, ‘recurring’ should be true if the timer will repeat, or false if it will only expire once, and ‘immed’ when true specifies that the timer should expire immediately (and again each duration if recurring) while false specifies that the timer will only expire the first time after it’s duration elapses.

# File lib/quartz_torrent/timermanager.rb, line 43
def add(duration, metainfo = nil, recurring = true, immed = false)
  raise "TimerManager.add: Timer duration may not be nil" if duration.nil?
  info = TimerInfo.new(duration, recurring, metainfo)
  info.expiry = Time.new if immed
  @mutex.synchronize{ @queue.push info }
  info
end
add_cancelled(duration, metainfo = nil, recurring = true, immed = false) click to toggle source

For testing. Add a cancelled timer.

# File lib/quartz_torrent/timermanager.rb, line 52
def add_cancelled(duration, metainfo = nil, recurring = true, immed = false)
  raise "TimerManager.add: Timer duration may not be nil" if duration.nil?
  info = TimerInfo.new(duration, recurring, metainfo)
  info.expiry = Time.new if immed
  info.cancelled = true
  @mutex.synchronize{ @queue.push info }
  info
end
cancel(timerInfo) click to toggle source

Cancel a timer.

# File lib/quartz_torrent/timermanager.rb, line 62
def cancel(timerInfo)
  timerInfo.cancelled = true
end
empty?() click to toggle source
# File lib/quartz_torrent/timermanager.rb, line 108
def empty?
  @queue.empty?
end
next() { |secondsUntilExpiry| ... } click to toggle source

Remove the next timer event from the queue and return it as a TimerHandler::TimerInfo object. Warning: if the timer is a recurring timer, the secondsUntilExpiry will be set to the NEXT time the timer would expire, instead of this time. If the original secondsUntilExpiry is needed, pass a block to this method, and the block will be called with the original secondsUntilExpiry.

# File lib/quartz_torrent/timermanager.rb, line 80
def next
  result = nil
  @mutex.synchronize do 
    clearCancelled
    result = @queue.pop 
  end
  if result
    yield result.secondsUntilExpiry if block_given?
    if result.recurring
      result.refresh
      @mutex.synchronize{ @queue.push result }
    end
  end
  result
end
peek() click to toggle source

Return the next timer event from the queue, but don’t remove it from the queue.

# File lib/quartz_torrent/timermanager.rb, line 67
def peek
  result = nil
  @mutex.synchronize do 
    clearCancelled
    result = @queue.top 
  end
  result
end
to_s() click to toggle source
# File lib/quartz_torrent/timermanager.rb, line 96
def to_s
  arr = nil
  @mutex.synchronize do
    arr = @queue.to_a
  end
  s = "now = #{Time.new}. Queue = ["
  arr.each do |e|
    s << "(#{e.object_id};#{e.expiry};#{e.metainfo[0]};#{e.secondsUntilExpiry}),"
  end
  s << "]"
end

Private Instance Methods

clearCancelled() click to toggle source
# File lib/quartz_torrent/timermanager.rb, line 114
def clearCancelled
  while @queue.top && @queue.top.cancelled
    @queue.pop 
  end
end