lbry-sdk/scripts/create_network.py

93 lines
2.7 KiB
Python
Raw Normal View History

2015-08-20 17:27:15 +02:00
#!/usr/bin/env python
#
# This library is free software, distributed under the terms of
# the GNU Lesser General Public License Version 3, or any later version.
# See the COPYING file included in this archive
#
# Thanks to Paul Cannon for IP-address resolution functions (taken from aspn.activestate.com)
import argparse
2017-03-31 20:23:35 +02:00
import os
import sys
import time
import signal
2015-08-20 17:27:15 +02:00
amount = 0
def destroyNetwork(nodes):
2017-01-04 23:10:36 +01:00
print 'Destroying Kademlia network'
2015-08-20 17:27:15 +02:00
i = 0
for node in nodes:
i += 1
2017-03-31 20:23:35 +02:00
hashAmount = i * 50 / amount
hashbar = '#' * hashAmount
2015-08-20 17:27:15 +02:00
output = '\r[%-50s] %d/%d' % (hashbar, i, amount)
sys.stdout.write(output)
time.sleep(0.15)
os.kill(node, signal.SIGTERM)
print
def main():
parser = argparse.ArgumentParser(description="Launch a network of dht nodes")
parser.add_argument("amount_of_nodes",
help="The number of nodes to create",
type=int)
2016-11-30 21:20:45 +01:00
parser.add_argument(
"--nic_ip_address",
help=("The network interface on which these nodes will listen for connections "
"from each other and from other nodes. If omitted, an attempt will be "
"made to automatically determine the system's IP address, but this may "
"result in the nodes being reachable only from this system"))
2015-08-20 17:27:15 +02:00
args = parser.parse_args()
global amount
amount = args.amount_of_nodes
if args.nic_ip_address:
ipAddress = args.nic_ip_address
else:
import socket
ipAddress = socket.gethostbyname(socket.gethostname())
2017-01-04 23:10:36 +01:00
print 'Network interface IP address omitted; using %s' % ipAddress
2015-08-20 17:27:15 +02:00
startPort = 4000
2017-03-31 20:23:35 +02:00
port = startPort + 1
2015-08-20 17:27:15 +02:00
nodes = []
2017-01-04 23:10:36 +01:00
print 'Creating Kademlia network'
2015-08-20 17:27:15 +02:00
try:
2016-11-30 21:20:45 +01:00
node = os.spawnlp(
os.P_NOWAIT, 'lbrynet-launch-node', 'lbrynet-launch-node', str(startPort))
nodes.append(node)
2017-03-31 20:23:35 +02:00
for i in range(amount - 1):
2015-08-20 17:27:15 +02:00
time.sleep(0.15)
2017-03-31 20:23:35 +02:00
hashAmount = i * 50 / amount
hashbar = '#' * hashAmount
2015-08-20 17:27:15 +02:00
output = '\r[%-50s] %d/%d' % (hashbar, i, amount)
sys.stdout.write(output)
2016-11-30 21:20:45 +01:00
node = os.spawnlp(
os.P_NOWAIT, 'lbrynet-launch-node', 'lbrynet-launch-node', str(port),
ipAddress, str(startPort))
nodes.append(node)
2015-08-20 17:27:15 +02:00
port += 1
except KeyboardInterrupt:
'\nNetwork creation cancelled.'
destroyNetwork(nodes)
sys.exit(1)
print '\n\n---------------\nNetwork running\n---------------\n'
try:
while 1:
time.sleep(1)
except KeyboardInterrupt:
pass
finally:
destroyNetwork(nodes)
2017-03-31 20:23:35 +02:00
2015-08-20 17:27:15 +02:00
if __name__ == '__main__':
2016-11-30 21:20:45 +01:00
main()