model.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 logging
  18. class Model:
  19. def __init__(self):
  20. self.info = Info()
  21. self.nodes = {}
  22. def update_node(self, key, value):
  23. self.nodes[key] = value
  24. def handle_nodes(self, node):
  25. channel_lookup = {}
  26. name = list(node.keys())[0]
  27. values = list(node.values())[0]
  28. info = values["result"]
  29. channels = info["channels"]
  30. for channel in channels:
  31. id = channel["id"]
  32. channel_lookup[id] = channel
  33. for channel in channels:
  34. if channel["session"] != "inbound":
  35. continue
  36. url = channel["url"]
  37. self.info.update_inbound("inbound", url)
  38. for i, id in enumerate(info["outbound_slots"]):
  39. if id == 0:
  40. self.info.update_outbound(f"{i}", "none")
  41. continue
  42. assert id in channel_lookup
  43. url = channel_lookup[id]["url"]
  44. self.info.update_outbound(f"{i}", url)
  45. for channel in channels:
  46. if channel["session"] != "seed":
  47. continue
  48. url = channel["url"]
  49. self.info.update_seed("seed", url)
  50. for channel in channels:
  51. if channel["session"] != "manual":
  52. continue
  53. url = channel["url"]
  54. self.info.update_manual("manual", url)
  55. self.update_node(name, self.info)
  56. def handle_event(self, event):
  57. name = list(event.keys())[0]
  58. params = list(event.values())[0]
  59. def __repr__(self):
  60. return f"{self.nodes}"
  61. class Info:
  62. def __init__(self):
  63. self.outbounds = {}
  64. self.inbound = {}
  65. self.manual = {}
  66. self.seed = {}
  67. self.msgs = {}
  68. def update_outbound(self, key, value):
  69. self.outbounds[key] = value
  70. def update_inbound(self, key, value):
  71. self.inbound[key] = value
  72. def update_manual(self, key, value):
  73. self.manual[key] = value
  74. def update_seed(self, key, value):
  75. self.seed[key] = value
  76. def update_msg(self, msg):
  77. self.msgs = msg
  78. def __repr__(self):
  79. return (
  80. f"outbound: {self.outbounds}"
  81. f"inbound: {self.inbound}"
  82. f"manual: {self.manual}"
  83. f"seed: {self.seed}"
  84. f"msg: {self.msgs}"
  85. )