model.py 6.0 KB

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