-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracker.py
More file actions
74 lines (59 loc) · 2.2 KB
/
tracker.py
File metadata and controls
74 lines (59 loc) · 2.2 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import socket
from threading import Thread
import json
from utils.protocol import create_response, parse_request, Status
def handle_user_submission(addr, conn):
# Buffer to handle multiple requests at once
buffer = ""
try:
while True:
data = conn.recv(4096)
if not data:
print(f"Connection closed by {addr}")
return
buffer += data.decode("utf-8")
while '\\' in buffer:
# Split the buffer into individual requests
request, buffer = buffer.split('\\', 1)
if not request:
continue
# Parse the request
command, payload = parse_request(request, isSeparated=True)
if command == "LIST":
response = create_response(Status.OK, peers)
conn.send(response)
elif command == "HOST":
# A host can have multiple channels
for peer in payload:
peers.append(peer)
response = create_response(Status.OK, {
"status": "success",
"channel_name": [peer["channel_name"] for peer in payload],
})
conn.send(response)
elif command == "MESSAGE":
pass
except ValueError as e:
print("Error parsing data:", e)
return
finally:
conn.close()
def listen(ip, port):
try:
tracker_socket = socket.socket()
tracker_socket.bind((ip, port))
tracker_socket.listen(10)
print(f"listening on {ip}:{port}...")
while True:
conn, addr = tracker_socket.accept()
Thread(target=handle_user_submission, args=(addr, conn), daemon=True).start()
except KeyboardInterrupt:
print("Exiting...")
finally:
tracker_socket.close()
if __name__ == "__main__":
# Dummy values for testing
peers = []
ip = '127.0.0.1'
port = 22236
listen(ip, port)