main.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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, node):
  30. name = node['name']
  31. host = node['host']
  32. port = node['port']
  33. type = node['type']
  34. info = {}
  35. while True:
  36. try:
  37. await rpc.start(host, port)
  38. logging.debug(f"Started {name} RPC on port {port}")
  39. break
  40. except Exception as e:
  41. info[name] = {}
  42. await self.queue.put(info)
  43. continue
  44. if type == "NORMAL":
  45. data = await rpc._make_request("p2p.get_info", [])
  46. info[name] = data
  47. await self.queue.put(info)
  48. await rpc.dnet_switch(True)
  49. await rpc.dnet_subscribe_events()
  50. while True:
  51. await asyncio.sleep(0.01)
  52. data = await rpc.reader.readline()
  53. try:
  54. data = json.loads(data)
  55. info[name] = data
  56. await self.queue.put(info)
  57. except:
  58. info[name] = {}
  59. await self.queue.put(info)
  60. await rpc.dnet_switch(False)
  61. if type == "LILITH":
  62. data = await rpc._make_request("spawns", [])
  63. info[name] = data
  64. await self.queue.put(info)
  65. await rpc.stop()
  66. def get_config(self):
  67. with open("config.toml") as f:
  68. cfg = toml.load(f)
  69. return cfg
  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 'spawns' in result:
  87. self.model.add_lilith(info)
  88. if 'channels' in result:
  89. self.model.add_node(info)
  90. if 'params' in values:
  91. self.model.add_event(info)
  92. self.queue.task_done()
  93. def main(self):
  94. logging.basicConfig(filename='dnet.log',
  95. encoding='utf-8',
  96. level=logging.DEBUG)
  97. nodes = self.config.get("nodes")
  98. loop = urwid.MainLoop(self.view.ui, self.view.palette,
  99. unhandled_input=self.unhandled_input,
  100. event_loop=urwid.AsyncioEventLoop(
  101. loop=self.ev))
  102. self.ev.create_task(self.start_connect_slots(nodes))
  103. self.ev.create_task(self.view.update_view(self.ev, loop))
  104. loop.run()
  105. def unhandled_input(self, key):
  106. if isinstance(key, tuple):
  107. return
  108. if key in ('q'):
  109. for task in asyncio.all_tasks():
  110. task.cancel()
  111. raise urwid.ExitMainLoop()
  112. if __name__ == '__main__':
  113. dnet = Dnetview()
  114. dnet.main()