main.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. logging.debug(f"get_info: {data}")
  40. await rpc.dnet_switch(True)
  41. await rpc.dnet_subscribe_events()
  42. while True:
  43. data = await rpc.reader.readline()
  44. data = json.loads(data)
  45. # TODO: update data structures
  46. logging.debug(f"events: {data}")
  47. await rpc.dnet_switch(False)
  48. await rpc.stop()
  49. def get_config(self):
  50. with open("config.toml") as f:
  51. cfg = toml.load(f)
  52. return cfg
  53. async def start_connect_slots(self, nodes):
  54. tasks = []
  55. async with asyncio.TaskGroup() as tg:
  56. for i, node in enumerate(nodes):
  57. rpc = JsonRpc()
  58. task = tg.create_task(self.subscribe(rpc, node['name'], node['port']))
  59. def main(self):
  60. logging.basicConfig(filename='dnet.log', encoding='utf-8', level=logging.DEBUG)
  61. nodes = self.config.get("nodes")
  62. self.ev.create_task(self.start_connect_slots(nodes))
  63. self.ev.create_task(self.view.update_view(self.model))
  64. loop = urwid.MainLoop(self.view.ui, self.view.palette,
  65. unhandled_input=self.unhandled_input,
  66. event_loop=urwid.AsyncioEventLoop(loop=self.ev))
  67. loop.run()
  68. def unhandled_input(self, key):
  69. if key in ('q'):
  70. for task in asyncio.all_tasks():
  71. task.cancel()
  72. raise urwid.ExitMainLoop()
  73. if __name__ == '__main__':
  74. dnet = Dnetview()
  75. dnet.main()