class QuartzTorrent::TorrentDataDelegate

Data about torrents for use by the end user.

Attributes

alarms[RW]

Array of currently raised alarms

bytesDownloaded[RW]
bytesDownloadedDataOnly[RW]
bytesUploaded[RW]
bytesUploadedDataOnly[RW]
completePieceBitfield[R]

Bitfield representing which pieces of the torrent are completed.

completedBytes[R]

Count of completed bytes of the torrent

downloadRate[R]

Download rate in bytes/second

downloadRateDataOnly[R]

Download rate limit in bytes/second if a limit is set, nil otherwise

downloadRateLimit[R]

Download rate limit in bytes/second if a limit is set, nil otherwise

info[RW]

Torrent Metainfo.info struct. This is nil if the torrent has no metadata and we haven’t downloaded it yet (i.e. a magnet link).

infoHash[RW]

Infohash of the torrent. This is binary data.

metainfoCompletedLength[R]

How much of the metainfo info we have downloaded in bytes. This is only set when the state is :downloading_metainfo

metainfoLength[R]

Length of metainfo info in bytes. This is only set when the state is :downloading_metainfo

paused[R]

Whether or not the torrent is paused.

peers[R]

Array of peers for the torrent. These include connected, disconnected, and handshaking peers

queued[R]

Whether or not the torrent is queued.

ratio[RW]

After we have completed downloading a torrent, we will continue to upload until we have uploaded ratio * torrent_size bytes. If nil, no limit on upload.

recommendedName[RW]

Recommended display name for this torrent.

state[R]

State of the torrent. This may be one of :downloading_metainfo, :error, :checking_pieces, :running, :downloading_metainfo, or :deleted. The :deleted state indicates that the torrent that this TorrentDataDelegate refers to is no longer being managed by the peer client.

uploadDuration[RW]
uploadRate[R]

Upload rate in bytes/second

uploadRateDataOnly[R]
uploadRateLimit[R]

Upload rate limit in bytes/second if a limit is set, nil otherwise

Public Class Methods

new(torrentData, peerClientHandler) click to toggle source

Create a new TorrentDataDelegate. This is meant to only be called internally.

# File lib/quartz_torrent/peerclient.rb, line 130
def initialize(torrentData, peerClientHandler)
  fillFrom(torrentData)
  @torrentData = torrentData
  @peerClientHandler = peerClientHandler
end

Public Instance Methods

internalRefresh() click to toggle source

Set the fields of this TorrentDataDelegate from the passed torrentData. This is meant to only be called internally.

# File lib/quartz_torrent/peerclient.rb, line 190
def internalRefresh
  fillFrom(@torrentData)
end
refresh() click to toggle source

Update the data in this TorrentDataDelegate from the torrentData object that it was created from. TODO: What if that torrentData is now gone?

# File lib/quartz_torrent/peerclient.rb, line 184
def refresh
  @peerClientHandler.updateDelegateTorrentData self
end

Private Instance Methods

buildPeersList(torrentData) click to toggle source
# File lib/quartz_torrent/peerclient.rb, line 246
def buildPeersList(torrentData)
  @peers = []
  torrentData.peers.all.each do |peer|
    @peers.push peer.clone
  end
end
fillFrom(torrentData) click to toggle source
# File lib/quartz_torrent/peerclient.rb, line 195
def fillFrom(torrentData)
  @infoHash = torrentData.infoHash
  @info = torrentData.info
  @bytesUploadedDataOnly = torrentData.bytesUploadedDataOnly
  @bytesDownloadedDataOnly = torrentData.bytesDownloadedDataOnly
  @bytesUploaded = torrentData.bytesUploaded
  @bytesDownloaded = torrentData.bytesDownloaded

  if torrentData.state == :checking_pieces
    # When checking pieces there is only one request pending with the piece manager.
    checkExistingRequestId = torrentData.pieceManagerRequestMetadata.keys.first
    progress = torrentData.pieceManager.progress checkExistingRequestId
    @completedBytes = progress ? progress * torrentData.info.dataLength / 100 : 0
  else
    @completedBytes = torrentData.blockState.nil? ? 0 : torrentData.blockState.completedLength
  end

  # This should really be a copy:
  @completePieceBitfield = torrentData.blockState.nil? ? nil : torrentData.blockState.completePieceBitfield
  buildPeersList(torrentData)
  @downloadRate = @peers.reduce(0){ |memo, peer| memo + peer.uploadRate }
  @uploadRate = @peers.reduce(0){ |memo, peer| memo + peer.downloadRate }
  @downloadRateDataOnly = @peers.reduce(0){ |memo, peer| memo + peer.uploadRateDataOnly }
  @uploadRateDataOnly = @peers.reduce(0){ |memo, peer| memo + peer.downloadRateDataOnly }
  @state = torrentData.state
  @metainfoLength = nil
  @paused = torrentData.paused
  @queued = torrentData.queued
  @metainfoCompletedLength = nil
  if torrentData.metainfoPieceState && torrentData.state == :downloading_metainfo
    @metainfoLength = torrentData.metainfoPieceState.metainfoLength
    @metainfoCompletedLength = torrentData.metainfoPieceState.metainfoCompletedLength
  end

  if torrentData.info
    @recommendedName = torrentData.info.name
  else
    if torrentData.magnet
      @recommendedName = torrentData.magnet.displayName
    else
      @recommendedName = nil
    end
  end

  @downloadRateLimit = torrentData.downRateLimit.unitsPerSecond if torrentData.downRateLimit
  @uploadRateLimit = torrentData.upRateLimit.unitsPerSecond if torrentData.upRateLimit
  @ratio = torrentData.ratio
  @uploadDuration = torrentData.uploadDuration
  @alarms = torrentData.alarms.all
end