From a560b14d91bb4dbc57eac0b5a1271a11eeb1a702 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Thu, 26 Mar 2020 12:24:07 +0000 Subject: [PATCH] hook up directory manager with share & unshare events --- .../main/groovy/com/muwire/core/Core.groovy | 2 + .../muwire/core/files/DirectoryWatcher.groovy | 4 +- .../muwire/core/files/FileSharedEvent.groovy | 3 +- .../muwire/core/files/HasherService.groovy | 1 - .../WatchedDirectoryManager.groovy | 38 +++++++++++++++++++ 5 files changed, 44 insertions(+), 4 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/Core.groovy b/core/src/main/groovy/com/muwire/core/Core.groovy index 9bc7acf6..aff08214 100644 --- a/core/src/main/groovy/com/muwire/core/Core.groovy +++ b/core/src/main/groovy/com/muwire/core/Core.groovy @@ -420,6 +420,8 @@ public class Core { eventBus.with { register(WatchedDirectoryConfigurationEvent.class, watchedDirectoryManager) register(WatchedDirectoryConvertedEvent.class, watchedDirectoryManager) + register(FileSharedEvent.class, watchedDirectoryManager) + register(DirectoryUnsharedEvent.class, watchedDirectoryManager) } log.info("initializing directory watcher") diff --git a/core/src/main/groovy/com/muwire/core/files/DirectoryWatcher.groovy b/core/src/main/groovy/com/muwire/core/files/DirectoryWatcher.groovy index 83d8e291..dca090b8 100644 --- a/core/src/main/groovy/com/muwire/core/files/DirectoryWatcher.groovy +++ b/core/src/main/groovy/com/muwire/core/files/DirectoryWatcher.groovy @@ -104,7 +104,7 @@ class DirectoryWatcher { File f= join(parent, path) log.fine("created entry $f") if (f.isDirectory()) - f.toPath().register(watchService, kinds) + eventBus.publish(new FileSharedEvent(file : f, fromWatch : true)) else waitingFiles.put(f, System.currentTimeMillis()) } @@ -142,7 +142,7 @@ class DirectoryWatcher { waitingFiles.each { file, timestamp -> if (now - timestamp > WAIT_TIME) { log.fine("publishing file $file") - eventBus.publish new FileSharedEvent(file : file) + eventBus.publish new FileSharedEvent(file : file, fromWatch: true) published << file } } diff --git a/core/src/main/groovy/com/muwire/core/files/FileSharedEvent.groovy b/core/src/main/groovy/com/muwire/core/files/FileSharedEvent.groovy index 4d67bbcb..a6bdd725 100644 --- a/core/src/main/groovy/com/muwire/core/files/FileSharedEvent.groovy +++ b/core/src/main/groovy/com/muwire/core/files/FileSharedEvent.groovy @@ -5,9 +5,10 @@ import com.muwire.core.Event class FileSharedEvent extends Event { File file + boolean fromWatch @Override public String toString() { - return super.toString() + " file: "+file.getAbsolutePath() + return super.toString() + " file: "+file.getAbsolutePath() + " fromWatch: $fromWatch" } } diff --git a/core/src/main/groovy/com/muwire/core/files/HasherService.groovy b/core/src/main/groovy/com/muwire/core/files/HasherService.groovy index 79856547..6a496a8a 100644 --- a/core/src/main/groovy/com/muwire/core/files/HasherService.groovy +++ b/core/src/main/groovy/com/muwire/core/files/HasherService.groovy @@ -53,7 +53,6 @@ class HasherService { private void process(File f) { if (f.isDirectory()) { - eventBus.publish(new DirectoryWatchedEvent(directory : f)) f.listFiles().each { eventBus.publish new FileSharedEvent(file: it) } diff --git a/core/src/main/groovy/com/muwire/core/files/directories/WatchedDirectoryManager.groovy b/core/src/main/groovy/com/muwire/core/files/directories/WatchedDirectoryManager.groovy index 6fa1a096..d02d54dc 100644 --- a/core/src/main/groovy/com/muwire/core/files/directories/WatchedDirectoryManager.groovy +++ b/core/src/main/groovy/com/muwire/core/files/directories/WatchedDirectoryManager.groovy @@ -7,12 +7,16 @@ import java.util.concurrent.Executors import java.util.concurrent.ThreadFactory import com.muwire.core.EventBus +import com.muwire.core.files.DirectoryUnsharedEvent import com.muwire.core.files.DirectoryWatchedEvent import com.muwire.core.files.FileManager +import com.muwire.core.files.FileSharedEvent import groovy.json.JsonOutput import groovy.json.JsonSlurper +import groovy.util.logging.Log +@Log class WatchedDirectoryManager { private final File home @@ -83,4 +87,38 @@ class WatchedDirectoryManager { targetFile.text = json } as Runnable) } + + void onFileSharedEvent(FileSharedEvent e) { + if (e.file.isFile()) + return + + def wd = new WatchedDirectory(e.file) + if (e.fromWatch) { + // parent should be already watched, copy settings + def parent = watchedDirs.get(e.file.getParentFile()) + if (parent == null) { + log.severe("watching found a directory without a watched parent? ${e.file}") + return + } + wd.autoWatch = parent.autoWatch + wd.syncInterval = parent.syncInterval + } else + wd.autoWatch = true + + watchedDirs.put(wd.directory, wd) + persist(wd) + if (wd.autoWatch) + eventBus.publish(new DirectoryWatchedEvent(directory: wd.directory)) + } + + void onDirectoryUnsharedEvent(DirectoryUnsharedEvent e) { + def wd = watchedDirs.remove(e.directory) + if (wd == null) { + log.warning("unshared a directory that wasn't watched? ${e.directory}") + return + } + + File persistFile = new File(home, wd.getEncodedName() + ".json") + persistFile.delete() + } }