forked from LBRYCommunity/lbry-sdk
100 lines
2.4 KiB
Python
100 lines
2.4 KiB
Python
|
from lbrynet.dht.node import Node
|
||
|
import binascii
|
||
|
from twisted.internet import reactor, task
|
||
|
import logging
|
||
|
import sys
|
||
|
from lbrynet.core.utils import generate_id
|
||
|
|
||
|
|
||
|
def print_usage():
|
||
|
print "Usage:\n%s UDP_PORT KNOWN_NODE_IP KNOWN_NODE_PORT HASH"
|
||
|
|
||
|
|
||
|
def join_network(udp_port, known_nodes):
|
||
|
lbryid = generate_id()
|
||
|
|
||
|
logging.info('Creating Node...')
|
||
|
node = Node(udpPort=udp_port, lbryid=lbryid)
|
||
|
|
||
|
logging.info('Joining network...')
|
||
|
d = node.joinNetwork(known_nodes)
|
||
|
|
||
|
def log_network_size():
|
||
|
logging.info("Approximate number of nodes in DHT: %s", str(node.getApproximateTotalDHTNodes()))
|
||
|
logging.info("Approximate number of blobs in DHT: %s", str(node.getApproximateTotalHashes()))
|
||
|
|
||
|
d.addCallback(lambda _: log_network_size())
|
||
|
|
||
|
d.addCallback(lambda _: node)
|
||
|
|
||
|
return d
|
||
|
|
||
|
|
||
|
def get_hosts(node, h):
|
||
|
|
||
|
def print_hosts(hosts):
|
||
|
print "Hosts returned from the DHT: "
|
||
|
print hosts
|
||
|
|
||
|
logging.info("Looking up %s", h)
|
||
|
d = node.getPeersForBlob(h)
|
||
|
d.addCallback(print_hosts)
|
||
|
return d
|
||
|
|
||
|
|
||
|
def announce_hash(node, h):
|
||
|
d = node.announceHaveBlob(h, 34567)
|
||
|
|
||
|
def log_results(results):
|
||
|
for success, result in results:
|
||
|
if success:
|
||
|
logging.info("Succeeded: %s", str(result))
|
||
|
else:
|
||
|
logging.info("Failed: %s", str(result.getErrorMessage()))
|
||
|
|
||
|
d.addCallback(log_results)
|
||
|
return d
|
||
|
|
||
|
|
||
|
def get_args():
|
||
|
if len(sys.argv) < 5:
|
||
|
print_usage()
|
||
|
sys.exit(1)
|
||
|
udp_port = int(sys.argv[1])
|
||
|
known_nodes = [(sys.argv[2], int(sys.argv[3]))]
|
||
|
h = binascii.unhexlify(sys.argv[4])
|
||
|
return udp_port, known_nodes, h
|
||
|
|
||
|
|
||
|
def run_dht_script(dht_func):
|
||
|
log_format = "(%(asctime)s)[%(filename)s:%(lineno)s] %(funcName)s(): %(message)s"
|
||
|
logging.basicConfig(level=logging.DEBUG, format=log_format)
|
||
|
|
||
|
udp_port, known_nodes, h = get_args()
|
||
|
|
||
|
d = task.deferLater(reactor, 0, join_network, udp_port, known_nodes)
|
||
|
|
||
|
def run_dht_func(node):
|
||
|
return dht_func(node, h)
|
||
|
|
||
|
d.addCallback(run_dht_func)
|
||
|
|
||
|
def log_err(err):
|
||
|
logging.error("An error occurred: %s", err.getTraceback())
|
||
|
return err
|
||
|
|
||
|
def shut_down():
|
||
|
logging.info("Shutting down")
|
||
|
reactor.stop()
|
||
|
|
||
|
d.addErrback(log_err)
|
||
|
d.addBoth(lambda _: shut_down())
|
||
|
reactor.run()
|
||
|
|
||
|
|
||
|
def get_hosts_for_hash_in_dht():
|
||
|
run_dht_script(get_hosts)
|
||
|
|
||
|
|
||
|
def announce_hash_to_dht():
|
||
|
run_dht_script(announce_hash)
|