class QuartzTorrent::Peer
This class represents a torrent peer.
Attributes
Am I choked by this peer
Am I interested in this peer
A Bitfield
representing the pieces that the peer has.
Download rate of us to peer.
Download rate of us to peer, only counting actual torrent data
Time when the peers connection was established the first time. This is nil when the peer has never had an established connection.
Info hash for the torrent of this peer
Is this peer ourself? Used to tell if we connected to ourself.
Maximum number of outstanding block requests allowed for this peer.
This peer is choked by me
Is this peer interested
A PeerWireMessageSerializer
that can unserialize and serialize messages to and from this peer.
A hash of the block indexes of the outstanding blocks requested from this peer
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.
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.
Upload rate of peer to us.
Upload rate of peer to us, only counting actual torrent data
Public Class Methods
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
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
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
Equate peers.
# File lib/quartz_torrent/peer.rb, line 102 def eql?(o) o.is_a?(Peer) && trackerPeer.eql?(o.trackerPeer) end
# 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
Return a string representation of the peer.
# File lib/quartz_torrent/peer.rb, line 92 def to_s @trackerPeer.to_s end
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
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