-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplications.py
435 lines (336 loc) · 12 KB
/
applications.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# system packages
import socket
import threading
import sys
import os
import json
import uuid
# custom modules
from .module.module import check_length, check_path, load_file, save_file
from .module.exceptions import ServerAuthenticationError
from .module.models import MessageModel
from .module.database import Database
class Server():
def __init__( self, ip:str, port:int):
self.IP = ip
self.PORT = port
self.client_ids: list[str] = []
self.client_list : list[socket] = []
self.message_list:dict = {}
self.app_list:dict = {}
self.APPNAME:str = None
self.APPID:str = None
self.MINIMUM_SIZE:int = 4
self.MAX_SIZE:int = (1024*100)
self.ENCODING:str = 'ascii'
self.DATAFILE:str = os.getcwd()+"/src/data.json"
self.APPFILE:str = os.getcwd()+"/src/app.json"
self.getInfo()
self.model : MessageModel = None
self.database = Database()
def start(self):
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.bind((self.IP, self.PORT))
self.server.listen()
if self.model == None:
raise AttributeError("messageModel is 'NoneType'. Set using 'objectName.setModel()'")
if self.APPNAME == None:
raise AttributeError("APP Name is 'NoneType'. Set Using 'objectName.generateID()'")
if self.APPID == None:
raise AttributeError("APP Id is 'NoneType'. Set Using 'objectName.generateID()'")
self.show_info()
listen_thread = threading.Thread(target=lambda:self.listener(), daemon =True)
listen_thread.start()
self.commands(self.server)
def generateID(self, app_name: str) -> str:
# Remove Space
app_name = app_name.replace(" ","")
if app_name == "":
raise AttributeError("App Name is 'NoneType'")
if app_name in self.app_list.keys():
raise KeyError("App Name {app_name} Already Exits!")
id = str(uuid.uuid4())
self.APPNAME = app_name
self.APPID = id
data = { 'app_name': self.APPNAME, 'app_id' : self.APPID }
save_file(self.DATAFILE, data)
#self.app_list[app_name] = id
#save_file(self.APPFILE, self.app_list)
return id
def setModel(self, messagemodel:MessageModel) -> None:
self.model = messagemodel
#text = f""" CREATE TABEL IF NOT EXITS {tabel_name} VALUES ({}, {})"""
def show_info(self):
print(f" Server Ip >> {self.IP} \n Server Port >> {self.PORT} \n App Name >> {self.APPNAME} \n APP ID >> {self.APPID}")
def getInfo(self) -> dict:
result = load_file(self.DATAFILE)
if type(result) == str:
return result
if not os.path.exists(os.getcwd()+'./src/'):
os.makedir(os.getcwd()+'./src/')
self.APPID = result['app_id']
self.APPNAME = result['app_name']
return result
def send_msg(self, id:str, message):
client = None
try:
if id in self.client_ids:
client_index = self.client_ids.index(id)
client = self.client_list[client_index]
except IndexError or KeyError:
print("Index-Error! ? Key-Error!")
return
if client == None:
print(f"User {id} Not Found")
return
try:
message_bytes = json.dumps(message).encode(self.ENCODING)
#print(f"Send Message to {id} -> {message_bytes}")
length =check_length(message_bytes)
client.send(length.encode(self.ENCODING))
client.send(message_bytes)
except ConnectionResetError:
self.client_ids.remove(id)
self.client_list.remove(client)
print(client, "Removed !")
def receive_message(self, client, id):
while True:
try:
length = int(client.recv(self.MINIMUM_SIZE).decode(self.ENCODING))
message = client.recv(length)
message_dict = json.loads(message.decode(self.ENCODING))
#id = message_dict['id']
print(message_dict)
result = self.database.addMessageDict(message_dict, self.model)
if result == "success":
print("added")
else:
print(result)
except ConnectionResetError:
client.close()
break
except Exception as e:
print(f"Error >>> {e} ")
self.check_message(message_dict['to_id'])
def check_message(self, id:str):
msg = []
if not id:
return
print(f"_{id}_")
print(f"Checking any Message received for {id}")
message_list = self.database.getMessages(id)
if type(message_list) == str:
if message_list.find("no such table") < 0:
print(f"No Message for {id}")
else:
print(message_list)
return
if len(message_list) == 0:
return
print(f"Message Found for {id} \n message -> {message_list}")
for message in message_list:
message = list(message)
m = (message[0], message[2])
message.pop(0)
print(message)
print(f" ")
try:
self.send_msg(id, message)
msg.append(m)
print(f"Message >>> {message}")
except Exception as e:
print(e)
try:
for i in msg:
print(i)
print(self.database.removeMessage(i[1], i[0]))
print(f"Message {i} Removed! ")
except KeyError:
print(f"User {id} Not Online")
def listener(self):
while True:
print("\n Waiting For New Connection.....")
client, addrs = self.server.accept()
print(f"\n New Client ({addrs}) is Connected ")
self.manage(client, addrs)
def manage(self, client, addrs):
try:
auth = client.recv(self.MAX_SIZE).decode(self.ENCODING)
if auth:
auth = auth.split(':')
if not auth[0] == self.APPNAME or not auth[1] == self.APPID:
client.send("NotAuth".encode(self.ENCODING))
client.close()
return
else:
client.send("OK".encode(self.ENCODING))
id = client.recv(self.MAX_SIZE).decode(self.ENCODING)
if not id in self.client_ids:
print(f"USer {id} is Newly Added")
self.client_ids.append(id)
self.client_list.insert(self.client_ids.index(id), client)
else:
print(f"USer {id} is Already Exits")
index = self.client_ids.index(id)
self.client_list[index] = client
#client.send("Already".encode(ENCODING))
client.send("OK".encode(self.ENCODING))
chaeck_thread = threading.Thread(target=lambda:self.check_message(id), daemon = True)
receive_thread = threading.Thread(target= lambda:self.receive_message(client, id), daemon = True)
chaeck_thread.start()
receive_thread.start()
except ConnectionResetError:
print(f"{client} is Connection Closed!")
return
print(f"{client} is DisConnected")
def commands(self, server):
while True:
text = input("\n /Server:")
text = text.lower()
match text:
case "":
pass
case "close" :
print("Closing Server...")
server.close()
server = None
break
sys.exit()
print("Closed !!")
case "list id":
for id in self.client_ids:
print(id)
case "list client":
for client in self.client_list:
print(client)
case "list":
print("list client | list id")
case "view":
print(self.message_list)
case "view data":
for id in self.message_list:
print(self.message_list[id])
case "view message":
for id in self.message_list:
print(id)
for message in self.message_list[id]:
print(message)
case "view keys":
for key in self.message_list.keys():
print(key)
case default:
print(f" Invalid Command {text}")
class Client():
def __init__( self, ip:str, port:int):
# Variables
self.IP:str = ip
self.PORT:int = port
self.MAX_SIZE:int = (1024*100)
self.MINIMUM_SIZE:int = 4
self.ENCODING:str = 'ascii'
self.APPID:str = None
self.APPNAME:str = None
self.ID:str = None
# Objects
self.server = ""
self.receiver = CustomSignal()
self.model : MessageModel = None
def start( self) -> None:
if self.APPID == None:
raise AttributeError("App Id is 'NoneType'. To set app id use Object(Client).setAppId()")
if self.APPNAME == None:
raise AttributeError("App Name is 'NoneType'. To set app id use Object(Client).setAppName()")
if self.ID == None:
raise AttributeError(" Id is 'NoneType'. To set app id use Object(Client).setId()")
if self.model == None:
raise AttributeError( "MessageModel is 'NoneType'. to set message model use objectName(Client).setModel()")
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
self.server.connect((self.IP, self.PORT))
except ConnectionRefusedError:
#print(f" Server {self.IP}:{self.PORT} is Not Found")
raise ConnectionRefusedError(f" Server {self.IP}:{self.PORT} is Not Found")
receive_thread = threading.Thread(target=lambda:self.receive_message(), daemon= True)
# client sending id+name for verification you can set id, name by ObjectName.setAppId, ObjectName.setAppName
self.server.send((self.APPNAME+":"+self.APPID).encode(self.ENCODING))
respond = self.server.recv(self.MAX_SIZE).decode(self.ENCODING)
if not respond == "OK":
print("Respond -> ",respond)
match respond:
case "NotAuth":
raise ServerAuthenticationError("Authentication Failed!")
case "Not":
raise Exception("")
#raise Exception("Error >>> ",respond)
self.server.send(self.ID.encode(self.ENCODING))
response = self.server.recv(self.MAX_SIZE).decode(self.ENCODING)
if not response == 'OK':
raise ConnectionRefusedError(f"Response is {response}!")
receive_thread.start()
def sendMessage(self, message:MessageModel) -> None:
msg = message.getVariables()
reponse = self.send_message(msg)
if reponse == "Closed":
raise ConnectionResetError(f" Server {self.IP}:{self.PORT} is Closed! ")
return True
def setAppName(self, name:str) -> None:
self.APPNAME = name
def setAppId(self, id) -> None:
self.APPID = id
def setId(self, id) -> None:
if id == None:
raise AttributeError("Enter Value Id is 'NoneType'")
id.replace(" ", "")
if id == "":
raise AttributeError("Id is Empty")
self.ID = id
def setModel(self, messagemodel:MessageModel) -> None:
self.model = messagemodel
def check_message(self, message:dict) -> dict:
if message['id'] == None:
raise TypeError("'Id' Is None!")
if message['to_id'] == None:
raise TypeError("'To' is None!")
return message
def load_model(self, message:list) -> MessageModel:
mod = self.model()
mod.setVariables( message)
return mod
def send_message(self, message) -> str:
message = self.check_message(message)
if message == None:
raise NoneType("Message Not Send")
message_bytes = json.dumps(message).encode(self.ENCODING)
try:
length = check_length(message_bytes)
if int(length) > self.MAX_SIZE:
raise ValueError(f"message Size is To Large, Max Messsage Size is {self.MAX_SIZE} in bytes")
self.server.send(length.encode(self.ENCODING))
self.server.send(message_bytes)
return "Ok"
except ConnectionResetError:
print('Server Connection is Closed!')
return "Closed"
def receive_message(self) -> None:
while True:
try:
print('Waiting For Receive.....')
length = int(self.server.recv(self.MINIMUM_SIZE).decode(self.ENCODING))
message = self.server.recv(length)
#print("Received Bytes >>",message)
message_dict = json.loads(message.decode(self.ENCODING))
#print(f"Message >> {message_dict}")
self.receiver.emit(self.load_model(message_dict))
except ConnectionResetError:
print(f" Server {self.IP}:{self.PORT} Connection is Closed! ")
break
class CustomSignal():
def __init__(self):
self.num : int
self.trigger_function = None
def emit(self, text):
if self.trigger_function == None:
return
self.trigger_function(text)
def connect(self, func):
self.trigger_function = func