-
Notifications
You must be signed in to change notification settings - Fork 1
/
conn.py
363 lines (298 loc) · 13.2 KB
/
conn.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import socket
import threading
import json
import time
from Crypto.Hash import SHA256
import database
import log
import managment
class ClientConnection():
def __init__(self, connection:socket.socket, conn_info:dict):
# sets up a the object of the client connection
# takes a connection(connection), a socket.socket object, and the information of the connection(conn_info), a dict
# also sstarts a thread to proses the queue(self.process_queue)
logger.log("client")
self.connection = connection
self.info = conn_info
self.queue = []
self.responses = []
self.temp_msgs = {}
self.send_responses = []
self.ip = None
self.read = 0
self.size = 0
self.message = ""
thread = threading.Thread(target=self.process_queue)
thread.start()
def manage_requests(self):
# function thaat manages requests
# when the client seends a message, the message is prossesed by this function
# in this function the messages are reassembled and sent to the corresponding queue
global clients, logger
while True:
try:
msg = self.connection.recv(1024).decode("utf-8")
logger.log(msg)
if msg == "":
logger.log("Closing because of empty message")
raise socket.error
for char in msg:
# The first 8 bytes of a message indicate the size of it
# This calculates the size of the message
if self.read < 8:
self.size += int(char)*(10**(7-self.read))
else:
self.message += char
if self.read == self.size+7 and not self.read == 0:
self.read = 0
self.size = 0
message_dict:dict = json.loads(self.message)
if message_dict["type"] == "ACTION":
self.queue.append(message_dict)
elif message_dict["type"] == "RESPONSE":
self.responses.append(message_dict)
self.message = ""
else:
self.read += 1
# if there is a socket error, which can be caused by a client disconecting or the internet going out
# the error will be logged, then the client will be removed from thee clients list
# and a command will be sent to kill the queue porrseor (self.prosses_queue)
except socket.error as e:
logger.log("[ERROR]" + str(e))
clients.remove(self)
self.queue.append("kill")
break
# if a jason that doesent work is sent, whch can be caused by a client cosing the connection
# an errorin the splitinng of parts, or an error in the client
# the error will be logged, then the client will be removed from thee clients list
# and a command will be sent to kill the queue porrseor (self.prosses_queue)
except json.decoder.JSONDecodeError as e:
logger.log("[ERROR]" + str(e) + " " + str(msg))
clients.remove(self)
self.queue.append("kill")
break
def process_queue(self):
global logger
logger.log("client queue")
while True:
if "kill" in self.queue:
break
if not len(self.queue) == 0:
msg_info = self.queue[0]
logger.log(f"recived: {msg_info} {type(msg_info)}")
if msg_info["action"] == "REGISTER":
user_actions.register_user(msg_info, self)
elif msg_info["action"] == "POST":
user_actions.new_post(msg_info, self)
elif msg_info["action"] == "GET POSTS":
user_actions.get_posts(msg_info, self)
elif msg_info["action"] == "GET USER":
user_actions.get_user_info(msg_info, self)
elif msg_info["action"] == "GET USERS":
user_actions.get_users_info(msg_info, self)
elif msg_info["action"] == "GET POST":
user_actions.get_post(msg_info, self)
elif msg_info["action"] == "GET IPS":
user_actions.get_ips(msg_info, self)
elif msg_info["action"] == "UPDATE PROFILE PICTURE":
user_actions.change_profile_picture(msg_info, self)
elif msg_info["action"] == "UPDATE INFO":
user_actions.change_info(msg_info, self)
elif msg_info["action"] == "UPDATE POS":
user_actions.update_pos(msg_info, self)
elif msg_info["action"] == "SEND":
self.send(msg_info["msg"])
self.queue.pop(0)
def recv_from_queue(self):
global logger
while True:
if not len(self.responses) == 0:
res = self.responses[0]
self.responses.pop(0)
return res
def recv_send_response(self, msg_id:str):
global logger
while True:
if not len(self.send_responses) == 0:
for i, response in enumerate(self.send_responses):
if response["id"] == msg_id:
res = response["response"]
self.send_responses.pop(i)
return res
def send(self, msg:str):
global logger
bmsg = msg.encode("utf-8")
logger.log("sending: "+msg)
lenghth = str(len(bmsg))
lenghth = ("0"*(8-len(lenghth))+lenghth).encode("utf-8")
bmsg = lenghth+bmsg
logger.log("sent", bmsg)
self.connection.send(bmsg)
class NodeConnection():
def __init__(self, connection:socket.socket, conn_info:dict, address:str):
# setsup a the object of the client connection
# takes a connection(connection), a socket.socket object, and the information of the connection(conn_info), a dict
# annd inialitzes all the needed variables
# also sstarts a thread to proses the queue(self.process_queue)
self.connection = connection
self.info = conn_info
self.queue = []
self.responses = []
self.send_responses = []
self.temp_msgs = {}
self.ip = self.info["ip"]
self.real_ip = address
self.read = 0
self.size = 0
self.message = ""
global logger
logger.log("connected by", self.ip)
thread = threading.Thread(target=self.process_queue)
thread.start()
def manage_requests(self):
# function thaat manages requests
# when the client seends a message, the message is prossesed by this function
# in this function the messages are reassembled and sent to the corresponding queue
global connections, logger
while True:
try:
msg = self.connection.recv(1024).decode("utf-8")
logger.log(msg)
if msg == "":
raise socket.error
for char in msg:
# The first 8 bytes of a message indicate the size of it
# This calculates the size of the message
if self.read < 8:
self.size += int(char)*(10**(7-self.read))
else:
self.message += char
if self.read == self.size+7 and not self.read == 0:
self.read = 0
self.size = 0
message_dict:dict = json.loads(self.message)
if message_dict["type"] == "ACTION":
self.queue.append(message_dict)
elif message_dict["type"] == "RESPONSE":
self.responses.append(message_dict)
self.message = ""
else:
self.read += 1
except socket.error as e:
logger.log("[ERROR]" + str(e))
connections.remove(self)
self.queue.append("kill")
break
except json.decoder.JSONDecodeError as e:
logger.log("[ERROR]" + str(e) + " " + str(msg))
connections.remove(self)
self.queue.append("kill")
break
def process_queue(self):
global logger
while True:
if "kill" in self.queue:
break
if not len(self.queue) == 0:
msg_info = self.queue[0]
logger.log(f"recived: {msg_info} {type(msg_info)}")
if msg_info["action"] == "IP":
managment.manage_ip(msg_info, self.ip)
if msg_info["action"] == "REGISTER":
user_actions.register_user(msg_info, self, ip=self.ip)
if msg_info["action"] == "POST":
user_actions.new_post(msg_info, self, ip=self.ip)
elif msg_info["action"] == "UPDATE PROFILE PICTURE":
user_actions.change_profile_picture(msg_info, self, ip=self.ip)
elif msg_info["action"] == "UPDATE INFO":
user_actions.change_info(msg_info, self, ip=self.ip)
elif msg_info["action"] == "UPDATE POS":
user_actions.update_pos(msg_info, self, ip=self.ip)
elif msg_info["action"] == "EXPORT":
managment.export_db(self)
elif msg_info["action"] == "IMPORT":
managment.import_db(self)
if msg_info["action"] == "SEND":
self.send(msg_info["msg"])
n_connected = len(connections)
n_nodes = len(db.querry("SELECT * FROM ips;"))
n_suposed_connections = managment.get_suposed_connected(n_nodes)
if n_connected < n_suposed_connections:
thread = threading.Thread(target=managment.connect_to_new_node)
thread.start()
self.queue.pop(0)
def recv_from_queue(self):
global logger
while True:
if not len(self.responses) == 0:
res = self.responses[0]
self.responses.pop(0)
return res
def recv_send_response(self, msg_id:str):
global logger
while True:
if not len(self.send_responses) == 0:
for i, response in enumerate(self.send_responses):
if response["id"] == msg_id:
res = response["response"]
self.send_responses.pop(i)
return res
def recv_from_queue(self):
while True:
if not len(self.responses) == 0:
res = self.responses[0]
self.responses.pop(0)
return res
def recv_send_response(self, msg_id:str):
global logger
while True:
if not len(self.send_responses) == 0:
for i, response in enumerate(self.send_responses):
if response["id"] == msg_id:
res = response["response"]
self.send_responses.pop(i)
return res
def send(self, msg:str):
global logger
bmsg = msg.encode("utf-8")
logger.log("sending: "+msg)
lenghth = str(len(bmsg))
lenghth = ("0"*(8-len(lenghth))+lenghth).encode("utf-8")
bmsg = lenghth+bmsg
logger.log("sent", bmsg)
self.connection.send(bmsg)
def send(self, msg:str):
global logger
logger.log("sending: "+msg)
msg_len = len(msg)
msg_id = SHA256.new(msg.encode("utf-8")).hexdigest()
num = int(msg_len/512)
num = num + 1 if not msg_len % 512 == 0 else num
send_msg = "{"+f'"type": "NUM", "num": {num}, "id": "{msg_id}"'+"}"
temp = self.connection.send(send_msg.encode("utf-8"))
temp = self.recv_send_response(msg_id)
if not temp == "OK":
logger.log("S1" + str(temp))
for i in range(num):
msg_part = msg[512*i:512*i+512].replace("\"", '\\"')
send_msg = "{"+f'"type": "MSG PART", "id": "{msg_id}", "content": "{msg_part}"'+"}"
self.connection.send(send_msg.encode("utf-8"))
temp = self.recv_send_response(msg_id)
if not temp == "OK":
logger.log("S2" + str(temp))
def init(get_logger:log.Logger, get_clients:list, get_connections:list, get_db:database.Database):
# sets global variables
# logger(log.Logger),
# clients(list, which contains conn.ClientConnection),
# connections(list, which contains conn.NodeConnections) and
# db(database.Database)
global logger, clients, connections, db
logger = get_logger
clients = get_clients
connections = get_connections
db = get_db
logger = log.Logger(None, real=False)
clients = []
connections = []
db = database.Database(real=False)
import user_actions