-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.py
52 lines (40 loc) · 1.64 KB
/
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
import sys
from utils.calculation import Calculation
from utils.filehandler import FileHandler
from peerserver.peerserverconfighandler import PeerServerConfigHandler
from peerserver.threadedpeerserver import ThreadedPeerServer
if __name__ == '__main__':
server = None
filehandle = None
peer_server_config = PeerServerConfigHandler()
temp_dir = peer_server_config.temp_dir
tracker_host = peer_server_config.tracker_host
tracker_port = peer_server_config.tracker_port
tracker_server_address = (tracker_host, tracker_port)
peer_server_host = ''
peer_server_port = peer_server_config.peer_server_port
peer_server_address = (peer_server_host, peer_server_port)
# port used by peer-server to communicate with tracker-server
bind_port = peer_server_config.server_tracker_bind_port
filehandle = FileHandler()
filehandle.create_dir(temp_dir)
try:
server = ThreadedPeerServer(peer_server_address)
# register the server with tracker
server.register_with_tracker(tracker_server_address, bind_port)
proxy = peer_server_config.proxy
threads = peer_server_config.threads
# listen for download requests from client
server.listen(temp_dir, threads, proxy)
except Exception as e:
print("Oops! Error: {}.".format(e))
finally:
# stop peer server
if server:
server.stop_server()
# unregister the server with tracker
server.unregister_with_tracker(tracker_server_address, bind_port)
# delete the temporary directory
filehandle.delete_dir(temp_dir)
# exit
sys.exit(0)