-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhandover_client2.py
83 lines (72 loc) · 2.67 KB
/
handover_client2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# handover_client.py
import socket
import sys
import time
import json
# Replace with the server's IP address and port
HOST = '127.0.0.1' # The server's IP address
PORT = 54321 # The port used by the server
def create_connection(host, port, retries=5, delay=2):
"""Attempts to create a socket connection to the server, with retries."""
attempt = 0
while attempt < retries:
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
print(f"Successfully connected to the server {host} on port {port}")
return s
except socket.error as err:
print(f"Connection failed with error: {err}. Retrying in {delay} seconds...")
time.sleep(delay)
attempt += 1
print("Maximum retries reached. Failed to connect to the server.")
return None
def send_handover_command(sock, ue_id, target_enb_id):
try:
# Construct the handover command as a JSON formatted string
command = {
"message_id": "some_unique_id", # You need to generate a unique ID for each message
"command": "handover",
"parameters": {
"ue_id": ue_id,
"target_enb_id": target_enb_id
}
}
message = json.dumps(command) + "\n"
sock.sendall(message.encode())
# Wait for a response from the server
response = sock.recv(1024)
print("Server response:", response.decode())
except socket.error as err:
print(f"Send/receive failed with error: {err}.")
return False
except ConnectionResetError:
print("Connection was closed by the server.")
return False
return True
def main():
sock = create_connection(HOST, PORT)
if not sock:
print("Failed to connect to the server. Exiting.")
sys.exit(1)
try:
while True:
# Example UE ID and target eNB ID for handover
ue_id = '123'
target_enb_id = '456'
if not send_handover_command(sock, ue_id, target_enb_id):
print("Attempting to reconnect to the server...")
sock.close() # Close the previous socket before creating a new one
sock = create_connection(HOST, PORT)
if not sock:
print("Reconnection failed. Exiting.")
break
# Example delay between handover commands
time.sleep(5)
except KeyboardInterrupt:
print("Interrupted by the user.")
finally:
if sock:
sock.close() # Ensure the socket is closed on exit
if __name__ == '__main__':
main()