Skip to content

Commit 08f47c6

Browse files
Refactor multicastUDP_receiver to support multiple filters and improve logging functionality
1 parent 9290eec commit 08f47c6

File tree

1 file changed

+35
-35
lines changed

1 file changed

+35
-35
lines changed

multicastUDP_receiver.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
multicast_group = '230.120.10.2'
1111
server_address = ('', 8123)
1212

13-
def start(filter = None):
13+
14+
def start(filters=None):
15+
if filters is None:
16+
filters = []
17+
1418
# Create the socket
1519
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
1620

@@ -19,55 +23,51 @@ def start(filter = None):
1923

2024
print("Start")
2125

22-
# Tell the operating system to add the socket to the multicast group
23-
# on all interfaces.
26+
# Tell the operating system to add the socket to the multicast group on the specified interface
2427
group = socket.inet_aton(multicast_group)
2528
mreq = struct.pack('4s4s', group, socket.inet_aton(MCAST_IF_IP))
2629
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
2730

2831
print("Ready")
29-
saveCounter = 0
3032

31-
buffer = queue.Queue(20)
33+
# Initialize buffers and save counters for each filter
34+
buffers = {filter_val: queue.Queue(20) for filter_val in filters}
35+
save_counters = {filter_val: 0 for filter_val in filters}
3236

3337
# Receive/respond loop
3438
while True:
3539
data, address = sock.recvfrom(1024)
3640
data_str = data.decode("utf-8").strip()
37-
if filter is not None and filter not in data_str:
38-
continue
39-
data_str = "[" + str(address[0]) + " - " + datetime.now().strftime('%b-%d-%Y_%H:%M:%S') + "] " + data_str
40-
print(data_str)
41-
buffer.put(data_str)
42-
if buffer.full():
43-
buffer.get()
44-
45-
46-
if "NTP-Update not successful" in data_str or "Start program" in data_str:
47-
f = open("log.txt",'a')
48-
while not buffer.empty():
49-
f.write(buffer.get())
50-
f.write("\n")
51-
f.close()
52-
saveCounter = 20
53-
54-
if saveCounter > 0:
55-
f = open("log.txt",'a')
56-
f.write(data_str)
57-
f.write("\n")
58-
if saveCounter == 1:
59-
f.write("\n")
60-
f.close()
61-
saveCounter -= 1
41+
timestamped_data = f"[{address[0]} - {datetime.now().strftime('%b-%d-%Y_%H:%M:%S')}] {data_str}"
6242

43+
# Check each filter and process data accordingly
44+
for filter_val in filters:
45+
if filter_val in data_str:
46+
print(timestamped_data)
47+
buffers[filter_val].put(timestamped_data)
48+
if buffers[filter_val].full():
49+
buffers[filter_val].get()
6350

64-
# Main
65-
if __name__ == '__main__':
51+
# Save data if specific keywords are found or if save counter is active
52+
if "NTP-Update not successful" in data_str or "Start program" in data_str:
53+
with open(f"log_{filter_val}.txt", 'a') as f:
54+
while not buffers[filter_val].empty():
55+
f.write(buffers[filter_val].get() + "\n")
56+
save_counters[filter_val] = 20 # Start the save counter
6657

67-
# Check if a filter is given
68-
# use filter as argument to filter messages: python3 multicastUDP_receiver.py "filter"
58+
if save_counters[filter_val] > 0:
59+
with open(f"log_{filter_val}.txt", 'a') as f:
60+
f.write(timestamped_data + "\n")
61+
if save_counters[filter_val] == 1:
62+
f.write("\n")
63+
save_counters[filter_val] -= 1
6964

65+
66+
# Main
67+
if __name__ == '__main__':
68+
# Check if filters are given
69+
# Use filters as arguments: python3 multicastUDP_receiver.py "filter1" "filter2" ...
7070
if len(sys.argv) > 1:
71-
start(sys.argv[1])
71+
start(sys.argv[1:])
7272
else:
7373
start()

0 commit comments

Comments
 (0)