main.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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
  18. from model import Model
  19. from rpc import JsonRpc
  20. from view import View
  21. class Dnetview:
  22. def __init__(self):
  23. self.ev = asyncio.new_event_loop()
  24. asyncio.set_event_loop(self.ev)
  25. self.queue = asyncio.Queue()
  26. self.config = self.get_config()
  27. self.model = Model()
  28. self.view = View(self.model)
  29. async def subscribe(self, rpc, name, host, port):
  30. info = {}
  31. while True:
  32. try:
  33. await rpc.start(host, port)
  34. logging.debug(f"Started {name} RPC on port {port}")
  35. break
  36. except Exception as e:
  37. logging.debug(f"failed to connect {host}:{port} {e}")
  38. pass
  39. data = await rpc._make_request("p2p.get_info", [])
  40. info[name] = data
  41. await self.queue.put(info)
  42. await rpc.dnet_switch(True)
  43. await rpc.dnet_subscribe_events()
  44. while True:
  45. await asyncio.sleep(0.01)
  46. data = await rpc.reader.readline()
  47. data = json.loads(data)
  48. info[name] = data
  49. await self.queue.put(info)
  50. await rpc.dnet_switch(False)
  51. await rpc.stop()
  52. def get_config(self):
  53. with open("config.toml") as f:
  54. cfg = toml.load(f)
  55. return cfg
  56. async def start_connect_slots(self, nodes):
  57. tasks = []
  58. async with asyncio.TaskGroup() as tg:
  59. for i, node in enumerate(nodes):
  60. rpc = JsonRpc()
  61. subscribe = tg.create_task(self.subscribe(
  62. rpc, node['name'], node['host'],
  63. node['port']))
  64. nodes = tg.create_task(self.update_info())
  65. async def update_info(self):
  66. while True:
  67. info = await self.queue.get()
  68. values = list(info.values())[0]
  69. method = values.get("method")
  70. if method == "dnet.subscribe_events":
  71. self.model.add_event(info)
  72. else:
  73. self.model.add_node(info)
  74. self.queue.task_done()
  75. def main(self):
  76. logging.basicConfig(filename='dnet.log',
  77. encoding='utf-8',
  78. level=logging.DEBUG)
  79. nodes = self.config.get("nodes")
  80. loop = urwid.MainLoop(self.view.ui, self.view.palette,
  81. unhandled_input=self.unhandled_input,
  82. event_loop=urwid.AsyncioEventLoop(
  83. loop=self.ev))
  84. self.ev.create_task(self.start_connect_slots(nodes))
  85. self.ev.create_task(self.view.update_view(self.ev, loop))
  86. self.ev.create_task(self.view.render_info(self.ev, loop))
  87. loop.run()
  88. def unhandled_input(self, key):
  89. if key in ('q'):
  90. for task in asyncio.all_tasks():
  91. task.cancel()
  92. raise urwid.ExitMainLoop()
  93. if __name__ == '__main__':
  94. dnet = Dnetview()
  95. dnet.main()