This repository has been archived by the owner on Feb 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscsynctest.py
executable file
·186 lines (143 loc) · 4.78 KB
/
scsynctest.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
#!/usr/bin/env python3
# coding: utf-8
"""
scsynctest.module
~~~~~~~~~~~~~
Tests for the assignemnt 3.
:copyright: 2018 Andreas Ebner, Julien Schmidt, Paul Schmiedmayer
:license: TBD
"""
import argparse
import logging
import os
import sys
import shutil
import copy
import threading
import asyncio
import time
import client
import server
# Client: scsync [-h <hostname|ip-addr>] [-p <port>] [-f <directory-path>]
# Server: scsync [-s] [-p <port>]
def create_or_clear(direcotry):
if not os.path.exists(direcotry):
os.makedirs(direcotry)
else:
for file in os.listdir(direcotry):
file_path = os.path.join(direcotry, file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path): shutil.rmtree(file_path)
except Exception as e:
print(e)
# Create a file with a given size in MB
def create_file(size, dir):
with open(dir + '.temp/' + str(size) + 'MB.txt', 'wb') as file:
file.write(os.urandom(1000 * 1000 * size))
file.flush()
os.fsync(file)
os.rename(dir + '.temp/' + str(size) + 'MB.txt', dir + str(size) + 'MB.txt')
# UNIX Signals
def signal(self, signame) -> None:
"""
UNIX signal handler.
"""
print("Got signal %s: exit" % signame)
self.stop()
def main():
"""
Initializes the program by parsing the command-line flags and either running
the client or the server module.
"""
if sys.platform == 'win32':
print("Windows is currently not supported.")
sys.exit(1)
# parse flags
parser = argparse.ArgumentParser(description='Cloud Sync', conflict_handler='resolve')
parser.add_argument('-f', dest='path', action='store',
default=os.getcwd(), help='directory to store the emporaty files')
group = parser.add_mutually_exclusive_group(required=False)
group.add_argument('--verbose', dest='verbose', action='store_true',
default=False, help='verbose output')
group.add_argument('--debug', dest='debug', action='store_true',
default=False, help='verbose debug output')
args = parser.parse_args()
# normalize path
args.path = os.path.abspath(args.path) + '/'
# Logging Modes
level = logging.WARNING
if args.verbose:
level = logging.INFO
elif args.debug:
level = logging.DEBUG
logging.basicConfig(format='[%(levelname)s] %(message)s', level=level)
# Create Dirs
server_dir = args.path + 'server/'
client_one_dir = args.path + 'client1/'
# client_two_dir = args.path + 'client2/'
create_or_clear(server_dir)
create_or_clear(client_one_dir)
create_or_clear(client_one_dir + '.temp')
# create_or_clear(client_two_dir)
args.test = True
args.server = True
args.port = 5000
args.host = 'localhost'
args.cc = 2000
args.chunk_size = 8192
# Start Server
def startServer(args):
args.path = server_dir
logging.info("Start Server 1")
new_event_loop = asyncio.new_event_loop()
asyncio.set_event_loop(new_event_loop)
server.run(args)
server_thread = threading.Thread(target=startServer, args=(copy.deepcopy(args), ))
server_thread.start()
time.sleep(2)
# Start Client 1
def startClient1(args):
args.path = client_one_dir
args.user = 'testuser'
args.password = 'testpassword'
args.server = False
logging.info("Start Client 1")
new_event_loop = asyncio.new_event_loop()
asyncio.set_event_loop(new_event_loop)
client.run(args)
client_1_thread = threading.Thread(target=startClient1, args=(copy.deepcopy(args),))
client_1_thread.start()
time.sleep(1)
# Uncomment to test with second client:
# # Start Client 2
# def startClient2(args):
# args.path = client_two_dir
# args.user = 'testuser'
# args.password = 'testpassword'
# args.server = False
#
# logging.info("Start Client 2")
#
# new_event_loop = asyncio.new_event_loop()
# asyncio.set_event_loop(new_event_loop)
# client.run(args)
#
# client_2_thread = threading.Thread(target=startClient2, args=(copy.deepcopy(args),))
# client_2_thread.start()
create_file(5, client_one_dir)
# TODO: Only start next upload when previous is complete
# TODO: Measure how much data is end and what the overhead is
time.sleep(2)
create_file(50, client_one_dir)
time.sleep(6)
create_file(100, client_one_dir)
time.sleep(11)
create_file(200, client_one_dir)
time.sleep(22)
create_file(500, client_one_dir)
time.sleep(55)
create_file(1000, client_one_dir)
if __name__ == "__main__":
main()