From 52ced669dd4d3efa1fe0af8d5a1eb871cab27861 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Mon, 17 Jun 2019 16:36:12 +0100 Subject: [PATCH] basic watching of directories --- .../main/groovy/com/muwire/core/Core.groovy | 7 ++ .../muwire/core/files/DirectoryWatcher.groovy | 65 +++++++++++++++++++ .../muwire/core/files/HasherService.groovy | 2 +- 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/core/src/main/groovy/com/muwire/core/Core.groovy b/core/src/main/groovy/com/muwire/core/Core.groovy index e77548c5..4dd272a5 100644 --- a/core/src/main/groovy/com/muwire/core/Core.groovy +++ b/core/src/main/groovy/com/muwire/core/Core.groovy @@ -23,6 +23,7 @@ import com.muwire.core.files.FileSharedEvent import com.muwire.core.files.FileUnsharedEvent import com.muwire.core.files.HasherService import com.muwire.core.files.PersisterService +import com.muwire.core.files.DirectoryWatcher import com.muwire.core.hostcache.CacheClient import com.muwire.core.hostcache.HostCache import com.muwire.core.hostcache.HostDiscoveredEvent @@ -70,6 +71,7 @@ public class Core { private final ConnectionEstablisher connectionEstablisher private final HasherService hasherService private final DownloadManager downloadManager + private final DirectoryWatcher directoryWatcher public Core(MuWireSettings props, File home, String myVersion) { this.home = home @@ -213,6 +215,9 @@ public class Core { connectionAcceptor = new ConnectionAcceptor(eventBus, connectionManager, props, i2pAcceptor, hostCache, trustService, searchManager, uploadManager, connectionEstablisher) + log.info("initializing directory watcher") + directoryWatcher = new DirectoryWatcher(eventBus) + eventBus.register(FileSharedEvent.class, directoryWatcher) log.info("initializing hasher service") hasherService = new HasherService(new FileHasher(), eventBus, fileManager) @@ -221,6 +226,7 @@ public class Core { public void startServices() { hasherService.start() + directoryWatcher.start() trustService.start() trustService.waitForLoad() persisterService.start() @@ -242,6 +248,7 @@ public class Core { connectionEstablisher.stop() log.info("shutting down connection manager") connectionManager.shutdown() + directoryWatcher.stop() } static main(args) { 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 1410786a..295e7557 100644 --- a/core/src/main/groovy/com/muwire/core/files/DirectoryWatcher.groovy +++ b/core/src/main/groovy/com/muwire/core/files/DirectoryWatcher.groovy @@ -1,4 +1,69 @@ package com.muwire.core.files +import java.nio.file.FileSystem +import java.nio.file.FileSystems +import java.nio.file.Path +import java.nio.file.Paths +import java.nio.file.StandardWatchEventKinds +import java.nio.file.WatchEvent +import java.nio.file.WatchKey +import java.nio.file.WatchService + +import com.muwire.core.EventBus + +import groovy.util.logging.Log + +@Log class DirectoryWatcher { + + private final EventBus eventBus + private final Thread watcherThread + private WatchService watchService + + DirectoryWatcher(EventBus eventBus) { + this.eventBus = eventBus + this.watcherThread = new Thread({watch() } as Runnable, "directory-watcher") + } + + void start() { + watchService = FileSystems.getDefault().newWatchService() + watcherThread.start() + } + + void stop() { + watcherThread.interrupt() + watchService.close() + } + + void onFileSharedEvent(FileSharedEvent e) { + if (!e.file.isDirectory()) + return + Path path = e.file.getCanonicalFile().toPath() + path.register(watchService, + StandardWatchEventKinds.ENTRY_MODIFY, + StandardWatchEventKinds.ENTRY_DELETE) + + } + + private void watch() { + while(true) { + WatchKey key = watchService.take() + key.pollEvents().each { + if (it.kind() == StandardWatchEventKinds.ENTRY_MODIFY) + processModified(key.watchable(), it.context()) + } + key.reset() + } + } + + + private void processModified(Path parent, Path path) { + File f = join(parent, path) + eventBus.publish(new FileSharedEvent(file : f)) + } + + private static File join(Path parent, Path path) { + File parentFile = parent.toFile().getCanonicalFile() + new File(parentFile, path.toFile().getName()) + } } 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 9607b1ea..b3701a30 100644 --- a/core/src/main/groovy/com/muwire/core/files/HasherService.groovy +++ b/core/src/main/groovy/com/muwire/core/files/HasherService.groovy @@ -32,7 +32,7 @@ class HasherService { private void process(File f) { f = f.getCanonicalFile() if (f.isDirectory()) { - f.listFiles().each {onFileSharedEvent new FileSharedEvent(file: it) } + f.listFiles().each {eventBus.publish new FileSharedEvent(file: it) } } else { if (f.length() == 0) { eventBus.publish new FileHashedEvent(error: "Not sharing empty file $f")