From 84a9bb9482effc5597ace120a725c46753e568d8 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Tue, 18 Jun 2019 04:15:44 +0100 Subject: [PATCH] watch deleting of files --- core/src/main/groovy/com/muwire/core/Core.groovy | 2 +- .../com/muwire/core/files/DirectoryWatcher.groovy | 14 +++++++++++++- .../models/com/muwire/gui/MainFrameModel.groovy | 13 +++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/Core.groovy b/core/src/main/groovy/com/muwire/core/Core.groovy index f844de58..5dee8d85 100644 --- a/core/src/main/groovy/com/muwire/core/Core.groovy +++ b/core/src/main/groovy/com/muwire/core/Core.groovy @@ -217,7 +217,7 @@ public class Core { i2pAcceptor, hostCache, trustService, searchManager, uploadManager, connectionEstablisher) log.info("initializing directory watcher") - directoryWatcher = new DirectoryWatcher(eventBus) + directoryWatcher = new DirectoryWatcher(eventBus, fileManager) eventBus.register(FileSharedEvent.class, directoryWatcher) log.info("initializing hasher service") 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 d2b162ef..01540058 100644 --- a/core/src/main/groovy/com/muwire/core/files/DirectoryWatcher.groovy +++ b/core/src/main/groovy/com/muwire/core/files/DirectoryWatcher.groovy @@ -11,6 +11,7 @@ import java.nio.file.WatchService import java.util.concurrent.ConcurrentHashMap import com.muwire.core.EventBus +import com.muwire.core.SharedFile import groovy.util.logging.Log @@ -20,13 +21,15 @@ class DirectoryWatcher { private static final long WAIT_TIME = 1000 private final EventBus eventBus + private final FileManager fileManager private final Thread watcherThread, publisherThread private final Map waitingFiles = new ConcurrentHashMap<>() private WatchService watchService private volatile boolean shutdown - DirectoryWatcher(EventBus eventBus) { + DirectoryWatcher(EventBus eventBus, FileManager fileManager) { this.eventBus = eventBus + this.fileManager = fileManager this.watcherThread = new Thread({watch() } as Runnable, "directory-watcher") watcherThread.setDaemon(true) this.publisherThread = new Thread({publish()} as Runnable, "watched-files-publisher") @@ -63,6 +66,8 @@ class DirectoryWatcher { key.pollEvents().each { if (it.kind() == StandardWatchEventKinds.ENTRY_MODIFY) processModified(key.watchable(), it.context()) + if (it.kind() == StandardWatchEventKinds.ENTRY_DELETE) + processDeleted(key.watchable(), it.context()) } key.reset() } @@ -78,6 +83,13 @@ class DirectoryWatcher { waitingFiles.put(f, System.currentTimeMillis()) } + private void processDeleted(Path parent, Path path) { + File f = join(parent, path) + SharedFile sf = fileManager.fileToSharedFile.get(f) + if (sf != null) + eventBus.publish(new FileUnsharedEvent(unsharedFile : sf)) + } + private static File join(Path parent, Path path) { File parentFile = parent.toFile().getCanonicalFile() new File(parentFile, path.toFile().getName()) diff --git a/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy b/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy index 9cc4710f..90b7911a 100644 --- a/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy +++ b/gui/griffon-app/models/com/muwire/gui/MainFrameModel.groovy @@ -20,6 +20,7 @@ import com.muwire.core.files.FileDownloadedEvent import com.muwire.core.files.FileHashedEvent import com.muwire.core.files.FileLoadedEvent import com.muwire.core.files.FileSharedEvent +import com.muwire.core.files.FileUnsharedEvent import com.muwire.core.search.QueryEvent import com.muwire.core.search.UIResultBatchEvent import com.muwire.core.search.UIResultEvent @@ -133,6 +134,7 @@ class MainFrameModel { core.eventBus.register(QueryEvent.class, this) core.eventBus.register(UpdateAvailableEvent.class, this) core.eventBus.register(FileDownloadedEvent.class, this) + core.eventBus.register(FileUnsharedEvent.class, this) timer.schedule({ int retryInterval = core.muOptions.downloadRetryInterval @@ -242,6 +244,17 @@ class MainFrameModel { } } + void onFileUnsharedEvent(FileUnsharedEvent e) { + InfoHash infohash = e.unsharedFile.infoHash + if (!infoHashes.remove(infohash)) + return + runInsideUIAsync { + shared.remove(e.unsharedFile) + JTable table = builder.getVariable("shared-files-table") + table.model.fireTableDataChanged() + } + } + void onUploadEvent(UploadEvent e) { runInsideUIAsync { uploads << e.uploader