model.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. # -------------------------------------------------------------------
  19. # TODO:
  20. # * on first get_info call, initialize data structure
  21. # * use channel id as key
  22. # * e.g. outbound[id] = [info1, info2, ...]
  23. # * create unique null id if not connected
  24. # -------------------------------------------------------------------
  25. class Model:
  26. def __init__(self):
  27. self.info = Info()
  28. self.nodes = {}
  29. def update_node(self, key, value):
  30. self.nodes[key] = value
  31. def handle_nodes(self, node):
  32. channel_lookup = {}
  33. name = list(node.keys())[0]
  34. values = list(node.values())[0]
  35. info = values["result"]
  36. channels = info["channels"]
  37. for channel in channels:
  38. id = channel["id"]
  39. channel_lookup[id] = channel
  40. for channel in channels:
  41. if channel["session"] != "inbound":
  42. continue
  43. url = channel["url"]
  44. self.info.update_inbound("inbound", url)
  45. for i, id in enumerate(info["outbound_slots"]):
  46. if id == 0:
  47. self.info.update_outbound(f"{i}", "none")
  48. continue
  49. assert id in channel_lookup
  50. url = channel_lookup[id]["url"]
  51. self.info.update_outbound(f"{i}", url)
  52. for channel in channels:
  53. if channel["session"] != "seed":
  54. continue
  55. url = channel["url"]
  56. self.info.update_seed("seed", url)
  57. for channel in channels:
  58. if channel["session"] != "manual":
  59. continue
  60. url = channel["url"]
  61. self.info.update_manual("manual", url)
  62. self.update_node(name, self.info)
  63. def handle_event(self, event):
  64. name = list(event.keys())[0]
  65. values = list(event.values())[0]
  66. params = values.get("params")
  67. event = params[0].get("event")
  68. info = params[0].get("info")
  69. t = time.localtime()
  70. current_time = time.strftime("%H:%M:%S", t)
  71. match event:
  72. case "send_msg":
  73. t = info.get("time")
  74. cmd = info.get("cmd")
  75. chan = info.get("chan")
  76. addr = info.get("addr")
  77. logging.debug(f"{t} {addr} {event} {cmd}")
  78. self.info.update_msg(addr, (t, event, cmd))
  79. case "recv_msg":
  80. t = info.get("time")
  81. cmd = info.get("cmd")
  82. chan = info.get("chan")
  83. addr = info.get("addr")
  84. logging.debug(f"{t} {addr} {event} {cmd}")
  85. self.info.update_msg(addr, (t, event, cmd))
  86. case "inbound_connected":
  87. addr = info["addr"]
  88. logging.debug(f"{current_time} inbound (connect): {addr}")
  89. case "inbound_disconnected":
  90. addr = info["addr"]
  91. logging.debug(f"{current_time} inbound (disconnect): {addr}")
  92. case "outbound_slot_sleeping":
  93. slot = info["slot"]
  94. logging.debug(f"{current_time} slot {slot}: sleeping")
  95. self.info.append_outbound(str(slot), "sleeping")
  96. case "outbound_slot_connecting":
  97. slot = info["slot"]
  98. addr = info["addr"]
  99. logging.debug(f"{current_time} slot {slot}: connecting addr={addr}")
  100. case "outbound_slot_connected":
  101. slot = info["slot"]
  102. addr = info["addr"]
  103. channel_id = info["channel_id"]
  104. logging.debug(f"{current_time} slot {slot}: connected addr={addr}")
  105. case "outbound_slot_disconnected":
  106. slot = info["slot"]
  107. err = info["err"]
  108. logging.debug(f"{current_time} slot {slot}: disconnected")
  109. case "outbound_peer_discovery":
  110. attempt = info["attempt"]
  111. state = info["state"]
  112. logging.debug(f"{current_time} peer_discovery: {state} (attempt {attempt})")
  113. def __repr__(self):
  114. return f"{self.nodes}"
  115. class Info:
  116. def __init__(self):
  117. self.outbounds = {}
  118. self.inbound = {}
  119. self.manual = {}
  120. self.seed = {}
  121. self.msgs = {}
  122. def update_outbound(self, key, value):
  123. self.outbounds[key] = [value]
  124. def update_inbound(self, key, value):
  125. self.inbound[key] = value
  126. def update_manual(self, key, value):
  127. self.manual[key] = value
  128. def update_seed(self, key, value):
  129. self.seed[key] = value
  130. def update_msg(self, key, value):
  131. if key in self.msgs:
  132. self.msgs[key] += [value]
  133. else:
  134. self.msgs[key] = [value]
  135. def append_outbound(self, key, value):
  136. if value not in self.outbounds[key]:
  137. self.outbounds[key].append(value)
  138. def __repr__(self):
  139. return (f"outbound: {self.outbounds}"
  140. f"inbound: {self.inbound}"
  141. f"manual: {self.manual}"
  142. f"seed: {self.seed}"
  143. f"msg: {self.msgs}")