main.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.get_event_loop()
  24. self.queue = asyncio.Queue()
  25. self.config = self.get_config()
  26. self.model = Model()
  27. self.view = View(self.model)
  28. async def subscribe(self, rpc, name, port):
  29. info = {}
  30. while True:
  31. try:
  32. logging.debug(f"Start {name} RPC on port {port}")
  33. await rpc.start("localhost", port)
  34. break
  35. # TODO: offline node handling
  36. except OSError:
  37. pass
  38. data = await rpc._make_request("p2p.get_info", [])
  39. info[name] = data
  40. try:
  41. self.queue.put_nowait(info)
  42. except:
  43. logging.debug("subscribe().put_nowait(): QueueFull")
  44. await rpc.dnet_switch(True)
  45. await rpc.dnet_subscribe_events()
  46. while True:
  47. data = await rpc.reader.readline()
  48. data = json.loads(data)
  49. info[name] = data
  50. #logging.debug(f"{info}")
  51. try:
  52. self.queue.put_nowait(info)
  53. except:
  54. logging.debug("subscribe().putnowait(): QueueFull")
  55. await rpc.dnet_switch(False)
  56. await rpc.stop()
  57. def get_config(self):
  58. with open("config.toml") as f:
  59. cfg = toml.load(f)
  60. return cfg
  61. async def start_connect_slots(self, nodes):
  62. tasks = []
  63. async with asyncio.TaskGroup() as tg:
  64. for i, node in enumerate(nodes):
  65. rpc = JsonRpc()
  66. subscribe = tg.create_task(self.subscribe(
  67. rpc, node['name'], node['port']))
  68. nodes = tg.create_task(self.update_info())
  69. async def update_info(self):
  70. while True:
  71. try:
  72. info = await self.queue.get()
  73. values = list(info.values())[0]
  74. # Update node info
  75. if "result" in values:
  76. self.model.handle_nodes(info)
  77. # Update event info: TODO
  78. if "params" in values:
  79. self.model.handle_event(info)
  80. self.queue.task_done()
  81. except OSError as e:
  82. logging.debug("update_model(): error {}", e)
  83. def main(self):
  84. logging.basicConfig(filename='dnet.log',
  85. encoding='utf-8',
  86. level=logging.DEBUG)
  87. nodes = self.config.get("nodes")
  88. self.ev.create_task(self.start_connect_slots(nodes))
  89. self.ev.create_task(self.view.update_view())
  90. self.ev.create_task(self.view.render_info())
  91. loop = urwid.MainLoop(self.view.ui, self.view.palette,
  92. unhandled_input=self.unhandled_input,
  93. event_loop=urwid.AsyncioEventLoop(
  94. loop=self.ev))
  95. loop.run()
  96. def unhandled_input(self, key):
  97. if key in ('q'):
  98. for task in asyncio.all_tasks():
  99. task.cancel()
  100. raise urwid.ExitMainLoop()
  101. if __name__ == '__main__':
  102. dnet = Dnetview()
  103. dnet.main()