mirror of https://github.com/zlatinb/muwire
downloads display, starting and stopping
parent
0bff4b55a5
commit
95dd5c4a7c
|
@ -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
|
||||
|
|
|
@ -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<Downloader> downloaders = new CopyOnWriteArrayList<>();
|
||||
|
||||
public DownloadManager(Core core) {
|
||||
this.core = core;
|
||||
}
|
||||
|
||||
public void onDownloadStartedEvent(DownloadStartedEvent e) {
|
||||
downloaders.add(e.getDownloader());
|
||||
}
|
||||
|
||||
public List<Downloader> getDownloaders() {
|
||||
return downloaders;
|
||||
}
|
||||
|
||||
public boolean isDownloading(InfoHash infoHash) {
|
||||
return !downloaders.stream().map(d -> d.getInfoHash()).filter(d -> d.equals(infoHash)).findAny().isEmpty();
|
||||
}
|
||||
}
|
|
@ -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<UIResultEvent> 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");
|
||||
}
|
||||
}
|
|
@ -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() {
|
||||
|
|
|
@ -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");
|
||||
%>
|
||||
<html>
|
||||
<head>
|
||||
<title>MuWire ${version}</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<p>Welcome to MuWire ${persona}</p>
|
||||
<p><a href="/MuWire/Home.jsp">Back to search</a></p>
|
||||
|
||||
<hr/>
|
||||
<p>Downloads:</p>
|
||||
<%
|
||||
DownloadManager downloadManager = (DownloadManager) client.getServletContext().getAttribute("downloadManager");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("<table width='100%'>");
|
||||
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("<tr>");
|
||||
sb.append("<td>").append(name).append("</td>");
|
||||
sb.append("<td>").append(state).append("</td>");
|
||||
sb.append("<td>").append(speed).append("</td>");
|
||||
sb.append("<td>").append(progress).append("</td>");
|
||||
sb.append("<td>").append(ETA).append("</td>");
|
||||
|
||||
|
||||
String form = "<form action='/MuWire/Download' method='post'><input type='hidden' name='infoHash' value='" +
|
||||
net.i2p.data.Base64.encode(downloader.getInfoHash().getRoot()) +
|
||||
"'><input type='hidden' name='action' value='cancel'><input type='submit' value='Cancel'></form>";
|
||||
|
||||
sb.append("<td>").append(form).append("</td>");
|
||||
sb.append("</tr>");
|
||||
|
||||
});
|
||||
sb.append("</table>");
|
||||
out.print(sb.toString());
|
||||
%>
|
||||
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -33,6 +33,7 @@
|
|||
<hr/>
|
||||
<%
|
||||
SearchManager searchManager = (SearchManager) client.getServletContext().getAttribute("searchManager");
|
||||
DownloadManager downloadManager = (DownloadManager) client.getServletContext().getAttribute("downloadManager");
|
||||
if (request.getParameter("uuid") == null) {
|
||||
out.print("Active Searches<br/>");
|
||||
for (SearchResults results : searchManager.getResults().values()) {
|
||||
|
@ -81,9 +82,15 @@
|
|||
sb.append("<tr>");
|
||||
sb.append("<td>").append(result.getName()).append("</td>");
|
||||
sb.append("<td>").append(DataHelper.formatSize2Decimal(result.getSize(),false)).append("B").append("</td>");
|
||||
sb.append("<td><form action='/MuWire/Download' method='post'><input type='hidden' name='infoHash' value='");
|
||||
sb.append(net.i2p.data.Base64.encode(result.getInfohash().getRoot()));
|
||||
sb.append("'/><input type='submit' value='Download'></form></td>");
|
||||
if (downloadManager.isDownloading(result.getInfohash())) {
|
||||
sb.append("<td>Downloading</td>");
|
||||
} else {
|
||||
sb.append("<td><form action='/MuWire/Download' method='post'><input type='hidden' name='infoHash' value='");
|
||||
sb.append(net.i2p.data.Base64.encode(result.getInfohash().getRoot()));
|
||||
sb.append("'/><input type='hidden' name='uuid' value='");
|
||||
sb.append(uuid.toString());
|
||||
sb.append("'/><input type='hidden' name='action' value='start'><input type='submit' value='Download'></form></td>");
|
||||
}
|
||||
sb.append("</tr>");
|
||||
});
|
||||
sb.append("</table>");
|
||||
|
|
|
@ -21,7 +21,10 @@
|
|||
<servlet-class>com.muwire.webui.SearchServlet</servlet-class>
|
||||
</servlet>
|
||||
|
||||
|
||||
<servlet>
|
||||
<servlet-name>com.muwire.webui.DownloadServlet</servlet-name>
|
||||
<servlet-class>com.muwire.webui.DownloadServlet</servlet-class>
|
||||
</servlet>
|
||||
|
||||
<servlet-mapping>
|
||||
<servlet-name>com.muwire.webui.MuWireServlet</servlet-name>
|
||||
|
@ -33,6 +36,11 @@
|
|||
<url-pattern>/Search</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
<servlet-mapping>
|
||||
<servlet-name>com.muwire.webui.DownloadServlet</servlet-name>
|
||||
<url-pattern>/Download</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
__JASPER__
|
||||
|
||||
</web-app>
|
||||
|
|
Loading…
Reference in New Issue