display direct and possible sources. Pass possible sources to core

This commit is contained in:
Zlatin Balevsky
2019-12-03 06:00:56 +00:00
parent d1c308f118
commit d0318e3e83
3 changed files with 34 additions and 7 deletions

View File

@ -45,8 +45,7 @@ public class DownloadServlet extends HttpServlet {
UIResultEvent[] resultsArray = results.toArray(new UIResultEvent[0]);
event.setResult(resultsArray);
// TODO: sequential
// TODO: possible sources
event.setSources(Collections.emptySet());
event.setSources(searchManager.getResults().get(uuid).getPossibleSources(infoHash));
event.setTarget(new File(core.getMuOptions().getDownloadLocation(), resultsArray[0].getName()));
core.getEventBus().publish(event);
} else if (action.equals("cancel")) {

View File

@ -1,6 +1,7 @@
package com.muwire.webui;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@ -14,11 +15,16 @@ import com.muwire.core.Persona;
import com.muwire.core.search.UIResultBatchEvent;
import com.muwire.core.search.UIResultEvent;
import net.i2p.data.Destination;
import net.i2p.util.ConcurrentHashSet;
public class SearchResults {
private final UUID uuid;
private final String search;
private final Map<Persona, Set<UIResultEvent>> bySender = new ConcurrentHashMap<>();
private final Map<InfoHash, Set<UIResultEvent>> byInfohash = new ConcurrentHashMap<>();
private final Map<InfoHash, Set<Destination>> possibleSources = new ConcurrentHashMap<>();
public SearchResults(UUID uuid, String search) {
this.uuid = uuid;
@ -29,10 +35,26 @@ public class SearchResults {
Persona sender = e.getResults()[0].getSender();
Set<UIResultEvent> existing = bySender.get(sender);
if (existing == null) {
existing = new HashSet<>();
existing = new ConcurrentHashSet<>();
bySender.put(sender, existing);
}
existing.addAll(Arrays.asList(e.getResults()));
for(UIResultEvent result : e.getResults()) {
existing = byInfohash.get(result.getInfohash());
if (existing == null) {
existing = new ConcurrentHashSet<>();
byInfohash.put(result.getInfohash(), existing);
}
existing.add(result);
Set<Destination> sources = possibleSources.get(result.getInfohash());
if (sources == null) {
sources = new ConcurrentHashSet<>();
possibleSources.put(result.getInfohash(), sources);
}
sources.addAll(result.getSources());
}
}
public UUID getUUID() {
@ -48,10 +70,11 @@ public class SearchResults {
}
public Set<UIResultEvent> getByInfoHash(InfoHash infoHash) {
return bySender.values().stream().
flatMap(r -> r.stream()).
filter(r -> r.getInfohash().equals(infoHash)).
collect(Collectors.toSet());
return byInfohash.get(infoHash);
}
public Set<Destination> getPossibleSources(InfoHash infoHash) {
return possibleSources.getOrDefault(infoHash, Collections.emptySet());
}
}

View File

@ -99,10 +99,15 @@
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 {