-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.py
244 lines (198 loc) · 6.33 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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# Backend Server
# @authors: Emmanuel & Javier
import socket, threading, sys, MRA
from pycrc.crc_algorithms import Crc
class ThreadedServer(object):
def __init__(self, host, port):
self.host = host
self.port = port
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.bind((self.host, self.port))
self.MessageHandler = MRA.MRA()
self.crc = Crc(width=16, poly = 0x8005,
reflect_in = True, xor_in = 0x0000,
reflect_out = True, xor_out = 0x0000)
print("Running server on host [{0}] and port [{1}]".format(host, port))
def listen(self):
'''
Activates the listening method of the socket and each client is handled via
another worker thread such that we can handle messages from other clients.
'''
self.sock.listen(2)
while True:
connection, client_address = self.sock.accept()
threading.Thread(target=self.handle_request, args=(connection, client_address)).start()
def generateCRC(self, message):
'''
message is a string that contains the entire message that we want to send
to the client. The code will be attached the message we want to send back to the
the client. We also use this is to check whether a receiving CRC code is equal to
our computed CRC code.
'''
code = self.crc.bit_by_bit(str(message))
return hex(code)
def handle_request(self,connection, client_address):
'''
connection is the client socket which sent a buffer to the server socket.
client_address is the ip address along with a tag that identifies each client request.
The message is built by continuously receiving characters until the null or \0 character
is reached.
'''
print("Client address: [{0}]".format(client_address))
message = ""
try:
while True:
char = connection.recv(1)
if char == b'\0':
break
elif char == b'':
break
else:
message += char.decode("utf-8")
finally:
self.handler(message, connection)
def stop_server (self):
#FIXME For all users that are logged in, log them off manually.
return self.sock.close()
def handler(self, request, conn):
'''
request is the message being sent by the client socket.
conn is the connection socket from the client.
Each command is analyzed and passed into the message handler.
'''
if "REGISTER" in request:
code, req, username, password, email = request.split()
# Check if CRC code is equal to the computed CRC
if code == self.generateCRC(" ".join(request.split()[1:])):
pass
else:
conn.send(b'RETRY\0')
return
if self.MessageHandler.Register(username, password, email):
conn.send(b'REGISTER SUCCESS\0')
else:
conn.send(b'REGISTER FAILED\0')
elif "LOGIN" in request:
code, req, username, password = request.split()
# Check if CRC code is equal to the computed CRC
if code == self.generateCRC(" ".join(request.split()[1:])):
pass
else:
conn.send(b'RETRY\0')
return
try:
self.MessageHandler.Login(username, password)
conn.send(b'LOGIN SUCCESS\0')
except:
conn.send(b'LOGIN FAILED\0')
elif "LOGOUT" in request:
code, req, username, password = request.split()
# Check if CRC code is equal to the computed CRC
if code == self.generateCRC(" ".join(request.split()[1:])):
pass
else:
conn.send(b'RETRY\0')
return
try:
self.MessageHandler.Logout(username, password)
conn.send(b'LOGOUT SUCCESS\0')
except:
conn.send(b'LOGOUT FAILED\0')
elif "MESSAGE" in request:
length_req = len(request.split())
code = request.split()[0]
username, password = request.split()[length_req - 2:]
message = " ".join(request.split()[2:length_req - 2])
# Check if CRC code is equal to the computed CRC
if code == self.generateCRC(" ".join(request.split()[1:])):
pass
else:
conn.send(b'RETRY\0')
return
try:
self.MessageHandler.Message(username, password, message)
conn.send(b'OK\0')
except Exception as e:
if e.message == 0:
conn.send(b'KO\0')
elif e.message == -1:
conn.send(b'LOGGED OUT\0')
elif "STORE" in request:
code, req,username, password = request.split()
# Check if CRC code is equal to the computed CRC
if code == self.generateCRC(" ".join(request.split()[1:])):
pass
else:
conn.send(b'RETRY\0')
return
try:
if self.MessageHandler.Store(username, password):
conn.send(b'OK\0')
else:
conn.send(b'KO\0')
except:
conn.send(b'LOGGED OUT\0')
elif "COUNT" in request:
code, req,username, password = request.split()
# Check if CRC code is equal to the computed CRC
if code == self.generateCRC(" ".join(request.split()[1:])):
pass
else:
conn.send(b'RETRY\0')
return
try:
if self.MessageHandler.Count(username, password) != -1:
conn.send(b'{0}\0'.format(self.MessageHandler.Count(username, password)))
else:
conn.send(b'0\0')
except:
conn.send(b'LOGGED OUT\0')
elif "DELMSG" in request:
code, req, username, password = request.split()
# Check if CRC code is equal to the computed CRC
if code == self.generateCRC(" ".join(request.split()[1:])):
pass
else:
conn.send(b'RETRY\0')
return
try:
if self.MessageHandler.DelMsg(username, password):
conn.send(b'OK\0')
else:
conn.send(b'KO\0')
except:
conn.send(b'LOGGED OUT\0')
elif "GETMSG" in request:
code, req, username, password = request.split(" ")
# Check if CRC code is equal to the computed CRC
if code == self.generateCRC(" ".join(request.split()[1:])):
pass
else:
conn.send(b'RETRY\0')
return
try:
message = self.MessageHandler.GetMsg(username, password)
if message != -1:
conn.send(b'Message : {0}\0'.format(message))
else:
conn.send(b'KO\0')
except:
conn.send(b'LOGGED OUT\0')
elif "DUMP" in request:
code, req, username, password = request.split()
# Check if CRC code is equal to the computed CRC
if code == self.generateCRC(" ".join(request.split()[1:])):
pass
else:
conn.send(b'RETRY\0')
return
try:
self.MessageHandler.Dump(username, password)
conn.send(b'OK\0')
except Exception as e:
if e.message == 0:
conn.send(b'KO\0')
elif e.message == -1:
conn.send(b'LOGGED OUT\0')
else:
conn.send(b'Invalid Syntax\0')