Skip to content

Commit

Permalink
dnet: render inbound connections on disconnect/reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
lunar-mining committed Oct 23, 2023
1 parent 3e5ddf5 commit a0777bd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 49 deletions.
10 changes: 2 additions & 8 deletions bin/dnet/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,23 @@ 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

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()

while True:
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()
Expand Down
12 changes: 2 additions & 10 deletions bin/dnet/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down
85 changes: 54 additions & 31 deletions bin/dnet/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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 = [
Expand All @@ -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)
Expand Down

0 comments on commit a0777bd

Please sign in to comment.