Search in sources :

Example 1 with ThreadExecutionQueue

use of net.robinfriedli.aiode.concurrent.ThreadExecutionQueue in project aiode by robinfriedli.

the class CommandListener method startCommandExecution.

private void startCommandExecution(String namePrefix, Message message, Guild guild, GuildContext guildContext, Session session, GuildMessageReceivedEvent event) {
    ThreadExecutionQueue queue = executionQueueManager.getForGuild(guild);
    String commandBody = message.getContentDisplay().substring(namePrefix.length()).trim();
    CommandContext commandContext = new CommandContext(event, guildContext, hibernateComponent.getSessionFactory(), spotifyApiBuilder, commandBody);
    ExecutionContext.Current.set(commandContext.fork());
    try {
        Optional<AbstractCommand> commandInstance = commandManager.instantiateCommandForContext(commandContext, session);
        commandInstance.ifPresent(command -> commandManager.runCommand(command, queue));
    } catch (UserException e) {
        EmbedBuilder embedBuilder = e.buildEmbed();
        messageService.sendTemporary(embedBuilder.build(), commandContext.getChannel());
    }
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) CommandContext(net.robinfriedli.aiode.command.CommandContext) ThreadExecutionQueue(net.robinfriedli.aiode.concurrent.ThreadExecutionQueue) AbstractCommand(net.robinfriedli.aiode.command.AbstractCommand) UserException(net.robinfriedli.aiode.exceptions.UserException)

Example 2 with ThreadExecutionQueue

use of net.robinfriedli.aiode.concurrent.ThreadExecutionQueue in project aiode by robinfriedli.

the class AudioTrafficSimulationCommand method runAdmin.

@Override
public void runAdmin() throws Exception {
    Aiode aiode = Aiode.get();
    AudioManager audioManager = aiode.getAudioManager();
    AudioPlayerManager playerManager = audioManager.getPlayerManager();
    PlayableFactory playableFactory = audioManager.createPlayableFactory(getSpotifyService(), new BlockingTrackLoadingExecutor());
    CommandContext context = getContext();
    Guild guild = context.getGuild();
    ThreadExecutionQueue threadExecutionQueue = aiode.getExecutionQueueManager().getForGuild(guild);
    SpotifyApi spotifyApi = context.getSpotifyApi();
    AudioTrackLoader audioTrackLoader = new AudioTrackLoader(playerManager);
    int streams = getArgumentValueOrElse("streams", DEFAULT_STREAM_COUNT);
    int durationSecs = getArgumentValueOrElse("duration", DEFAULT_DURATION);
    String playbackUrl = getArgumentValueOrElse("url", DEFAULT_PLAYBACK_URL);
    Integer nativeBuffer = getArgumentValueWithTypeOrElse("nativeBuffer", Integer.class, null);
    List<Playable> playables = playableFactory.createPlayables(playbackUrl, spotifyApi, true);
    if (playables.isEmpty()) {
        throw new InvalidCommandException("No playables found for url " + playbackUrl);
    }
    Playable playable = playables.get(0);
    AudioItem audioItem = audioTrackLoader.loadByIdentifier(playable.getPlaybackUrl());
    AudioTrack track;
    if (audioItem instanceof AudioTrack) {
        track = (AudioTrack) audioItem;
    } else {
        throw new IllegalStateException("Could not get AudioTrack for Playable " + playable);
    }
    LoopTrackListener loopTrackListener = new LoopTrackListener(track);
    List<Thread> playbackThreads = nativeBuffer != null ? null : Lists.newArrayListWithCapacity(streams);
    List<Tuple2<IAudioSendSystem, AudioPlayer>> audioSendSystems = nativeBuffer != null ? Lists.newArrayListWithCapacity(streams) : null;
    NativeAudioSendFactory nativeAudioSendFactory = nativeBuffer != null ? new NativeAudioSendFactory(nativeBuffer) : null;
    LoggingUncaughtExceptionHandler eh = new LoggingUncaughtExceptionHandler();
    for (int i = 0; i < streams; i++) {
        AudioPlayer player = playerManager.createPlayer();
        player.addListener(loopTrackListener);
        player.playTrack(track.makeClone());
        if (nativeAudioSendFactory != null) {
            IAudioSendSystem sendSystem = nativeAudioSendFactory.createSendSystem(new FakePacketProvider(player));
            audioSendSystems.add(new Tuple2<>(sendSystem, player));
        } else {
            Thread playbackThread = new Thread(new PlayerPollingRunnable(player));
            playbackThread.setDaemon(true);
            playbackThread.setUncaughtExceptionHandler(eh);
            playbackThread.setName("simulated-playback-thread-" + i);
            playbackThreads.add(playbackThread);
        }
    }
    QueuedTask playbackThreadsManagementTask = new QueuedTask(threadExecutionQueue, new FakePlayerManagementTask(playbackThreads, audioSendSystems, durationSecs)) {

        @Override
        protected boolean isPrivileged() {
            return true;
        }
    };
    playbackThreadsManagementTask.setName("simulated-playback-management-task");
    threadExecutionQueue.add(playbackThreadsManagementTask, false);
}
Also used : CommandContext(net.robinfriedli.aiode.command.CommandContext) NativeAudioSendFactory(com.sedmelluq.discord.lavaplayer.jdaudp.NativeAudioSendFactory) ThreadExecutionQueue(net.robinfriedli.aiode.concurrent.ThreadExecutionQueue) AudioTrackLoader(net.robinfriedli.aiode.audio.AudioTrackLoader) AudioPlayer(com.sedmelluq.discord.lavaplayer.player.AudioPlayer) Guild(net.dv8tion.jda.api.entities.Guild) AudioItem(com.sedmelluq.discord.lavaplayer.track.AudioItem) AudioManager(net.robinfriedli.aiode.audio.AudioManager) SpotifyApi(se.michaelthelin.spotify.SpotifyApi) LoggingUncaughtExceptionHandler(net.robinfriedli.aiode.exceptions.handler.handlers.LoggingUncaughtExceptionHandler) BlockingTrackLoadingExecutor(net.robinfriedli.aiode.audio.exec.BlockingTrackLoadingExecutor) PlayableFactory(net.robinfriedli.aiode.audio.PlayableFactory) IAudioSendSystem(net.dv8tion.jda.api.audio.factory.IAudioSendSystem) Aiode(net.robinfriedli.aiode.Aiode) QueuedTask(net.robinfriedli.aiode.concurrent.QueuedTask) AudioPlayerManager(com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) Playable(net.robinfriedli.aiode.audio.Playable) Tuple2(groovy.lang.Tuple2) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack)

