Class: QuartzTorrent::Peer
- Inherits:
-
Object
- Object
- QuartzTorrent::Peer
- Defined in:
- lib/quartz_torrent/peer.rb
Overview
This class represents a torrent peer.
Constant Summary collapse
- @@stateChangeListeners =
[]
Instance Attribute Summary collapse
-
#amChoked ⇒ Object
Am I choked by this peer.
-
#amInterested ⇒ Object
Am I interested in this peer.
-
#bitfield ⇒ Object
A Bitfield representing the pieces that the peer has.
-
#downloadRate ⇒ Object
Download rate of us to peer.
-
#downloadRateDataOnly ⇒ Object
Download rate of us to peer, only counting actual torrent data.
-
#firstEstablishTime ⇒ Object
Time when the peers connection was established the first time.
-
#infoHash ⇒ Object
Info hash for the torrent of this peer.
-
#isUs ⇒ Object
Is this peer ourself? Used to tell if we connected to ourself.
-
#maxRequestedBlocks ⇒ Object
Maximum number of outstanding block requests allowed for this peer.
-
#peerChoked ⇒ Object
This peer is choked by me.
-
#peerInterested ⇒ Object
Is this peer interested.
-
#peerMsgSerializer ⇒ Object
A PeerWireMessageSerializer that can unserialize and serialize messages to and from this peer.
-
#requestedBlocks ⇒ Object
A hash of the block indexes of the outstanding blocks requested from this peer.
-
#requestedBlocksSizeLastPass ⇒ Object
Returns the value of attribute requestedBlocksSizeLastPass.
-
#state ⇒ Object
Peer connection state.
-
#trackerPeer ⇒ Object
A TrackerPeer class with the information about the peer retrieved from the tracker.
-
#uploadRate ⇒ Object
Upload rate of peer to us.
-
#uploadRateDataOnly ⇒ Object
Upload rate of peer to us, only counting actual torrent data.
Class Method Summary collapse
-
.addStateChangeListener(l) ⇒ Object
Add a proc to the list of state change listeners.
Instance Method Summary collapse
-
#clone ⇒ Object
Create a clone of this peer.
-
#eql?(o) ⇒ Boolean
Equate peers.
-
#initialize(trackerPeer) ⇒ Peer
constructor
Create a new Peer using the information from the passed TrackerPeer object.
-
#to_s ⇒ Object
Return a string representation of the peer.
-
#updateDownloadRate(msg) ⇒ Object
Update the download rate of the peer from the passed PeerWireMessage.
-
#updateUploadRate(msg) ⇒ Object
Update the upload rate of the peer from the passed PeerWireMessage.
Constructor Details
#initialize(trackerPeer) ⇒ Peer
Create a new Peer using the information from the passed TrackerPeer object.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# 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 |
Instance Attribute Details
#amChoked ⇒ Object
Am I choked by this peer
37 38 39 |
# File 'lib/quartz_torrent/peer.rb', line 37 def amChoked @amChoked end |
#amInterested ⇒ Object
Am I interested in this peer
39 40 41 |
# File 'lib/quartz_torrent/peer.rb', line 39 def amInterested @amInterested end |
#bitfield ⇒ Object
A Bitfield representing the pieces that the peer has.
82 83 84 |
# File 'lib/quartz_torrent/peer.rb', line 82 def bitfield @bitfield end |
#downloadRate ⇒ Object
Download rate of us to peer.
74 75 76 |
# File 'lib/quartz_torrent/peer.rb', line 74 def downloadRate @downloadRate end |
#downloadRateDataOnly ⇒ Object
Download rate of us to peer, only counting actual torrent data
79 80 81 |
# File 'lib/quartz_torrent/peer.rb', line 79 def downloadRateDataOnly @downloadRateDataOnly end |
#firstEstablishTime ⇒ Object
Time when the peers connection was established the first time. This is nil when the peer has never had an established connection.
51 52 53 |
# File 'lib/quartz_torrent/peer.rb', line 51 def firstEstablishTime @firstEstablishTime end |
#infoHash ⇒ Object
Info hash for the torrent of this peer
47 48 49 |
# File 'lib/quartz_torrent/peer.rb', line 47 def infoHash @infoHash end |
#isUs ⇒ Object
Is this peer ourself? Used to tell if we connected to ourself.
69 70 71 |
# File 'lib/quartz_torrent/peer.rb', line 69 def isUs @isUs end |
#maxRequestedBlocks ⇒ Object
Maximum number of outstanding block requests allowed for this peer.
54 55 56 |
# File 'lib/quartz_torrent/peer.rb', line 54 def maxRequestedBlocks @maxRequestedBlocks end |
#peerChoked ⇒ Object
This peer is choked by me
42 43 44 |
# File 'lib/quartz_torrent/peer.rb', line 42 def peerChoked @peerChoked end |
#peerInterested ⇒ Object
Is this peer interested
44 45 46 |
# File 'lib/quartz_torrent/peer.rb', line 44 def peerInterested @peerInterested end |
#peerMsgSerializer ⇒ Object
A PeerWireMessageSerializer that can unserialize and serialize messages to and from this peer.
89 90 91 |
# File 'lib/quartz_torrent/peer.rb', line 89 def peerMsgSerializer @peerMsgSerializer end |
#requestedBlocks ⇒ Object
A hash of the block indexes of the outstanding blocks requested from this peer
85 86 87 |
# File 'lib/quartz_torrent/peer.rb', line 85 def requestedBlocks @requestedBlocks end |
#requestedBlocksSizeLastPass ⇒ Object
Returns the value of attribute requestedBlocksSizeLastPass.
86 87 88 |
# File 'lib/quartz_torrent/peer.rb', line 86 def requestedBlocksSizeLastPass @requestedBlocksSizeLastPass end |
#state ⇒ Object
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.
60 61 62 |
# File 'lib/quartz_torrent/peer.rb', line 60 def state @state end |
#trackerPeer ⇒ Object
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.
34 35 36 |
# File 'lib/quartz_torrent/peer.rb', line 34 def trackerPeer @trackerPeer end |
#uploadRate ⇒ Object
Upload rate of peer to us.
72 73 74 |
# File 'lib/quartz_torrent/peer.rb', line 72 def uploadRate @uploadRate end |
#uploadRateDataOnly ⇒ Object
Upload rate of peer to us, only counting actual torrent data
77 78 79 |
# File 'lib/quartz_torrent/peer.rb', line 77 def uploadRateDataOnly @uploadRateDataOnly end |
Class Method Details
.addStateChangeListener(l) ⇒ Object
Add a proc to the list of state change listeners.
97 98 99 |
# File 'lib/quartz_torrent/peer.rb', line 97 def self.addStateChangeListener(l) @@stateChangeListeners.push l end |
Instance Method Details
#clone ⇒ Object
Create a clone of this peer. This method does not clone listeners.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# 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) ⇒ Boolean
Equate peers.
102 103 104 |
# File 'lib/quartz_torrent/peer.rb', line 102 def eql?(o) o.is_a?(Peer) && trackerPeer.eql?(o.trackerPeer) end |
#to_s ⇒ Object
Return a string representation of the peer.
92 93 94 |
# File 'lib/quartz_torrent/peer.rb', line 92 def to_s @trackerPeer.to_s end |
#updateDownloadRate(msg) ⇒ Object
Update the download rate of the peer from the passed PeerWireMessage.
115 116 117 118 119 120 |
# 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) ⇒ Object
Update the upload rate of the peer from the passed PeerWireMessage.
107 108 109 110 111 112 |
# 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 |