view.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. # This file is part of DarkFi (https://dark.fi)
  2. #
  3. # Copyright (C) 2020-2023 Dyne.org foundation
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import urwid
  18. import logging
  19. import asyncio
  20. import datetime as dt
  21. from scroll import ScrollBar, Scrollable
  22. from model import Model
  23. class NodeView(urwid.WidgetWrap):
  24. def __init__(self, info):
  25. self.type = "node"
  26. self.name = info
  27. self.text = urwid.Text(f"{self.name}")
  28. super().__init__(self.text)
  29. self._w = urwid.AttrWrap(self._w, None)
  30. self.update_w()
  31. def selectable(self):
  32. return True
  33. def keypress(self, size, key):
  34. return key
  35. def update_w(self):
  36. self._w.focus_attr = 'line'
  37. def get_widget(self):
  38. return "NodeView"
  39. def get_name(self):
  40. return self.name
  41. def get_type(self):
  42. return self.type
  43. class ConnectView(urwid.WidgetWrap):
  44. def __init__(self, node, kind):
  45. self.type = f"{kind}-connect"
  46. self.name = (f"{node}", f"{kind}")
  47. self.text = urwid.Text(f" {kind}")
  48. super().__init__(self.text)
  49. self._w = urwid.AttrWrap(self._w, None)
  50. self.update_w()
  51. def selectable(self):
  52. return True
  53. def keypress(self, size, key):
  54. return key
  55. def update_w(self):
  56. self._w.focus_attr = 'line'
  57. def get_widget(self):
  58. return "ConnectView"
  59. def get_name(self):
  60. return self.name
  61. def get_type(self):
  62. return self.type
  63. class SlotView(urwid.WidgetWrap):
  64. def __init__(self, node, type, id, info):
  65. self.id = id
  66. self.type = type
  67. self.name = (f"{node}", f"{id}")
  68. self.addr = info
  69. if len(id) == 1:
  70. self.text = urwid.Text(f" {id}: {self.addr}")
  71. else:
  72. self.text = urwid.Text(f" {self.addr}")
  73. super().__init__(self.text)
  74. self._w = urwid.AttrWrap(self._w, None)
  75. self.update_w()
  76. def selectable(self):
  77. return True
  78. def keypress(self, size, key):
  79. return key
  80. def update_w(self):
  81. self._w.focus_attr = 'line'
  82. def get_widget(self):
  83. return "SlotView"
  84. def get_name(self):
  85. return self.name
  86. def get_addr(self):
  87. return self.addr
  88. def get_type(self):
  89. return self.type
  90. class View():
  91. palette = [
  92. ('body','light gray','default', 'standout'),
  93. ('line','dark cyan','default','standout'),
  94. ]
  95. def __init__(self, model):
  96. self.model = model
  97. info_text = urwid.Text("")
  98. self.pile = urwid.Pile([info_text])
  99. scroll = ScrollBar(Scrollable(self.pile))
  100. rightbox = urwid.LineBox(scroll)
  101. self.listbox_content = []
  102. self.listwalker = urwid.SimpleListWalker(self.listbox_content)
  103. self.list = urwid.ListBox(self.listwalker)
  104. leftbox = urwid.LineBox(self.list)
  105. columns = urwid.Columns([leftbox, rightbox], focus_column=0)
  106. self.ui = urwid.Frame(urwid.AttrWrap( columns, 'body' ))
  107. async def update_view(self, evloop: asyncio.AbstractEventLoop,
  108. loop: urwid.MainLoop):
  109. known_nodes = []
  110. known_inbound = []
  111. while True:
  112. await asyncio.sleep(0.1)
  113. # Redraw the screen
  114. evloop.call_soon(loop.draw_screen)
  115. for index, item in enumerate(self.listwalker.contents):
  116. known_nodes.append(item.get_name())
  117. # Render get_info()
  118. for node, values in self.model.nodes.items():
  119. if node in known_nodes:
  120. continue
  121. else:
  122. widget = NodeView(node)
  123. self.listwalker.contents.append(widget)
  124. if values['outbound']:
  125. widget = ConnectView(node, "outbound")
  126. self.listwalker.contents.append(widget)
  127. for i, info in values['outbound'].items():
  128. widget = SlotView(node, "outbound", i, info)
  129. self.listwalker.contents.append(widget)
  130. if values['inbound']:
  131. widget = ConnectView(node, "inbound")
  132. self.listwalker.contents.append(widget)
  133. for i, info in values['inbound'].items():
  134. widget = SlotView(node, "inbound", i, info)
  135. self.listwalker.contents.append(widget)
  136. if values['manual']:
  137. widget = ConnectView(node, "manual")
  138. self.listwalker.contents.append(widget)
  139. for i, info in values['manual'].items():
  140. widget = SlotView(node, "manual", i, info)
  141. self.listwalker.contents.append(widget)
  142. if values['seed']:
  143. widget = ConnectView(node, "seed")
  144. self.listwalker.contents.append(widget)
  145. for i, info in values['seed'].items():
  146. widget = SlotView(node, "seed", i, info)
  147. self.listwalker.contents.append(widget)
  148. # Update outbound slot info
  149. for index, item in enumerate(self.listwalker.contents):
  150. if item.get_type() == "outbound":
  151. name = item.get_name()
  152. node = name[0]
  153. if name in self.model.nodes[node]['event']:
  154. value = self.model.nodes[node]['event'].get(name)
  155. widget = SlotView(node, "outbound", name[1], value)
  156. self.listwalker.contents[index] = widget
  157. # Update new inbound connections
  158. for index, item in enumerate(self.listwalker.contents):
  159. if item.get_type() == "inbound":
  160. name = item.get_name()
  161. if name[1] not in known_inbound:
  162. known_inbound.append(name[1])
  163. for node, value in self.model.nodes.items():
  164. for id, addr in value['inbound'].items():
  165. if id in known_inbound:
  166. continue
  167. else:
  168. widget = SlotView(node, "inbound", id, addr)
  169. self.listwalker.contents.append(widget)
  170. # Remove disconnected inbounds
  171. for inbound in known_inbound:
  172. for value in self.model.nodes.values():
  173. if inbound in value['inbound']:
  174. continue
  175. for index, item in enumerate(self.listwalker.contents):
  176. name = item.get_name()
  177. if name[1] == id:
  178. del self.listwalker.contents[index]
  179. # Render subscribe_events() (right menu)
  180. async def render_info(self, evloop: asyncio.AbstractEventLoop,
  181. loop: urwid.MainLoop):
  182. while True:
  183. await asyncio.sleep(0.01)
  184. # Redraw the screen
  185. evloop.call_soon(loop.draw_screen)
  186. self.pile.contents.clear()
  187. focus_w = self.list.get_focus()
  188. if focus_w[0] is None:
  189. continue
  190. else:
  191. match focus_w[0].get_widget():
  192. case "NodeView":
  193. # TODO: We will display additional node info here.
  194. self.pile.contents.append((
  195. urwid.Text(f""),
  196. self.pile.options()))
  197. case "ConnectView":
  198. name = focus_w[0].get_name()
  199. info = self.model.nodes.get(name[0])
  200. if name in info['event']:
  201. ev = info['event'].get(name)
  202. self.pile.contents.append((
  203. urwid.Text(f" {ev}"),
  204. self.pile.options()))
  205. case "SlotView":
  206. addr = focus_w[0].get_addr()
  207. name = focus_w[0].get_name()
  208. info = self.model.nodes.get(name[0])
  209. if addr in info['msgs']:
  210. msg = info['msgs'].get(addr)
  211. for m in msg:
  212. time = m[0]
  213. event = m[1]
  214. msg = m[2]
  215. self.pile.contents.append((urwid.Text(
  216. f"{time}: {event}: {msg}"),
  217. self.pile.options()))