Ejemplo n.º 1
0
 def handleGotNodes(self, nodes):
     """Process any received node contact info in the response.
     
     Not called by default, but suitable for being called by
     L{processResponse} in a recursive node search.
     """
     if nodes and type(nodes) != list:
         raise ValueError, "got a malformed response, from bittorrent perhaps"
     for compact_node in nodes:
         node_contact = uncompact(compact_node)
         node = self.caller.Node(node_contact)
         if not self.found.has_key(node.id):
             self.found[node.id] = node
Ejemplo n.º 2
0
 def handleGotNodes(self, nodes):
     """Process any received node contact info in the response.
     
     Not called by default, but suitable for being called by
     L{processResponse} in a recursive node search.
     """
     if nodes and type(nodes) != list:
         raise ValueError, "got a malformed response, from bittorrent perhaps"
     for compact_node in nodes:
         node_contact = uncompact(compact_node)
         node = self.caller.Node(node_contact)
         if not self.found.has_key(node.id):
             self.found[node.id] = node
Ejemplo n.º 3
0
    def run(self):
        """Start the downloading process."""
        log.msg('Checking for pieces for %s' % self.path)
        self.defer = defer.Deferred()
        self.peers = {}
        no_pieces = 0
        pieces_string = {0: 0}
        pieces_hash = {0: 0}
        pieces_dl_hash = {0: 0}

        for compact_peer in self.compact_peers:
            # Build a list of all the peers for this download
            site = uncompact(compact_peer['c'])
            peer = self.manager.getPeer(site)
            self.peers.setdefault(site, {})['peer'] = peer

            # Extract any piece information from the peers list
            if 't' in compact_peer:
                self.peers[site]['t'] = compact_peer['t']['t']
                pieces_string.setdefault(compact_peer['t']['t'], 0)
                pieces_string[compact_peer['t']['t']] += 1
            elif 'h' in compact_peer:
                self.peers[site]['h'] = compact_peer['h']
                pieces_hash.setdefault(compact_peer['h'], 0)
                pieces_hash[compact_peer['h']] += 1
            elif 'l' in compact_peer:
                self.peers[site]['l'] = compact_peer['l']
                pieces_dl_hash.setdefault(compact_peer['l'], 0)
                pieces_dl_hash[compact_peer['l']] += 1
            else:
                no_pieces += 1
        
        # Select the most popular piece info
        max_found = max(no_pieces, max(pieces_string.values()),
                        max(pieces_hash.values()), max(pieces_dl_hash.values()))

        if max_found < len(self.peers):
            log.msg('Misleading piece information found, using most popular %d of %d peers' % 
                    (max_found, len(self.peers)))

        if max_found == no_pieces:
            # The file is not split into pieces
            log.msg('No pieces were found for the file')
            self.pieces = [self.hash.expected()]
            self.startDownload()
        elif max_found == max(pieces_string.values()):
            # Small number of pieces in a string
            for pieces, num in pieces_string.items():
                # Find the most popular piece string
                if num == max_found:
                    self.pieces = [pieces[x:x+20] for x in xrange(0, len(pieces), 20)]
                    log.msg('Peer info contained %d piece hashes' % len(self.pieces))
                    self.startDownload()
                    break
        elif max_found == max(pieces_hash.values()):
            # Medium number of pieces stored in the DHT
            for pieces, num in pieces_hash.items():
                # Find the most popular piece hash to lookup
                if num == max_found:
                    log.msg('Found a hash for pieces to lookup in the DHT: %r' % pieces)
                    self.getDHTPieces(pieces)
                    break
        elif max_found == max(pieces_dl_hash.values()):
            # Large number of pieces stored in peers
            for pieces, num in pieces_dl_hash.items():
                # Find the most popular piece hash to download
                if num == max_found:
                    log.msg('Found a hash for pieces to lookup in peers: %r' % pieces)
                    self.getPeerPieces(pieces)
                    break
        return self.defer
Ejemplo n.º 4
0
    def run(self):
        """Start the downloading process."""
        log.msg('Checking for pieces for %s' % self.path)
        self.defer = defer.Deferred()
        self.peers = {}
        no_pieces = 0
        pieces_string = {0: 0}
        pieces_hash = {0: 0}
        pieces_dl_hash = {0: 0}

        for compact_peer in self.compact_peers:
            # Build a list of all the peers for this download
            site = uncompact(compact_peer['c'])
            peer = self.manager.getPeer(site)
            self.peers.setdefault(site, {})['peer'] = peer

            # Extract any piece information from the peers list
            if 't' in compact_peer:
                self.peers[site]['t'] = compact_peer['t']['t']
                pieces_string.setdefault(compact_peer['t']['t'], 0)
                pieces_string[compact_peer['t']['t']] += 1
            elif 'h' in compact_peer:
                self.peers[site]['h'] = compact_peer['h']
                pieces_hash.setdefault(compact_peer['h'], 0)
                pieces_hash[compact_peer['h']] += 1
            elif 'l' in compact_peer:
                self.peers[site]['l'] = compact_peer['l']
                pieces_dl_hash.setdefault(compact_peer['l'], 0)
                pieces_dl_hash[compact_peer['l']] += 1
            else:
                no_pieces += 1

        # Select the most popular piece info
        max_found = max(no_pieces, max(pieces_string.values()),
                        max(pieces_hash.values()),
                        max(pieces_dl_hash.values()))

        if max_found < len(self.peers):
            log.msg(
                'Misleading piece information found, using most popular %d of %d peers'
                % (max_found, len(self.peers)))

        if max_found == no_pieces:
            # The file is not split into pieces
            log.msg('No pieces were found for the file')
            self.pieces = [self.hash.expected()]
            self.startDownload()
        elif max_found == max(pieces_string.values()):
            # Small number of pieces in a string
            for pieces, num in pieces_string.items():
                # Find the most popular piece string
                if num == max_found:
                    self.pieces = [
                        pieces[x:x + 20] for x in xrange(0, len(pieces), 20)
                    ]
                    log.msg('Peer info contained %d piece hashes' %
                            len(self.pieces))
                    self.startDownload()
                    break
        elif max_found == max(pieces_hash.values()):
            # Medium number of pieces stored in the DHT
            for pieces, num in pieces_hash.items():
                # Find the most popular piece hash to lookup
                if num == max_found:
                    log.msg(
                        'Found a hash for pieces to lookup in the DHT: %r' %
                        pieces)
                    self.getDHTPieces(pieces)
                    break
        elif max_found == max(pieces_dl_hash.values()):
            # Large number of pieces stored in peers
            for pieces, num in pieces_dl_hash.items():
                # Find the most popular piece hash to download
                if num == max_found:
                    log.msg('Found a hash for pieces to lookup in peers: %r' %
                            pieces)
                    self.getPeerPieces(pieces)
                    break
        return self.defer