nodetool.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 asyncio, json, random, sys, time
  18. class JsonRpc:
  19. async def start(self, server, port):
  20. reader, writer = await asyncio.open_connection(server, port)
  21. self.reader = reader
  22. self.writer = writer
  23. async def stop(self):
  24. self.writer.close()
  25. await self.writer.wait_closed()
  26. async def _make_request(self, method, params):
  27. ident = random.randint(0, 2**16)
  28. print(ident)
  29. request = {
  30. "jsonrpc": "2.0",
  31. "method": method,
  32. "params": params,
  33. "id": ident,
  34. }
  35. message = json.dumps(request) + "\n"
  36. self.writer.write(message.encode())
  37. await self.writer.drain()
  38. data = await self.reader.readline()
  39. message = data.decode().strip()
  40. response = json.loads(message)
  41. print(response)
  42. return response
  43. async def _subscribe(self, method, params):
  44. ident = random.randint(0, 2**16)
  45. request = {
  46. "jsonrpc": "2.0",
  47. "method": method,
  48. "params": params,
  49. "id": ident,
  50. }
  51. message = json.dumps(request) + "\n"
  52. self.writer.write(message.encode())
  53. await self.writer.drain()
  54. print("Subscribed")
  55. async def ping(self):
  56. return await self._make_request("ping", [])
  57. async def dnet_switch(self, state):
  58. return await self._make_request("dnet.switch", [state])
  59. async def dnet_subscribe_events(self):
  60. return await self._subscribe("dnet.subscribe_events", [])
  61. async def main(argv):
  62. rpc = JsonRpc()
  63. while True:
  64. try:
  65. await rpc.start("localhost", 26660)
  66. break
  67. except OSError:
  68. pass
  69. await rpc.dnet_switch(True)
  70. await rpc.dnet_subscribe_events()
  71. while True:
  72. data = await rpc.reader.readline()
  73. #with open("rpclog", "a") as f:
  74. # f.write(data.decode())
  75. data = json.loads(data)
  76. params = data["params"][0]
  77. ev = params["event"]
  78. if ev in ["send", "recv"]:
  79. continue
  80. info = params["info"]
  81. t = time.localtime()
  82. current_time = time.strftime("%H:%M:%S", t)
  83. match ev:
  84. case "inbound_connected":
  85. addr = info["addr"]
  86. print(f"{current_time} inbound (connect): {addr}")
  87. case "inbound_disconnected":
  88. addr = info["addr"]
  89. print(f"{current_time} inbound (disconnect): {addr}")
  90. case "outbound_slot_sleeping":
  91. slot = info["slot"]
  92. print(f"{current_time} slot {slot}: sleeping")
  93. case "outbound_slot_connecting":
  94. slot = info["slot"]
  95. addr = info["addr"]
  96. print(f"{current_time} slot {slot}: connecting addr={addr}")
  97. case "outbound_slot_connected":
  98. slot = info["slot"]
  99. addr = info["addr"]
  100. channel_id = info["channel_id"]
  101. print(f"{current_time} slot {slot}: connected addr={addr}")
  102. case "outbound_slot_disconnected":
  103. slot = info["slot"]
  104. err = info["err"]
  105. print(f"{current_time} slot {slot}: disconnected err='{err}'")
  106. case "outbound_peer_discovery":
  107. attempt = info["attempt"]
  108. state = info["state"]
  109. print(f"{current_time} peer_discovery: {state} (attempt {attempt})")
  110. #print(data)
  111. await rpc.dnet_switch(False)
  112. await rpc.stop()
  113. asyncio.run(main(sys.argv))