from apt_p2p_Khashmir.bencode import bdecode
from apt_p2p_conf import config
+
+class PeerError(Exception):
+ """An error occurred downloading from peers."""
+
class GrowingFileStream(stream.FileStream):
"""Modified to stream data from a file as it becomes available.
def _gotData(self, data):
"""Process the received data."""
if self.outFile.closed:
- raise Exception, "outFile was unexpectedly closed"
+ raise PeerError, "outFile was unexpectedly closed"
if data is None:
- raise Exception, "Data is None?"
+ raise PeerError, "Data is None?"
# Make sure we don't go too far
if self.length is not None and self.position + len(data) > self.length:
if self.pieces is None:
# Send a request to one or more peers
- log.msg('Checking for a peer to request piece hashes from')
for site in self.peers:
if self.peers[site].get('failed', False) != True:
log.msg('Sending a piece hash request to %r' % (site, ))
if self.outstanding >= 4:
break
- log.msg('Done sending piece hash requests for now, %d outstanding' % self.outstanding)
if self.pieces is None and self.outstanding <= 0:
# Continue without the piece hashes
log.msg('Could not retrieve the piece hashes from the peers')
log.msg('Got a piece hash response %d from %r' % (response.code, site))
if response.code != 200:
# Request failed, try a different peer
- log.msg('Did not like response %d from %r' % (response.code, site))
self.getPeerPieces(key, site)
else:
# Read the response stream to a string
#{ Downloading the pieces
def getPieces(self):
"""Download the next pieces from the peers."""
- log.msg('Checking for more piece requests to send')
self.sort()
piece = self.nextFinish
while self.outstanding < 4 and self.peerlist and piece < len(self.completePieces):
- log.msg('Checking piece %d' % piece)
if self.completePieces[piece] == False:
# Send a request to the highest ranked peer
peer = self.peerlist.pop()
'errbackArgs': (piece, peer)})
piece += 1
- log.msg('Finished checking pieces, %d outstanding, next piece %d of %d' % (self.outstanding, self.nextFinish, len(self.completePieces)))
# Check if we're done
if self.outstanding <= 0 and self.nextFinish >= len(self.completePieces):
log.msg('We seem to be done with all pieces')
from node import Node, NULL_ID
+class KNodeError(Exception):
+ """An error occurred in contacting the node."""
+
class KNodeBase(Node):
"""A basic node that can only be pinged and help find other nodes."""
senderid = dict['id']
except KeyError:
log.msg("No peer id in response")
- raise Exception, "No peer id in response."
+ raise KNodeError, "No peer id in response."
else:
if self.id != NULL_ID and senderid != self.id:
log.msg("Got response from different node than expected.")
self.table.invalidateNode(self)
- raise Exception, "Node ID has changed"
+ raise KNodeError, "Node ID has changed"
return dict