refresh search results and connection count with ajax
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package com.muwire.webui;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.ServletConfig;
|
||||
import javax.servlet.ServletException;
|
||||
@@ -8,9 +10,13 @@ import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.muwire.core.Persona;
|
||||
import com.muwire.core.search.UIResultEvent;
|
||||
|
||||
public class SearchServlet extends HttpServlet {
|
||||
|
||||
private SearchManager searchManager;
|
||||
private ConnectionCounter connectionCounter;
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
@@ -18,10 +24,46 @@ public class SearchServlet extends HttpServlet {
|
||||
searchManager.newSearch(search);
|
||||
resp.sendRedirect("/MuWire/Home.jsp");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
String section = req.getParameter("section");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("<?xml version='1.0' encoding='UTF-8'?>");
|
||||
if (section.equals("activeSearches")) {
|
||||
sb.append("<Searches>");
|
||||
for (SearchResults results : searchManager.getResults().values()) {
|
||||
sb.append("<Search>");
|
||||
sb.append("<uuid>").append(results.getUUID()).append("</uuid>");
|
||||
sb.append("<Query>").append(results.getSearch()).append("</Query>");
|
||||
Map<Persona, Set<UIResultEvent>> bySender = results.getBySender();
|
||||
sb.append("<Senders>").append(bySender.size()).append("</Senders>");
|
||||
int total = 0;
|
||||
for (Set<UIResultEvent> s : bySender.values()) {
|
||||
total += s.size();
|
||||
}
|
||||
sb.append("<Results>").append(total).append("</Results>");
|
||||
sb.append("</Search>");
|
||||
}
|
||||
sb.append("</Searches>");
|
||||
} else if (section.equals("connectionsCount")) {
|
||||
sb.append("<Connections>");
|
||||
sb.append(connectionCounter.getConnections());
|
||||
sb.append("</Connections>");
|
||||
}
|
||||
resp.setContentType("text/xml");
|
||||
resp.getWriter().write(sb.toString());
|
||||
resp.flushBuffer();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void init(ServletConfig config) throws ServletException {
|
||||
searchManager = (SearchManager) config.getServletContext().getAttribute("searchManager");
|
||||
connectionCounter = (ConnectionCounter) config.getServletContext().getAttribute("connectionCounter");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,38 +1,30 @@
|
||||
<%@ page language="java" contentType="text/html; charset=UTF-8"
|
||||
pageEncoding="UTF-8"%>
|
||||
<%@ page import="java.io.*" %>
|
||||
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) application.getAttribute("mwClient");
|
||||
ConnectionCounter connectionCounter = (ConnectionCounter) client.getServletContext().getAttribute("connectionCounter");
|
||||
|
||||
String persona = client.getCore().getMe().getHumanReadableName();
|
||||
String version = client.getCore().getVersion();
|
||||
|
||||
session.setAttribute("persona", persona);
|
||||
session.setAttribute("version", version);
|
||||
|
||||
%>
|
||||
<html>
|
||||
<head>
|
||||
<title>MuWire ${version}</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<head>
|
||||
<title>MuWire ${version}</title>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%">
|
||||
<tr>
|
||||
<td>
|
||||
Welcome to MuWire ${persona}
|
||||
</td>
|
||||
<td>
|
||||
Connections <%= connectionCounter.getConnections() %>
|
||||
<span id="connectionsCount">Connections : 0</span>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
@@ -52,79 +44,79 @@
|
||||
</table>
|
||||
|
||||
<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()) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(results.getSearch());
|
||||
sb.append(" senders: ");
|
||||
Map<Persona, Set<UIResultEvent>> bySender = results.getBySender();
|
||||
sb.append(bySender.size());
|
||||
|
||||
int total = 0;
|
||||
for (Set<UIResultEvent> s : bySender.values()) {
|
||||
total += s.size();
|
||||
<table width="100%">
|
||||
<tr>
|
||||
<th>
|
||||
Active Searches
|
||||
</th>
|
||||
<th>
|
||||
Results
|
||||
</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<div id="activeSearches"></div>
|
||||
</td>
|
||||
<td>
|
||||
<div id="results"></div>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<script>
|
||||
var uuid = null;
|
||||
|
||||
function refreshConnectionsCount() {
|
||||
var xmlhttp = new XMLHttpRequest();
|
||||
xmlhttp.onreadystatechange = function() {
|
||||
if (this.readyState == 4 && this.status == 200) {
|
||||
var connections = this.responseXML.getElementsByTagName("Connections");
|
||||
var count = connections[0].childNodes[0].nodeValue
|
||||
var connectionCountSpan = document.getElementById("connectionsCount");
|
||||
var countString = "Connections: "+count;
|
||||
connectionCountSpan.innerHTML = countString;
|
||||
}
|
||||
sb.append(" results: ");
|
||||
sb.append(total);
|
||||
|
||||
out.print("<a href='/MuWire/Home.jsp?uuid="+results.getUUID()+"'>"+sb.toString()+"</a><br/>");
|
||||
}
|
||||
} else if (request.getParameter("sender") == null) {
|
||||
UUID uuid = UUID.fromString(request.getParameter("uuid"));
|
||||
SearchResults results = searchManager.getResults().get(uuid);
|
||||
|
||||
out.print("Results for "+results.getSearch()+"<br/>");
|
||||
|
||||
Map<Persona, Set<UIResultEvent>> bySender = results.getBySender();
|
||||
for (Persona sender : bySender.keySet()) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(sender.getHumanReadableName());
|
||||
sb.append(" count: ");
|
||||
sb.append(bySender.get(sender).size());
|
||||
String link = "/MuWire/Home.jsp?uuid="+uuid.toString()+"&sender="+sender.toBase64();
|
||||
out.print("<a href='"+link+"'>"+sb.toString()+"</a><br/>");
|
||||
}
|
||||
} else {
|
||||
UUID uuid = UUID.fromString(request.getParameter("uuid"));
|
||||
SearchResults searchResults = searchManager.getResults().get(uuid);
|
||||
|
||||
String senderB64 = request.getParameter("sender");
|
||||
Persona sender = new Persona(new ByteArrayInputStream(net.i2p.data.Base64.decode(senderB64)));
|
||||
|
||||
Set<UIResultEvent> results = searchResults.getBySender().get(sender);
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("<table width='100%'>");
|
||||
sb.append("<tr><td>Name</td><td>Size</td><td>Direct Sources</td><td>Possible Sources</td><td>Download</td></tr>");
|
||||
results.forEach(result -> {
|
||||
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>").append(searchResults.getByInfoHash(result.getInfohash()).size()).append("</td>");
|
||||
sb.append("<td>").append(searchResults.getPossibleSources(result.getInfohash()).size()).append("</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>");
|
||||
out.print(sb.toString());
|
||||
|
||||
xmlhttp.open("GET", "/MuWire/Search?section=connectionsCount", true);
|
||||
xmlhttp.send();
|
||||
}
|
||||
%>
|
||||
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
||||
function updateUUID(resultUUID) {
|
||||
uuid = resultUUID;
|
||||
// TODO: update results table
|
||||
}
|
||||
|
||||
function refreshActiveSearches() {
|
||||
var xmlhttp = new XMLHttpRequest();
|
||||
xmlhttp.onreadystatechange = function () {
|
||||
if (this.readyState == 4 && this.status == 200) {
|
||||
var xmlDoc = this.responseXML;
|
||||
var i;
|
||||
var table = "<table><tr><th>Search</th><th>Senders</th><th>Results</th></tr>";
|
||||
var activeSearchesDiv = document.getElementById("activeSearches");
|
||||
var x = xmlDoc.getElementsByTagName("Search");
|
||||
for (i = 0; i < x.length; i ++) {
|
||||
var resultUUID = x[i].getElementsByTagName("uuid")[0].childNodes[0].nodeValue;
|
||||
table += "<tr><td><a href='#' onclick='updateUUID(resultUUID);return false;'>"
|
||||
table += x[i].getElementsByTagName("Query")[0].childNodes[0].nodeValue;
|
||||
table += "</a></td><td>";
|
||||
table += x[i].getElementsByTagName("Senders")[0].childNodes[0].nodeValue;
|
||||
table += "</td><td>";
|
||||
table += x[i].getElementsByTagName("Results")[0].childNodes[0].nodeValue;
|
||||
table += "</td></tr>"
|
||||
}
|
||||
table += "</table>"
|
||||
if (x.length > 0)
|
||||
activeSearchesDiv.innerHTML = table;
|
||||
}
|
||||
}
|
||||
xmlhttp.open("GET", "/MuWire/Search?section=activeSearches", true);
|
||||
xmlhttp.send();
|
||||
}
|
||||
|
||||
setInterval(refreshActiveSearches, 3000);
|
||||
setTimeout(refreshActiveSearches, 1);
|
||||
setInterval(refreshConnectionsCount, 3000);
|
||||
setTimeout(refreshConnectionsCount, 1);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user