model.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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, time
  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. t = info.get("time")
  64. cmd = info.get("cmd")
  65. chan = info.get("chan")
  66. addr = chan.get("addr")
  67. self.info.update_msg(addr, (t, event, cmd))
  68. else:
  69. t = time.localtime()
  70. current_time = time.strftime("%H:%M:%S", t)
  71. logging.debug(current_time)
  72. match event:
  73. case "inbound_connected":
  74. addr = info["addr"]
  75. logging.debug(f"{current_time} inbound (connect): {addr}")
  76. case "inbound_disconnected":
  77. addr = info["addr"]
  78. logging.debug(f"{current_time} inbound (disconnect): {addr}")
  79. case "outbound_slot_sleeping":
  80. slot = info["slot"]
  81. logging.debug(f"{current_time} slot {slot}: sleeping")
  82. case "outbound_slot_connecting":
  83. slot = info["slot"]
  84. addr = info["addr"]
  85. logging.debug(f"{current_time} slot {slot}: connecting addr={addr}")
  86. case "outbound_slot_connected":
  87. slot = info["slot"]
  88. addr = info["addr"]
  89. channel_id = info["channel_id"]
  90. logging.debug(f"{current_time} slot {slot}: connected addr={addr}")
  91. case "outbound_slot_disconnected":
  92. slot = info["slot"]
  93. err = info["err"]
  94. logging.debug(f"{current_time} slot {slot}: disconnected err='{err}'")
  95. case "outbound_peer_discovery":
  96. attempt = info["attempt"]
  97. state = info["state"]
  98. logging.debug(f"{current_time} peer_discovery: {state} (attempt {attempt})")
  99. def __repr__(self):
  100. return f"{self.nodes}"
  101. class Info:
  102. def __init__(self):
  103. self.outbounds = {}
  104. self.inbound = {}
  105. self.manual = {}
  106. self.seed = {}
  107. self.msgs = {}
  108. def update_outbound(self, key, value):
  109. self.outbounds[key] = value
  110. def update_inbound(self, key, value):
  111. self.inbound[key] = value
  112. def update_manual(self, key, value):
  113. self.manual[key] = value
  114. def update_seed(self, key, value):
  115. self.seed[key] = value
  116. def update_msg(self, key, value):
  117. if key in self.msgs:
  118. self.msgs[key] += [value]
  119. else:
  120. self.msgs[key] = [value]
  121. def __repr__(self):
  122. return (f"outbound: {self.outbounds}"
  123. f"inbound: {self.inbound}"
  124. f"manual: {self.manual}"
  125. f"seed: {self.seed}"
  126. f"msg: {self.msgs}")