model.py 6.0 KB

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