deg 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/usr/bin/python3
  2. # This file is part of DarkFi (https://dark.fi)
  3. #
  4. # Copyright (C) 2020-2024 Dyne.org foundation
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as
  8. # published by the Free Software Foundation, either version 3 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. import json, urwid, asyncio, logging
  19. import src.util
  20. from os.path import join
  21. from src.model import Model
  22. from src.rpc import JsonRpc
  23. from src.view import View
  24. class Degview:
  25. def __init__(self):
  26. self.ev = asyncio.new_event_loop()
  27. asyncio.set_event_loop(self.ev)
  28. self.queue = asyncio.Queue()
  29. os = src.util.get_os()
  30. config_path = src.util.user_config_dir('darkfi', os)
  31. suffix = '.toml'
  32. filename = 'deg_config'
  33. path = join(config_path, filename + suffix)
  34. self.config = src.util.spawn_config(path)
  35. self.model = Model()
  36. self.view = View(self.model)
  37. async def subscribe(self, rpc, node):
  38. name = node['name']
  39. host = node['host']
  40. port = node['port']
  41. type = node['type']
  42. info = {}
  43. while True:
  44. try:
  45. await rpc.start(host, port)
  46. logging.debug(f'Started {name} RPC on port {port}')
  47. break
  48. except Exception as e:
  49. info[name] = {}
  50. await self.queue.put(info)
  51. continue
  52. if type == 'NORMAL':
  53. data = await rpc._make_request('eventgraph.get_info', [])
  54. info[name] = data
  55. await self.queue.put(info)
  56. await rpc.deg_switch(True)
  57. await rpc.deg_subscribe_events()
  58. while True:
  59. await asyncio.sleep(0.01)
  60. data = await rpc.reader.readline()
  61. try:
  62. data = json.loads(data)
  63. info[name] = data
  64. await self.queue.put(info)
  65. except:
  66. info[name] = {}
  67. await self.queue.put(info)
  68. await rpc.deg_switch(False)
  69. await rpc.stop()
  70. async def start_connect_slots(self, nodes):
  71. tasks = []
  72. async with asyncio.TaskGroup() as tg:
  73. for i, node in enumerate(nodes):
  74. rpc = JsonRpc()
  75. subscribe = tg.create_task(self.subscribe(
  76. rpc, node))
  77. nodes = tg.create_task(self.update_info())
  78. async def update_info(self):
  79. while True:
  80. info = await self.queue.get()
  81. values = list(info.values())[0]
  82. if not values:
  83. self.model.add_offline(info)
  84. if 'result' in values:
  85. result = values.get('result')
  86. if 'eventgraph_info' in result:
  87. self.model.add_eg(info)
  88. if 'params' in values:
  89. self.model.add_event(info)
  90. self.queue.task_done()
  91. def main(self):
  92. logging.basicConfig(filename='deg.log',
  93. encoding='utf-8',
  94. level=logging.DEBUG)
  95. nodes = self.config.get('nodes')
  96. loop = urwid.MainLoop(self.view.ui, self.view.palette,
  97. unhandled_input=self.unhandled_input,
  98. event_loop=urwid.AsyncioEventLoop(
  99. loop=self.ev))
  100. self.ev.create_task(self.start_connect_slots(nodes))
  101. self.ev.create_task(self.view.update_view(self.ev, loop))
  102. loop.run()
  103. def unhandled_input(self, key):
  104. if isinstance(key, tuple):
  105. return
  106. if key in ('q'):
  107. for task in asyncio.all_tasks():
  108. task.cancel()
  109. raise urwid.ExitMainLoop()
  110. if __name__ == '__main__':
  111. deg = Degview()
  112. deg.main()