class QuartzTorrent::Peer

This class represents a torrent peer.

Attributes

amChoked[RW]

Am I choked by this peer

amInterested[RW]

Am I interested in this peer

bitfield[RW]

A Bitfield representing the pieces that the peer has.

downloadRate[RW]

Download rate of us to peer.

downloadRateDataOnly[RW]

Download rate of us to peer, only counting actual torrent data

firstEstablishTime[RW]

Time when the peers connection was established the first time. This is nil when the peer has never had an established connection.

infoHash[RW]

Info hash for the torrent of this peer

isUs[RW]

Is this peer ourself? Used to tell if we connected to ourself.

maxRequestedBlocks[RW]

Maximum number of outstanding block requests allowed for this peer.

peerChoked[RW]

This peer is choked by me

peerInterested[RW]

Is this peer interested

peerMsgSerializer[RW]

A PeerWireMessageSerializer that can unserialize and serialize messages to and from this peer.

requestedBlocks[RW]

A hash of the block indexes of the outstanding blocks requested from this peer

requestedBlocksSizeLastPass[RW]
state[R]

Peer connection state. All peers start of in :disconnected. When trying to handshake, they are in state :handshaking. Once handshaking is complete and we can send/accept requests, the state is :established.

trackerPeer[RW]

A TrackerPeer class with the information about the peer retrieved from the tracker. When initially created the trackerPeer.id property may be null, but once the peer has connected it is set.

uploadRate[RW]

Upload rate of peer to us.

uploadRateDataOnly[RW]

Upload rate of peer to us, only counting actual torrent data

Public Class Methods

addStateChangeListener(l) click to toggle source

Add a proc to the list of state change listeners.

# File lib/quartz_torrent/peer.rb, line 97
def self.addStateChangeListener(l)
  @@stateChangeListeners.push l
end
new(trackerPeer) click to toggle source

Create a new Peer using the information from the passed TrackerPeer object.

# File lib/quartz_torrent/peer.rb, line 10
def initialize(trackerPeer)
  @trackerPeer = trackerPeer
  @amChoked = true
  @amInterested = false
  @peerChoked = true
  @peerInterested = false
  @infoHash = nil
  @state = :disconnected
  @uploadRate = Rate.new
  @downloadRate = Rate.new
  @uploadRateDataOnly = Rate.new
  @downloadRateDataOnly = Rate.new
  @bitfield = nil
  @firstEstablishTime = nil
  @isUs = false
  @requestedBlocks = {}
  @requestedBlocksSizeLastPass = nil
  @maxRequestedBlocks = 50
  @peerMsgSerializer = PeerWireMessageSerializer.new
end

Public Instance Methods

clone() click to toggle source

Create a clone of this peer. This method does not clone listeners.

# File lib/quartz_torrent/peer.rb, line 123
def clone
  peer = Peer.new(@trackerPeer)
  peer.amChoked = amChoked
  peer.amInterested = amInterested
  peer.peerChoked = peerChoked
  peer.peerInterested = peerInterested
  peer.firstEstablishTime = firstEstablishTime
  peer.state = state
  peer.isUs = isUs
  # Take the values of the rates. This is so that if the caller doesn't read the rates
  # in a timely fashion, they don't decay.
  peer.uploadRate = uploadRate.value
  peer.downloadRate = downloadRate.value
  peer.uploadRateDataOnly = uploadRateDataOnly.value
  peer.downloadRateDataOnly = downloadRateDataOnly.value
  if bitfield
    peer.bitfield = Bitfield.new(bitfield.length)
    peer.bitfield.copyFrom bitfield
  end
  peer.requestedBlocks = requestedBlocks.clone
  peer.maxRequestedBlocks = maxRequestedBlocks
  peer.peerMsgSerializer = peerMsgSerializer

  peer
end
eql?(o) click to toggle source

Equate peers.

# File lib/quartz_torrent/peer.rb, line 102
def eql?(o)
  o.is_a?(Peer) && trackerPeer.eql?(o.trackerPeer)
end
state=(state) click to toggle source
# File lib/quartz_torrent/peer.rb, line 61
def state=(state)
  oldState = @state
  @state = state
  @@stateChangeListeners.each{ |l| l.call(self, oldState, @state) }
  @firstEstablishTime = Time.new if @state == :established && ! @firstEstablishTime
end
to_s() click to toggle source

Return a string representation of the peer.

# File lib/quartz_torrent/peer.rb, line 92
def to_s
  @trackerPeer.to_s
end
updateDownloadRate(msg) click to toggle source

Update the download rate of the peer from the passed PeerWireMessage.

# File lib/quartz_torrent/peer.rb, line 115
def updateDownloadRate(msg)
  @downloadRate.update msg.length
  if msg.is_a? Piece
    @downloadRateDataOnly.update msg.data.length
  end
end
updateUploadRate(msg) click to toggle source

Update the upload rate of the peer from the passed PeerWireMessage.

# File lib/quartz_torrent/peer.rb, line 107
def updateUploadRate(msg)
  @uploadRate.update msg.length
  if msg.is_a? Piece
    @uploadRateDataOnly.update msg.data.length
  end
end