From a0777bda57b1088114ea332533552d3688cfdb75 Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Mon, 23 Oct 2023 12:58:39 +0200 Subject: [PATCH] dnet: render inbound connections on disconnect/reconnect --- bin/dnet/main.py | 10 ++---- bin/dnet/model.py | 12 ++----- bin/dnet/view.py | 85 ++++++++++++++++++++++++++++++----------------- 3 files changed, 58 insertions(+), 49 deletions(-) diff --git a/bin/dnet/main.py b/bin/dnet/main.py index 78b8e17dde3a..135ca869b7d7 100644 --- a/bin/dnet/main.py +++ b/bin/dnet/main.py @@ -39,7 +39,6 @@ async def subscribe(self, rpc, name, host, port): await rpc.start(host, port) logging.debug(f"Started {name} RPC on port {port}") break - # TODO: offline node handling except Exception as e: logging.debug(f"failed to connect {host}:{port} {e}") pass @@ -47,11 +46,7 @@ async def subscribe(self, rpc, name, host, port): data = await rpc._make_request("p2p.get_info", []) info[name] = data - try: - self.queue.put_nowait(info) - except: - logging.debug("subscribe().put_nowait(): QueueFull") - + await self.queue.put(info) await rpc.dnet_switch(True) await rpc.dnet_subscribe_events() @@ -59,9 +54,8 @@ async def subscribe(self, rpc, name, host, port): await asyncio.sleep(0.01) data = await rpc.reader.readline() data = json.loads(data) - info[name] = data - self.queue.put_nowait(info) + await self.queue.put(info) await rpc.dnet_switch(False) await rpc.stop() diff --git a/bin/dnet/model.py b/bin/dnet/model.py index 8919a557c3c5..a3a50846c110 100644 --- a/bin/dnet/model.py +++ b/bin/dnet/model.py @@ -112,43 +112,36 @@ def handle_event(self, event): addr = info["addr"] id = info.get("channel_id") self.info.update_inbound(f"{id}", addr) - logging.debug(f"{current_time} inbound (connect): {addr}") case "inbound_disconnected": addr = info["addr"] id = info.get("channel_id") - self.info.remove_inbound(id) - + self.info.remove_inbound(f"{id}") logging.debug(f"{current_time} inbound (disconnect): {addr}") case "outbound_slot_sleeping": slot = info["slot"] self.info.update_event((f"{name}", f"{slot}"), "sleeping") - logging.debug(f"{current_time} slot {slot}: sleeping") case "outbound_slot_connecting": slot = info["slot"] addr = info["addr"] self.info.update_event((f"{name}", f"{slot}"), f"connecting: addr={addr}") - logging.debug(f"{current_time} slot {slot}: connecting addr={addr}") case "outbound_slot_connected": slot = info["slot"] addr = info["addr"] channel_id = info["channel_id"] self.info.update_event((f"{name}", f"{slot}"), f"connected: addr={addr}") - logging.debug(f"{current_time} slot {slot}: connected addr={addr}") case "outbound_slot_disconnected": slot = info["slot"] err = info["err"] self.info.update_event((f"{name}", f"{slot}"), f"disconnected: {err}") - logging.debug(f"{current_time} slot {slot}: disconnected err='{err}'") case "outbound_peer_discovery": attempt = info["attempt"] state = info["state"] self.info.update_event((f"{name}", "outbound"), f"peer discovery: {state} (attempt {attempt})") - logging.debug(f"{current_time} peer_discovery: {state} (attempt {attempt})") def __repr__(self): @@ -172,8 +165,7 @@ def update_inbound(self, key, value): self.inbound[key] = value def remove_inbound(self, key): - if key in self.inbound: - del self.inbound[key] + del self.inbound[key] def update_manual(self, key, value): self.manual[key] = value diff --git a/bin/dnet/view.py b/bin/dnet/view.py index 7d459d126201..fb27ff45988a 100644 --- a/bin/dnet/view.py +++ b/bin/dnet/view.py @@ -55,6 +55,7 @@ def focus_previous(self): class NodeView(urwid.WidgetWrap): def __init__(self, info): + self.type = "node" self.name = info self.text = urwid.Text(f"{self.name}") super().__init__(self.text) @@ -78,9 +79,13 @@ def get_widget(self): def get_name(self): return self.name + def get_type(self): + return self.type + class ConnectView(urwid.WidgetWrap): def __init__(self, node, kind): + self.type = f"{kind}-connect" self.name = (f"{node}", f"{kind}") self.text = urwid.Text(f" {kind}") super().__init__(self.text) @@ -102,15 +107,18 @@ def get_widget(self): def get_name(self): return self.name + def get_type(self): + return self.type + class SlotView(urwid.WidgetWrap): - def __init__(self, node, num, info): - self.num = num - self.name = (f"{node}", f"{num}") - #self.name = info[0] + def __init__(self, node, type, id, info): + self.id = id + self.type = type + self.name = (f"{node}", f"{id}") self.addr = info - if len(num) == 1: - self.text = urwid.Text(f" {num}: {self.addr}") + if len(id) == 1: + self.text = urwid.Text(f" {id}: {self.addr}") else: self.text = urwid.Text(f" {self.addr}") super().__init__(self.text) @@ -135,6 +143,9 @@ def get_name(self): def get_addr(self): return self.addr + def get_type(self): + return self.type + class View(): palette = [ @@ -157,62 +168,74 @@ def __init__(self, model): async def update_view(self): - online = [] + known_nodes = [] + known_inbound = [] while True: - await asyncio.sleep(0.1) + await asyncio.sleep(0.01) for index, item in enumerate(self.listwalker.contents): - online.append(item.get_name()) + known_nodes.append(item.get_name()) + # Render get_info() for node, values in self.model.nodes.items(): - if node in online: + if node in known_nodes: continue else: widget = NodeView(node) self.listwalker.contents.append(widget) - outbounds = values.outbound inbound = values.inbound manual = values.manual seed = values.seed - if len(outbounds) != 0: widget = ConnectView(node, "outbound") self.listwalker.contents.append(widget) for i, info in outbounds.items(): - widget = SlotView(node, i, info) + widget = SlotView(node, "outbound", i, info) self.listwalker.contents.append(widget) - if len(inbound) != 0: widget = ConnectView(node, "inbound") self.listwalker.contents.append(widget) for i, info in inbound.items(): - widget = SlotView(node, i, info) + widget = SlotView(node, "inbound", i, info) self.listwalker.contents.append(widget) - #logging.debug(len(self.listwalker.contents)) if len(seed) != 0: widget = ConnectView(node, "seed") self.listwalker.contents.append(widget) - if len(manual) != 0: widget = ConnectView(node, "manual") self.listwalker.contents.append(widget) + # Update outbound slot info + for index, item in enumerate(self.listwalker.contents): + if item.get_type() == "outbound": + name = item.get_name() + if name in self.model.info.event.keys(): + value = self.model.info.event.get(name) + widget = SlotView(node, "outbound", name[1], value) + self.listwalker.contents[index] = widget + + # Update new inbound connections for index, item in enumerate(self.listwalker.contents): - name = item.get_name() - if name in self.model.info.event.keys(): - postfix = name[1] - match postfix: - case "outbound": - # Outhound event info (displayed in render_info()) - continue - case "inbound": - continue - case _: - # Slot event info - value = self.model.info.event.get(name) - widget = SlotView(node, postfix, value) - self.listwalker.contents[index] = widget + if item.get_type() == "inbound": + name = item.get_name() + known_inbound.append(name[1]) + for id, addr in self.model.info.inbound.items(): + if id in known_inbound: + continue + else: + widget = SlotView(node, "inbound", id, addr) + self.listwalker.contents.append(widget) + # Remove disconnected inbounds + for id in known_inbound: + if id in self.model.info.inbound.keys(): + continue + for index, item in enumerate(self.listwalker.contents): + name = item.get_name() + if name[1] == id: + del self.listwalker.contents[index] + + # Render subscribe_events() (right menu) async def render_info(self): while True: await asyncio.sleep(0.1)