model.py 5.9 KB

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