-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimer_Server.py
80 lines (66 loc) · 2.57 KB
/
Timer_Server.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
import socket
import threading
import time
#ifconfig | grep "inet " | grep -Fv 127.0.0.1 | awk '{print $2}'
class FocusTimer:
def __init__(self, session_length, break_length):
self.session_length = session_length * 60
self.break_length = break_length * 60
self.current_time = 0
self.is_study = True
self.timer_started = False
def start_timer(self, stop_event):
self.timer_started = True
while not stop_event.is_set():
for self.current_time in range(self.session_length, 0, -1):
self.is_study = True
if stop_event.is_set():
break
time.sleep(1)
for self.current_time in range(self.break_length, 0, -1):
self.is_study = False
if stop_event.is_set():
break
time.sleep(1)
def get_time(self):
status = "Study" if self.is_study else "Break"
return f"{status}: {self.current_time // 60:02d}:{self.current_time % 60:02d}"
def handle_client(conn, addr, focus_timer, clients, stop_event):
name = conn.recv(1024).decode('utf-8')
clients[name] = conn
print(f"Client {name} ({addr}) connected")
print("Connected clients:", ", ".join(clients.keys()))
if not focus_timer.timer_started:
threading.Thread(target=focus_timer.start_timer, args=(stop_event,)).start()
while not stop_event.is_set():
try:
conn.sendall(f"{focus_timer.get_time()} | Connected: {', '.join(clients.keys())}".encode('utf-8'))
time.sleep(1)
except (ConnectionResetError, BrokenPipeError):
break
print(f"Client {name} ({addr}) disconnected")
del clients[name]
print("Connected clients:", ", ".join(clients.keys()))
conn.close()
def main():
host = '0.0.0.0'
port = 12345
session_length = int(input("Enter session length (minutes): "))
break_length = int(input("Enter break length (minutes): "))
focus_timer = FocusTimer(session_length, break_length)
clients = {}
stop_event = threading.Event()
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))
server.listen(5)
print(f"Server listening on {host}:{port}")
try:
while True:
conn, addr = server.accept()
threading.Thread(target=handle_client, args=(conn, addr, focus_timer, clients, stop_event)).start()
except KeyboardInterrupt:
print("\nShutting down server...")
stop_event.set()
server.close()
if __name__ == "__main__":
main()