hook up directory manager with share & unshare events

pull/53/head
Zlatin Balevsky 2020-03-26 12:24:07 +00:00
parent faad6b6b0e
commit a560b14d91
5 changed files with 44 additions and 4 deletions

View File

@ -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")

View File

@ -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
}
}

View File

@ -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"
}
}

View File

@ -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)
}

View File

@ -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()
}
}