diff options
Diffstat (limited to 'src/main')
3 files changed, 59 insertions, 17 deletions
diff --git a/src/main/java/soundchan/BotListener/BotListener.java b/src/main/java/soundchan/BotListener/BotListener.java index c7740c0..6350d6d 100644 --- a/src/main/java/soundchan/BotListener/BotListener.java +++ b/src/main/java/soundchan/BotListener/BotListener.java @@ -19,11 +19,12 @@ import net.dv8tion.jda.core.hooks.ListenerAdapter; import net.dv8tion.jda.core.managers.AudioManager; import soundchan.*; +import java.nio.file.WatchEvent; +import java.sql.SQLOutput; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; -import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; @@ -69,14 +70,28 @@ public class BotListener extends ListenerAdapter{ localManager = new LocalAudioManager(localFilePath, userAudioPath); if(settingEnableCheck(properties.getProperty("watchUserSoundFile"))) { - addWatcherTask(userAudioPath, "watchUserSoundFile"); + MediaWatcherListener listener = (event) -> System.out.println("thing"); + addWatcherTask(listener, userAudioPath, "watchUserSoundFile"); + /*addWatcherTask(new MediaWatcherListener() { + @Override + public void runTask(WatchEvent event) { + System.out.println("Aaaa"); + //localManager.UpdateUserAudio(); + } + }, userAudioPath, "watchUserSoundFile");*/ } } else localManager = new LocalAudioManager(localFilePath); if(settingEnableCheck(properties.getProperty("watchLocalFilePath"))) { - addWatcherTask(localFilePath, "watchLocalFilePath"); + addWatcherTask(new MediaWatcherListener() { + @Override + public void runTask(WatchEvent event) { + System.out.println("Oooo"); + //localManager.UpdateFiles(); + } + }, localFilePath, "watchLocalFilePath"); } } @@ -391,13 +406,14 @@ public class BotListener extends ListenerAdapter{ /** * Adds a new MediaWatcher to the list of running tasks + * @param listener Listener that will get callback during watching of media * @param filepath Path to either directory or file * @param taskName Thing to name task as */ - private void addWatcherTask(String filepath, String taskName) { + private void addWatcherTask(@NotNull MediaWatcherListener listener, String filepath, String taskName) { ExecutorService executorService = Executors.newSingleThreadExecutor(); - MediaWatcher directoryWatcher = new MediaWatcher(localManager, filepath); - otherTasks.put(taskName, executorService.submit(directoryWatcher)); + MediaWatcher watcher = new MediaWatcher(listener, filepath); + otherTasks.put(taskName, executorService.submit(watcher)); executorService.shutdown(); } diff --git a/src/main/java/soundchan/BotListener/MediaWatcher.java b/src/main/java/soundchan/BotListener/MediaWatcher.java index 20f6ee8..684be94 100644 --- a/src/main/java/soundchan/BotListener/MediaWatcher.java +++ b/src/main/java/soundchan/BotListener/MediaWatcher.java @@ -1,7 +1,5 @@ package soundchan.BotListener; -import soundchan.LocalAudioManager; - import java.io.File; import java.io.IOException; import java.nio.file.*; @@ -13,12 +11,13 @@ import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY; public class MediaWatcher implements Runnable { - private LocalAudioManager localAudioManager; + private MediaWatcherListener listener; private String mediaFilename; private Path mediaDir; private WatchService watchService; private WatchKey watchKey; private boolean isDirectory; + private int sleepTime = 5000; @SuppressWarnings("unchecked") static <T> WatchEvent<T> cast(WatchEvent<?> event) { @@ -27,13 +26,32 @@ public class MediaWatcher implements Runnable { /** * Creates a MediaWatcher, which monitors changes to files either within a directory or for a specific file. - * If the given filepath is name of a directory, it is assumed that we want to monitor changes to the sound files. - * If the given filepath is a single file, it is assumed that we want to monitor changes to the userSoundFile. - * @param audioManager AudioManager for this bot instance + * Defaults to scanning every 5 seconds. + * @param listener Object that will get a callback when there is a watch event * @param filepath Path to either directory or specific file */ - public MediaWatcher(LocalAudioManager audioManager, String filepath) { - this.localAudioManager = audioManager; + public MediaWatcher(MediaWatcherListener listener, String filepath) { + this.listener = listener; + startWatchService(filepath); + } + + /** + * Creates a MediaWatcher, which monitors changes to files either within a directory or for a specific file. + * @param listener Object that will get a callback when there is a watch event + * @param filepath Path to either directory or specific file + * @param sleepTime How long to put the scanner thread to sleep between rescans (time in milliseconds) + */ + public MediaWatcher(MediaWatcherListener listener, String filepath, int sleepTime) { + this.listener = listener; + this.sleepTime = sleepTime; + startWatchService(filepath); + } + + /** + * Sets up watch service for the file/directory + * @param filepath Path to file or directory to be scanned + */ + private void startWatchService(String filepath) { File mediaFile = new File(filepath); this.mediaFilename = mediaFile.getName(); if(mediaFile.isFile()) { @@ -70,10 +88,10 @@ public class MediaWatcher implements Runnable { for(WatchEvent<?> event : key.pollEvents()) { WatchEvent<Path> pathEvent = cast(event); if(isDirectory) { - localAudioManager.UpdateFiles(); + listener.runTask(event); } else { if(pathEvent.context().endsWith(mediaFilename)) { - localAudioManager.UpdateUserAudio(); + listener.runTask(event); } } } @@ -82,7 +100,7 @@ public class MediaWatcher implements Runnable { break; } - sleep(5000); + sleep(sleepTime); } } catch(InterruptedException e) { return; diff --git a/src/main/java/soundchan/BotListener/MediaWatcherListener.java b/src/main/java/soundchan/BotListener/MediaWatcherListener.java new file mode 100644 index 0000000..8997a49 --- /dev/null +++ b/src/main/java/soundchan/BotListener/MediaWatcherListener.java @@ -0,0 +1,8 @@ +package soundchan.BotListener; + +import java.nio.file.WatchEvent; + +public interface MediaWatcherListener { + + void runTask(WatchEvent event); +} |