model.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. values = list(event.values())[0]
  59. params = values.get("params")
  60. event = params[0].get("event")
  61. info = params[0].get("info")
  62. if "chan" in info:
  63. time = info.get("time")
  64. cmd = info.get("cmd")
  65. chan = info.get("chan")
  66. addr = chan.get("addr")
  67. self.info.update_msg(addr, (time, event, cmd))
  68. else:
  69. # TODO
  70. #slot = info.get("slot")
  71. logging.debug(info)
  72. logging.debug(event)
  73. #self.info.update_msgs(addr, (time, event, cmd))
  74. def __repr__(self):
  75. return f"{self.nodes}"
  76. class Info:
  77. def __init__(self):
  78. self.outbounds = {}
  79. self.inbound = {}
  80. self.manual = {}
  81. self.seed = {}
  82. self.msgs = {}
  83. def update_outbound(self, key, value):
  84. self.outbounds[key] = value
  85. def update_inbound(self, key, value):
  86. self.inbound[key] = value
  87. def update_manual(self, key, value):
  88. self.manual[key] = value
  89. def update_seed(self, key, value):
  90. self.seed[key] = value
  91. def update_msg(self, key, value):
  92. if key in self.msgs:
  93. self.msgs[key] += [value]
  94. else:
  95. self.msgs[key] = [value]
  96. def __repr__(self):
  97. return (
  98. f"outbound: {self.outbounds}"
  99. f"inbound: {self.inbound}"
  100. f"manual: {self.manual}"
  101. f"seed: {self.seed}"
  102. f"msg: {self.msgs}"
  103. )