2016-10-28 21:48:51 +02:00
|
|
|
import logging
|
2017-02-06 19:18:41 +01:00
|
|
|
from twisted.internet import reactor
|
|
|
|
from twisted.internet.error import ConnectionLost, ConnectionDone
|
2016-10-28 21:48:51 +02:00
|
|
|
from lbrynet.reflector import BlobClientFactory, ClientFactory
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def _check_if_reflector_has_stream(lbry_file, reflector_server):
|
|
|
|
reflector_address, reflector_port = reflector_server[0], reflector_server[1]
|
|
|
|
factory = BlobClientFactory(
|
|
|
|
lbry_file.blob_manager,
|
|
|
|
[lbry_file.sd_hash]
|
|
|
|
)
|
|
|
|
d = reactor.resolve(reflector_address)
|
|
|
|
d.addCallback(lambda ip: reactor.connectTCP(ip, reflector_port, factory))
|
|
|
|
d.addCallback(lambda _: factory.finished_deferred)
|
|
|
|
d.addCallback(lambda _: not factory.sent_blobs)
|
|
|
|
return d
|
|
|
|
|
|
|
|
|
2017-02-07 20:27:38 +01:00
|
|
|
def log_result(result, uri):
|
|
|
|
if len(result) == 0:
|
|
|
|
log.info("Reflector has all blobs for lbry://%s", uri)
|
|
|
|
else:
|
|
|
|
log.info("Reflected %i blobs for lbry://%s", len(result), uri)
|
|
|
|
|
|
|
|
|
2016-10-28 21:48:51 +02:00
|
|
|
def _reflect_stream(lbry_file, reflector_server):
|
|
|
|
reflector_address, reflector_port = reflector_server[0], reflector_server[1]
|
|
|
|
factory = ClientFactory(
|
|
|
|
lbry_file.blob_manager,
|
|
|
|
lbry_file.stream_info_manager,
|
2017-02-07 20:27:38 +01:00
|
|
|
lbry_file.stream_hash,
|
|
|
|
lbry_file.uri
|
2016-10-28 21:48:51 +02:00
|
|
|
)
|
|
|
|
d = reactor.resolve(reflector_address)
|
|
|
|
d.addCallback(lambda ip: reactor.connectTCP(ip, reflector_port, factory))
|
|
|
|
d.addCallback(lambda _: factory.finished_deferred)
|
2017-02-07 20:27:38 +01:00
|
|
|
d.addCallback(log_result, lbry_file.uri)
|
2016-10-28 21:48:51 +02:00
|
|
|
return d
|
|
|
|
|
|
|
|
|
2016-11-01 18:19:13 +01:00
|
|
|
def _catch_error(err, uri):
|
2017-01-25 19:10:38 +01:00
|
|
|
msg = "An error occurred while checking availability for lbry://%s: %s"
|
|
|
|
log.error(msg, uri, err.getTraceback())
|
2016-11-01 18:05:19 +01:00
|
|
|
|
|
|
|
|
2016-10-28 21:56:22 +02:00
|
|
|
def check_and_restore_availability(lbry_file, reflector_server):
|
2017-01-25 19:03:48 +01:00
|
|
|
d = _reflect_stream(lbry_file, reflector_server)
|
2017-02-06 19:18:41 +01:00
|
|
|
d.addErrback(lambda err: err.trap(ConnectionDone, ConnectionLost))
|
2017-01-25 19:03:48 +01:00
|
|
|
d.addErrback(_catch_error, lbry_file.uri)
|
2016-10-28 21:48:51 +02:00
|
|
|
return d
|