view.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. #-----------------------------------------------------------------
  111. # Render subscribe_events() (left menu)
  112. #-----------------------------------------------------------------
  113. def fill_left_box(self):
  114. known_inbound = []
  115. new_inbound= {}
  116. for index, item in enumerate(self.listwalker.contents):
  117. # Update outbound slot info
  118. if item.session == "outbound-slot":
  119. key = (f"{item.node_name}", f"{item.i}")
  120. if key in self.model.nodes[item.node_name]['event']:
  121. info = self.model.nodes[item.node_name]['event'].get(key)
  122. slot = Slot(item.node_name, item.session)
  123. slot.set_txt(item.i, info)
  124. self.listwalker.contents[index] = slot
  125. #-----------------------------------------------------------------
  126. # Render subscribe_events() (right menu)
  127. #-----------------------------------------------------------------
  128. def fill_right_box(self):
  129. self.pile.contents.clear()
  130. focus_w = self.list.get_focus()
  131. session = focus_w[0].session
  132. if session == "outbound":
  133. key = (focus_w[0].node_name, "outbound")
  134. info = self.model.nodes.get(focus_w[0].node_name)
  135. if key in info['event']:
  136. ev = info['event'].get(key)
  137. self.pile.contents.append((
  138. urwid.Text(f" {ev}"),
  139. self.pile.options()))
  140. if (session == "outbound-slot" or session == "inbound-slot"
  141. or session == "manual-slot" or session == "seed-slot"):
  142. addr = focus_w[0].addr
  143. node_name = focus_w[0].node_name
  144. info = self.model.nodes.get(node_name)
  145. if addr in info['msgs']:
  146. msg = info['msgs'].get(addr)
  147. for m in msg:
  148. time = m[0]
  149. event = m[1]
  150. msg = m[2]
  151. self.pile.contents.append((urwid.Text(
  152. f"{time}: {event}: {msg}"),
  153. self.pile.options()))
  154. async def update_view(self, evloop: asyncio.AbstractEventLoop,
  155. loop: urwid.MainLoop):
  156. known_nodes = []
  157. off_nodes = []
  158. while True:
  159. await asyncio.sleep(0.1)
  160. # Redraw the screen
  161. evloop.call_soon(loop.draw_screen)
  162. for index, item in enumerate(self.listwalker.contents):
  163. known_nodes.append(item.node_name)
  164. for node_name, info in self.model.nodes.items():
  165. if not info:
  166. off_nodes.append(node_name)
  167. # TODO:
  168. # Right now we ignore info if it's in the set of known nodes.
  169. # However, there are a few events that should trigger a redraw:
  170. # * a new inbound connection comes online
  171. # * a new node comes online (FIXME)
  172. # * a known node has gone offline
  173. # * a inbound connection has gone offline
  174. # * when RPC can't connect, display the node as offline.
  175. for node_name, info in self.model.nodes.items():
  176. if node_name in known_nodes:
  177. continue
  178. if node_name in off_nodes:
  179. continue
  180. else:
  181. self.draw_info(node_name, info)
  182. self.fill_left_box()
  183. self.fill_right_box()