-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chunk_Announcer.py
90 lines (71 loc) · 2.61 KB
/
Chunk_Announcer.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
84
85
86
87
88
89
90
# Import Libraries
import os
import json
import time
from socket import *
from sys import platform
import sys
from datetime import datetime
#Color
import colorama
from colorama import Fore, Back, Style
colorama.init(autoreset=True)
# Define Constant Variables
IP = '10.0.31.255' # '192.168.2.255' # provide your own local address here! (you can use ifconfig command in your terminal)
PORT = 5000
'''# Retrieve the local IP address dynamically
if platform == "linux" or platform == "linux2":
# Linux
IP = gethostbyname_ex(gethostname())[2][0]
elif platform == "darwin":
# macOS
IP = gethostbyname_ex(gethostname())[2][0]
elif platform == "win32":
# Windows
IP = gethostbyname_ex(gethostname())[2][0]
else:
IP = '127.0.0.1' # fallback to loopback address if the platform is unknown'''
print('IP:',IP)
# Get the username from the user
username = input('Please enter a username: ')
username = Fore.RED + f'{username}' + Fore.RESET + Fore.YELLOW + Fore.RESET
# Get the list of sliced files
sliced_files = os.listdir('sliced_files')
# Create a dictionary containing the username and files
user_dictionary = {
'username': username,
'files': sliced_files}
# Convert the dictionary to JSON
user_json = json.dumps(user_dictionary)
# Create the directory for json_files
if not os.path.exists('json_files'):
os.makedirs('json_files')
# Write the JSON to the announceFile
with open('json_files/announceFile.json', 'w') as announceFile:
announceFile.write(user_json)
print('announce file created' + Fore.GREEN + ' successfully.')
# Create and configure the socket
socket = socket(AF_INET, SOCK_DGRAM)
socket.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
lastSentTime = 0
announcePeriod = 5
# Clear the console before starting
os.system('cls' if os.name == 'nt' else 'clear')
while True:
# Check if it's time to send an announcement
if (time.time() - lastSentTime) > announcePeriod:
# Send the JSON data as bytes over the socket
socket.sendto(bytes(user_json.encode('utf-8')), (IP, PORT))
lastSentTime = time.time()
now = datetime.now()
dt_string = now.strftime('%H:%M:%S')
dt_string = Fore.RED + f'{dt_string}' + Fore.RESET
# Clear the console before printing the new announcement
os.system('cls' if os.name == 'nt' else 'clear')
print(f'\nAnnouncement sent by {username} at {dt_string}: ')
print(Fore.YELLOW + f'Files announced - {sliced_files}\n')
# print(Back.CYAN + '=' * 70)
# Flush the output to ensure immediate display
sys.stdout.flush()
# Sleep for 1 second before checking for changes
time.sleep(1)