From 95dd5c4a7cf7dcce35cd616dc791306a083cbfbc Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Sat, 30 Nov 2019 23:34:59 +0000 Subject: [PATCH] downloads display, starting and stopping --- .../muwire/core/download/Downloader.groovy | 16 ++++ .../com/muwire/webui/DownloadManager.java | 31 ++++++++ .../com/muwire/webui/DownloadServlet.java | 64 ++++++++++++++++ .../java/com/muwire/webui/MuWireClient.java | 5 ++ webui/src/main/webapp/Downloads.jsp | 73 +++++++++++++++++++ webui/src/main/webapp/Home.jsp | 13 +++- webui/templates/web.xml.template | 10 ++- 7 files changed, 208 insertions(+), 4 deletions(-) create mode 100644 webui/src/main/java/com/muwire/webui/DownloadManager.java create mode 100644 webui/src/main/java/com/muwire/webui/DownloadServlet.java create mode 100644 webui/src/main/webapp/Downloads.jsp diff --git a/core/src/main/groovy/com/muwire/core/download/Downloader.groovy b/core/src/main/groovy/com/muwire/core/download/Downloader.groovy index 1af33ea4..a2b1eb1f 100644 --- a/core/src/main/groovy/com/muwire/core/download/Downloader.groovy +++ b/core/src/main/groovy/com/muwire/core/download/Downloader.groovy @@ -92,6 +92,22 @@ public class Downloader { public synchronized InfoHash getInfoHash() { infoHash } + + public File getFile() { + file + } + + public int getNPieces() { + nPieces + } + + public int getPieceSize() { + pieceSize + } + + public long getLength() { + length + } private synchronized void setInfoHash(InfoHash infoHash) { this.infoHash = infoHash diff --git a/webui/src/main/java/com/muwire/webui/DownloadManager.java b/webui/src/main/java/com/muwire/webui/DownloadManager.java new file mode 100644 index 00000000..8e26edf1 --- /dev/null +++ b/webui/src/main/java/com/muwire/webui/DownloadManager.java @@ -0,0 +1,31 @@ +package com.muwire.webui; + +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +import com.muwire.core.Core; +import com.muwire.core.InfoHash; +import com.muwire.core.download.DownloadStartedEvent; +import com.muwire.core.download.Downloader; + +public class DownloadManager { + private final Core core; + + private final List downloaders = new CopyOnWriteArrayList<>(); + + public DownloadManager(Core core) { + this.core = core; + } + + public void onDownloadStartedEvent(DownloadStartedEvent e) { + downloaders.add(e.getDownloader()); + } + + public List getDownloaders() { + return downloaders; + } + + public boolean isDownloading(InfoHash infoHash) { + return !downloaders.stream().map(d -> d.getInfoHash()).filter(d -> d.equals(infoHash)).findAny().isEmpty(); + } +} diff --git a/webui/src/main/java/com/muwire/webui/DownloadServlet.java b/webui/src/main/java/com/muwire/webui/DownloadServlet.java new file mode 100644 index 00000000..e3087351 --- /dev/null +++ b/webui/src/main/java/com/muwire/webui/DownloadServlet.java @@ -0,0 +1,64 @@ +package com.muwire.webui; + +import java.io.File; +import java.io.IOException; +import java.util.Collections; +import java.util.Set; +import java.util.UUID; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import com.muwire.core.Core; +import com.muwire.core.InfoHash; +import com.muwire.core.download.Downloader; +import com.muwire.core.download.UIDownloadCancelledEvent; +import com.muwire.core.download.UIDownloadEvent; +import com.muwire.core.search.UIResultEvent; + +import net.i2p.data.Base64; + +public class DownloadServlet extends HttpServlet { + + private DownloadManager downloadManager; + private SearchManager searchManager; + private Core core; + + public void init(ServletConfig config) throws ServletException { + downloadManager = (DownloadManager) config.getServletContext().getAttribute("downloadManager"); + searchManager = (SearchManager) config.getServletContext().getAttribute("searchManager"); + core = (Core) config.getServletContext().getAttribute("core"); + } + + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String infoHashB64 = req.getParameter("infoHash"); + InfoHash infoHash = new InfoHash(Base64.decode(infoHashB64)); + String action = req.getParameter("action"); + if (action.equals("start")) { + UUID uuid = UUID.fromString(req.getParameter("uuid")); + Set results = searchManager.getResults().get(uuid).getByInfoHash(infoHash); + + UIDownloadEvent event = new UIDownloadEvent(); + UIResultEvent[] resultsArray = results.toArray(new UIResultEvent[0]); + event.setResult(resultsArray); + // TODO: sequential + // TODO: possible sources + event.setSources(Collections.emptySet()); + event.setTarget(new File(core.getMuOptions().getDownloadLocation(), resultsArray[0].getName())); + core.getEventBus().publish(event); + } else if (action.equals("cancel")) { + 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); + }); + } + resp.sendRedirect("/MuWire/Downloads.jsp"); + } +} diff --git a/webui/src/main/java/com/muwire/webui/MuWireClient.java b/webui/src/main/java/com/muwire/webui/MuWireClient.java index 9d843c9b..fa6a2a07 100644 --- a/webui/src/main/java/com/muwire/webui/MuWireClient.java +++ b/webui/src/main/java/com/muwire/webui/MuWireClient.java @@ -18,6 +18,7 @@ import javax.servlet.ServletContext; import com.muwire.core.Core; import com.muwire.core.MuWireSettings; +import com.muwire.core.download.DownloadStartedEvent; import com.muwire.core.search.UIResultBatchEvent; import net.i2p.app.ClientAppManager; @@ -112,7 +113,11 @@ public class MuWireClient { SearchManager searchManager = new SearchManager(core); core.getEventBus().register(UIResultBatchEvent.class, searchManager); + DownloadManager downloadManager = new DownloadManager(core); + core.getEventBus().register(DownloadStartedEvent.class, downloadManager); + servletContext.setAttribute("searchManager", searchManager); + servletContext.setAttribute("downloadManager", downloadManager); } public String getHome() { diff --git a/webui/src/main/webapp/Downloads.jsp b/webui/src/main/webapp/Downloads.jsp new file mode 100644 index 00000000..5890de9c --- /dev/null +++ b/webui/src/main/webapp/Downloads.jsp @@ -0,0 +1,73 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" + pageEncoding="UTF-8"%> +<%@ page import="java.io.*" %> +<%@ page import="java.util.*" %> +<%@ page import="com.muwire.webui.*" %> +<%@ page import="com.muwire.core.*" %> +<%@ page import="com.muwire.core.search.*" %> +<%@ page import="net.i2p.data.*" %> + +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + +<% + MuWireClient client = (MuWireClient) session.getAttribute("mwClient"); +%> + + + MuWire ${version} + + + +

Welcome to MuWire ${persona}

+

Back to search

+ +
+

Downloads:

+ <% + DownloadManager downloadManager = (DownloadManager) client.getServletContext().getAttribute("downloadManager"); + StringBuilder sb = new StringBuilder(); + sb.append(""); + downloadManager.getDownloaders().forEach(downloader -> { + + String name = downloader.getFile().getName(); + String state = downloader.getCurrentState().toString(); + int rawSpeed = downloader.speed(); + String speed = DataHelper.formatSize2Decimal(rawSpeed,false) + "B/sec"; + String ETA; + if (rawSpeed == 0) + ETA = "Unknown"; + else { + long remaining = (downloader.getNPieces() - downloader.donePieces()) * downloader.getPieceSize() / rawSpeed; + ETA = DataHelper.formatDuration(remaining * 1000); + } + + int percentDone = -1; + if (downloader.getNPieces() != 0) + percentDone = (int)(downloader.donePieces() * 100 / downloader.getNPieces()); + String totalSize = DataHelper.formatSize2Decimal(downloader.getLength(), false) + "B"; + String progress = String.format("%2d", percentDone) + "% of "+totalSize; + + + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + sb.append(""); + + + String form = ""; + + sb.append(""); + sb.append(""); + + }); + sb.append("
").append(name).append("").append(state).append("").append(speed).append("").append(progress).append("").append(ETA).append("
").append(form).append("
"); + out.print(sb.toString()); + %> + + + + diff --git a/webui/src/main/webapp/Home.jsp b/webui/src/main/webapp/Home.jsp index 47a051f8..5ff8febd 100644 --- a/webui/src/main/webapp/Home.jsp +++ b/webui/src/main/webapp/Home.jsp @@ -33,6 +33,7 @@
<% SearchManager searchManager = (SearchManager) client.getServletContext().getAttribute("searchManager"); + DownloadManager downloadManager = (DownloadManager) client.getServletContext().getAttribute("downloadManager"); if (request.getParameter("uuid") == null) { out.print("Active Searches
"); for (SearchResults results : searchManager.getResults().values()) { @@ -81,9 +82,15 @@ sb.append(""); sb.append("").append(result.getName()).append(""); sb.append("").append(DataHelper.formatSize2Decimal(result.getSize(),false)).append("B").append(""); - sb.append("
"); + if (downloadManager.isDownloading(result.getInfohash())) { + sb.append("Downloading"); + } else { + sb.append("
"); + } sb.append(""); }); sb.append(""); diff --git a/webui/templates/web.xml.template b/webui/templates/web.xml.template index 87a6d77a..3887d55b 100644 --- a/webui/templates/web.xml.template +++ b/webui/templates/web.xml.template @@ -21,7 +21,10 @@ com.muwire.webui.SearchServlet - + + com.muwire.webui.DownloadServlet + com.muwire.webui.DownloadServlet + com.muwire.webui.MuWireServlet @@ -33,6 +36,11 @@ /Search + + com.muwire.webui.DownloadServlet + /Download + + __JASPER__