Skip to content

Commit 9e15d3c

Browse files
committed
Now "backend" can be used as a valid ip for socket declaration. Now using "backend" as an ip uses the backend ip in general_info.json
1 parent d3cefb2 commit 9e15d3c

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

Core/Inc/Code_generation/Packet_generation/Packet_descriptions.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,22 @@ def __init__(self,name:str,board:dict,JSONpath:str):
66
self.name = name
77
self.id = board["board_id"]
88
self.ip = board["board_ip"]
9+
10+
# Load backend IP from general_info.json
11+
backend_ip = "0.0.0.0"
12+
try:
13+
with open(JSONpath + "/general_info.json") as f:
14+
general_info = json.load(f)
15+
if "addresses" in general_info and "backend" in general_info["addresses"]:
16+
backend_ip = general_info["addresses"]["backend"]
17+
except Exception as e:
18+
print(f"Warning: Could not load backend IP from general_info.json: {e}")
19+
920
#Sockets:
1021
try:
1122
with open(JSONpath+"/boards/"+name+"/sockets.json") as s:
1223
socks = json.load(s)
13-
self.sockets=self.SocketsDescription(socks,self.ip)
24+
self.sockets = self.SocketsDescription(socks, self.ip, backend_ip)
1425
except Exception as e:
1526
raise Exception(f"Error in file {JSONpath}/boards/{name}/sockets.json: {e}")
1627
#Packets:
@@ -76,12 +87,13 @@ def fix_sendind_packets(sending_packets:list):
7687

7788

7889
class SocketsDescription:
79-
def __init__(self, sockets: list, board_ip: str):
90+
def __init__(self, sockets: list, board_ip: str, backend_ip: str):
8091
self.allSockets = []
8192
self.ServerSockets = []
8293
self.Sockets = []
8394
self.DatagramSockets = []
8495
self.board_ip = board_ip
96+
self.backend_ip = backend_ip
8597
for sock in sockets:
8698
name = sock["name"].replace(" ", "_").replace("-", "_")
8799
sock_type = sock["type"]
@@ -90,9 +102,15 @@ def __init__(self, sockets: list, board_ip: str):
90102
if sock_type == "ServerSocket":
91103
self.ServerSockets.append({"name": name, "type": sock_type, "board_ip": self.board_ip, "port": sock["port"]})
92104
elif sock_type == "Socket":
93-
self.Sockets.append({"name": name, "type": sock_type, "board_ip": self.board_ip, "local_port": sock["local_port"], "remote_ip": sock["remote_ip"], "remote_port": sock["remote_port"]})
105+
remote_ip = sock["remote_ip"]
106+
if remote_ip == "backend":
107+
remote_ip = self.backend_ip
108+
self.Sockets.append({"name": name, "type": sock_type, "board_ip": self.board_ip, "local_port": sock["local_port"], "remote_ip": remote_ip, "remote_port": sock["remote_port"]})
94109
elif sock_type == "DatagramSocket":
95-
self.DatagramSockets.append({"name": name, "type": sock_type, "board_ip": self.board_ip, "port": sock["port"], "remote_ip": sock["remote_ip"]})
110+
remote_ip = sock["remote_ip"]
111+
if remote_ip == "backend":
112+
remote_ip = self.backend_ip
113+
self.DatagramSockets.append({"name": name, "type": sock_type, "board_ip": self.board_ip, "port": sock["port"], "remote_ip": remote_ip})
96114

97115

98116

0 commit comments

Comments
 (0)