Пример #1
0
 def interest_state(self, peer):
     if peer.peer_interested:
         if sum([not p.am_choking for p in self.peers]) < 4:
             peer.choke(False) #unchoke this peer if it's okay
         elif random.randint(0, 9) == 0:
             #sometimes, randomly choke someone else and unchoke anyway
             random.choice([p for p in self.peers if not p.am_choking]).choke(True)
             peer.choke(False)
     else:
         peer.choke(True)
         #optomistically unchoke
         random.choice([p for p in self.peers if p.am_choking]).choke(False)
Пример #2
0
    def _unchoke_peers(self):
        with self._manager.peers:
            del self._unchoken[:]
            del self._downloaders[:]

            which = lambda p: p.upload_speed() if not \
                self._manager.completed() else lambda p: p.download_speed()

            peers = sorted(\
                self._manager.peers, key = which)

            if self._round % 3 == 0:
                for i in range(self._optimistic):
                    if peers:
                        peer = random.choice(peers)
                        if peer.interested:
                            self._downloaders.append(peer)
                            
                        if peer.choking:
                            peer.choke(False)

                        self._unchoken.append(peer)
                        peers.remove(peer)
                    else:
                        return

            for peer in peers:
                if len(self._downloaders) < MAX_DOWNLOADERS:
                    if peer.interested:
                        self._downloaders.append(peer)

                    if peer.choking:
                        peer.choke(False)

                    self._unchoken.append(peer)
                elif not peer.choking:
                    peer.choke(True)

            if self._round % 2 == 0:
                for peer in self._manager.peers:
                    peer.mark()

            self._round += 1
Пример #3
0
            self._torrent = t

        def has(self, piece):
            return False

        def completed(self):
            return False

        def piece(self, index):
            return storage.Piece.create_empty_piece(\
                index, 256000, self._torrent.piece_hash(index))

        def write_piece(self, piece):
            print("Writting piece ", piece)

    manager = PeerManager(peer_id, t, None, None, StorageMock(t))
    peer = peer.handshake(sock, addr, peer_id, t, manager)

    field = bitfield.Bitfield(t.number_of_pieces)
    #for i in range(len(field)):
    #    if random.random() < 0.5:
    #        field.set(i)
    field.set(8)

    peer.bitfield(field)
    peer.choke(True)
    peer.choke(False)
    peer.have(9)
    time.sleep(5)
    peer.close()
Пример #4
0
            self._torrent = t

        def has(self, piece):
            return False

        def completed(self):
            return False

        def piece(self, index):
            return storage.Piece.create_empty_piece(\
                index, 256000, self._torrent.piece_hash(index))

        def write_piece(self, piece):
            print("Writting piece ", piece)

    manager = PeerManager(peer_id, t, None, None, StorageMock(t))
    peer = peer.handshake(sock, addr, peer_id, t, manager)

    field = bitfield.Bitfield(t.number_of_pieces)
    #for i in range(len(field)):
    #    if random.random() < 0.5:
    #        field.set(i)
    field.set(8)

    peer.bitfield(field)
    peer.choke(True)
    peer.choke(False)
    peer.have(9)
    time.sleep(5)
    peer.close()