-
Notifications
You must be signed in to change notification settings - Fork 0
/
StorageServer.py
61 lines (51 loc) · 1.82 KB
/
StorageServer.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
from threading import Thread
import SProtocol.NSP.storage as NSP
import SProtocol.storage as SP
import SServer.Jobs as Jobs
from Common.AskInput import GetIPOrDomain
from Common.Constants import StorageServerPort, TEST
from Common.Logger import ServerLogger
from Common.Socket import BindAndListen, Accept, socket, SocketError
LogPath = 'Slog.txt'
Logger = ServerLogger(LogPath, not TEST)
MainSocket: socket = ...
IPPath = 'myip.txt'
def serve(sock: socket, host: tuple):
host = '%s:%d' % host
Logger.add(host + ' has connected')
try:
SP.Serve(sock, Logger)
except SocketError as e:
Logger.addError('A socket error occurred during serving %s' % host, e)
except SP.SPException as e:
Logger.addError('A protocol error occurred during serving %s' % host, e)
except OSError as e:
Logger.addError('An OS error occurred during serving %s' % host, e)
except Exception as e:
Logger.addError('A unknown error occurred during serving %s' % host, e)
finally:
Logger.add(host + ' has disconnected')
Jobs.AbortJob(sock)
sock.close()
def SetPublicIP():
ip = GetIPOrDomain(IPPath, 'Input public IP address or domain name of this server')
NSP.SetPublicIP(ip)
def incoming():
global MainSocket
MainSocket = BindAndListen('', StorageServerPort)
print('Server started')
while True:
client_sock, addr = Accept(MainSocket)
Thread(daemon=True, target=serve, args=(client_sock, addr)).start()
if __name__ == '__main__':
try:
SP.SetLogger(Logger)
SetPublicIP()
incoming()
except KeyboardInterrupt:
Logger.add('Application was interrupted')
except Exception as e:
Logger.addError('An error occurred', e)
finally:
if isinstance(MainSocket, socket):
MainSocket.close()