Class: QuartzTorrent::TorrentQueue
- Inherits:
-
Object
- Object
- QuartzTorrent::TorrentQueue
- Defined in:
- lib/quartz_torrent/torrentqueue.rb
Instance Method Summary collapse
-
#dequeue(torrentDatas) ⇒ Object
Compute which torrents can now be unqueued based on the state of running torrents.
-
#initialize(maxIncomplete, maxActive) ⇒ TorrentQueue
constructor
The maxIncomplete and maxActive parameters specify how many torrents may be unpaused and unqueued at once.
- #push(torrentData) ⇒ Object
- #size ⇒ Object
- #unshift(torrentData) ⇒ Object
Constructor Details
#initialize(maxIncomplete, maxActive) ⇒ TorrentQueue
The maxIncomplete and maxActive parameters specify how many torrents may be unpaused and unqueued at once. Parameter maxActive is the total maximum number of active torrents (unpaused, unqueued), and maxIncomplete is a subset of maxActive that are incomplete and thus downloading (as opposed to only uploading). An exception is thrown if maxIncomplete > maxActive.
10 11 12 13 14 15 16 |
# File 'lib/quartz_torrent/torrentqueue.rb', line 10 def initialize(maxIncomplete, maxActive) raise "The maximum number of running torrents may not be larger than the maximum number of unqueued torrents" if maxIncomplete > maxActive @maxIncomplete = maxIncomplete @maxActive = maxActive @queue = [] @logger = LogManager.getLogger("queue") end |
Instance Method Details
#dequeue(torrentDatas) ⇒ Object
Compute which torrents can now be unqueued based on the state of running torrents. Parameter torrentDatas should be an array of TorrentData that the decision will be based off. At a minimum these items should respond to ‘paused’, ‘queued’, ‘state’, ‘queued=’
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 |
# File 'lib/quartz_torrent/torrentqueue.rb', line 21 def dequeue(torrentDatas) numActive = 0 numIncomplete = 0 torrentDatas.each do |torrentData| next if torrentData.paused || torrentData.queued numIncomplete += 1 if incomplete?(torrentData) numActive += 1 end @logger.debug "incomplete: #{numIncomplete}/#{@maxIncomplete} active: #{numActive}/#{@maxActive}" @logger.debug "Queue contains #{size} torrents" torrents = [] while numActive < @maxActive torrentData = nil if numIncomplete < @maxIncomplete # Unqueue first incomplete torrent from queue torrentData = dequeueFirstMatching{ |torrentData| incomplete?(torrentData)} @logger.debug "#{torrentData ? "dequeued" : "failed to dequeue"} an incomplete torrent" numIncomplete += 1 if torrentData end if ! torrentData # Unqueue first complete (uploading) torrent from queue torrentData = dequeueFirstMatching{ |torrentData| !incomplete?(torrentData)} @logger.debug "#{torrentData ? "dequeued" : "failed to dequeue"} a complete torrent" end numActive += 1 if torrentData if torrentData torrentData.queued = false torrents.push torrentData else break end end @logger.debug "Will dequeue #{torrents.size}" torrents end |
#push(torrentData) ⇒ Object
62 63 64 65 |
# File 'lib/quartz_torrent/torrentqueue.rb', line 62 def push(torrentData) torrentData.queued = true @queue.push torrentData end |
#size ⇒ Object
72 73 74 |
# File 'lib/quartz_torrent/torrentqueue.rb', line 72 def size @queue.size end |
#unshift(torrentData) ⇒ Object
67 68 69 70 |
# File 'lib/quartz_torrent/torrentqueue.rb', line 67 def unshift(torrentData) torrentData.queued = true @queue.unshift torrentData end |