Class: Torrenter::TorrentReader::Piece
- Inherits:
-
Object
- Object
- Torrenter::TorrentReader::Piece
- Defined in:
- lib/torrenter/torrent_reader/piece.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#hash ⇒ Object
Returns the value of attribute hash.
-
#index ⇒ Object
Returns the value of attribute index.
-
#path ⇒ Object
Returns the value of attribute path.
-
#peers ⇒ Object
readonly
Returns the value of attribute peers.
-
#range ⇒ Object
Returns the value of attribute range.
-
#status ⇒ Object
Returns the value of attribute status.
Instance Method Summary collapse
- #<<(buffer) ⇒ Object
- #add(range, path) ⇒ Object
- #add_peer(peer) ⇒ Object
- #block ⇒ Object
- #chunk ⇒ Object
- #complete? ⇒ Boolean
- #full? ⇒ Boolean
- #hash_match? ⇒ Boolean
- #include?(socket) ⇒ Boolean
-
#initialize(piece_length) ⇒ Piece
constructor
A new instance of Piece.
- #left ⇒ Object
- #multiple_files? ⇒ Boolean
- #offset(i) ⇒ Object
- #peer_count(socket) ⇒ Object
- #peer_size ⇒ Object
- #percent ⇒ Object
- #remove_peer ⇒ Object
- #tally ⇒ Object
- #verify ⇒ Object
- #write_to_file ⇒ Object
Constructor Details
#initialize(piece_length) ⇒ Piece
Returns a new instance of Piece.
6 7 8 9 10 11 12 13 |
# File 'lib/torrenter/torrent_reader/piece.rb', line 6 def initialize(piece_length) @range = [] @path = [] @piece_length = piece_length @status = :available @data = '' @peers = [] end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
5 6 7 |
# File 'lib/torrenter/torrent_reader/piece.rb', line 5 def data @data end |
#hash ⇒ Object
Returns the value of attribute hash.
4 5 6 |
# File 'lib/torrenter/torrent_reader/piece.rb', line 4 def hash @hash end |
#index ⇒ Object
Returns the value of attribute index.
4 5 6 |
# File 'lib/torrenter/torrent_reader/piece.rb', line 4 def index @index end |
#path ⇒ Object
Returns the value of attribute path.
4 5 6 |
# File 'lib/torrenter/torrent_reader/piece.rb', line 4 def path @path end |
#peers ⇒ Object (readonly)
Returns the value of attribute peers.
5 6 7 |
# File 'lib/torrenter/torrent_reader/piece.rb', line 5 def peers @peers end |
#range ⇒ Object
Returns the value of attribute range.
4 5 6 |
# File 'lib/torrenter/torrent_reader/piece.rb', line 4 def range @range end |
#status ⇒ Object
Returns the value of attribute status.
4 5 6 |
# File 'lib/torrenter/torrent_reader/piece.rb', line 4 def status @status end |
Instance Method Details
#<<(buffer) ⇒ Object
80 81 82 |
# File 'lib/torrenter/torrent_reader/piece.rb', line 80 def <<(buffer) @data << buffer end |
#add(range, path) ⇒ Object
31 32 33 34 |
# File 'lib/torrenter/torrent_reader/piece.rb', line 31 def add(range, path) @range << range @path << path end |
#add_peer(peer) ⇒ Object
92 93 94 |
# File 'lib/torrenter/torrent_reader/piece.rb', line 92 def add_peer(peer) @peers << peer end |
#block ⇒ Object
72 73 74 |
# File 'lib/torrenter/torrent_reader/piece.rb', line 72 def block (tally - @data.size) < BLOCK ? tally - @data.size : BLOCK end |
#chunk ⇒ Object
76 77 78 |
# File 'lib/torrenter/torrent_reader/piece.rb', line 76 def chunk (@data.bytesize / BLOCK) * BLOCK end |
#complete? ⇒ Boolean
68 69 70 |
# File 'lib/torrenter/torrent_reader/piece.rb', line 68 def complete? @data.bytesize >= tally && hash_match? end |
#full? ⇒ Boolean
27 28 29 |
# File 'lib/torrenter/torrent_reader/piece.rb', line 27 def full? tally == @piece_length end |
#hash_match? ⇒ Boolean
100 101 102 |
# File 'lib/torrenter/torrent_reader/piece.rb', line 100 def hash_match? Digest::SHA1.digest(@data) == @hash end |
#include?(socket) ⇒ Boolean
36 37 38 |
# File 'lib/torrenter/torrent_reader/piece.rb', line 36 def include?(socket) @peers.include?(socket) end |
#left ⇒ Object
23 24 25 |
# File 'lib/torrenter/torrent_reader/piece.rb', line 23 def left @piece_length - tally end |
#multiple_files? ⇒ Boolean
54 55 56 |
# File 'lib/torrenter/torrent_reader/piece.rb', line 54 def multiple_files? @path.length > 1 end |
#offset(i) ⇒ Object
50 51 52 |
# File 'lib/torrenter/torrent_reader/piece.rb', line 50 def offset(i) @range[i].end - @range[i].begin end |
#peer_count(socket) ⇒ Object
84 85 86 |
# File 'lib/torrenter/torrent_reader/piece.rb', line 84 def peer_count(socket) @peers.count(socket) end |
#peer_size ⇒ Object
88 89 90 |
# File 'lib/torrenter/torrent_reader/piece.rb', line 88 def peer_size @peers.length end |
#percent ⇒ Object
104 105 106 107 108 109 110 111 112 |
# 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 ⇒ Object
96 97 98 |
# File 'lib/torrenter/torrent_reader/piece.rb', line 96 def remove_peer @peers.each { |p| @peers.delete(p) if p.closed? } end |
#tally ⇒ Object
15 16 17 18 19 20 21 |
# 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 ⇒ Object
40 41 42 43 44 45 46 47 48 |
# 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 ⇒ Object
58 59 60 61 62 63 64 65 66 |
# 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 |