def download_blob_from_peer(self, peer, timeout, blob_hash, blob_manager): log.debug("Try to download %s from %s", blob_hash, peer.host) blob_manager = blob_manager blob = BlobCallback(blob_manager.blob_dir, blob_hash, timeout) download_manager = SingleBlobDownloadManager(blob) peer_finder = SinglePeerFinder(peer) requester = BlobRequester(blob_manager, peer_finder, self._payment_rate_manager, self._wallet, download_manager) downloader = StandaloneBlobDownloader(blob_hash, blob_manager, peer_finder, self._rate_limiter, self._payment_rate_manager, self._wallet, timeout=timeout) info_exchanger = self._wallet.get_info_exchanger() connection_manager = ConnectionManager(downloader, self._rate_limiter, [requester], [info_exchanger]) connection_manager.start() result = yield blob.callback if not result: log.debug("Failed to downloaded %s from %s", blob_hash[:16], peer.host) yield connection_manager.stop() defer.returnValue(result)
def download(self): if not is_valid_blobhash(self.blob_hash): return defer.fail(Failure(InvalidBlobHashError(self.blob_hash))) def cancel_download(d): self.stop() self.finished_deferred = defer.Deferred(canceller=cancel_download) self.download_manager = DownloadManager(self.blob_manager, True) self.download_manager.blob_requester = BlobRequester(self.blob_manager, self.peer_finder, self.payment_rate_manager, self.wallet, self.download_manager) self.download_manager.blob_info_finder = SingleBlobMetadataHandler(self.blob_hash, self.download_manager) self.download_manager.progress_manager = SingleProgressManager(self._blob_downloaded, self.download_manager) self.download_manager.blob_handler = DummyBlobHandler() self.download_manager.wallet_info_exchanger = self.wallet.get_info_exchanger() self.download_manager.connection_manager = ConnectionManager( self, self.rate_limiter, [self.download_manager.blob_requester], [self.download_manager.wallet_info_exchanger] ) d = self.download_manager.start_downloading() d.addCallback(lambda _: self.finished_deferred) return d
def _start(self): self.download_manager = DownloadManager(self.blob_manager, True) info_finder = BlindMetadataHandler(self.info_manager, self.peers, self.peer_finder, self.approved_peers, self.payment_rate_manager, self.wallet, self.download_manager) self.download_manager.blob_info_finder = info_finder blob_requester = BlobRequester(self.blob_manager, self.peer_finder, self.payment_rate_manager, self.wallet, self.download_manager) self.download_manager.blob_requester = blob_requester self.progress_manager = BlindProgressManager(self.blob_manager, self.peers, self.max_space, [PeerScoreBasedScorer(), LengthBasedScorer()], self.download_manager) self.download_manager.progress_manager = self.progress_manager self.download_manager.blob_handler = BlindBlobHandler() wallet_info_exchanger = self.wallet.get_info_exchanger() self.download_manager.wallet_info_exchanger = wallet_info_exchanger connection_manager = ConnectionManager(self, self.rate_limiter, [info_finder, blob_requester], [wallet_info_exchanger]) self.download_manager.connection_manager = connection_manager d = defer.maybeDeferred(self.download_manager.start_downloading) d.addCallback(lambda _: self._update_status(stopped=False)) return d
def _get_blob_requester(self, download_manager): return BlobRequester(self.blob_manager, self.peer_finder, self.payment_rate_manager, self.wallet, download_manager)