-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainServer.py
88 lines (74 loc) · 2.68 KB
/
mainServer.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
import socket
import client
import task_card
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('192.168.1.76', 65535))
clients = []
cards = []
def del_card(card):
i = 0
for crd in cards:
flag = task_card.compare_cards(crd,card)
if flag == 0 or flag == 1:
del cards[i]
i+=1
def is_card_in_cards(card):
for crd in cards:
flag = task_card.compare_cards(crd, card)
if flag == 0:
return 0
elif flag == 1:
return 1
return 2
def get_client_addres(nikname):
for clt in clients:
if clt.is_in_group(nikname):
return clt.addres
return None
def is_new_client(addres):
for client1 in clients:
if client1.addres[0] == addres[0] and client1.addres[1] == addres[1]:
return False
return True
print ('Start Server')
while True:
data, addres = sock.recvfrom(1024)
content = data.decode('utf-8')
for clt in clients:
sock.sendto("success".encode('utf-8'), clt.addres)
#Авторизация на сервере
if content.startswith('autorize'):
if is_new_client(addres):
cl_new = client.Client()
cl_new.set_client(content[9:].split(','), addres)
clients.append(cl_new)
print("Client \""+cl_new.group[len(cl_new.group)-1]+"\" autorized succesfull!", addres)
sock.sendto("success".encode('utf-8'), addres)
#Перенаправление задач
if content.startswith('card'):
content = content[5:]
list_content = content.split(',')
nikname = list_content[0]
del list_content[0]
content = ''.join(list_content)
tc = task_card.convert_string_to_card(content)
flag = is_card_in_cards(tc)
if flag == 2:
cards.append(tc)
nik = tc.group
addres = get_client_addres(nik)
if addres:
sock.sendto(tc.convert_to_string().encode('utf-8'), addres)
print('Send message to ', addres, ' data = \"', tc.convert_to_string()[:10], '...\"')
else:
print('Failed to send data to '+nik)
elif flag == 1:
nik = tc.author
addres = get_client_addres(nik)
if addres:
sock.sendto(tc.convert_to_string().encode('utf-8'), addres)
print('Send message to ', addres, ' data = \"', tc.convert_to_string()[:10], '...\"')
if tc.status == 'ready':
del_card(tc)
else:
print('Failed to send data to ' + nik)