view.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. session = Session(node_name, "inbound")
  88. session.set_txt()
  89. self.listwalker.contents.append(session)
  90. for i, addr in info['inbound'].items():
  91. slot = Slot(node_name, "inbound-slot")
  92. slot.set_txt(i, addr)
  93. self.listwalker.contents.append(slot)
  94. if info['manual']:
  95. session = Session(node_name, "manual")
  96. session.set_txt()
  97. self.listwalker.contents.append(session)
  98. for i, addr in info['manual'].items():
  99. slot = Slot(node_name, "manual-slot")
  100. slot.set_txt(i, addr)
  101. self.listwalker.contents.append(slot)
  102. if info['seed']:
  103. session = Session(node_name, "seed")
  104. session.set_txt()
  105. self.listwalker.contents.append(session)
  106. for i, info in info['seed'].items():
  107. slot = Slot(node_name, "seed-slot")
  108. slot.set_txt(i, addr)
  109. self.listwalker.contents.append(slot)
  110. def draw_empty(self, node_name, info):
  111. name = node_name + " (offline)"
  112. node = Node(name, "node")
  113. node.set_txt()
  114. self.listwalker.contents.append(node)
  115. #-----------------------------------------------------------------
  116. # Render subscribe_events() (left menu)
  117. #-----------------------------------------------------------------
  118. def fill_left_box(self):
  119. known_inbound = []
  120. new_inbound= {}
  121. for index, item in enumerate(self.listwalker.contents):
  122. # Update outbound slot info
  123. if item.session == "outbound-slot":
  124. key = (f"{item.node_name}", f"{item.i}")
  125. if key in self.model.nodes[item.node_name]['event']:
  126. info = self.model.nodes[item.node_name]['event'].get(key)
  127. slot = Slot(item.node_name, item.session)
  128. slot.set_txt(item.i, info)
  129. self.listwalker.contents[index] = slot
  130. #-----------------------------------------------------------------
  131. # Render subscribe_events() (right menu)
  132. #-----------------------------------------------------------------
  133. def fill_right_box(self):
  134. self.pile.contents.clear()
  135. focus_w = self.list.get_focus()
  136. if focus_w[0] is None:
  137. return
  138. session = focus_w[0].session
  139. if session == "outbound":
  140. key = (focus_w[0].node_name, "outbound")
  141. info = self.model.nodes.get(focus_w[0].node_name)
  142. if key in info['event']:
  143. ev = info['event'].get(key)
  144. self.pile.contents.append((
  145. urwid.Text(f" {ev}"),
  146. self.pile.options()))
  147. if (session == "outbound-slot" or session == "inbound-slot"
  148. or session == "manual-slot" or session == "seed-slot"):
  149. addr = focus_w[0].addr
  150. node_name = focus_w[0].node_name
  151. info = self.model.nodes.get(node_name)
  152. if addr in info['msgs']:
  153. msg = info['msgs'].get(addr)
  154. for m in msg:
  155. time = m[0]
  156. event = m[1]
  157. msg = m[2]
  158. self.pile.contents.append((urwid.Text(
  159. f"{time}: {event}: {msg}"),
  160. self.pile.options()))
  161. async def update_view(self, evloop: asyncio.AbstractEventLoop,
  162. loop: urwid.MainLoop):
  163. known_nodes = []
  164. empty_nodes = []
  165. while True:
  166. await asyncio.sleep(0.1)
  167. # Redraw the screen
  168. evloop.call_soon(loop.draw_screen)
  169. for index, item in enumerate(self.listwalker.contents):
  170. known_nodes.append(item.node_name)
  171. # Draw get_info() -> called once
  172. for node_name, info in self.model.nodes.items():
  173. if node_name in known_nodes:
  174. continue
  175. else:
  176. self.draw_info(node_name, info)
  177. # TODO:
  178. # There are a few events that should trigger a redraw:
  179. # * a new inbound connection comes online
  180. # * a inbound connection has gone offline
  181. # * a new node comes online (FIXME)
  182. # * when RPC can't connect, display the node as offline.
  183. # Check for offline nodes
  184. for node_name, info in self.model.nodes.items():
  185. if not bool(info):
  186. if node_name in empty_nodes:
  187. continue
  188. else:
  189. empty_nodes.append(node_name)
  190. self.listwalker.contents.clear()
  191. self.draw_empty(node_name, info)
  192. for name, info in self.model.nodes.items():
  193. if name not in empty_nodes:
  194. self.draw_info(name, info)
  195. # Only render info if the node is online
  196. self.fill_left_box()
  197. self.fill_right_box()