mirror of https://github.com/zlatinb/muwire
hook up directory manager with share & unshare events
parent
faad6b6b0e
commit
a560b14d91
|
@ -420,6 +420,8 @@ public class Core {
|
||||||
eventBus.with {
|
eventBus.with {
|
||||||
register(WatchedDirectoryConfigurationEvent.class, watchedDirectoryManager)
|
register(WatchedDirectoryConfigurationEvent.class, watchedDirectoryManager)
|
||||||
register(WatchedDirectoryConvertedEvent.class, watchedDirectoryManager)
|
register(WatchedDirectoryConvertedEvent.class, watchedDirectoryManager)
|
||||||
|
register(FileSharedEvent.class, watchedDirectoryManager)
|
||||||
|
register(DirectoryUnsharedEvent.class, watchedDirectoryManager)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.info("initializing directory watcher")
|
log.info("initializing directory watcher")
|
||||||
|
|
|
@ -104,7 +104,7 @@ class DirectoryWatcher {
|
||||||
File f= join(parent, path)
|
File f= join(parent, path)
|
||||||
log.fine("created entry $f")
|
log.fine("created entry $f")
|
||||||
if (f.isDirectory())
|
if (f.isDirectory())
|
||||||
f.toPath().register(watchService, kinds)
|
eventBus.publish(new FileSharedEvent(file : f, fromWatch : true))
|
||||||
else
|
else
|
||||||
waitingFiles.put(f, System.currentTimeMillis())
|
waitingFiles.put(f, System.currentTimeMillis())
|
||||||
}
|
}
|
||||||
|
@ -142,7 +142,7 @@ class DirectoryWatcher {
|
||||||
waitingFiles.each { file, timestamp ->
|
waitingFiles.each { file, timestamp ->
|
||||||
if (now - timestamp > WAIT_TIME) {
|
if (now - timestamp > WAIT_TIME) {
|
||||||
log.fine("publishing file $file")
|
log.fine("publishing file $file")
|
||||||
eventBus.publish new FileSharedEvent(file : file)
|
eventBus.publish new FileSharedEvent(file : file, fromWatch: true)
|
||||||
published << file
|
published << file
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,9 +5,10 @@ import com.muwire.core.Event
|
||||||
class FileSharedEvent extends Event {
|
class FileSharedEvent extends Event {
|
||||||
|
|
||||||
File file
|
File file
|
||||||
|
boolean fromWatch
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return super.toString() + " file: "+file.getAbsolutePath()
|
return super.toString() + " file: "+file.getAbsolutePath() + " fromWatch: $fromWatch"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,7 +53,6 @@ class HasherService {
|
||||||
|
|
||||||
private void process(File f) {
|
private void process(File f) {
|
||||||
if (f.isDirectory()) {
|
if (f.isDirectory()) {
|
||||||
eventBus.publish(new DirectoryWatchedEvent(directory : f))
|
|
||||||
f.listFiles().each {
|
f.listFiles().each {
|
||||||
eventBus.publish new FileSharedEvent(file: it)
|
eventBus.publish new FileSharedEvent(file: it)
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,12 +7,16 @@ import java.util.concurrent.Executors
|
||||||
import java.util.concurrent.ThreadFactory
|
import java.util.concurrent.ThreadFactory
|
||||||
|
|
||||||
import com.muwire.core.EventBus
|
import com.muwire.core.EventBus
|
||||||
|
import com.muwire.core.files.DirectoryUnsharedEvent
|
||||||
import com.muwire.core.files.DirectoryWatchedEvent
|
import com.muwire.core.files.DirectoryWatchedEvent
|
||||||
import com.muwire.core.files.FileManager
|
import com.muwire.core.files.FileManager
|
||||||
|
import com.muwire.core.files.FileSharedEvent
|
||||||
|
|
||||||
import groovy.json.JsonOutput
|
import groovy.json.JsonOutput
|
||||||
import groovy.json.JsonSlurper
|
import groovy.json.JsonSlurper
|
||||||
|
import groovy.util.logging.Log
|
||||||
|
|
||||||
|
@Log
|
||||||
class WatchedDirectoryManager {
|
class WatchedDirectoryManager {
|
||||||
|
|
||||||
private final File home
|
private final File home
|
||||||
|
@ -83,4 +87,38 @@ class WatchedDirectoryManager {
|
||||||
targetFile.text = json
|
targetFile.text = json
|
||||||
} as Runnable)
|
} 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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue