mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-01-09 14:48:08 -05:00
script/dnet: improve model in preparation for events handling
This commit is contained in:
@@ -57,7 +57,7 @@ class Dnetview:
|
||||
data = await rpc.reader.readline()
|
||||
data = json.loads(data)
|
||||
info[name] = data
|
||||
#logging.debug(f"events: {data}")
|
||||
#logging.debug(f"{info}")
|
||||
|
||||
try:
|
||||
self.queue.put_nowait(info)
|
||||
@@ -88,11 +88,13 @@ class Dnetview:
|
||||
|
||||
# Update node info
|
||||
if "result" in values:
|
||||
self.model.update(info)
|
||||
self.model.handle_nodes(info)
|
||||
|
||||
# Update event info: TODO
|
||||
if "params" in values:
|
||||
logging.debug("update_info(): Event detected")
|
||||
continue
|
||||
# TODO
|
||||
#self.model.handle_event(info)
|
||||
|
||||
self.queue.task_done()
|
||||
except self.queue.is_empty():
|
||||
@@ -110,7 +112,6 @@ class Dnetview:
|
||||
unhandled_input=self.unhandled_input,
|
||||
event_loop=urwid.AsyncioEventLoop(loop=self.ev))
|
||||
|
||||
#loop.set_alarm_in(2, self.view.update_view)
|
||||
loop.run()
|
||||
|
||||
def unhandled_input(self, key):
|
||||
|
||||
@@ -19,12 +19,89 @@ import logging
|
||||
|
||||
class Model:
|
||||
def __init__(self):
|
||||
self.info = Info()
|
||||
self.nodes = {}
|
||||
|
||||
def update(self, node):
|
||||
def update_node(self, key, value):
|
||||
self.nodes[key] = value
|
||||
|
||||
def handle_nodes(self, node):
|
||||
channel_lookup = {}
|
||||
|
||||
name = list(node.keys())[0]
|
||||
values = list(node.values())[0]
|
||||
self.nodes[name] = values
|
||||
|
||||
info = values["result"]
|
||||
channels = info["channels"]
|
||||
|
||||
for channel in channels:
|
||||
id = channel["id"]
|
||||
channel_lookup[id] = channel
|
||||
|
||||
for channel in channels:
|
||||
if channel["session"] != "inbound":
|
||||
continue
|
||||
url = channel["url"]
|
||||
self.info.update_inbound("inbound", url)
|
||||
|
||||
for i, id in enumerate(info["outbound_slots"]):
|
||||
if id == 0:
|
||||
self.info.update_outbound(f"{i}", "none")
|
||||
continue
|
||||
|
||||
assert id in channel_lookup
|
||||
url = channel_lookup[id]["url"]
|
||||
self.info.update_outbound(f"{i}", url)
|
||||
|
||||
for channel in channels:
|
||||
if channel["session"] != "seed":
|
||||
continue
|
||||
url = channel["url"]
|
||||
self.info.update_seed("seed", url)
|
||||
|
||||
for channel in channels:
|
||||
if channel["session"] != "manual":
|
||||
continue
|
||||
url = channel["url"]
|
||||
self.info.update_manual("manual", url)
|
||||
|
||||
self.update_node(name, self.info)
|
||||
|
||||
def handle_event(self, event):
|
||||
name = list(event.keys())[0]
|
||||
params = list(event.values())[0]
|
||||
|
||||
def __repr__(self):
|
||||
return f"{self.nodes}"
|
||||
|
||||
class Info:
|
||||
def __init__(self):
|
||||
self.outbounds = {}
|
||||
self.inbound = {}
|
||||
self.manual = {}
|
||||
self.seed = {}
|
||||
self.msgs = {}
|
||||
|
||||
def update_outbound(self, key, value):
|
||||
self.outbounds[key] = value
|
||||
|
||||
def update_inbound(self, key, value):
|
||||
self.inbound[key] = value
|
||||
|
||||
def update_manual(self, key, value):
|
||||
self.manual[key] = value
|
||||
|
||||
def update_seed(self, key, value):
|
||||
self.seed[key] = value
|
||||
|
||||
def update_msg(self, msg):
|
||||
self.msgs = msg
|
||||
|
||||
def __repr__(self):
|
||||
return (
|
||||
f"outbound: {self.outbounds}"
|
||||
f"inbound: {self.inbound}"
|
||||
f"manual: {self.manual}"
|
||||
f"seed: {self.seed}"
|
||||
f"msg: {self.msgs}"
|
||||
)
|
||||
|
||||
@@ -147,54 +147,28 @@ class View():
|
||||
widget = NodeView(name)
|
||||
self.listwalker.contents.append(widget)
|
||||
|
||||
info = values["result"]
|
||||
channels = info["channels"]
|
||||
channel_lookup = {}
|
||||
for channel in channels:
|
||||
id = channel["id"]
|
||||
channel_lookup[id] = channel
|
||||
outbounds = values.outbounds
|
||||
inbound = values.inbound
|
||||
manual = values.manual
|
||||
seed = values.seed
|
||||
|
||||
for channel in channels:
|
||||
if channel["session"] != "inbound":
|
||||
continue
|
||||
widget = ConnectView("inbound")
|
||||
if len(outbounds) != 0:
|
||||
widget = ConnectView(" outbound")
|
||||
self.listwalker.contents.append(widget)
|
||||
|
||||
url = channel["url"]
|
||||
widget = SlotView(f" {url}")
|
||||
self.listwalker.contents.append(widget)
|
||||
|
||||
widget = ConnectView(" outbound")
|
||||
self.listwalker.contents.append(widget)
|
||||
for i, id in enumerate(info["outbound_slots"]):
|
||||
if id == 0:
|
||||
widget = SlotView(f" {i}: none")
|
||||
for num, name in outbounds.items():
|
||||
widget = SlotView(f" {num}: {name}")
|
||||
self.listwalker.contents.append(widget)
|
||||
continue
|
||||
|
||||
assert id in channel_lookup
|
||||
url = channel_lookup[id]["url"]
|
||||
widget = SlotView(f" {i}: {url}")
|
||||
if len(inbound) != 0:
|
||||
widget = ConnectView(" inbound")
|
||||
self.listwalker.contents.append(widget)
|
||||
|
||||
for channel in channels:
|
||||
if channel["session"] != "seed":
|
||||
continue
|
||||
widget = ConnectView("seed")
|
||||
if len(seed) != 0:
|
||||
widget = ConnectView(" seed")
|
||||
self.listwalker.contents.append(widget)
|
||||
|
||||
url = channel["url"]
|
||||
widget = SlotView(f" {url}")
|
||||
self.listwalker.contents.append(widget)
|
||||
|
||||
for channel in channels:
|
||||
if channel["session"] != "manual":
|
||||
continue
|
||||
widget = ConnectView("manual")
|
||||
self.listwalker.contents.append(widget)
|
||||
|
||||
url = channel["url"]
|
||||
widget = SlotView(f" {url}")
|
||||
if len(manual) != 0:
|
||||
widget = ConnectView(" manual")
|
||||
self.listwalker.contents.append(widget)
|
||||
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
Reference in New Issue
Block a user