-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
162 lines (132 loc) · 5.21 KB
/
Copy pathclient.py
File metadata and controls
162 lines (132 loc) · 5.21 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
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
import socket
import sys
import time
import os
from utils import send_all, receive_folders, send_event, readline, receive_changes
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
chunk = 1000000
event_list = list()
# notify about creating a new file in the back-up folder
def on_created(event):
if ('.goutputstream' not in event.src_path) and ('.swp' not in event.src_path):
event_list.append((event.src_path, '', 'created'))
# notify about deleting a file in the back-up folder
def on_deleted(event):
if ('.goutputstream' not in event.src_path) and ('.swp' not in event.src_path):
for e in reversed(event_list):
if e[2] == 'modified' and e[0] == event.src_path:
event_list.remove(e)
if e[2] == 'created' and e[0] == event.src_path:
event_list.remove(e)
return
event_list.append((event.src_path, '', 'deleted'))
# notify about modify a file in the back-up folder
def on_modified(event):
if (not os.path.isdir(event.src_path)) and ('.goutputstream' not in event.src_path) \
and ('.swp' not in event.src_path):
event_list.append((event.src_path, '', 'modified'))
# notify about move a file in the back-up folder
def on_moved(event):
if '.goutputstream' in event.src_path:
event_list.append((event.dest_path, '', 'modified'))
else:
length = len(event_list)
for i in range(length):
if event_list[length - i - 1][2] == 'modified' and event_list[length - i - 1][0] == event.src_path:
l = list(event_list[length - i - 1])
l[0] = event.dest_path
event_list[length - i - 1] = tuple(l)
if event_list[length - i - 1][2] == 'created' and event_list[length - i - 1][0] == event.src_path:
l = list(event_list[length - i - 1])
l[0] = event.dest_path
event_list[length - i - 1] = tuple(l)
return
event_list.append((event.src_path, event.dest_path, 'moved'))
# connect the computer in the first time to the server of an existing client
def first_connection_new_computer(s, arguments):
key = arguments[5]
s.send(b'n' + key.encode())
try:
computer_id = s.recv(7).decode()
receive_folders(s, arguments[3])
except:
computer_id = ''
return key, computer_id
# connect the client in the first time to the server
def first_connection_new_client(s, arguments):
s.send('Hi'.encode())
computer_id = s.recv(7).decode()
key = s.recv(128).decode()
if not os.path.isdir(arguments[3]):
os.makedirs(arguments[3], exist_ok=True)
try:
send_all(s, arguments[3])
except:
pass
return key, computer_id
# get new socket for the client
def get_socket(ip, port):
sc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sc.connect((ip, int(port)))
return sc
if __name__ == "__main__":
# get the arguments of the client
ip = sys.argv[1]
port = sys.argv[2]
directory = sys.argv[3]
time_sleep = sys.argv[4]
s = get_socket(ip, port)
# connect the client to the server
if len(sys.argv) > 5:
key, computer_id = first_connection_new_computer(s, sys.argv)
else:
key, computer_id = first_connection_new_client(s, sys.argv)
# define the watchdog functions
my_event = PatternMatchingEventHandler(["*"], None, False, True)
my_event.on_created = on_created
my_event.on_deleted = on_deleted
my_event.on_modified = on_modified
my_event.on_moved = on_moved
# define an observer for the changes in the back-up directory of the client
my_observer = Observer()
my_observer.schedule(my_event, directory, recursive=True)
my_observer.start()
try:
while True:
# disconnect the client and go to sleep
s.close()
time.sleep(int(time_sleep))
s = get_socket(ip, port)
option = ""
try:
# connect the client again
s.send(b'o' + key.encode())
s.send(computer_id.encode())
option = readline(s)
except:
pass
# get new update from the server
if option == 'updates from another computer':
my_observer.stop()
try:
size = int(readline(s))
receive_changes(s, directory, size)
except:
pass
# define an observer for the changes in the back-up directory of the client
my_observer = Observer()
my_observer.schedule(my_event, directory, recursive=True)
my_observer.start()
# send new changes in the back-up folder of the client in this computer
try:
s.send(str(len(event_list)).encode() + b'\n')
copy_event_list = event_list.copy()
for event in copy_event_list:
send_event(event[2], s, directory, event[0], event[1])
event_list.remove(event)
except:
pass
except KeyboardInterrupt:
my_observer.stop()
my_observer.join()