当前位置: 首页>>代码示例>>Python>>正文


Python Tracker.update_download_stats方法代码示例

本文整理汇总了Python中tracker.Tracker.update_download_stats方法的典型用法代码示例。如果您正苦于以下问题:Python Tracker.update_download_stats方法的具体用法?Python Tracker.update_download_stats怎么用?Python Tracker.update_download_stats使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tracker.Tracker的用法示例。


在下文中一共展示了Tracker.update_download_stats方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Client

# 需要导入模块: from tracker import Tracker [as 别名]
# 或者: from tracker.Tracker import update_download_stats [as 别名]

#.........这里部分代码省略.........
    def add_peer_to_piece_peer_list(self, piece_index, peer):
        # print 'Adding piece', piece_index, 'to peer', peer
        self.pieces[piece_index].add_peer_to_peer_list(peer)

    def manage_requests(self, num_pieces=1):
        logging.info('Sending more piece requests')
        logging.info('Piece queue has %s pieces', self.piece_queue.length())
        if not self.piece_queue.empty():
            self.manage_piece_queue_state();
            for i in xrange(num_pieces):
                self.request_next_piece();
            logging.info('Cleaning up piece queue')
        else:
            # Count outstanding requests to decide when to go into endgame
            self.torrent_state = 'endgame'
            self.start_endgame()

    def start_endgame(self):
        self.blasted_requests = []
        for i in xrange(ENDGAME_MAX_BLASTS):
            self.send_endgame_request()
            
    def send_endgame_request(self):
        block_info = self.select_outstanding_request()
        if block_info:
            self.blasted_requests.append(block_info)
            self.pieces(block_info[0]).request_block_endgame(block_info)

    def select_outstanding_request(self):
        # TODO: Use filter instead of picking at random
        peers_with_requests = filter(lambda peer: len(peer.outstanding_requests) > 0, self.peers)
        if len(peers_with_requests):
            peer = random.choice(peers_with_requests)
            block_info = random.choice(peer.outstanding_requests)
            return block_info

    def manage_piece_queue_state(self):
        # This should probably only get called occasionally
        logging.debug('Have received %s pieces, need %s more', self.bitfield.count(1), self.bitfield.count(0))
        if self.bitfield.count(1) > PIECE_THRESHOLD and self.piece_queue.length() > PIECE_THRESHOLD:
            self.piece_queue.update_piece_order()
            self.torrent_state = 'rarest_first'

    # DISPATCHES TO PIECE
    def request_block(self, block_info):
        piece_index = block_info[0]
        self.pieces[piece_index].request_block(block_info)

    def request_next_piece(self):
        next_piece = self.piece_queue.get_next_piece(self.torrent_state)
        logging.info('Requesting piece %s', next_piece)
        if next_piece:
            try:
                next_piece.request_all_blocks()
            except IndexError as e:
                self.piece_queue.put(next_piece)
                logging.error(e)

    def add_block(self, block_info, block):
        (piece_index, begin, block_length) = block_info
        logging.info('Writing block of length %s at index %s for piece %s',
                block_length, begin, piece_index)
        piece = self.pieces[piece_index]
        logging.info('Piece has index %s', piece.index)
        piece.add_block(begin, block)
        self.tracker.update_download_stats(block_length)
        if self.torrent_state == 'endgame' and block_info in self.blasted_requests:
            piece = self.pieces[block_info[0]]
            piece.cancel_block(block_info, self)
            if self.bitfield.count(0) > 0:
                self.send_endgame_request()
        if self.num_pieces - self.bitfield.count(1) == 0:
            self.finalize_download()

    def finalize_download(self):
        logging.info('Finalizing download')
        if not self.tracker.is_download_complete():
            raise SystemExit('Download didnt complete. Shutting down.')
        self.stitch_files()
        self.tracker.send_completed_msg_to_tracker_server()
        logging.info('Shutting down connection with peers')
        for peer in self.peers:
            peer.close()
        print 'Quitting client'
        logging.info('Download completed. Quitting client.')
        sys.exit()

    def stitch_files(self):
        print 'stitching...'
        logging.info('Wrote all pieces, stitching them together')
        self.stitcher.stitch()
        logging.info('Stitching completed.')

    def finalize_piece(self, piece):
        if piece.check_info_hash():
            logging.debug('Yay! Correct info hash!')
            self.add_piece_to_bitfield(piece_index)
        else:
            logging.debug('Incorrect infohash, starting over with piece %s', piece_index)
            piece.reset()
开发者ID:wevial,项目名称:downpour,代码行数:104,代码来源:client.py


注:本文中的tracker.Tracker.update_download_stats方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。