view.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. class Node(DnetWidget):
  36. def set_txt(self):
  37. txt = urwid.Text(f"{self.node_name}")
  38. super().update(txt)
  39. class Session(DnetWidget):
  40. def set_txt(self):
  41. txt = urwid.Text(f" {self.session}")
  42. super().update(txt)
  43. class Slot(DnetWidget):
  44. def set_txt(self, i, addr):
  45. self.i = i
  46. self.addr = addr
  47. if len(self.i) == 1:
  48. txt = urwid.Text(f" {self.i}: {self.addr}")
  49. else:
  50. txt = urwid.Text(f" {self.addr}")
  51. super().update(txt)
  52. class View():
  53. palette = [
  54. ('body','light gray','default', 'standout'),
  55. ('line','dark cyan','default','standout'),
  56. ]
  57. def __init__(self, model):
  58. self.model = model
  59. info_text = urwid.Text("")
  60. self.pile = urwid.Pile([info_text])
  61. scroll = ScrollBar(Scrollable(self.pile))
  62. rightbox = urwid.LineBox(scroll)
  63. self.listbox_content = []
  64. self.listwalker = urwid.SimpleListWalker(self.listbox_content)
  65. self.list = urwid.ListBox(self.listwalker)
  66. leftbox = urwid.LineBox(self.list)
  67. columns = urwid.Columns([leftbox, rightbox], focus_column=0)
  68. self.ui = urwid.Frame(urwid.AttrWrap( columns, 'body' ))
  69. async def update_view(self, evloop: asyncio.AbstractEventLoop,
  70. loop: urwid.MainLoop):
  71. known_nodes = []
  72. known_inbound = []
  73. while True:
  74. await asyncio.sleep(0.1)
  75. # Redraw the screen
  76. evloop.call_soon(loop.draw_screen)
  77. for index, item in enumerate(self.listwalker.contents):
  78. known_nodes.append(item.node_name)
  79. #-----------------------------------------------------------------
  80. # Render get_info()
  81. #-----------------------------------------------------------------
  82. for node_name, info in self.model.nodes.items():
  83. if node_name in known_nodes:
  84. continue
  85. else:
  86. node = Node(node_name, "node")
  87. node.set_txt()
  88. self.listwalker.contents.append(node)
  89. if info['outbound']:
  90. session = Session(node_name, "outbound")
  91. session.set_txt()
  92. self.listwalker.contents.append(session)
  93. for i, addr in info['outbound'].items():
  94. slot = Slot(node_name, "outbound-slot")
  95. slot.set_txt(i, addr)
  96. self.listwalker.contents.append(slot)
  97. if info['inbound']:
  98. session = Session(node_name, "inbound")
  99. session.set_txt()
  100. self.listwalker.contents.append(session)
  101. for i, addr in info['inbound'].items():
  102. slot = Slot(node_name, "inbound-slot")
  103. slot.set_txt(i, addr)
  104. self.listwalker.contents.append(slot)
  105. if info['manual']:
  106. session = Session(node_name, "manual")
  107. session.set_txt()
  108. self.listwalker.contents.append(session)
  109. for i, addr in info['manual'].items():
  110. slot = Slot(node_name, "manual-slot")
  111. slot.set_txt(i, addr)
  112. self.listwalker.contents.append(slot)
  113. if info['seed']:
  114. session = Session(node_name, "seed")
  115. session.set_txt()
  116. self.listwalker.contents.append(session)
  117. for i, info in info['seed'].items():
  118. slot = Slot(node_name, "seed-slot")
  119. slot.set_txt(i, addr)
  120. self.listwalker.contents.append(slot)
  121. #-----------------------------------------------------------------
  122. # Render subscribe_events() (left menu)
  123. #-----------------------------------------------------------------
  124. new_inbound = {}
  125. for index, item in enumerate(self.listwalker.contents):
  126. # Update outbound slot info
  127. if item.session == "outbound-slot":
  128. key = (f"{item.node_name}", f"{item.i}")
  129. if key in self.model.nodes[item.node_name]['event']:
  130. info = self.model.nodes[item.node_name]['event'].get(key)
  131. slot = Slot(item.node_name, item.session)
  132. slot.set_txt(item.i, info)
  133. self.listwalker.contents[index] = slot
  134. # Get known inbounds
  135. if item.session == "inbound-slot":
  136. if item.i not in known_inbound:
  137. known_inbound.append(item.i)
  138. # Get unknown inbounds
  139. inbound = self.model.nodes[item.node_name]['inbound']
  140. for i, addr in inbound.items():
  141. if i in known_inbound:
  142. continue
  143. else:
  144. new_inbound[i] = (addr, item.node_name)
  145. # TODO: update new inbounds
  146. # Render subscribe_events() (right menu)
  147. async def render_info(self, evloop: asyncio.AbstractEventLoop,
  148. loop: urwid.MainLoop):
  149. while True:
  150. await asyncio.sleep(0.01)
  151. # Redraw the screen
  152. evloop.call_soon(loop.draw_screen)
  153. self.pile.contents.clear()
  154. focus_w = self.list.get_focus()
  155. if focus_w[0] is None:
  156. continue
  157. else:
  158. session = focus_w[0].session
  159. if session == "node":
  160. continue
  161. if session == "outbound":
  162. key = (focus_w[0].node_name, "outbound")
  163. info = self.model.nodes.get(focus_w[0].node_name)
  164. if key in info['event']:
  165. ev = info['event'].get(key)
  166. self.pile.contents.append((
  167. urwid.Text(f" {ev}"),
  168. self.pile.options()))
  169. if (session == "outbound-slot" or session == "inbound-slot"
  170. or session == "manual-slot" or session == "seed-slot"):
  171. addr = focus_w[0].addr
  172. node_name = focus_w[0].node_name
  173. info = self.model.nodes.get(node_name)
  174. if addr in info['msgs']:
  175. msg = info['msgs'].get(addr)
  176. for m in msg:
  177. time = m[0]
  178. event = m[1]
  179. msg = m[2]
  180. self.pile.contents.append((urwid.Text(
  181. f"{time}: {event}: {msg}"),
  182. self.pile.options()))