model.py 6.4 KB

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