main.py 2.5 KB

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