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 handle_normal_request(self, request_dict): if self.blob_write is None: # we haven't opened a blob yet, meaning we must be waiting for the # next message containing a blob hash and a length. this message # should be it. if it's one we want, open the blob for writing, and # return a nice response dict (in a Deferred) saying go ahead if not 'blob_hash' in request_dict or not 'blob_size' in request_dict: raise ValueError("Expected a blob hash and a blob size") if not is_valid_blobhash(request_dict['blob_hash']): raise ValueError("Got a bad blob hash: {}".format( request_dict['blob_hash'])) log.debug('Recieved info for blob: %s', request_dict['blob_hash']) d = self.blob_manager.get_blob(request_dict['blob_hash'], True, int(request_dict['blob_size'])) d.addCallback(self.determine_blob_needed) else: # we have a blob open already, so this message should have nothing # important in it. to the deferred that fires when the blob is done, # add a callback which returns a nice response dict saying to keep # sending, and then return that deferred log.debug('blob is already open') self.receiving_blob = True d = self.blob_finished_d d.addCallback(lambda _: self.close_blob()) d.addCallback(lambda _: {'received_blob': True}) return d
def handle_normal_request(self, request_dict): if self.blob_write is None: # we haven't opened a blob yet, meaning we must be waiting for the # next message containing a blob hash and a length. this message # should be it. if it's one we want, open the blob for writing, and # return a nice response dict (in a Deferred) saying go ahead if not 'blob_hash' in request_dict or not 'blob_size' in request_dict: raise ValueError("Expected a blob hash and a blob size") if not is_valid_blobhash(request_dict['blob_hash']): raise ValueError("Got a bad blob hash: {}".format(request_dict['blob_hash'])) log.debug('Recieved info for blob: %s', request_dict['blob_hash']) d = self.blob_manager.get_blob( request_dict['blob_hash'], True, int(request_dict['blob_size']) ) d.addCallback(self.determine_blob_needed) else: # we have a blob open already, so this message should have nothing # important in it. to the deferred that fires when the blob is done, # add a callback which returns a nice response dict saying to keep # sending, and then return that deferred log.debug('blob is already open') self.receiving_blob = True d = self.blob_finished_d d.addCallback(lambda _: self.close_blob()) d.addCallback(lambda _: {'received_blob': True}) return d
def __init__(self, blob_hash, length=None): assert is_valid_blobhash(blob_hash) self.blob_hash = blob_hash self.length = length self.writers = {} # {Peer: writer, finished_deferred} self.finished_deferred = None self._verified = False self.readers = 0
def __init__(self, blob_hash, upload_allowed, length=None): assert is_valid_blobhash(blob_hash) self.blob_hash = blob_hash self.length = length self.writers = {} # {Peer: writer, finished_deferred} self.finished_deferred = None self._verified = False self.upload_allowed = upload_allowed self.readers = 0
def __init__(self, blob_dir, blob_hash, length=None): if not is_valid_blobhash(blob_hash): raise InvalidBlobHashError(blob_hash) self.blob_hash = blob_hash self.length = length self.writers = {} # {Peer: writer, finished_deferred} self._verified = False self.readers = 0 self.blob_dir = blob_dir self.file_path = os.path.join(blob_dir, self.blob_hash) self.blob_write_lock = defer.DeferredLock() self.saved_verified_blob = False if os.path.isfile(self.file_path): self.set_length(os.path.getsize(self.file_path)) # This assumes that the hash of the blob has already been # checked as part of the blob creation process. It might # be worth having a function that checks the actual hash; # its probably too expensive to have that check be part of # this call. self._verified = True
def is_blob_request(self, request_dict): if BLOB_HASH not in request_dict or BLOB_SIZE not in request_dict: return False if not is_valid_blobhash(request_dict[BLOB_HASH]): raise InvalidBlobHashError(request_dict[BLOB_HASH]) return True
def is_descriptor_request(self, request_dict): if SD_BLOB_HASH not in request_dict or SD_BLOB_SIZE not in request_dict: return False if not is_valid_blobhash(request_dict[SD_BLOB_HASH]): raise InvalidBlobHashError(request_dict[SD_BLOB_HASH]) return True