Class: Torrenter::TorrentReader::Piece

Inherits:
Object
  • Object
show all
Defined in:
lib/torrenter/torrent_reader/piece.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/torrenter/torrent_reader/piece.rb', line 5

def data
  @data
end

#hashObject

Returns the value of attribute hash.



4
5
6
# File 'lib/torrenter/torrent_reader/piece.rb', line 4

def hash
  @hash
end

#indexObject

Returns the value of attribute index.



4
5
6
# File 'lib/torrenter/torrent_reader/piece.rb', line 4

def index
  @index
end

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/torrenter/torrent_reader/piece.rb', line 4

def path
  @path
end

#peersObject (readonly)

Returns the value of attribute peers.



5
6
7
# File 'lib/torrenter/torrent_reader/piece.rb', line 5

def peers
  @peers
end

#rangeObject

Returns the value of attribute range.



4
5
6
# File 'lib/torrenter/torrent_reader/piece.rb', line 4

def range
  @range
end

#statusObject

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

#blockObject



72
73
74
# File 'lib/torrenter/torrent_reader/piece.rb', line 72

def block
  (tally - @data.size) < BLOCK ? tally - @data.size : BLOCK
end

#chunkObject



76
77
78
# File 'lib/torrenter/torrent_reader/piece.rb', line 76

def chunk
  (@data.bytesize / BLOCK) * BLOCK
end

#complete?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/torrenter/torrent_reader/piece.rb', line 68

def complete?
  @data.bytesize >= tally && hash_match?
end

#full?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/torrenter/torrent_reader/piece.rb', line 27

def full?
  tally == @piece_length
end

#hash_match?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


36
37
38
# File 'lib/torrenter/torrent_reader/piece.rb', line 36

def include?(socket)
  @peers.include?(socket)
end

#leftObject



23
24
25
# File 'lib/torrenter/torrent_reader/piece.rb', line 23

def left
  @piece_length - tally
end

#multiple_files?Boolean

Returns:

  • (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_sizeObject



88
89
90
# File 'lib/torrenter/torrent_reader/piece.rb', line 88

def peer_size
  @peers.length
end

#percentObject



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_peerObject



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

#tallyObject



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

#verifyObject



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_fileObject



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