From ede0d1d78371f7980b98beaf94d2dd642077a762 Mon Sep 17 00:00:00 2001 From: Matt Kohls Date: Mon, 12 Nov 2018 16:43:25 -0500 Subject: Cleaning up watcher stuff --- .../soundchan/BotListener/DirectoryWatcher.java | 62 ------------------- .../java/soundchan/BotListener/MediaWatcher.java | 2 +- .../soundchan/BotListener/UserSoundWatcher.java | 69 ---------------------- 3 files changed, 1 insertion(+), 132 deletions(-) delete mode 100644 src/main/java/soundchan/BotListener/DirectoryWatcher.java delete mode 100644 src/main/java/soundchan/BotListener/UserSoundWatcher.java diff --git a/src/main/java/soundchan/BotListener/DirectoryWatcher.java b/src/main/java/soundchan/BotListener/DirectoryWatcher.java deleted file mode 100644 index 7d63bd1..0000000 --- a/src/main/java/soundchan/BotListener/DirectoryWatcher.java +++ /dev/null @@ -1,62 +0,0 @@ -package soundchan.BotListener; - -import soundchan.LocalAudioManager; - -import java.io.File; -import java.io.IOException; -import java.nio.file.*; - -import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE; -import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE; -import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY; - -public class DirectoryWatcher implements Runnable { - - private LocalAudioManager localAudioManager; - private Path soundDir; - private WatchService watchService; - private WatchKey watchKey; - - @SuppressWarnings("unchecked") - static WatchEvent cast(WatchEvent event) { - return (WatchEvent) event; - } - - public DirectoryWatcher(LocalAudioManager audioManager, String filepath) { - this.localAudioManager = audioManager; - this.soundDir = new File(filepath).toPath(); - try { - this.watchService = FileSystems.getDefault().newWatchService(); - this.watchKey = soundDir.register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); - } catch(IOException e) { - System.out.println("Error setting up watcher for " + filepath); - } - } - - /** - * Called by an executor, checks for changes in the directory - */ - public void run() { - try { - while(true) { - WatchKey key = watchService.take(); - if(this.watchKey != key) { - System.out.println("Error with WatchKey"); - continue; - } - - for(WatchEvent event : key.pollEvents()) { - WatchEvent pathEvent = cast(event); - //System.out.format("%s: %s\n", pathEvent.kind(), soundDir.resolve(pathEvent.context())); - localAudioManager.UpdateFiles(); - } - - if(!key.reset()) { - break; - } - } - } catch(InterruptedException e) { - return; - } - } -} diff --git a/src/main/java/soundchan/BotListener/MediaWatcher.java b/src/main/java/soundchan/BotListener/MediaWatcher.java index 1f15cc1..20f6ee8 100644 --- a/src/main/java/soundchan/BotListener/MediaWatcher.java +++ b/src/main/java/soundchan/BotListener/MediaWatcher.java @@ -56,7 +56,7 @@ public class MediaWatcher implements Runnable { } /** - * Called by an executor, checks for changes in the directory + * Called by an executor, checks for changes to file(s) */ public void run() { try { diff --git a/src/main/java/soundchan/BotListener/UserSoundWatcher.java b/src/main/java/soundchan/BotListener/UserSoundWatcher.java deleted file mode 100644 index 4c10cef..0000000 --- a/src/main/java/soundchan/BotListener/UserSoundWatcher.java +++ /dev/null @@ -1,69 +0,0 @@ -package soundchan.BotListener; - -import soundchan.LocalAudioManager; - -import java.io.File; -import java.io.IOException; -import java.nio.file.*; - -import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY; - -public class UserSoundWatcher implements Runnable { - - private LocalAudioManager localAudioManager; - private String userSoundFile; - private Path soundFileDirectory; - private WatchService watchService; - private WatchKey watchKey; - - @SuppressWarnings("unchecked") - static WatchEvent cast(WatchEvent event) { - return (WatchEvent) event; - } - - public UserSoundWatcher(LocalAudioManager audioManager, String filepath) { - this.localAudioManager = audioManager; - File soundFile = new File(filepath); - this.userSoundFile = soundFile.getName(); - try { - this.soundFileDirectory = soundFile.getCanonicalFile().getParentFile().toPath(); - } catch(IOException e) { - System.out.println("Error getting parent path of " + userSoundFile); - } - try { - this.watchService = FileSystems.getDefault().newWatchService(); - this.watchKey = soundFileDirectory.register(watchService, ENTRY_MODIFY); - } catch(IOException e) { - System.out.println(e.getMessage()); - System.out.println("Error setting up watcher for " + filepath); - } - } - - /** - * Called by an executor, checks for changes of the userSoundFile - */ - public void run() { - try { - while(true) { - WatchKey key = watchService.take(); - if(this.watchKey != key) { - System.out.println("Error with WatchKey"); - continue; - } - - for(WatchEvent event : key.pollEvents()) { - WatchEvent pathEvent = cast(event); - if(pathEvent.context().endsWith(userSoundFile)) { - localAudioManager.UpdateUserAudio(); - } - } - - if(!key.reset()) { - break; - } - } - } catch(InterruptedException e) { - return; - } - } -} -- cgit v1.2.3