2015-08-20 17:27:15 +02:00
|
|
|
import logging
|
|
|
|
from lbrynet.core.BlobInfo import BlobInfo
|
|
|
|
from lbrynet.core.client.BlobRequester import BlobRequester
|
|
|
|
from lbrynet.core.client.ConnectionManager import ConnectionManager
|
|
|
|
from lbrynet.core.client.DownloadManager import DownloadManager
|
2017-09-27 23:23:41 +02:00
|
|
|
from lbrynet.core.Error import InvalidBlobHashError, DownloadSDTimeout
|
2017-06-20 21:03:17 +02:00
|
|
|
from lbrynet.core.utils import is_valid_blobhash, safe_start_looping_call, safe_stop_looping_call
|
2015-09-16 22:27:46 +02:00
|
|
|
from twisted.python.failure import Failure
|
2015-08-20 17:27:15 +02:00
|
|
|
from twisted.internet import defer
|
2017-06-21 00:56:08 +02:00
|
|
|
from twisted.internet.task import LoopingCall
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2015-09-08 21:42:56 +02:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
class SingleBlobMetadataHandler(object):
|
2018-07-03 06:51:25 +02:00
|
|
|
#implements(interfaces.IMetadataHandler)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def __init__(self, blob_hash, download_manager):
|
|
|
|
self.blob_hash = blob_hash
|
|
|
|
self.download_manager = download_manager
|
|
|
|
|
|
|
|
######## IMetadataHandler #########
|
|
|
|
|
|
|
|
def get_initial_blobs(self):
|
2015-09-08 21:42:56 +02:00
|
|
|
log.debug("Returning the blob info")
|
2015-08-20 17:27:15 +02:00
|
|
|
return defer.succeed([BlobInfo(self.blob_hash, 0, None)])
|
|
|
|
|
|
|
|
def final_blob_num(self):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
class SingleProgressManager(object):
|
2017-06-21 00:56:08 +02:00
|
|
|
def __init__(self, download_manager, finished_callback, timeout_callback, timeout):
|
2015-08-20 17:27:15 +02:00
|
|
|
self.finished_callback = finished_callback
|
2017-06-21 00:56:08 +02:00
|
|
|
self.timeout_callback = timeout_callback
|
2015-08-20 17:27:15 +02:00
|
|
|
self.download_manager = download_manager
|
|
|
|
|
2017-06-21 00:56:08 +02:00
|
|
|
self.timeout = timeout
|
|
|
|
self.timeout_counter = 0
|
|
|
|
self.checker = LoopingCall(self._check_if_finished)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2017-06-20 21:03:17 +02:00
|
|
|
def start(self):
|
2017-06-21 00:56:08 +02:00
|
|
|
safe_start_looping_call(self.checker, 1)
|
2015-08-20 17:27:15 +02:00
|
|
|
return defer.succeed(True)
|
|
|
|
|
|
|
|
def stop(self):
|
2017-06-20 21:03:17 +02:00
|
|
|
safe_stop_looping_call(self.checker)
|
2015-08-20 17:27:15 +02:00
|
|
|
return defer.succeed(True)
|
|
|
|
|
|
|
|
def _check_if_finished(self):
|
2017-06-20 21:03:17 +02:00
|
|
|
if self.stream_position() == 1:
|
|
|
|
blob_downloaded = self.download_manager.blobs[0]
|
2017-06-21 17:48:35 +02:00
|
|
|
log.debug("The blob %s has been downloaded. Calling the finished callback",
|
|
|
|
str(blob_downloaded))
|
2017-06-20 21:03:17 +02:00
|
|
|
safe_stop_looping_call(self.checker)
|
|
|
|
self.finished_callback(blob_downloaded)
|
2017-06-21 00:56:08 +02:00
|
|
|
elif self.timeout is not None:
|
2017-06-21 17:45:38 +02:00
|
|
|
self.timeout_counter += 1
|
2017-06-21 00:56:08 +02:00
|
|
|
if self.timeout_counter >= self.timeout:
|
|
|
|
safe_stop_looping_call(self.checker)
|
|
|
|
self.timeout_callback()
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def stream_position(self):
|
|
|
|
blobs = self.download_manager.blobs
|
2017-09-20 20:02:34 +02:00
|
|
|
if blobs and blobs[0].get_is_verified():
|
2015-08-20 17:27:15 +02:00
|
|
|
return 1
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def needed_blobs(self):
|
|
|
|
blobs = self.download_manager.blobs
|
|
|
|
assert len(blobs) == 1
|
2018-07-21 23:11:44 +02:00
|
|
|
return [b for b in blobs.values() if not b.get_is_verified()]
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
class DummyBlobHandler(object):
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def handle_blob(self, blob, blob_info):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class StandaloneBlobDownloader(object):
|
2016-11-30 21:20:45 +01:00
|
|
|
def __init__(self, blob_hash, blob_manager, peer_finder,
|
2017-06-21 00:56:08 +02:00
|
|
|
rate_limiter, payment_rate_manager, wallet,
|
2017-06-21 17:45:38 +02:00
|
|
|
timeout=None):
|
2015-08-20 17:27:15 +02:00
|
|
|
self.blob_hash = blob_hash
|
|
|
|
self.blob_manager = blob_manager
|
|
|
|
self.peer_finder = peer_finder
|
|
|
|
self.rate_limiter = rate_limiter
|
|
|
|
self.payment_rate_manager = payment_rate_manager
|
|
|
|
self.wallet = wallet
|
2017-06-21 00:56:08 +02:00
|
|
|
self.timeout = timeout
|
2015-08-20 17:27:15 +02:00
|
|
|
self.download_manager = None
|
|
|
|
self.finished_deferred = None
|
|
|
|
|
|
|
|
def download(self):
|
2015-09-16 22:27:46 +02:00
|
|
|
if not is_valid_blobhash(self.blob_hash):
|
|
|
|
return defer.fail(Failure(InvalidBlobHashError(self.blob_hash)))
|
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
def cancel_download(d):
|
|
|
|
self.stop()
|
|
|
|
|
|
|
|
self.finished_deferred = defer.Deferred(canceller=cancel_download)
|
2016-12-20 03:16:37 +01:00
|
|
|
self.download_manager = DownloadManager(self.blob_manager)
|
2015-08-20 17:27:15 +02:00
|
|
|
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)
|
2017-06-21 00:56:08 +02:00
|
|
|
self.download_manager.progress_manager = SingleProgressManager(self.download_manager,
|
|
|
|
self._blob_downloaded,
|
|
|
|
self._download_timedout,
|
|
|
|
self.timeout)
|
2015-08-20 17:27:15 +02:00
|
|
|
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 stop(self):
|
|
|
|
return self.download_manager.stop_downloading()
|
|
|
|
|
|
|
|
def _blob_downloaded(self, blob):
|
|
|
|
self.stop()
|
2015-09-17 07:43:41 +02:00
|
|
|
if not self.finished_deferred.called:
|
|
|
|
self.finished_deferred.callback(blob)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2017-06-21 00:56:08 +02:00
|
|
|
def _download_timedout(self):
|
|
|
|
self.stop()
|
|
|
|
if not self.finished_deferred.called:
|
2017-09-27 23:23:41 +02:00
|
|
|
self.finished_deferred.errback(DownloadSDTimeout(self.blob_hash))
|
2017-06-21 00:56:08 +02:00
|
|
|
|
2015-09-17 07:43:41 +02:00
|
|
|
def insufficient_funds(self, err):
|
|
|
|
self.stop()
|
|
|
|
if not self.finished_deferred.called:
|
2016-09-26 02:18:29 +02:00
|
|
|
self.finished_deferred.errback(err)
|