Class: Torrenter::TorrentReader::PieceIndex
- Inherits:
-
Object
- Object
- Torrenter::TorrentReader::PieceIndex
- Defined in:
- lib/torrenter/torrent_reader/piece_index.rb
Instance Attribute Summary collapse
-
#piece_index ⇒ Object
readonly
Returns the value of attribute piece_index.
-
#piece_length ⇒ Object
readonly
Returns the value of attribute piece_length.
Instance Method Summary collapse
- #<<(piece) ⇒ Object
- #[](index) ⇒ Object
- #[]=(i, val) ⇒ Object
- #add_peer(index, peer) ⇒ Object
- #all? ⇒ Boolean
- #available_pieces ⇒ Object
- #clean_peers ⇒ Object
- #count(type) ⇒ Object
- #find_least(socket, i = 1) ⇒ Object
-
#initialize(piece_length) ⇒ PieceIndex
constructor
A new instance of PieceIndex.
- #last ⇒ Object
- #none_remain? ⇒ Boolean
- #peer_count ⇒ Object
- #size ⇒ Object
- #to_json ⇒ Object
- #verify_status ⇒ Object
Constructor Details
#initialize(piece_length) ⇒ PieceIndex
Returns a new instance of PieceIndex.
5 6 7 8 |
# File 'lib/torrenter/torrent_reader/piece_index.rb', line 5 def initialize(piece_length) @piece_length = piece_length @piece_index = [] end |
Instance Attribute Details
#piece_index ⇒ Object (readonly)
Returns the value of attribute piece_index.
4 5 6 |
# File 'lib/torrenter/torrent_reader/piece_index.rb', line 4 def piece_index @piece_index end |
#piece_length ⇒ Object (readonly)
Returns the value of attribute piece_length.
4 5 6 |
# File 'lib/torrenter/torrent_reader/piece_index.rb', line 4 def piece_length @piece_length end |
Instance Method Details
#<<(piece) ⇒ Object
26 27 28 |
# File 'lib/torrenter/torrent_reader/piece_index.rb', line 26 def <<(piece) @piece_index << piece end |
#[](index) ⇒ Object
62 63 64 |
# File 'lib/torrenter/torrent_reader/piece_index.rb', line 62 def [](index) @piece_index[index] end |
#[]=(i, val) ⇒ Object
66 67 68 |
# File 'lib/torrenter/torrent_reader/piece_index.rb', line 66 def []=(i, val) @piece_index[i] = Piece.new(i, piece_length, val) end |
#add_peer(index, peer) ⇒ Object
54 55 56 |
# File 'lib/torrenter/torrent_reader/piece_index.rb', line 54 def add_peer(index, peer) @piece_index[index].peers << peer if @piece_index[index] end |
#all? ⇒ Boolean
50 51 52 |
# File 'lib/torrenter/torrent_reader/piece_index.rb', line 50 def all? @piece_index.all? { |piece| piece.status == :downloaded } end |
#available_pieces ⇒ Object
38 39 40 |
# File 'lib/torrenter/torrent_reader/piece_index.rb', line 38 def available_pieces @piece_index.select { |piece| piece.status == :available } end |
#clean_peers ⇒ Object
22 23 24 |
# File 'lib/torrenter/torrent_reader/piece_index.rb', line 22 def clean_peers @piece_index.each { |piece| piece.remove_peer } end |
#count(type) ⇒ Object
42 43 44 |
# File 'lib/torrenter/torrent_reader/piece_index.rb', line 42 def count(type) @piece_index.select { |piece| piece.status == type }.length end |
#find_least(socket, i = 1) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/torrenter/torrent_reader/piece_index.rb', line 10 def find_least(socket, i=1) return nil if none_remain? || i > peer_count.sort.last index = peer_count.find_index(i) if index && available_pieces[index].include?(socket) piece = available_pieces[index] piece.status = :downloading return piece else find_least(socket, i+1) end end |
#last ⇒ Object
70 71 72 |
# File 'lib/torrenter/torrent_reader/piece_index.rb', line 70 def last @piece_index.last end |
#none_remain? ⇒ Boolean
30 31 32 |
# File 'lib/torrenter/torrent_reader/piece_index.rb', line 30 def none_remain? count(:available) == 0 end |
#peer_count ⇒ Object
34 35 36 |
# File 'lib/torrenter/torrent_reader/piece_index.rb', line 34 def peer_count available_pieces.map { |piece| piece.peers.length } end |
#size ⇒ Object
58 59 60 |
# File 'lib/torrenter/torrent_reader/piece_index.rb', line 58 def size @piece_index.size end |
#to_json ⇒ Object
74 75 76 |
# File 'lib/torrenter/torrent_reader/piece_index.rb', line 74 def to_json JSON.generate({ :master_index => @piece_index.map { |p| p.percent } }) end |
#verify_status ⇒ Object
46 47 48 |
# File 'lib/torrenter/torrent_reader/piece_index.rb', line 46 def verify_status @piece_index.each { |piece| piece.verify } end |