Class: QuartzTorrent::Peer

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

Overview

This class represents a torrent peer.

Constant Summary collapse

@@stateChangeListeners =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#amChokedObject

Am I choked by this peer



37
38
39
# File 'lib/quartz_torrent/peer.rb', line 37

def amChoked
  @amChoked
end

#amInterestedObject

Am I interested in this peer



39
40
41
# File 'lib/quartz_torrent/peer.rb', line 39

def amInterested
  @amInterested
end

#bitfieldObject

A Bitfield representing the pieces that the peer has.



82
83
84
# File 'lib/quartz_torrent/peer.rb', line 82

def bitfield
  @bitfield
end

#downloadRateObject

Download rate of us to peer.



74
75
76
# File 'lib/quartz_torrent/peer.rb', line 74

def downloadRate
  @downloadRate
end

#downloadRateDataOnlyObject

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

#firstEstablishTimeObject

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

#infoHashObject

Info hash for the torrent of this peer



47
48
49
# File 'lib/quartz_torrent/peer.rb', line 47

def infoHash
  @infoHash
end

#isUsObject

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

#maxRequestedBlocksObject

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

#peerChokedObject

This peer is choked by me



42
43
44
# File 'lib/quartz_torrent/peer.rb', line 42

def peerChoked
  @peerChoked
end

#peerInterestedObject

Is this peer interested



44
45
46
# File 'lib/quartz_torrent/peer.rb', line 44

def peerInterested
  @peerInterested
end

#peerMsgSerializerObject

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

#requestedBlocksObject

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

#requestedBlocksSizeLastPassObject

Returns the value of attribute requestedBlocksSizeLastPass.



86
87
88
# File 'lib/quartz_torrent/peer.rb', line 86

def requestedBlocksSizeLastPass
  @requestedBlocksSizeLastPass
end

#stateObject

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

#trackerPeerObject

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

#uploadRateObject

Upload rate of peer to us.



72
73
74
# File 'lib/quartz_torrent/peer.rb', line 72

def uploadRate
  @uploadRate
end

#uploadRateDataOnlyObject

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

#cloneObject

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.

Returns:

  • (Boolean)


102
103
104
# File 'lib/quartz_torrent/peer.rb', line 102

def eql?(o)
  o.is_a?(Peer) && trackerPeer.eql?(o.trackerPeer)
end

#to_sObject

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