Example 3 with ThreadExecutionQueue

use of net.robinfriedli.aiode.concurrent.ThreadExecutionQueue in project aiode by robinfriedli.

the class AbortCommand method doRun.

@Override
public void doRun() {
    CommandExecutionTask commandExecutionTask = getTask();
    Thread abortThread = new Thread(() -> {
        if (commandExecutionTask != null) {
            try {
                commandExecutionTask.await();
            } catch (InterruptedException ignored) {
            }
        }
        CommandExecutionQueueManager executionQueueManager = Aiode.get().getExecutionQueueManager();
        GuildContext guildContext = getContext().getGuildContext();
        ThreadExecutionQueue executionQueue = executionQueueManager.getForGuild(getContext().getGuild());
        PooledTrackLoadingExecutor pooledTrackLoadingExecutor = guildContext.getPooledTrackLoadingExecutor();
        ReplaceableTrackLoadingExecutor replaceableTrackLoadingExecutor = guildContext.getReplaceableTrackLoadingExecutor();
        if (executionQueue.isIdle() && pooledTrackLoadingExecutor.isIdle() && replaceableTrackLoadingExecutor.isIdle()) {
            EmbedBuilder embedBuilder = new EmbedBuilder();
            embedBuilder.setDescription("No commands are currently running");
            getMessageService().sendTemporary(embedBuilder, getContext().getChannel());
            setFailed(true);
        } else {
            executionQueue.abortAll();
            pooledTrackLoadingExecutor.abortAll();
            replaceableTrackLoadingExecutor.abort();
            sendSuccess("Sent all currently running commands an interrupt signal and cancelled queued commands.");
        }
    });
    abortThread.setName("aiode abort thread");
    abortThread.setUncaughtExceptionHandler(new LoggingUncaughtExceptionHandler());
    abortThread.start();
}
Also used : CommandExecutionTask(net.robinfriedli.aiode.concurrent.CommandExecutionTask) GuildContext(net.robinfriedli.aiode.discord.GuildContext) PooledTrackLoadingExecutor(net.robinfriedli.aiode.audio.exec.PooledTrackLoadingExecutor) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) ReplaceableTrackLoadingExecutor(net.robinfriedli.aiode.audio.exec.ReplaceableTrackLoadingExecutor) ThreadExecutionQueue(net.robinfriedli.aiode.concurrent.ThreadExecutionQueue) LoggingUncaughtExceptionHandler(net.robinfriedli.aiode.exceptions.handler.handlers.LoggingUncaughtExceptionHandler) CommandExecutionQueueManager(net.robinfriedli.aiode.concurrent.CommandExecutionQueueManager)

Aggregations

ThreadExecutionQueue (net.robinfriedli.aiode.concurrent.ThreadExecutionQueue)3 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)2 CommandContext (net.robinfriedli.aiode.command.CommandContext)2 LoggingUncaughtExceptionHandler (net.robinfriedli.aiode.exceptions.handler.handlers.LoggingUncaughtExceptionHandler)2 NativeAudioSendFactory (com.sedmelluq.discord.lavaplayer.jdaudp.NativeAudioSendFactory)1 AudioPlayer (com.sedmelluq.discord.lavaplayer.player.AudioPlayer)1 AudioPlayerManager (com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager)1 AudioItem (com.sedmelluq.discord.lavaplayer.track.AudioItem)1 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)1 Tuple2 (groovy.lang.Tuple2)1 IAudioSendSystem (net.dv8tion.jda.api.audio.factory.IAudioSendSystem)1 Guild (net.dv8tion.jda.api.entities.Guild)1 Aiode (net.robinfriedli.aiode.Aiode)1 AudioManager (net.robinfriedli.aiode.audio.AudioManager)1 AudioTrackLoader (net.robinfriedli.aiode.audio.AudioTrackLoader)1 Playable (net.robinfriedli.aiode.audio.Playable)1 PlayableFactory (net.robinfriedli.aiode.audio.PlayableFactory)1 BlockingTrackLoadingExecutor (net.robinfriedli.aiode.audio.exec.BlockingTrackLoadingExecutor)1 PooledTrackLoadingExecutor (net.robinfriedli.aiode.audio.exec.PooledTrackLoadingExecutor)1 ReplaceableTrackLoadingExecutor (net.robinfriedli.aiode.audio.exec.ReplaceableTrackLoadingExecutor)1