-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
323 lines (293 loc) · 12.4 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
from tornado.httpclient import HTTPRequest, AsyncHTTPClient
from tornado.simple_httpclient import SimpleAsyncHTTPClient
import tornado.ioloop
import tornado.web
import os,sys,re,time
import threading
from tornado import gen
from functools import partial
import urllib.parse
import mimetypes
import math
import uuid
from concurrent.futures import ThreadPoolExecutor
import tornado.iostream
from tornado.escape import utf8
from tornado.log import gen_log
from ctypes import *
import struct
import os
#import time,datat
readchunky = False
total_downloaded = 0
threadpool = ThreadPoolExecutor(1) # A thread for reading chunks from the file
DEBUG = False
class NoQueueTimeoutHTTPClient(SimpleAsyncHTTPClient):
def fetch_impl(self, request, callback):
key = object()
self.queue.append((key, request, callback))
self.waiting[key] = (request, callback, None)
self._process_queue()
if self.queue:
gen_log.debug("max_clients limit reached, request queued. %d active, %d queued requests." % (len(self.active), len(self.queue)))
class Client:
def _gen_boundary(self, file_size):
if file_size < 1024:
blen = 10
else:
blen = math.ceil(math.log(file_size, 2))
bcnt = max(blen / 32, 1)
return "".join([str(uuid.uuid1()).replace("-", "") for _ in range(bcnt)])
print("_gen_boundary file_size",file_size)
def put_stream(self, url, pos, size, filename, on_response, chunk_size=1024):
'''Uploads file to provided url.
:param url: URL to PUT the file data to
:param filename: Name of the file (Content-Disposition header)
:param file_size: Size of the file in bytes. Used to produce a Content-Size header from file_size.
:param on_response: See AsyncHTTPClient.fetch
:return: Future content value under the provided url as binary string.
'''
uploadpos = pos
#file_size = os.path.getsize(filename) # Be aware: this could also block! In production, you need to do this in a separated thread too!
file_size = size # Be aware: this could also block! In production, you need to do this in a separated thread too!
print("filesize in put_stream is :",file_size)
ext = os.path.splitext(filename)[1].lower()
if ext:
content_type = mimetypes.types_map.get(ext, "application/octet-stream")
else:
content_type = "application/octet-stream"
enc_filename = urllib.parse.quote(filename)
# http://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean/28380690#28380690
boundary = self._gen_boundary(file_size)
CRLF = '\r\n'
post_head = b''.join(map(utf8, [
'--', boundary, CRLF,
# See https://tools.ietf.org/html/rfc5987 section 3.2.2 examples
'Content-Disposition: form-data; name="file"; filename*=utf-8\'\'%s' % enc_filename, CRLF,
'Content-Type: ', content_type, CRLF,
'Content-Transfer-Encoding: binary', CRLF,
CRLF,
]))
post_tail = b''.join(map(utf8, [
CRLF, '--', boundary, '--', CRLF
]))
content_length = len(post_head) + int(file_size) + len(post_tail)
#print("content_length is:",content_length)
headers = {
'Content-Type': 'multipart/form-data; boundary=' + boundary,
'Content-Transfer-Encoding': 'binary',
'Content-Length': str(content_length),
}
@gen.coroutine
def body_producer(write):
if DEBUG:
sys.stdout.write(post_head.decode('ascii'))
write(post_head)
remaining = file_size
#print("remaining in body_produceris :",remaining)
with open(filename, "rb") as fileobj:
fileobj.seek(int(uploadpos))
while remaining > 0:
#print("uploadpos in while is :",int(uploadpos))
# data = yield threadpool.submit(fileobj.read(int(remaining)), chunk_size)
data = fileobj.read(int(remaining))
# print(str(data,encoding = "utf-8"))
if data:
remaining -= len(data)
print("len(data) in if is :",len(data),"uploadpos in while is :",int(uploadpos),"remaining in if is :",len(data))
#print("data in if is :",data)
#print("remaining in if is :",len(data))
if DEBUG:
sys.stdout.write(data.decode('utf-8'))
yield write(data)
else:
break
if DEBUG:
sys.stdout.write(post_tail.decode('ascii'))
write(post_tail)
request = tornado.httpclient.HTTPRequest(url=url, request_timeout=1200, method='POST', headers=headers,body_producer=body_producer)
print("before1:"+threading.current_thread().getName()+url)
return tornado.httpclient.AsyncHTTPClient().fetch(request, callback=on_response)
def geturlread(action,host,filepath,uid,gid,pos,size):
if action=="read":
url = "http://"+host+"/read?filepath="+filepath+"&uid="+uid+"&gid="+gid+"&pos="+str(pos)+"&size="+str(size)
print(url)
return url
def geturlupload(action,host,targetpath,pos,size,totalsize):
if action=="upload":
url = "http://"+host+"/upload?targetpath="+targetpath+"&pos="+str(pos)+"&size="+str(size)+"&totalsize="+str(totalsize)
print(url)
return url
'''def chunky(path,pos,totalsize,chunk):
#print("self._max_body_size",self._max_body_size)
print("chunk length",len(chunk))
global total_downloaded
global readchunky
if readchunky == False and os.path.exists(path):
os.remove(path)
readchunky = True
if not os.path.exists(path):
f = open(path,'w')
f.close()
f = open(path,'rb+')
chunklength = len(chunk)-8
pos,chunknew = struct.unpack('l%ds'%chunklength,chunk)
print("decode pos is",pos)
f.seek(pos)
f.write(chunknew)
print("after write position,chunksize",f.tell(),len(chunknew))
pos = pos+len(chunknew)
#f.flush()
if pos==totalsize:
f.close()
total_downloaded += len(chunknew)'''
# the OS blocks on file reads + writes -- beware how big the chunks is as it could effect things
def sizebwchunky(chunk):
global FILESIZE
FILESIZE = int(chunk)
@gen.coroutine
def sizebw(host,filepath):
url = "http://"+host+"/sizebw?filepath="+filepath
print(url)
request = HTTPRequest(url, streaming_callback=partial(sizebwchunky), request_timeout=300)
AsyncHTTPClient.configure('tornado.simple_httpclient.SimpleAsyncHTTPClient', max_body_size=1024*1024*1024)
http_client = AsyncHTTPClient(force_instance=True)
#AsyncHTTPClient.configure("tornado.curl_httpclient.CurlAsyncHTTPClient")
#http_client = AsyncHTTPClient()
response = yield http_client.fetch(request)
tornado.ioloop.IOLoop.instance().stop()
@gen.coroutine
def writer(host,filepath,targetdir,uid,gid,pos,size):
file_name = targetdir
path = file_name
chunk_size = 80*1024*1024
no = int(size) // chunk_size
i = 0
global total_downloaded
global readchunky
lib=cdll.LoadLibrary('./libpycall.so')
func=lib.update_bitmap
func.argtypes=(c_int,c_int,c_char_p,c_int)
#if readchunky == False and os.path.exists(path):
# os.remove(path)
# readchunky = True
#if not os.path.exists(path):
# f = open(path,'w')
# f.close()
while i<no:
request = HTTPRequest(geturlread("read",host,filepath,uid,gid,pos,str(chunk_size)),request_timeout=300)
pos = str(int(pos)+chunk_size)
i = i+1
http_client = AsyncHTTPClient()
response = yield http_client.fetch(request)
response = response.body
print("chunk length",len(response))
f = open(path,'rb+')
chunklength = len(response)-8
posnew,chunknew = struct.unpack('l%ds'%chunklength,response)
print("posnew is",posnew)
print("chunkleng",len(chunknew))
print("filesize",FILESIZE)
f.seek(posnew)
f.write(chunknew)
total_downloaded=total_downloaded+len(chunknew)
#print("after write position,chunksize",f.tell(),len(chunknew))
f.close()
func= lib.update_bitmap(int(posnew),int(len(chunknew)),filepath.encode("utf-8"),FILESIZE)
print ("finish")
print("total bytes downloaded was", total_downloaded)
if (int(size) % chunk_size) != 0:
last = int(size) % chunk_size
request = HTTPRequest(geturlread("read",host,filepath,uid,gid,pos,str(last)),request_timeout=300)
http_client = AsyncHTTPClient()
response = yield http_client.fetch(request)
response = response.body
#print("chunk length",len(response))
f = open(path,'rb+')
chunklength = len(response)-8
posnew,chunknew = struct.unpack('l%ds'%chunklength,response)
#print("posnew is",posnew)
f.seek(posnew)
f.write(chunknew)
total_downloaded=total_downloaded+len(chunknew)
#print("after write position,chunksize",f.tell(),len(chunknew))
f.close()
func= lib.update_bitmap(int(posnew),int(len(chunknew)),filepath.encode("utf-8"),FILESIZE)
print ("finish")
print("total bytes downloaded was", total_downloaded)
if total_downloaded==realsize:
tornado.ioloop.IOLoop.instance().stop()
''' lib=cdll.LoadLibrary('./libpycall.so')
print("start")
func=lib.update_bitmap
func.argtypes=(c_int,c_int,c_char_p,c_int)
func=lib.update_bitmap(int(pos),int(size),filepath.encode("utf-8"),FILESIZE)
print("finish")
'''
@gen.coroutine
def upload(host,filepath,targetpath,pos,size):
def on_response(request):
print("on_response:"+threading.current_thread().getName())
print("=============== GOT RESPONSE ======")
print(request.body.decode('ascii'))
#print(request.body)
print("===================================")
tornado.ioloop.IOLoop.current().stop() # Stop the loop when the upload is complete.
client = Client()
print("before"+threading.current_thread().getName())
#tornado.ioloop.IOLoop.instance().start()
yield client.put_stream(geturlupload("upload",host,targetpath,pos,size,os.path.getsize(filepath)), pos, size, filepath, on_response)
print("after:"+threading.current_thread().getName())
#tornado.ioloop.IOLoop.current().stop() # Stop the loop when the upload is complete.
def readentrance(host,filepath,targetdir,uid,gid,pos,size):
print(host,filepath,targetdir,uid,gid,pos,size)
sizebw(host,filepath)
tornado.ioloop.IOLoop.instance().start()
filesize = FILESIZE
streamno = 3
start_time = time.time()
global realsize
if(int(size)>=filesize):
realsize = filesize
streamsize = (filesize-int(pos)) // (streamno-1)
else:
realsize = int(size)
streamsize = (int(size)) // (streamno-1)
i = 0
threads = []
while i < (streamno-1):
threads.append(threading.Thread(target=writer,args=(host,filepath,targetdir,uid,gid,int(pos)+streamsize*i,streamsize)))
i=i+1
if (streamsize*i) < realsize:
threads.append(threading.Thread(target=writer,args=(host,filepath,targetdir,uid,gid,int(pos)+streamsize*i,realsize-streamsize*i)))
for t in threads:
t.setDaemon(True)
print("thread name is :",t.getName())
t.start()
tornado.ioloop.IOLoop.instance().start()
for t in threads:
t.join()
end_time = time.time()
print("Total time :{}".format(end_time-start_time))
def uploadentrance(host,filepath,targetpath):
streamno = 3
start_time = time.time()
filesize = os.path.getsize(filepath)
streamsize = filesize // (streamno-1)
i = 0
threads = []
while i < (streamno-1):
threads.append(threading.Thread(target=upload,args=(host,filepath,targetpath,streamsize*i,streamsize)))
i=i+1
if (streamsize*i) < filesize:
threads.append(threading.Thread(target=upload,args=(host,filepath,targetpath,streamsize*i,filesize-streamsize*i)))
for t in threads:
t.setDaemon(True)
print("thread name is :",t.getName())
t.start()
tornado.ioloop.IOLoop.instance().start()
#for t in threads:
t.join()
end_time = time.time()
print("Total time :{}".format(end_time-start_time))
#tornado.ioloop.IOLoop.current().stop() # Stop the loop when the upload is complete.