2017-02-10 16:56:22 +01:00
|
|
|
import random
|
2016-10-28 21:48:51 +02:00
|
|
|
|
2017-02-10 16:56:22 +01:00
|
|
|
from twisted.internet import reactor, defer
|
|
|
|
from lbrynet import conf
|
|
|
|
from lbrynet.reflector import ClientFactory, BlobClientFactory
|
2016-10-28 21:48:51 +02:00
|
|
|
|
|
|
|
|
2017-08-04 04:06:12 +02:00
|
|
|
def _is_ip(host):
|
|
|
|
try:
|
|
|
|
if len(host.split(".")) == 4 and all([0 <= int(x) <= 255 for x in host.split(".")]):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
except ValueError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def resolve(host):
|
|
|
|
if _is_ip(host):
|
|
|
|
ip = host
|
|
|
|
else:
|
|
|
|
ip = yield reactor.resolve(host)
|
|
|
|
defer.returnValue(ip)
|
|
|
|
|
|
|
|
|
2017-02-10 16:56:22 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _reflect_stream(lbry_file, reflector_server):
|
2016-10-28 21:48:51 +02:00
|
|
|
reflector_address, reflector_port = reflector_server[0], reflector_server[1]
|
2017-02-10 16:56:22 +01:00
|
|
|
factory = ClientFactory(lbry_file)
|
2017-08-04 04:06:12 +02:00
|
|
|
ip = yield resolve(reflector_address)
|
2017-02-10 16:56:22 +01:00
|
|
|
yield reactor.connectTCP(ip, reflector_port, factory)
|
|
|
|
yield factory.finished_deferred
|
2016-10-28 21:48:51 +02:00
|
|
|
|
|
|
|
|
2017-02-10 16:56:22 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _reflect_blobs(blob_manager, blob_hashes, reflector_server):
|
2016-10-28 21:48:51 +02:00
|
|
|
reflector_address, reflector_port = reflector_server[0], reflector_server[1]
|
2017-02-10 16:56:22 +01:00
|
|
|
factory = BlobClientFactory(blob_manager, blob_hashes)
|
2017-08-04 04:06:12 +02:00
|
|
|
ip = yield resolve(reflector_address)
|
2017-02-10 16:56:22 +01:00
|
|
|
yield reactor.connectTCP(ip, reflector_port, factory)
|
|
|
|
yield factory.finished_deferred
|
|
|
|
|
|
|
|
|
|
|
|
def reflect_stream(lbry_file):
|
|
|
|
reflector_server = random.choice(conf.settings['reflector_servers'])
|
|
|
|
return _reflect_stream(lbry_file, reflector_server)
|
|
|
|
|
|
|
|
|
|
|
|
def reflect_blob_hashes(blob_hashes, blob_manager):
|
|
|
|
reflector_server = random.choice(conf.settings['reflector_servers'])
|
|
|
|
return _reflect_blobs(blob_manager, blob_hashes, reflector_server)
|