-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReceiver.py
More file actions
56 lines (44 loc) · 1.89 KB
/
Copy pathReceiver.py
File metadata and controls
56 lines (44 loc) · 1.89 KB
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
# This discovery tool runs for 5 seconds.
import socket
import json
import threading
import time
multicast_ip = "239.255.255.250"
multicast_port = 4002
# Create a UDP socket for listening to multicast messages
listen_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listen_socket.bind(('', multicast_port))
listen_socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, socket.inet_aton(multicast_ip) + socket.inet_aton("0.0.0.0"))
listen_socket.settimeout(5) # Sets the discovery time
responses = [] # Holds dictionaries containing all details of each response
lock = threading.Lock()
#Processes each response and adds it to the list
def process_response(data, addr):
received_message = data.decode('utf-8')
try:
received_json = json.loads(received_message)
device_info = {
'ip': addr[0], # Extract the IP address from the source address tuple
'device': received_json.get("msg", {}).get("data", {}).get("device")
}
if device_info['device']:
with lock:
responses.append(device_info)
print(f"Received response from {addr[0]}: {device_info}")
except json.JSONDecodeError:
print("Received non-JSON message:", received_message)
# Start listening for responses
try:
while True:
data, addr = listen_socket.recvfrom(10240)
threading.Thread(target=process_response, args=(data, addr)).start() # Starts a thread once a response is received to process it concurrently
except socket.timeout:
pass
# Prints all the stored devices from the list
with lock:
for device_info in responses:
print(f"Device at {device_info['ip']} with device ID {device_info['device']} responded.")
print(responses) # Prints the list
# Ensures proper closure of the socket
listen_socket.close()