diff --git a/core/src/main/groovy/com/muwire/core/files/FileManager.groovy b/core/src/main/groovy/com/muwire/core/files/FileManager.groovy new file mode 100644 index 00000000..e9db4929 --- /dev/null +++ b/core/src/main/groovy/com/muwire/core/files/FileManager.groovy @@ -0,0 +1,59 @@ +package com.muwire.core.files + +import com.muwire.core.EventBus +import com.muwire.core.SharedFile + +class FileManager { + + + final EventBus eventBus + final Map> rootToFiles = new HashMap<>() + final Map fileToSharedFile = new HashMap<>() + + FileManager(EventBus eventBus) { + this.eventBus = eventBus + } + + void onFileHashedEvent(FileHashedEvent e) { + addToIndex(e.sharedFile) + } + + void onFileLoadedEvent(FileLoadedEvent e) { + addToIndex(e.loadedFile) + } + + void onFileDownloadedEvent(FileDownloadedEvent e) { + addToIndex(e.downloadedFile) + } + + private void addToIndex(SharedFile sf) { + byte [] root = sf.getInfoHash().getRoot() + Set existing = rootToFiles.get(root) + if (existing == null) { + existing = new HashSet<>() + rootToFiles.put(root, existing); + } + existing.add(sf) + fileToSharedFile.put(sf.file, sf) + } + + Map getSharedFiles() { + // TODO: figure out locking + fileToSharedFile + } + + void onSearchEvent(SearchEvent e) { + // hash takes precedence + ResultsEvent re = null + if (e.searchHash != null) { + Set found = rootToFiles.get e.searchHash + if (found != null && !found.isEmpty()) + re = new ResultsEvent(results: found.asList(), uuid: e.uuid) + } else { + // TODO: keyword search + } + + if (re != null) + eventBus.publish(re) + } +} diff --git a/core/src/main/groovy/com/muwire/core/files/ResultsEvent.groovy b/core/src/main/groovy/com/muwire/core/files/ResultsEvent.groovy new file mode 100644 index 00000000..e78694ee --- /dev/null +++ b/core/src/main/groovy/com/muwire/core/files/ResultsEvent.groovy @@ -0,0 +1,11 @@ +package com.muwire.core.files + +import com.muwire.core.Event +import com.muwire.core.SharedFile + +class ResultsEvent extends Event { + + SharedFile[] results + UUID uuid + +} diff --git a/core/src/main/groovy/com/muwire/core/files/SearchEvent.groovy b/core/src/main/groovy/com/muwire/core/files/SearchEvent.groovy new file mode 100644 index 00000000..a045e1b8 --- /dev/null +++ b/core/src/main/groovy/com/muwire/core/files/SearchEvent.groovy @@ -0,0 +1,10 @@ +package com.muwire.core.files + +import com.muwire.core.Event + +class SearchEvent extends Event { + + String[] searchTerms + byte [] searchHash + UUID uuid +}