def _on_completed_blob(self, blob, response_key): should_announce = False if response_key == RECEIVED_SD_BLOB: sd_info = yield BlobStreamDescriptorReader(blob).get_info() yield save_sd_info(self.stream_info_manager, sd_info) yield self.stream_info_manager.save_sd_blob_hash_to_stream(sd_info['stream_hash'], blob.blob_hash) yield self.lbry_file_manager.add_lbry_file(sd_info['stream_hash'], blob.blob_hash) should_announce = True # if we already have the head blob, set it to be announced now that we know it's # a head blob d = self.check_head_blob_announce(sd_info['stream_hash']) else: d = defer.succeed(None) stream_hash = yield self.stream_info_manager.get_stream_of_blob(blob.blob_hash) if stream_hash is not None: blob_num = yield self.stream_info_manager._get_blob_num_by_hash(stream_hash, blob.blob_hash) if blob_num == 0: should_announce = True sd_hashes = yield self.stream_info_manager.get_sd_blob_hashes_for_stream( stream_hash) # if we already have the sd blob, set it to be announced now that we know it's # a sd blob for sd_hash in sd_hashes: d.addCallback(lambda _: self.check_sd_blob_announce(sd_hash)) yield self.blob_manager.blob_completed(blob, should_announce=should_announce) yield self.close_blob() yield d log.info("Received %s", blob) yield self.send_response({response_key: True})
def _on_completed_blob(self, blob, response_key): should_announce = False if response_key == RECEIVED_SD_BLOB: sd_info = yield BlobStreamDescriptorReader(blob).get_info() yield save_sd_info(self.stream_info_manager, sd_info) yield self.stream_info_manager.save_sd_blob_hash_to_stream(sd_info['stream_hash'], blob.blob_hash) self.lbry_file_manager.add_lbry_file(sd_info['stream_hash']) should_announce = True # if we already have the head blob, set it to be announced now that we know it's # a head blob d = self.check_head_blob_announce(sd_info['stream_hash']) else: d = defer.succeed(None) stream_hash = yield self.stream_info_manager.get_stream_of_blob(blob.blob_hash) if stream_hash is not None: blob_num = yield self.stream_info_manager._get_blob_num_by_hash(stream_hash, blob.blob_hash) if blob_num == 0: should_announce = True sd_hashes = yield self.stream_info_manager.get_sd_blob_hashes_for_stream( stream_hash) # if we already have the sd blob, set it to be announced now that we know it's # a sd blob for sd_hash in sd_hashes: d.addCallback(lambda _: self.check_sd_blob_announce(sd_hash)) yield self.blob_manager.blob_completed(blob, should_announce=should_announce) yield self.close_blob() yield d log.info("Received %s", blob) yield self.send_response({response_key: True})
def make_downloader(self, metadata, options, payment_rate_manager, **kwargs): assert len(options) == 1 data_rate = options[0] payment_rate_manager.min_blob_data_payment_rate = data_rate def save_source_if_blob(stream_hash): if metadata.metadata_source == StreamMetadata.FROM_BLOB: d = self.stream_info_manager.save_sd_blob_hash_to_stream( stream_hash, metadata.source_blob_hash) else: d = defer.succeed(True) d.addCallback(lambda _: stream_hash) return d def create_downloader(stream_hash): downloader = self._make_downloader(stream_hash, payment_rate_manager, metadata.validator.raw_info) d = downloader.set_stream_info() d.addCallback(lambda _: downloader) return d d = save_sd_info(self.stream_info_manager, metadata.validator.raw_info) d.addCallback(save_source_if_blob) d.addCallback(create_downloader) return d
def make_downloader(self, metadata, options, payment_rate_manager, download_directory=None, file_name=None): assert len(options) == 1 data_rate = options[0] stream_hash = yield save_sd_info(self.lbry_file_manager.stream_info_manager, metadata.validator.raw_info) if metadata.metadata_source == StreamMetadata.FROM_BLOB: yield self.lbry_file_manager.save_sd_blob_hash_to_stream(stream_hash, metadata.source_blob_hash) lbry_file = yield self.lbry_file_manager.add_lbry_file(stream_hash, payment_rate_manager, data_rate, download_directory, file_name) defer.returnValue(lbry_file)
def make_downloader(self, metadata, options, payment_rate_manager, download_directory=None): assert len(options) == 1 data_rate = options[0] stream_hash = yield save_sd_info(self.lbry_file_manager.stream_info_manager, metadata.validator.raw_info) if metadata.metadata_source == StreamMetadata.FROM_BLOB: yield self.lbry_file_manager.save_sd_blob_hash_to_stream(stream_hash, metadata.source_blob_hash) lbry_file = yield self.lbry_file_manager.add_lbry_file(stream_hash, metadata.source_blob_hash, payment_rate_manager, data_rate, download_directory) defer.returnValue(lbry_file)
def check_sd_blob_announce(self, sd_hash): if sd_hash in self.blob_manager.blobs: sd_blob = self.blob_manager.blobs[sd_hash] if sd_blob.get_is_verified(): should_announce = yield self.blob_manager.get_should_announce(sd_hash) if should_announce == 0: yield self.blob_manager.set_should_announce(sd_hash, 1) log.info("Discovered previously completed sd blob (%s), " "setting it to be announced", sd_hash[:8]) try: yield self.stream_info_manager.get_stream_hash_for_sd_hash(sd_hash) except NoSuchSDHash: log.info("Adding blobs to stream") sd_info = yield BlobStreamDescriptorReader(sd_blob).get_info() yield save_sd_info(self.stream_info_manager, sd_info) yield self.stream_info_manager.save_sd_blob_hash_to_stream( sd_info['stream_hash'], sd_hash) defer.returnValue(None)
def get_descriptor_response(self, sd_blob): if sd_blob.get_is_verified(): # if we already have the sd blob being offered, make sure we have it and the head blob # marked as such for announcement now that we know it's an sd blob that we have. yield self.check_sd_blob_announce(sd_blob.blob_hash) try: stream_hash = yield self.stream_info_manager.get_stream_hash_for_sd_hash( sd_blob.blob_hash) except NoSuchSDHash: sd_info = yield BlobStreamDescriptorReader(sd_blob).get_info() stream_hash = sd_info['stream_hash'] yield save_sd_info(self.stream_info_manager, sd_info) yield self.stream_info_manager.save_sd_blob_hash_to_stream(stream_hash, sd_blob.blob_hash) yield self.check_head_blob_announce(stream_hash) response = yield self.request_needed_blobs({SEND_SD_BLOB: False}, sd_blob) else: self.incoming_blob = sd_blob self.receiving_blob = True self.handle_incoming_blob(RECEIVED_SD_BLOB) response = {SEND_SD_BLOB: True} defer.returnValue(response)