-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.py
274 lines (239 loc) · 9.35 KB
/
client.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
import uuid
import datetime
import logging
import json
from flask import Flask, flash, request, redirect, url_for, render_template_string, send_file
from pathlib import Path
import os
import grpc
import dataverse_pb2 as service
import dataverse_pb2_grpc as rpc
from google.protobuf.json_format import MessageToJson
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg',
'.gif', 'mp4', '.mp4', 'mp3', '.mp3'}
CHUNK_SIZE = 1024 * 1024 # decrease the value here to evaluate memory usage
fileName_g = None
app = Flask(__name__)
logging.basicConfig(level=logging.INFO)
#stub = None
stub = None
channel = None
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.after_request
def add_header(r):
"""
"""
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
return r
@app.template_filter('json_loads')
def json_loads_filter(s):
return json.loads(s) if s else None
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
if 'selected_files' not in request.files:
flash("No 'selected_files' part")
return redirect(request.url)
selected_files = request.files.getlist("selected_files")
results = []
uname = request.form["username"]
IP = request.form["IP"]
PORT = request.form["PORT"]
connectTo(IP, PORT)
for i, file in enumerate(selected_files, 1):
if file.filename == '':
flash('You must select at least one file')
return redirect(request.url)
if file and allowed_file(file.filename):
fileid = f'{datetime.datetime.utcnow().isoformat()}-{uuid.uuid4()}'
def upload_request_generator(): # this generates our grpc `stream ImageUploadRequest`
i = 1
print(file)
# username = request.form.get("username")
# logging.info(f'{request}')
while True:
b = file.read(CHUNK_SIZE)
if b:
result = service.ImageUploadRequest(Content=b, Id=file.filename, StatusCode=service.ImageUploadStatusCode.InProgress, Username=uname) # noqa
else:
result = service.ImageUploadRequest(Id=file.filename, StatusCode=service.ImageUploadStatusCode.Ok, Username=uname) # noqa
yield result
if not b:
file.seek(0)
break
global stub
output_result = stub.Upload(upload_request_generator())
if output_result.nodeConnections:
arr = output_result.nodeConnections[0].split(":")
connectTo(arr[0], arr[1])
replicate = stub.Upload(upload_request_generator())
logging.info(f'file {i} {file.name} was upload successfully')
results.append(MessageToJson(output_result))
# we need a safe string to pass as url param
return redirect(url_for('upload_file', json=json.dumps(results)))
return render_template_string('''
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data>
<h3>Server</h3>
IPv4 Address <input type=text name=IP required><br><br>
Port Number <input type=text name=PORT required><br><br>
<input type=file name=selected_files multiple required>
<input type=text name=username required>
<input type=submit value=Upload>
</form>
{% if json_response %}
<h1>Last upload</h1>
<ol>
{% for item in (json_response|json_loads) %}
<li>
{{ (item|json_loads) }}
</li>
{% endfor %}
</ol>
{% endif %}
''', json_response=request.args.get('json'))
@app.route('/download')
def download_file():
global fileName_g
path = "./downloadsTemp/"+fileName_g
print("FILENAME----------------------------------")
print(fileName_g)
file = send_file(path, as_attachment=True)
os.remove(path)
return file
@app.route('/search', methods=['GET', 'POST'])
def search_file():
if request.method == 'POST':
results = []
IP = request.form["IP"]
PORT = request.form["PORT"]
uname = request.form["username"]
fname = request.form["filename"]
logging.info(f' Params Uname {fname} | Fname {fname}')
# connectTo(IP,PORT)
global stub
# searchlogic
visited = [] # List to keep track of visited nodes.
queue = [] # Initialize a queue
queue.append(IP+':'+PORT)
while queue:
size = len(queue)
for i in range(size):
s = queue.pop(0)
print(s, end=" ")
arr = s.split(":")
connectTo(arr[0], arr[1])
result = stub.Search(service.SearchRequest(
Filename=fname, Username=uname))
results.append(MessageToJson(result))
visited.append(s)
print(result)
if result.found == "YES":
if(arr[0] != IP):
path = "./downloadsTemp"
Path(path).mkdir(parents=True, exist_ok=True)
f = open('./downloadsTemp/'+fname, 'wb')
f.write(result.Content)
f.close()
img = None
connectTo(IP, PORT)
stub.Relocate(service.RelocateRequest(Content=result.Content, Filename=fname, Username=uname))
print("HERE ", result.found)
break
for node in result.nodeConnections:
if node not in visited:
queue.append(node)
logging.info(f' Params Uname {uname} | Fname {fname}')
# results.append(MessageToJson(result))
t = json.loads(results[len(results) - 1])
# print("187", t)
global fileName_g
fileName_g = fname
# print("----------------")
# print(t['File'])
# print("----------------")
path = "./downloadsTemp"
Path(path).mkdir(parents=True, exist_ok=True)
f = open('./downloadsTemp/'+fname, 'wb')
f.write(result.Content)
f.close()
# we need a safe string to pass as url param
return redirect(url_for('search_file', json=json.dumps("{'bar': ('baz', None, 1.0, 2)}")))
return render_template_string('''
<!doctype html>
<title>Search File</title>
<h1>Search File</h1>
<form method=post>
<h3>Server </h3>
IPv4 Address <input type=text name=IP required><br><br>
Port Number <input type=text name=PORT required><br><br>
Username <input type=text name=username required><br><br>
Filename <input type=text name=filename required><br><br>
<input type=submit value=Search>
</form>
<a href = "/download">Download</a>
''', json_response="dummy")
@app.route('/config', methods=['GET', 'POST'])
def config():
if request.method == 'POST':
results = []
IP1 = request.form["IP1"]
PORT1 = request.form["PORT1"]
IP2 = request.form["IP2"]
PORT2 = request.form["PORT2"]
connectTo(IP1, PORT1)
global stub
result1 = stub.Config(service.ConfigRequest(Server=IP2+':'+PORT2))
connectTo(IP2, PORT2)
result2 = stub.Config(service.ConfigRequest(Server=IP1+':'+PORT1))
logging.info(f' Server1 Params IP {IP1} | PORT {PORT1}')
logging.info(f' Server2 Params IP {IP2} | PORT {PORT2}')
results.append(MessageToJson(result1))
results.append(MessageToJson(result2))
print("----------------")
print(json.dumps(results))
print("----------------")
# we need a safe string to pass as url param
return redirect(url_for('config', json=json.dumps(results)))
return render_template_string('''
<!doctype html>
<title>Connect to a Node</title>
<h1>Connect to a Node</h1>
<form method=post>
<h3>Server 1 </h3>
IPv4 Address: <input type=text name=IP1 required><br><br>
Port Number: <input type=text name=PORT1 required><br>
<h3>Server 2 </h3>
IPv4 Address: <input type=text name=IP2 required><br><br>
Port Number: <input type=text name=PORT2 required><br><br>
<input type=submit value=Connect>
</form>
{% if json_response %}
<h1>Response from server</h1>
<ol>
{% for item in (json_response|json_loads) %}
<li>
{{ (item|json_loads) }}
</li>
{% endfor %}
</ol>
{% endif %}
''', json_response=request.args.get('json'))
def connectTo(serverip, serverport):
global channel
if channel != None:
print('Closed the existing channel!')
channel.close()
print('Creating Connection! ' + serverip+':'+serverport)
channel = grpc.insecure_channel(serverip+':'+serverport)
global stub
stub = rpc.GreeterStub(channel)
if __name__ == "__main__":
print('hellllllllllllllllladkjashdjhasjdkhasjdhjaskdhjk')
app.run(host='0.0.0.0', port=5001, debug=True)