diff options
| author | Matt Kohls <mattkohls13@gmail.com> | 2018-11-12 16:43:25 -0500 | 
|---|---|---|
| committer | Matt Kohls <mattkohls13@gmail.com> | 2018-11-12 16:43:25 -0500 | 
| commit | ede0d1d78371f7980b98beaf94d2dd642077a762 (patch) | |
| tree | 606320979e4289a1f062ad63cd2634b2f125687c /src/main/java | |
| parent | 4db359db8fa95424446f5789a1af3fac7e800bda (diff) | |
| download | SoundChan-ede0d1d78371f7980b98beaf94d2dd642077a762.tar.gz SoundChan-ede0d1d78371f7980b98beaf94d2dd642077a762.tar.bz2 SoundChan-ede0d1d78371f7980b98beaf94d2dd642077a762.zip | |
Cleaning up watcher stuff
Diffstat (limited to 'src/main/java')
3 files changed, 1 insertions, 132 deletions
| 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 <T> WatchEvent<T> cast(WatchEvent<?> event) { -        return (WatchEvent<T>) 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<Path> 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 <T> WatchEvent<T> cast(WatchEvent<?> event) { -        return (WatchEvent<T>) 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<Path> pathEvent = cast(event); -                    if(pathEvent.context().endsWith(userSoundFile)) { -                        localAudioManager.UpdateUserAudio(); -                    } -                } - -                if(!key.reset()) { -                    break; -                } -            } -        } catch(InterruptedException e) { -            return; -        } -    } -} | 
