renamed check_for_requests to heartbeat, and made the heartbeat serve all items in the queue instead of just the latest (#36)

This commit is contained in:
Micah Lee 2014-06-11 19:55:28 -04:00
parent 68bba73a8c
commit 1cc817db3c
3 changed files with 36 additions and 26 deletions

View file

@ -29,13 +29,13 @@ REQUEST_LOAD = 0
REQUEST_DOWNLOAD = 1 REQUEST_DOWNLOAD = 1
REQUEST_PROGRESS = 2 REQUEST_PROGRESS = 2
REQUEST_OTHER = 3 REQUEST_OTHER = 3
request_q = Queue.Queue() q = Queue.Queue()
download_count = 0 download_count = 0
def add_request(type, path, data=None): def add_request(type, path, data=None):
global request_q global q
request_q.put({ q.put({
'type': type, 'type': type,
'path': path, 'path': path,
'data': data 'data': data
@ -50,7 +50,7 @@ def index():
@app.route("/{0}/download".format(slug)) @app.route("/{0}/download".format(slug))
def download(): def download():
global filename, filesize, request_q, download_count global filename, filesize, q, download_count
global REQUEST_DOWNLOAD, REQUEST_PROGRESS global REQUEST_DOWNLOAD, REQUEST_PROGRESS
# each download has a unique id # each download has a unique id

View file

@ -25,24 +25,28 @@ $(function(){
var REQUEST_OTHER = 3; var REQUEST_OTHER = 3;
function check_for_requests() { function check_for_requests() {
$.ajax({ $.ajax({
url: '/check_for_requests', url: '/heartbeat',
success: function(data, textStatus, jqXHR){ success: function(data, textStatus, jqXHR){
if(data != '') { if(data != '') {
var r = JSON.parse(data); var events = JSON.parse(data);
if(r.type == REQUEST_LOAD) { for(var i=0; i<events.length; i++) {
update($('<span>').addClass('weblog').html(onionshare.strings['download_page_loaded'])); var r = events[i];
} else if(r.type == REQUEST_DOWNLOAD) {
var $download = $('<span>') if(r.type == REQUEST_LOAD) {
.attr('id', 'download-'+r.data.id) update($('<span>').addClass('weblog').html(onionshare.strings['download_page_loaded']));
.addClass('weblog').html(onionshare.strings['download_started']) } else if(r.type == REQUEST_DOWNLOAD) {
.append($('<span>').addClass('progress')); var $download = $('<span>')
update($download); .attr('id', 'download-'+r.data.id)
} else if(r.type == REQUEST_PROGRESS) { .addClass('weblog').html(onionshare.strings['download_started'])
var percent = Math.floor((r.data.bytes / onionshare.filesize) * 100); .append($('<span>').addClass('progress'));
$('#download-'+r.data.id+' .progress').html(' '+human_readable_filesize(r.data.bytes)+', '+percent+'%'); update($download);
} else { } else if(r.type == REQUEST_PROGRESS) {
if(r.path != '/favicon.ico') var percent = Math.floor((r.data.bytes / onionshare.filesize) * 100);
update($('<span>').addClass('weblog-error').html(onionshare.strings['other_page_loaded']+': '+r.path)); $('#download-'+r.data.id+' .progress').html(' '+human_readable_filesize(r.data.bytes)+', '+percent+'%');
} else {
if(r.path != '/favicon.ico')
update($('<span>').addClass('weblog-error').html(onionshare.strings['other_page_loaded']+': '+r.path));
}
} }
} }

View file

@ -51,12 +51,18 @@ def copy_url():
clipboard.set_text(url) clipboard.set_text(url)
return '' return ''
@app.route("/check_for_requests") @app.route("/heartbeat")
def check_for_requests(): def check_for_requests():
global onionshare global onionshare
try: events = []
r = onionshare.request_q.get(False)
return json.dumps(r) done = False
except onionshare.Queue.Empty: while not done:
return '' try:
r = onionshare.q.get(False)
events.append(r)
except onionshare.Queue.Empty:
done = True
return json.dumps(events)