From b057e848d04d2ba85f21d4b3e05733401c550c05 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Thu, 5 Dec 2019 21:18:29 +0000 Subject: [PATCH] use ajax for starting downloads --- .../com/muwire/webui/DownloadManager.java | 24 +++++++++++++++---- .../com/muwire/webui/DownloadServlet.java | 10 +------- .../java/com/muwire/webui/SearchServlet.java | 8 +++++-- webui/src/main/js/search.js | 22 ++++++++++++++--- 4 files changed, 45 insertions(+), 19 deletions(-) diff --git a/webui/src/main/java/com/muwire/webui/DownloadManager.java b/webui/src/main/java/com/muwire/webui/DownloadManager.java index 8e26edf1..9b4aa594 100644 --- a/webui/src/main/java/com/muwire/webui/DownloadManager.java +++ b/webui/src/main/java/com/muwire/webui/DownloadManager.java @@ -1,31 +1,45 @@ package com.muwire.webui; import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; +import java.util.stream.Stream; import com.muwire.core.Core; import com.muwire.core.InfoHash; import com.muwire.core.download.DownloadStartedEvent; import com.muwire.core.download.Downloader; +import com.muwire.core.download.UIDownloadCancelledEvent; public class DownloadManager { private final Core core; - private final List downloaders = new CopyOnWriteArrayList<>(); + private final Map downloaders = new ConcurrentHashMap<>(); public DownloadManager(Core core) { this.core = core; } public void onDownloadStartedEvent(DownloadStartedEvent e) { - downloaders.add(e.getDownloader()); + downloaders.put(e.getDownloader().getInfoHash(),e.getDownloader()); } - public List getDownloaders() { - return downloaders; + public Stream getDownloaders() { + return downloaders.values().stream(); } public boolean isDownloading(InfoHash infoHash) { - return !downloaders.stream().map(d -> d.getInfoHash()).filter(d -> d.equals(infoHash)).findAny().isEmpty(); + return downloaders.containsKey(infoHash); + } + + void cancel(InfoHash infoHash) { + Downloader d = downloaders.remove(infoHash); + if (d == null) + return; + d.cancel(); + UIDownloadCancelledEvent event = new UIDownloadCancelledEvent(); + event.setDownloader(d); + core.getEventBus().publish(event); } } diff --git a/webui/src/main/java/com/muwire/webui/DownloadServlet.java b/webui/src/main/java/com/muwire/webui/DownloadServlet.java index 9641805a..11a174d8 100644 --- a/webui/src/main/java/com/muwire/webui/DownloadServlet.java +++ b/webui/src/main/java/com/muwire/webui/DownloadServlet.java @@ -115,15 +115,7 @@ public class DownloadServlet extends HttpServlet { resp.sendError(403, "Not initialized"); return; } - downloadManager.getDownloaders().stream().filter(d -> d.getInfoHash().equals(infoHash)).findAny(). - ifPresent(d -> { - d.cancel(); - downloadManager.getDownloaders().remove(d); - UIDownloadCancelledEvent event = new UIDownloadCancelledEvent(); - event.setDownloader(d); - core.getEventBus().publish(event); - }); + downloadManager.cancel(infoHash); } - resp.sendRedirect("/MuWire/Downloads.jsp"); } } diff --git a/webui/src/main/java/com/muwire/webui/SearchServlet.java b/webui/src/main/java/com/muwire/webui/SearchServlet.java index 90090baa..fa5de4cd 100644 --- a/webui/src/main/java/com/muwire/webui/SearchServlet.java +++ b/webui/src/main/java/com/muwire/webui/SearchServlet.java @@ -21,6 +21,7 @@ public class SearchServlet extends HttpServlet { private SearchManager searchManager; private ConnectionCounter connectionCounter; + private DownloadManager downloadManager; @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { @@ -45,7 +46,7 @@ public class SearchServlet extends HttpServlet { StringBuilder sb = new StringBuilder(); sb.append(""); if (section.equals("groupBySender")) { - if (searchManager == null) { + if (searchManager == null || downloadManager == null) { resp.sendError(403, "Not initialized"); return; } @@ -73,6 +74,7 @@ public class SearchServlet extends HttpServlet { sb.append(""); sb.append(infohash); sb.append(""); + sb.append("").append(downloadManager.isDownloading(result.getInfohash())).append(""); sb.append(""); }); sb.append(""); @@ -82,7 +84,7 @@ public class SearchServlet extends HttpServlet { } sb.append(""); } else if (section.equals("groupByFile")) { - if (searchManager == null) { + if (searchManager == null || downloadManager == null) { resp.sendError(403, "Not initialized"); return; } @@ -97,6 +99,7 @@ public class SearchServlet extends HttpServlet { sb.append(""); UIResultEvent first = resultSet.iterator().next(); sb.append("").append(Base64.encode(infoHash.getRoot())).append(""); + sb.append("").append(downloadManager.isDownloading(infoHash)).append(""); sb.append("").append(DataHelper.escapeHTML(first.getName())).append(""); sb.append("").append(DataHelper.formatSize2Decimal(first.getSize(), false)).append("B").append(""); resultSet.forEach(result -> { @@ -137,6 +140,7 @@ public class SearchServlet extends HttpServlet { public void init(ServletConfig config) throws ServletException { searchManager = (SearchManager) config.getServletContext().getAttribute("searchManager"); connectionCounter = (ConnectionCounter) config.getServletContext().getAttribute("connectionCounter"); + downloadManager = (DownloadManager) config.getServletContext().getAttribute("downloadManager"); } } diff --git a/webui/src/main/js/search.js b/webui/src/main/js/search.js index 52298f8c..5ddd878d 100644 --- a/webui/src/main/js/search.js +++ b/webui/src/main/js/search.js @@ -63,6 +63,7 @@ class ResultBySender { this.name = xmlNode.getElementsByTagName("Name")[0].childNodes[0].nodeValue; this.size = xmlNode.getElementsByTagName("Size")[0].childNodes[0].nodeValue; this.infoHash = xmlNode.getElementsByTagName("InfoHash")[0].childNodes[0].nodeValue; + this.downloading = xmlNode.getElementsByTagName("Downloading")[0].childNodes[0].nodeValue; } } @@ -79,6 +80,19 @@ var sender = null; var lastXML = null; var infoHash = null; +function download(resultInfoHash) { + var xmlhttp = new XMLHttpRequest(); + xmlhttp.onreadystatechange = function() { + if (this.readyState == 4 && this.status == 200) { + var resultSpan = document.getElementById("download-"+resultInfoHash); + resultSpan.innerHTML = "Downloading"; + } + } + xmlhttp.open("POST", "/MuWire/Download", true); + xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); + xmlhttp.send(encodeURI("action=start&infoHash="+resultInfoHash+"&uuid="+uuid)); +} + function updateSender(senderName) { sender = senderName; @@ -99,9 +113,11 @@ function updateSender(senderName) { table += x[i].size; table += ""; table += ""; - table += "
"; + if (x[i].downloading == "false") { + table += "Download"; + } else { + table += "Downloading"; + } table += ""; table += ""; }