Class: QuartzTorrent::PieceMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/quartz_torrent/filemanager.rb

Overview

Maps pieces to sections of files.

Instance Method Summary collapse

Constructor Details

#initialize(baseDirectory, torrinfo) ⇒ PieceMapper

Create a new PieceMapper that will map to files inside ‘baseDirectory’. Parameter ‘torrinfo’ should be a Metainfo::Info object (the info part of the metainfo).



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/quartz_torrent/filemanager.rb', line 48

def initialize(baseDirectory, torrinfo)
  @torrinfo = torrinfo
  @pieceSize = torrinfo.pieceLen
  @logger = LogManager.getLogger("piecemapper")

  @fileRegionMap = RegionMap.new
  offset = 0
  @logger.debug "Map (offset to path):"
  torrinfo.files.each do |file|
    offset += file.length
    path = baseDirectory + File::SEPARATOR + file.path
    @fileRegionMap.add offset-1, path
    @logger.debug "  #{offset-1}\t#{path}"
  end      
end

Instance Method Details

#findBlock(pieceIndex, offset, length) ⇒ Object

Return a list of FileRegion objects. The FileRegion offsets specify in order which regions of files the piece covers.



75
76
77
78
79
80
# File 'lib/quartz_torrent/filemanager.rb', line 75

def findBlock(pieceIndex, offset, length)
  leftOffset = @pieceSize*pieceIndex + offset
  rightOffset = leftOffset + length-1

  findPart(leftOffset, rightOffset)
end

#findPiece(pieceIndex) ⇒ Object

Return a list of FileRegion objects. The FileRegion offsets specify in order which regions of files the piece covers.



66
67
68
69
70
71
# File 'lib/quartz_torrent/filemanager.rb', line 66

def findPiece(pieceIndex)
  leftOffset = @pieceSize*pieceIndex
  rightOffset = leftOffset + @pieceSize-1

  findPart(leftOffset, rightOffset)
end