class Torrenter::TorrentReader::Piece
Attributes
data[R]
hash[RW]
index[RW]
path[RW]
peers[R]
range[RW]
status[RW]
Public Class Methods
new(piece_length)
click to toggle source
# File lib/torrenter/torrent_reader/piece.rb, line 6 def initialize(piece_length) @range = [] @path = [] @piece_length = piece_length @status = :available @data = '' @peers = [] end
Public Instance Methods
<<(buffer)
click to toggle source
# File lib/torrenter/torrent_reader/piece.rb, line 80 def <<(buffer) @data << buffer end
add(range, path)
click to toggle source
# File lib/torrenter/torrent_reader/piece.rb, line 31 def add(range, path) @range << range @path << path end
add_peer(peer)
click to toggle source
# File lib/torrenter/torrent_reader/piece.rb, line 92 def add_peer(peer) @peers << peer end
block()
click to toggle source
# File lib/torrenter/torrent_reader/piece.rb, line 72 def block (tally - @data.size) < BLOCK ? tally - @data.size : BLOCK end
chunk()
click to toggle source
# File lib/torrenter/torrent_reader/piece.rb, line 76 def chunk (@data.bytesize / BLOCK) * BLOCK end
complete?()
click to toggle source
# File lib/torrenter/torrent_reader/piece.rb, line 68 def complete? @data.bytesize >= tally && hash_match? end
full?()
click to toggle source
# File lib/torrenter/torrent_reader/piece.rb, line 27 def full? tally == @piece_length end
hash_match?()
click to toggle source
# File lib/torrenter/torrent_reader/piece.rb, line 100 def hash_match? Digest::SHA1.digest(@data) == @hash end
include?(socket)
click to toggle source
# File lib/torrenter/torrent_reader/piece.rb, line 36 def include?(socket) @peers.include?(socket) end
left()
click to toggle source
# File lib/torrenter/torrent_reader/piece.rb, line 23 def left @piece_length - tally end
multiple_files?()
click to toggle source
# File lib/torrenter/torrent_reader/piece.rb, line 54 def multiple_files? @path.length > 1 end
offset(i)
click to toggle source
# File lib/torrenter/torrent_reader/piece.rb, line 50 def offset(i) @range[i].end - @range[i].begin end
peer_count(socket)
click to toggle source
# File lib/torrenter/torrent_reader/piece.rb, line 84 def peer_count(socket) @peers.count(socket) end
peer_size()
click to toggle source
# File lib/torrenter/torrent_reader/piece.rb, line 88 def peer_size @peers.length end
percent()
click to toggle source
# File lib/torrenter/torrent_reader/piece.rb, line 104 def percent if @status == :downloading return (@data.size).fdiv(@piece_length) elsif @status == :downloaded return 1 else return 0 end end
remove_peer()
click to toggle source
# File lib/torrenter/torrent_reader/piece.rb, line 96 def remove_peer @peers.each { |p| @peers.delete(p) if p.closed? } end
tally()
click to toggle source
# File lib/torrenter/torrent_reader/piece.rb, line 15 def tally if @range.length > 0 @range.map { |r| r.end - r.begin }.inject { |x,y| x + y } else 0 end end
verify()
click to toggle source
# File lib/torrenter/torrent_reader/piece.rb, line 40 def verify @data = if multiple_files? @path.map.with_index { |p, i| read_file(p, i) }.join else File.file?(@path.join) ? (IO.read(@path.join, @piece_length, @range.first.begin) || '') : '' end @status = hash_match? ? :downloaded : :available @data.clear end
write_to_file()
click to toggle source
# File lib/torrenter/torrent_reader/piece.rb, line 58 def write_to_file if multiple_files? @path.each_with_index { |p,i| write_file(p, i) } else IO.write(@path.first, @data, @range.first.begin) end @status = :downloaded @data.clear end
Private Instance Methods
read_file(p, i)
click to toggle source
# File lib/torrenter/torrent_reader/piece.rb, line 115 def read_file(p, i) binding.pry if offset(i) < 0 File.file?(p) ? IO.read(p, offset(i), @range[i].begin) : '' end
write_file(p, i)
click to toggle source
# File lib/torrenter/torrent_reader/piece.rb, line 120 def write_file(p, i) IO.write(p, @data.slice!(0...offset(i)), @range[i].begin) end