main.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 sys, toml, json, urwid, asyncio, logging, util
  18. from os.path import exists, join
  19. from pathlib import Path
  20. from model import Model
  21. from rpc import JsonRpc
  22. from view import View
  23. class Dnetview:
  24. def __init__(self):
  25. self.ev = asyncio.new_event_loop()
  26. asyncio.set_event_loop(self.ev)
  27. self.queue = asyncio.Queue()
  28. os = util.get_os()
  29. config_path = util.user_config_dir('darkfi', os)
  30. suffix = '.toml'
  31. filename = 'dnet_config'
  32. path = join(config_path, filename + suffix)
  33. self.config = util.spawn_config(path)
  34. self.model = Model()
  35. self.view = View(self.model)
  36. async def subscribe(self, rpc, node):
  37. name = node['name']
  38. host = node['host']
  39. port = node['port']
  40. type = node['type']
  41. info = {}
  42. while True:
  43. try:
  44. await rpc.start(host, port)
  45. logging.debug(f'Started {name} RPC on port {port}')
  46. break
  47. except Exception as e:
  48. info[name] = {}
  49. await self.queue.put(info)
  50. continue
  51. if type == 'NORMAL':
  52. data = await rpc._make_request('p2p.get_info', [])
  53. info[name] = data
  54. await self.queue.put(info)
  55. await rpc.dnet_switch(True)
  56. await rpc.dnet_subscribe_events()
  57. while True:
  58. await asyncio.sleep(0.01)
  59. data = await rpc.reader.readline()
  60. try:
  61. data = json.loads(data)
  62. info[name] = data
  63. await self.queue.put(info)
  64. except:
  65. info[name] = {}
  66. await self.queue.put(info)
  67. await rpc.dnet_switch(False)
  68. if type == 'LILITH':
  69. data = await rpc._make_request('spawns', [])
  70. info[name] = data
  71. await self.queue.put(info)
  72. await rpc.stop()
  73. async def start_connect_slots(self, nodes):
  74. tasks = []
  75. async with asyncio.TaskGroup() as tg:
  76. for i, node in enumerate(nodes):
  77. rpc = JsonRpc()
  78. subscribe = tg.create_task(self.subscribe(
  79. rpc, node))
  80. nodes = tg.create_task(self.update_info())
  81. async def update_info(self):
  82. while True:
  83. info = await self.queue.get()
  84. values = list(info.values())[0]
  85. if not values:
  86. self.model.add_offline(info)
  87. if 'result' in values:
  88. result = values.get('result')
  89. if 'spawns' in result:
  90. self.model.add_lilith(info)
  91. if 'channels' in result:
  92. self.model.add_node(info)
  93. if 'params' in values:
  94. self.model.add_event(info)
  95. self.queue.task_done()
  96. def main(self):
  97. logging.basicConfig(filename='dnet.log',
  98. encoding='utf-8',
  99. level=logging.DEBUG)
  100. nodes = self.config.get('nodes')
  101. loop = urwid.MainLoop(self.view.ui, self.view.palette,
  102. unhandled_input=self.unhandled_input,
  103. event_loop=urwid.AsyncioEventLoop(
  104. loop=self.ev))
  105. self.ev.create_task(self.start_connect_slots(nodes))
  106. self.ev.create_task(self.view.update_view(self.ev, loop))
  107. loop.run()
  108. def unhandled_input(self, key):
  109. if isinstance(key, tuple):
  110. return
  111. if key in ('q'):
  112. for task in asyncio.all_tasks():
  113. task.cancel()
  114. raise urwid.ExitMainLoop()
  115. if __name__ == '__main__':
  116. dnet = Dnetview()
  117. dnet.main()