skeleton of uploader subsysten

This commit is contained in:
Zlatin Balevsky
2019-05-28 14:23:03 +01:00
parent edfe301080
commit 6d3d2d94dc
8 changed files with 125 additions and 2 deletions

View File

@@ -165,6 +165,11 @@ class ConnectionAcceptor {
private void processGET(Endpoint e) {
byte[] et = new byte[3]
final DataInputStream dis = new DataInputStream(e.getInputStream())
dis.readFully(et)
if (et != "ET ".getBytes(StandardCharsets.US_ASCII))
throw new IOException("Invalid GET connection")
// TODO: implement
}

View File

@@ -10,7 +10,7 @@ class FileManager {
final EventBus eventBus
final Map<byte[], Set<SharedFile>> rootToFiles = new HashMap<>()
final Map<byte[], Set<SharedFile>> rootToFiles = Collections.synchronizedMap(new HashMap<>())
final Map<File, SharedFile> fileToSharedFile = Collections.synchronizedMap(new HashMap<>())
final Map<String, Set<File>> nameToFiles = new HashMap<>()
final SearchIndex index = new SearchIndex()
@@ -56,6 +56,7 @@ class FileManager {
void onFileUnsharedEvent(FileUnsharedEvent e) {
SharedFile sf = e.unsharedFile
byte [] root = sf.getInfoHash().getRoot()
Set<SharedFile> existing
Set<SharedFile> existing = rootToFiles.get(root)
if (existing != null) {
existing.remove(sf)
@@ -83,12 +84,17 @@ class FileManager {
return new HashMap<>(fileToSharedFile)
}
}
Set<SharedFile> getSharedFiles(byte []root) {
return rootToFiles.get(root)
}
void onSearchEvent(SearchEvent e) {
// hash takes precedence
ResultsEvent re = null
if (e.searchHash != null) {
Set<SharedFile> found = rootToFiles.get e.searchHash
Set<SharedFile> found
found = rootToFiles.get e.searchHash
if (found != null && !found.isEmpty())
re = new ResultsEvent(results: found.asList(), uuid: e.uuid)
} else {

View File

@@ -0,0 +1,10 @@
package com.muwire.core.upload
class Range {
final long start, end
Range(long start, long end) {
this.start = start
this.end = end
}
}

View File

@@ -0,0 +1,16 @@
package com.muwire.core.upload
import com.muwire.core.InfoHash
import net.i2p.data.Base64
class Request {
InfoHash infoHash
Range range
Map<String, String> headers
static Range parse(InfoHash infoHash, InputStream is) throws IOException {
}
}

View File

@@ -0,0 +1,7 @@
package com.muwire.core.upload
import com.muwire.core.Event
public class UploadEvent extends Event {
Uploader uploader
}

View File

@@ -0,0 +1,5 @@
package com.muwire.core.upload
class UploadFinishedEvent {
Uploader uploader
}

View File

@@ -0,0 +1,57 @@
package com.muwire.core.upload
import java.nio.charset.StandardCharsets
import com.muwire.core.EventBus
import com.muwire.core.InfoHash
import com.muwire.core.SharedFile
import com.muwire.core.connection.Endpoint
import com.muwire.core.files.FileManager
import groovy.util.logging.Log
import net.i2p.data.Base64
@Log
public class UploadManager {
private final EventBus eventBus
private final FileManager fileManager
public UploadManager(EventBus eventBus, FileManager fileManager) {
this.eventBus = eventBus
this.fileManager = fileManager
}
public void processEndpoint(Endpoint e) throws IOException {
while(true) {
byte [] infoHashStringBytes = new byte[44]
DataInputStream dis = new DataInputStream(e.getInputStream())
dis.readFully(infoHashStringBytes)
String infoHashString = new String(infoHashStringBytes, StandardCharsets.US_ASCII)
log.info("Responding to upload request for root $infoHashString")
byte [] infoHashRoot = Base64.decode(infoHashStringBytes)
Set<SharedFile> sharedFiles = fileManager.getSharedFiles(infoHashRoot)
if (sharedFiles == null || sharedFiles.isEmpty()) {
log.info "file not found"
e.getOutputStream().write("404 File Not Found".getBytes(StandardCharsets.US_ASCII))
e.getOutputStream().flush()
return
}
byte [] rn = new byte[2]
dis.readFully(rn)
if (rn != "\r\n".getBytes(StandardCharsets.US_ASCII)) {
log.warning("Malformed GET header")
return
}
Request request = Request.parse(new InfoHash(infoHashRoot), e.getInputStream())
Uploader uploader = new Uploader(request, e)
eventBus.publish(new UploadEvent(uploader))
uploader.respond()
eventBus.publish(new UploadFinishedEvent(uploader))
}
}
}

View File

@@ -0,0 +1,17 @@
package com.muwire.core.upload
import com.muwire.core.connection.Endpoint
class Uploader {
private final Request request
private final Endpoint endpoint
Uploader(Request request, Endpoint endpoint) {
this.request = request
this.endpoint = endpoint
}
void respond() {
}
}