basic watching of directories
This commit is contained in:
@ -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) {
|
||||
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
@ -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")
|
||||
|
Reference in New Issue
Block a user