-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
101 lines (86 loc) · 2.98 KB
/
server.py
File metadata and controls
101 lines (86 loc) · 2.98 KB
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
import socket
import sys
import random
import string
import os
from utils import send_all, receive_folders, receive_changes, send_event, readline
# get new id for a new computer
def get_computer_id(map_clients):
id = ''
flag = True
while flag:
id = ''.join(random.choice(string.digits) for i in range(7))
flag = False
for client_changes in map_clients.values():
if id in client_changes.keys():
flag = True
break
return id
# get new id for a new client
def get_client_id():
key = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(128))
while os.path.isdir(key):
key = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(128))
return key
if __name__ == "__main__":
name, port = sys.argv
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('', int(port)))
server.listen(3)
map_key_of_map_client_and_changes = {}
computer_id = 0
key = ''
while True:
client_socket, client_address = server.accept()
try:
data = client_socket.recv(129).decode()
# new client
if data == 'Hi':
# rand a computer id and client id and send it to the client
computer_id = get_computer_id(map_key_of_map_client_and_changes)
key = get_client_id()
map_key_of_map_client_and_changes[key] = {computer_id: None}
print(key)
client_socket.send(computer_id.encode())
client_socket.send(key.encode())
os.makedirs(key, exist_ok=True)
# receive from the client the back-up folder
try:
receive_folders(client_socket, key)
except:
pass
else:
# new computer of an existing client
if data[0] == 'n':
if data[1:] not in map_key_of_map_client_and_changes.keys():
server.close()
break
# rand a computer id
computer_id = get_computer_id(map_key_of_map_client_and_changes)
map_key_of_map_client_and_changes[data[1:]][computer_id] = None
# send the computer its new id
try:
client_socket.send(computer_id.encode())
send_all(client_socket, data[1:])
except:
pass
# existing computer
else:
computer_id = client_socket.recv(7).decode()
# send the changes in the back-up folder of the client account
if map_key_of_map_client_and_changes[data[1:]][computer_id] is not None:
client_socket.send('updates from another computer'.encode() + b'\n')
client_socket.send(str(len(map_key_of_map_client_and_changes[data[1:]][computer_id])).encode() + b'\n')
for event in map_key_of_map_client_and_changes[data[1:]][computer_id]:
send_event(event[2], client_socket, data[1:], event[0], event[1])
map_key_of_map_client_and_changes[data[1:]][computer_id] = None
else:
client_socket.send('receive changes from client'.encode() + b'\n')
# receive the changes in the back-up folder of the client
try:
size = int(readline(client_socket))
receive_changes(client_socket, data[1:], size, map_key_of_map_client_and_changes, computer_id)
except:
pass
except:
pass