From 1cc817db3cc1e04a89cbf3dd1630be2edd34fcca Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Wed, 11 Jun 2014 19:55:28 -0400 Subject: [PATCH] renamed check_for_requests to heartbeat, and made the heartbeat serve all items in the queue instead of just the latest (#36) --- onionshare/onionshare.py | 8 +++---- onionshare_gui/static/onionshare.js | 36 ++++++++++++++++------------- onionshare_gui/webapp.py | 18 ++++++++++----- 3 files changed, 36 insertions(+), 26 deletions(-) diff --git a/onionshare/onionshare.py b/onionshare/onionshare.py index 1b0895cb..155a0082 100644 --- a/onionshare/onionshare.py +++ b/onionshare/onionshare.py @@ -29,13 +29,13 @@ REQUEST_LOAD = 0 REQUEST_DOWNLOAD = 1 REQUEST_PROGRESS = 2 REQUEST_OTHER = 3 -request_q = Queue.Queue() +q = Queue.Queue() download_count = 0 def add_request(type, path, data=None): - global request_q - request_q.put({ + global q + q.put({ 'type': type, 'path': path, 'data': data @@ -50,7 +50,7 @@ def index(): @app.route("/{0}/download".format(slug)) def download(): - global filename, filesize, request_q, download_count + global filename, filesize, q, download_count global REQUEST_DOWNLOAD, REQUEST_PROGRESS # each download has a unique id diff --git a/onionshare_gui/static/onionshare.js b/onionshare_gui/static/onionshare.js index 42c9120f..9abfd5b7 100644 --- a/onionshare_gui/static/onionshare.js +++ b/onionshare_gui/static/onionshare.js @@ -25,24 +25,28 @@ $(function(){ var REQUEST_OTHER = 3; function check_for_requests() { $.ajax({ - url: '/check_for_requests', + url: '/heartbeat', success: function(data, textStatus, jqXHR){ if(data != '') { - var r = JSON.parse(data); - if(r.type == REQUEST_LOAD) { - update($('').addClass('weblog').html(onionshare.strings['download_page_loaded'])); - } else if(r.type == REQUEST_DOWNLOAD) { - var $download = $('') - .attr('id', 'download-'+r.data.id) - .addClass('weblog').html(onionshare.strings['download_started']) - .append($('').addClass('progress')); - update($download); - } else if(r.type == REQUEST_PROGRESS) { - var percent = Math.floor((r.data.bytes / onionshare.filesize) * 100); - $('#download-'+r.data.id+' .progress').html(' '+human_readable_filesize(r.data.bytes)+', '+percent+'%'); - } else { - if(r.path != '/favicon.ico') - update($('').addClass('weblog-error').html(onionshare.strings['other_page_loaded']+': '+r.path)); + var events = JSON.parse(data); + for(var i=0; i').addClass('weblog').html(onionshare.strings['download_page_loaded'])); + } else if(r.type == REQUEST_DOWNLOAD) { + var $download = $('') + .attr('id', 'download-'+r.data.id) + .addClass('weblog').html(onionshare.strings['download_started']) + .append($('').addClass('progress')); + update($download); + } else if(r.type == REQUEST_PROGRESS) { + var percent = Math.floor((r.data.bytes / onionshare.filesize) * 100); + $('#download-'+r.data.id+' .progress').html(' '+human_readable_filesize(r.data.bytes)+', '+percent+'%'); + } else { + if(r.path != '/favicon.ico') + update($('').addClass('weblog-error').html(onionshare.strings['other_page_loaded']+': '+r.path)); + } } } diff --git a/onionshare_gui/webapp.py b/onionshare_gui/webapp.py index 773849c1..b82c5f87 100644 --- a/onionshare_gui/webapp.py +++ b/onionshare_gui/webapp.py @@ -51,12 +51,18 @@ def copy_url(): clipboard.set_text(url) return '' -@app.route("/check_for_requests") +@app.route("/heartbeat") def check_for_requests(): global onionshare - try: - r = onionshare.request_q.get(False) - return json.dumps(r) - except onionshare.Queue.Empty: - return '' + events = [] + + done = False + while not done: + try: + r = onionshare.q.get(False) + events.append(r) + except onionshare.Queue.Empty: + done = True + + return json.dumps(events)