Class: QuartzTorrent::TimerManager

Inherits:
Object
  • Object
show all
Defined in:
lib/quartz_torrent/timermanager.rb

Overview

Class used to manage timers.

Defined Under Namespace

Classes: TimerInfo

Instance Method Summary collapse

Constructor Details

#initialize(logger = nil) ⇒ TimerManager

Returns a new instance of TimerManager.



31
32
33
34
35
# 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

Instance Method Details

#add(duration, metainfo = nil, recurring = true, immed = false) ⇒ Object

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.



43
44
45
46
47
48
49
# 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) ⇒ Object

For testing. Add a cancelled timer.



52
53
54
55
56
57
58
59
# 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) ⇒ Object

Cancel a timer.



62
63
64
# File 'lib/quartz_torrent/timermanager.rb', line 62

def cancel(timerInfo)
  timerInfo.cancelled = true
end

#empty?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/quartz_torrent/timermanager.rb', line 108

def empty?
  @queue.empty?
end

#nextObject

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.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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

#peekObject

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



67
68
69
70
71
72
73
74
# File 'lib/quartz_torrent/timermanager.rb', line 67

def peek
  result = nil
  @mutex.synchronize do 
    clearCancelled
    result = @queue.top 
  end
  result
end

#to_sObject



96
97
98
99
100
101
102
103
104
105
106
# 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