From 37db5a068c698628412bb3994ba350e437b1dd6f Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Wed, 18 Oct 2023 12:14:35 +0200 Subject: [PATCH] dnet: generalize rpc to work with remote hosts as well as localhost --- bin/dnet/config.toml | 7 +++++++ bin/dnet/main.py | 12 +++++++----- bin/dnet/model.py | 1 + bin/dnet/rpc.py | 5 +++-- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/bin/dnet/config.toml b/bin/dnet/config.toml index 0b159dfa7ef0..21780aaa4a87 100644 --- a/bin/dnet/config.toml +++ b/bin/dnet/config.toml @@ -1,8 +1,15 @@ [[nodes]] name = "darkirc" +host = "localhost" port = 26660 [[nodes]] name = "taud" +host = "localhost" port = 23330 +[[nodes]] +name = "agorism" +host = "agorism.dev" +port = 26660 + diff --git a/bin/dnet/main.py b/bin/dnet/main.py index 62038abc1c1d..5abb6e0da350 100644 --- a/bin/dnet/main.py +++ b/bin/dnet/main.py @@ -32,15 +32,16 @@ def __init__(self): self.model = Model() self.view = View(self.model) - async def subscribe(self, rpc, name, port): + async def subscribe(self, rpc, name, host, port): info = {} while True: try: - #logging.debug(f"Start {name} RPC on port {port}") - await rpc.start("localhost", port) + await rpc.start(host, port) + logging.debug(f"Started {name} RPC on port {port}") break # TODO: offline node handling - except OSError: + except Exception as e: + logging.debug(f"failed to connect {host}:{port} {e}") pass data = await rpc._make_request("p2p.get_info", []) @@ -79,7 +80,8 @@ async def start_connect_slots(self, nodes): for i, node in enumerate(nodes): rpc = JsonRpc() subscribe = tg.create_task(self.subscribe( - rpc, node['name'], node['port'])) + rpc, node['name'], node['host'], + node['port'])) nodes = tg.create_task(self.update_info()) async def update_info(self): diff --git a/bin/dnet/model.py b/bin/dnet/model.py index 84cefd8c3b64..f1817fc5e717 100644 --- a/bin/dnet/model.py +++ b/bin/dnet/model.py @@ -37,6 +37,7 @@ def update_node(self, key, value): self.nodes[key] = value def handle_nodes(self, node): + logging.debug(node) channel_lookup = {} name = list(node.keys())[0] values = list(node.values())[0] diff --git a/bin/dnet/rpc.py b/bin/dnet/rpc.py index 6cc63c6fec75..7c2cfc98bde8 100644 --- a/bin/dnet/rpc.py +++ b/bin/dnet/rpc.py @@ -24,8 +24,9 @@ class JsonRpc: - async def start(self, server, port): - reader, writer = await asyncio.open_connection(server, port) + async def start(self, host, port): + logging.info(f"trying to connect to {host}:{port}") + reader, writer = await asyncio.open_connection(host, port) self.reader = reader self.writer = writer