view.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 DnetWidget(urwid.WidgetWrap):
  24. def __init__(self, node_name, session):
  25. self.node_name = node_name
  26. self.session = session
  27. def selectable(self):
  28. return True
  29. def keypress(self, size, key):
  30. return key
  31. def update(self, txt):
  32. super().__init__(txt)
  33. self._w = urwid.AttrWrap(self._w, None)
  34. self._w.focus_attr = 'line'
  35. def is_empty(self):
  36. self.is_empty == True
  37. class Node(DnetWidget):
  38. def set_txt(self):
  39. txt = urwid.Text(f"{self.node_name}")
  40. super().update(txt)
  41. class Session(DnetWidget):
  42. def set_txt(self):
  43. txt = urwid.Text(f" {self.session}")
  44. super().update(txt)
  45. class Slot(DnetWidget):
  46. def set_txt(self, i, addr):
  47. self.i = i
  48. self.addr = addr
  49. if len(self.i) == 1:
  50. txt = urwid.Text(f" {self.i}: {self.addr}")
  51. else:
  52. txt = urwid.Text(f" {self.addr}")
  53. super().update(txt)
  54. class View():
  55. palette = [
  56. ('body','light gray','default', 'standout'),
  57. ('line','dark cyan','default','standout'),
  58. ]
  59. def __init__(self, model):
  60. self.model = model
  61. info_text = urwid.Text("")
  62. self.pile = urwid.Pile([info_text])
  63. scroll = ScrollBar(Scrollable(self.pile))
  64. rightbox = urwid.LineBox(scroll)
  65. self.listbox_content = []
  66. self.listwalker = urwid.SimpleListWalker(self.listbox_content)
  67. self.list = urwid.ListBox(self.listwalker)
  68. leftbox = urwid.LineBox(self.list)
  69. columns = urwid.Columns([leftbox, rightbox], focus_column=0)
  70. self.ui = urwid.Frame(urwid.AttrWrap( columns, 'body' ))
  71. #-----------------------------------------------------------------
  72. # Render get_info()
  73. #-----------------------------------------------------------------
  74. def draw_info(self, node_name, info):
  75. node = Node(node_name, "node")
  76. node.set_txt()
  77. self.listwalker.contents.append(node)
  78. if info['outbound']:
  79. session = Session(node_name, "outbound")
  80. session.set_txt()
  81. self.listwalker.contents.append(session)
  82. for i, addr in info['outbound'].items():
  83. slot = Slot(node_name, "outbound-slot")
  84. slot.set_txt(i, addr)
  85. self.listwalker.contents.append(slot)
  86. if info['inbound']:
  87. if any(info['inbound'].values()):
  88. session = Session(node_name, "inbound")
  89. session.set_txt()
  90. self.listwalker.contents.append(session)
  91. for i, addr in info['inbound'].items():
  92. if bool(addr):
  93. slot = Slot(node_name, "inbound-slot")
  94. slot.set_txt(i, addr)
  95. self.listwalker.contents.append(slot)
  96. if info['manual']:
  97. session = Session(node_name, "manual")
  98. session.set_txt()
  99. self.listwalker.contents.append(session)
  100. for i, addr in info['manual'].items():
  101. slot = Slot(node_name, "manual-slot")
  102. slot.set_txt(i, addr)
  103. self.listwalker.contents.append(slot)
  104. if info['seed']:
  105. session = Session(node_name, "seed")
  106. session.set_txt()
  107. self.listwalker.contents.append(session)
  108. for i, info in info['seed'].items():
  109. slot = Slot(node_name, "seed-slot")
  110. slot.set_txt(i, addr)
  111. self.listwalker.contents.append(slot)
  112. def draw_empty(self, node_name, info):
  113. name = node_name + " (offline)"
  114. node = Node(name, "node")
  115. node.set_txt()
  116. self.listwalker.contents.append(node)
  117. #-----------------------------------------------------------------
  118. # Render subscribe_events() (left menu)
  119. #-----------------------------------------------------------------
  120. def fill_left_box(self):
  121. live_inbound = []
  122. new_inbound= {}
  123. for index, item in enumerate(self.listwalker.contents):
  124. # Update outbound slot info
  125. if item.session == "outbound-slot":
  126. key = (f"{item.node_name}", f"{item.i}")
  127. if key in self.model.nodes[item.node_name]['event']:
  128. info = self.model.nodes[item.node_name]['event'].get(key)
  129. slot = Slot(item.node_name, item.session)
  130. slot.set_txt(item.i, info)
  131. self.listwalker.contents[index] = slot
  132. #-----------------------------------------------------------------
  133. # Render subscribe_events() (right menu)
  134. #-----------------------------------------------------------------
  135. def fill_right_box(self):
  136. self.pile.contents.clear()
  137. focus_w = self.list.get_focus()
  138. if focus_w[0] is None:
  139. return
  140. session = focus_w[0].session
  141. if session == "outbound":
  142. key = (focus_w[0].node_name, "outbound")
  143. info = self.model.nodes.get(focus_w[0].node_name)
  144. if key in info['event']:
  145. ev = info['event'].get(key)
  146. self.pile.contents.append((
  147. urwid.Text(f" {ev}"),
  148. self.pile.options()))
  149. if (session == "outbound-slot" or session == "inbound-slot"
  150. or session == "manual-slot" or session == "seed-slot"):
  151. addr = focus_w[0].addr
  152. node_name = focus_w[0].node_name
  153. info = self.model.nodes.get(node_name)
  154. if addr in info['msgs']:
  155. msg = info['msgs'].get(addr)
  156. for m in msg:
  157. time = m[0]
  158. event = m[1]
  159. msg = m[2]
  160. self.pile.contents.append((urwid.Text(
  161. f"{time}: {event}: {msg}"),
  162. self.pile.options()))
  163. async def update_view(self, evloop: asyncio.AbstractEventLoop,
  164. loop: urwid.MainLoop):
  165. live_nodes = []
  166. dead_nodes = []
  167. live_inbound = []
  168. dead_inbound = []
  169. while True:
  170. await asyncio.sleep(0.1)
  171. evloop.call_soon(loop.draw_screen)
  172. for index, item in enumerate(self.listwalker.contents):
  173. live_nodes.append(item.node_name)
  174. if item.session == "inbound-slot":
  175. live_inbound.append(item.i)
  176. # Draw get_info(). Called only once.
  177. for node_name, info in self.model.nodes.items():
  178. if node_name in live_nodes:
  179. continue
  180. self.draw_info(node_name, info)
  181. # TODO: when RPC can't connect, display the node as offline.
  182. # If a node goes offline, trigger a redraw.
  183. for node_name, info in self.model.nodes.items():
  184. if not bool(info):
  185. if node_name in dead_nodes:
  186. continue
  187. dead_nodes.append(node_name)
  188. self.listwalker.contents.clear()
  189. self.draw_empty(node_name, info)
  190. for name, info in self.model.nodes.items():
  191. if name not in dead_nodes:
  192. self.draw_info(name, info)
  193. # Only render info if the node is online.
  194. self.fill_left_box()
  195. self.fill_right_box()
  196. # If a new inbound comes online, trigger a redraw.
  197. for key in info['inbound'].keys():
  198. if key not in live_inbound:
  199. self.listwalker.contents.clear()
  200. for name, info in self.model.nodes.items():
  201. self.draw_info(name, info)
  202. # If an inbound goes offline, trigger a redraw.
  203. addr = info['inbound'].get(key)
  204. if not bool(addr):
  205. if key in dead_inbound:
  206. continue
  207. dead_inbound.append(key)
  208. self.listwalker.contents.clear()
  209. for name, info in self.model.nodes.items():
  210. if name not in dead_nodes:
  211. self.draw_info(name, info)