write personas on the wire part1

This commit is contained in:
Zlatin Balevsky
2019-06-21 15:26:18 +01:00
parent 40410eba63
commit 89e761f53b
3 changed files with 10 additions and 9 deletions

View File

@@ -1,7 +1,7 @@
package com.muwire.core.mesh
import com.muwire.core.InfoHash
import com.muwire.core.Persona
import com.muwire.core.download.Pieces
import net.i2p.data.Destination
@@ -9,7 +9,7 @@ import net.i2p.util.ConcurrentHashSet
class Mesh {
private final InfoHash infoHash
private final Set<Destination> sources = new ConcurrentHashSet<>()
private final Set<Persona> sources = new ConcurrentHashSet<>()
private final Pieces pieces
Mesh(InfoHash infoHash, Pieces pieces) {
@@ -17,8 +17,8 @@ class Mesh {
this.pieces = pieces
}
Set<Destination> getRandom(int n, Destination exclude) {
List<Destination> tmp = new ArrayList<>(sources)
Set<Persona> getRandom(int n, Persona exclude) {
List<Persona> tmp = new ArrayList<>(sources)
tmp.remove(exclude)
Collections.shuffle(tmp)
if (tmp.size() < n)

View File

@@ -38,6 +38,6 @@ class MeshManager {
Mesh mesh = meshes.get(e.infoHash)
if (mesh == null)
return
mesh.sources.add(e.source.destination)
mesh.sources.add(e.source)
}
}

View File

@@ -7,6 +7,7 @@ import java.nio.file.Files
import java.nio.file.StandardOpenOption
import java.util.stream.Collectors
import com.muwire.core.Persona
import com.muwire.core.connection.Endpoint
import com.muwire.core.mesh.Mesh
import com.muwire.core.util.DataUtil
@@ -32,7 +33,7 @@ class ContentUploader extends Uploader {
Range range = request.getRange()
if (range.start >= file.length() || range.end >= file.length()) {
os.write("416 Range Not Satisfiable\r\n".getBytes(StandardCharsets.US_ASCII))
writeMesh()
writeMesh(request.downloader)
os.write("\r\n".getBytes(StandardCharsets.US_ASCII))
os.flush()
return
@@ -40,7 +41,7 @@ class ContentUploader extends Uploader {
os.write("200 OK\r\n".getBytes(StandardCharsets.US_ASCII))
os.write("Content-Range: $range.start-$range.end\r\n".getBytes(StandardCharsets.US_ASCII))
writeMesh()
writeMesh(request.downloader)
os.write("\r\n".getBytes(StandardCharsets.US_ASCII))
FileChannel channel
@@ -62,11 +63,11 @@ class ContentUploader extends Uploader {
}
}
private void writeMesh() {
private void writeMesh(Persona toExclude) {
String xHave = DataUtil.encodeXHave(mesh.pieces.getDownloaded(), mesh.pieces.nPieces)
endpoint.getOutputStream().write("X-Have: $xHave\r\n".getBytes(StandardCharsets.US_ASCII))
Set<Destination> sources = mesh.getRandom(3, endpoint.destination)
Set<Persona> sources = mesh.getRandom(3, toExclude)
String xAlts = sources.stream().map({ it.toBase64() }).collect(Collectors.joining(","))
endpoint.getOutputStream().write("X-Alt: $xAlts\r\n".getBytes(StandardCharsets.US_ASCII))
}