main.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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
  18. import urwid
  19. import asyncio
  20. from rpc import JsonRpc
  21. from view import Dnetview
  22. async def get_info(rpc):
  23. while True:
  24. try:
  25. await rpc.start("localhost", 26660)
  26. break
  27. except OSError:
  28. pass
  29. response = await rpc._make_request("p2p.get_info", [])
  30. info = response["result"]
  31. channels = info["channels"]
  32. channel_lookup = {}
  33. for channel in channels:
  34. id = channel["id"]
  35. channel_lookup[id] = channel
  36. #print("inbound:")
  37. for channel in channels:
  38. if channel["session"] != "inbound":
  39. continue
  40. url = channel["url"]
  41. #print(f" {url}")
  42. #print("outbound:")
  43. for i, id in enumerate(info["outbound_slots"]):
  44. if id == 0:
  45. #print(f" {i}: none")
  46. continue
  47. assert id in channel_lookup
  48. url = channel_lookup[id]["url"]
  49. #print(f" {i}: {url}")
  50. #print("seed:")
  51. for channel in channels:
  52. if channel["session"] != "seed":
  53. continue
  54. url = channel["url"]
  55. #print(f" {url}")
  56. #print("manual:")
  57. for channel in channels:
  58. if channel["session"] != "manual":
  59. continue
  60. url = channel["url"]
  61. #print(f" {url}")
  62. await rpc.stop()
  63. if __name__ == '__main__':
  64. ev = asyncio.get_event_loop()
  65. rpc = JsonRpc()
  66. ev.create_task(get_info(rpc))
  67. dnet = Dnetview()
  68. ev.create_task(dnet.render_info())
  69. loop = urwid.MainLoop(dnet.view, dnet.palette,
  70. event_loop=urwid.AsyncioEventLoop(loop=ev))
  71. loop.run()