Эх сурвалжийг харах

dnet: render inbound connections on disconnect/reconnect

lunar-mining 2 жил өмнө
parent
commit
7d2ad6f167
3 өөрчлөгдсөн 58 нэмэгдсэн , 49 устгасан
  1. 2 8
      bin/dnet/main.py
  2. 2 10
      bin/dnet/model.py
  3. 54 31
      bin/dnet/view.py

+ 2 - 8
bin/dnet/main.py

@@ -39,7 +39,6 @@ class Dnetview:
                 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 @@ class Dnetview:
         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 @@ class Dnetview:
             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()

+ 2 - 10
bin/dnet/model.py

@@ -112,43 +112,36 @@ class Model:
                 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 @@ class Info:
         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

+ 54 - 31
bin/dnet/view.py

@@ -55,6 +55,7 @@ class LeftList(urwid.ListBox):
 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 @@ class NodeView(urwid.WidgetWrap):
     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 @@ class ConnectView(urwid.WidgetWrap):
     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 @@ class SlotView(urwid.WidgetWrap):
     def get_addr(self):
         return self.addr
 
+    def get_type(self):
+        return self.type
+
 
 class View():
     palette = [
@@ -157,62 +168,74 @@ class View():
 
 
     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)