rns-simulator/util/rnssim.py

73 lines
1.6 KiB
Python
Raw Normal View History

2023-03-15 13:26:12 +00:00
import subprocess
import os
import threading
import time
node_state = {}
def main():
if not os.path.exists("nodes"):
os.mkdir("nodes")
os.chdir("nodes")
watchdog_thread = threading.Thread(target=watchdog)
watchdog_thread.start()
node_add(1)
node_add(2)
node_add(3)
node_add(4)
node_add(5)
def watchdog():
while True:
for node_id in os.listdir():
if not node_id in node_state:
node_state[node_id] = {
'active': False,
'stdout': None
}
if not node_state[node_id]["active"]:
rnsd_thread = threading.Thread(target=rnsd, args=(node_id,))
rnsd_thread.start()
print(node_state)
time.sleep(1)
def rnsd(node_id):
node_state[node_id]["active"] = True
subprocess.run(f"cd {node_id} && rnsd --config ./", shell=True, stdout=node_state[node_id]['stdout'])
node_state[node_id]["active"] = False
def node_add(node_id: int):
config = f"""
[reticulum]
enable_transport = False
share_instance = Yes
shared_instance_port = {37428 + node_id}
instance_control_port = {38429 + node_id}
[interfaces]
[[Autogenerated TCP Server Interface]]
type = TCPServerInterface
interface_enabled = True
listen_ip = localhost
listen_port = {42069 + node_id}
"""
node_id = str(node_id)
if not os.path.exists(node_id):
os.mkdir(node_id)
with open(node_id + "/config", "w+") as file:
file.write(config)
def node_connect(node1, node2):
pass
if __name__ == "__main__":
main()