lbry-sdk/scripts/dht_monitor.py

73 lines
1.6 KiB
Python
Raw Normal View History

2017-10-10 19:31:07 +02:00
import curses
import time
import logging
2019-01-22 23:45:13 +01:00
import asyncio
from lbrynet import conf
from lbrynet.extras.daemon.client import LBRYAPIClient
2017-10-10 19:31:07 +02:00
log = logging.getLogger(__name__)
log.addHandler(logging.FileHandler("dht contacts.log"))
# log.addHandler(logging.StreamHandler())
log.setLevel(logging.INFO)
stdscr = curses.initscr()
def init_curses():
curses.noecho()
curses.cbreak()
stdscr.nodelay(1)
stdscr.keypad(1)
def teardown_curses():
curses.nocbreak()
stdscr.keypad(0)
curses.echo()
curses.endwin()
2019-01-22 23:45:13 +01:00
def refresh(routing_table_info):
2017-10-10 19:31:07 +02:00
height, width = stdscr.getmaxyx()
2019-01-22 23:45:13 +01:00
node_id = routing_table_info['node_id']
2017-10-10 19:31:07 +02:00
for y in range(height):
stdscr.addstr(y, 0, " " * (width - 1))
buckets = routing_table_info['buckets']
2019-01-22 23:45:13 +01:00
stdscr.addstr(0, 0, f"node id: {node_id}")
stdscr.addstr(1, 0, f"{len(buckets)} buckets")
2017-10-10 19:31:07 +02:00
y = 3
2019-01-22 23:45:13 +01:00
for i in range(len(buckets)):
2017-10-10 19:31:07 +02:00
stdscr.addstr(y, 0, "bucket %s" % i)
y += 1
2019-01-22 23:45:13 +01:00
for peer in buckets[str(i)]:
stdscr.addstr(y, 0, f"{peer['node_id'][:8]} ({peer['address']}:{peer['udp_port']})")
2017-10-10 19:31:07 +02:00
y += 1
y += 1
stdscr.addstr(y + 1, 0, str(time.time()))
stdscr.refresh()
2019-01-22 23:45:13 +01:00
async def main():
conf.initialize_settings()
api = await LBRYAPIClient.get_client()
2017-10-10 19:31:07 +02:00
try:
init_curses()
2019-01-22 23:45:13 +01:00
c = None
while c not in [ord('q'), ord('Q')]:
routing_info = await api.routing_table_get()
refresh(routing_info)
c = stdscr.getch()
time.sleep(0.1)
2017-10-10 19:31:07 +02:00
finally:
2019-01-22 23:45:13 +01:00
await api.session.close()
2017-10-10 19:31:07 +02:00
teardown_curses()
if __name__ == "__main__":
2019-01-22 23:45:13 +01:00
asyncio.run(main())