use ajax for starting downloads
This commit is contained in:
@@ -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<Downloader> downloaders = new CopyOnWriteArrayList<>();
|
||||
private final Map<InfoHash,Downloader> 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<Downloader> getDownloaders() {
|
||||
return downloaders;
|
||||
public Stream<Downloader> 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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("<?xml version='1.0' encoding='UTF-8'?>");
|
||||
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("<InfoHash>");
|
||||
sb.append(infohash);
|
||||
sb.append("</InfoHash>");
|
||||
sb.append("<Downloading>").append(downloadManager.isDownloading(result.getInfohash())).append("</Downloading>");
|
||||
sb.append("</Result>");
|
||||
});
|
||||
sb.append("</ResultsFromSender>");
|
||||
@@ -82,7 +84,7 @@ public class SearchServlet extends HttpServlet {
|
||||
}
|
||||
sb.append("</Searches>");
|
||||
} 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("<ResultsForFile>");
|
||||
UIResultEvent first = resultSet.iterator().next();
|
||||
sb.append("<InfoHash>").append(Base64.encode(infoHash.getRoot())).append("</InfoHash>");
|
||||
sb.append("<Downloading>").append(downloadManager.isDownloading(infoHash)).append("</Downloading>");
|
||||
sb.append("<Name>").append(DataHelper.escapeHTML(first.getName())).append("</Name>");
|
||||
sb.append("<Size>").append(DataHelper.formatSize2Decimal(first.getSize(), false)).append("B").append("</Size>");
|
||||
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");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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 += "</td>";
|
||||
table += "<td>";
|
||||
table += "<form action='/MuWire/Download' target='_blank' method='post'><input type='hidden' name='infoHash' value='"+x[i].infoHash;
|
||||
table += "'><input type='hidden' name='action' value='start'><input type='hidden' name='uuid' value='"+uuid;
|
||||
table += "'><input type='submit' value='Download'></form>";
|
||||
if (x[i].downloading == "false") {
|
||||
table += "<span id='download-"+ x[i].infoHash+"'><a href='#' onclick='window.download(\"" + x[i].infoHash + "\");return false;'>Download</a></span>";
|
||||
} else {
|
||||
table += "Downloading";
|
||||
}
|
||||
table += "</td>";
|
||||
table += "</tr>";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user