Class: RubyTorrent::PieceOrder
Overview
keeps pieces in order
Constant Summary collapse
- POP_RECALC_THRESH =
20
- POP_RECALC_LIMIT =
popularity of all pieces is recalculated (expensive sort) when this number of pieces have arrived at any of the peers, OR:
30
Instance Method Summary collapse
- #dec_all(bitfield) ⇒ Object
- #each(in_fuseki, num_peers) ⇒ Object
-
#inc(i) ⇒ Object
increment the popularity of a piece.
-
#inc_all(bitfield, inc = 1) ⇒ Object
increment the popularity of multiple pieces.
-
#initialize(package) ⇒ PieceOrder
constructor
…
Constructor Details
#initialize(package) ⇒ PieceOrder
… when this many seconds have passed when at least one piece has changed in popularity, or if we’re in fuseki mode.
27 28 29 30 31 32 33 34 35 |
# File 'lib/rubytorrent/controller.rb', line 27 def initialize(package) @package = package @order = nil @num_changed = 0 @pop = Array.new(@package.pieces.length, 0) @jitter = Array.new(@package.pieces.length) { rand } @m = Mutex.new @last_recalc = nil end |
Instance Method Details
#dec_all(bitfield) ⇒ Object
57 58 59 |
# File 'lib/rubytorrent/controller.rb', line 57 def dec_all(bitfield) inc_all(bitfield, -1) end |
#each(in_fuseki, num_peers) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/rubytorrent/controller.rb', line 61 def each(in_fuseki, num_peers) if (@num_changed > POP_RECALC_THRESH) || @last_recalc.nil? || (((@num_changed > 0) || in_fuseki) && ((Time.now - @last_recalc) > POP_RECALC_LIMIT)) rt_debug "* reordering pieces: (#@num_changed changed, last recalc #{(@last_recalc.nil? ? '(never)' : (Time.now - @last_recalc).round)}s ago)..." recalc_order(in_fuseki, num_peers) end @order.each { |i| yield i } end |
#inc(i) ⇒ Object
increment the popularity of a piece
38 39 40 41 42 43 |
# File 'lib/rubytorrent/controller.rb', line 38 def inc(i) @m.synchronize do @pop[i.to_i] += 1 @num_changed += 1 end end |
#inc_all(bitfield, inc = 1) ⇒ Object
increment the popularity of multiple pieces
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rubytorrent/controller.rb', line 46 def inc_all(bitfield, inc=1) @m.synchronize do bitfield.each_index do |i| if bitfield[i] @pop[i] += inc @num_changed += 1 end end end end |