class Torrenter::TorrentReader::PieceIndex

Attributes

piece_index[R]
piece_length[R]

Public Class Methods

new(piece_length) click to toggle source
# File lib/torrenter/torrent_reader/piece_index.rb, line 5
def initialize(piece_length)
  @piece_length = piece_length
  @piece_index  = []
end

Public Instance Methods

<<(piece) click to toggle source
# File lib/torrenter/torrent_reader/piece_index.rb, line 26
def <<(piece)
  @piece_index << piece
end
[](index) click to toggle source
# File lib/torrenter/torrent_reader/piece_index.rb, line 62
def [](index)
  @piece_index[index]
end
[]=(i, val) click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# File lib/torrenter/torrent_reader/piece_index.rb, line 50
def all?
  @piece_index.all? { |piece| piece.status == :downloaded }
end
available_pieces() click to toggle source
# File lib/torrenter/torrent_reader/piece_index.rb, line 38
def available_pieces
  @piece_index.select { |piece| piece.status == :available }
end
clean_peers() click to toggle source
# File lib/torrenter/torrent_reader/piece_index.rb, line 22
def clean_peers
  @piece_index.each { |piece| piece.remove_peer }
end
count(type) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/torrenter/torrent_reader/piece_index.rb, line 70
def last
  @piece_index.last
end
none_remain?() click to toggle source
# File lib/torrenter/torrent_reader/piece_index.rb, line 30
def none_remain?
  count(:available) == 0
end
peer_count() click to toggle source
# File lib/torrenter/torrent_reader/piece_index.rb, line 34
def peer_count
  available_pieces.map { |piece| piece.peers.length }
end
size() click to toggle source
# File lib/torrenter/torrent_reader/piece_index.rb, line 58
def size
  @piece_index.size
end
to_json() click to toggle source
# 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() click to toggle source
# File lib/torrenter/torrent_reader/piece_index.rb, line 46
def verify_status
  @piece_index.each { |piece| piece.verify }
end