001package jmri.jmrit.audio;
002
003import org.slf4j.Logger;
004import org.slf4j.LoggerFactory;
005
006/**
007 * Represents a thread for processing commands contained within AudioCommand
008 * objects. All commands are processed in the order in which thet were queued
009 *
010 * <hr>
011 * This file is part of JMRI.
012 * <p>
013 * JMRI is free software; you can redistribute it and/or modify it under the
014 * terms of version 2 of the GNU General Public License as published by the Free
015 * Software Foundation. See the "COPYING" file for a copy of this license.
016 * <p>
017 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
018 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
019 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
020 *
021 * @author Matthew Harris copyright (c) 2009
022 */
023public class AudioCommandThread extends AbstractAudioThread {
024
025    /**
026     * Reference to active audio factory
027     */
028    private AudioFactory activeAudioFactory;
029
030    /**
031     * Constructor that takes handle to current active audio factory object
032     *
033     * @param activeAudioFactory handle to activeAudioFactory
034     */
035    public AudioCommandThread(AudioFactory activeAudioFactory) {
036        super();
037        this.setName("command-" + super.getName());
038        this.activeAudioFactory = activeAudioFactory;
039        if (log.isDebugEnabled()) {
040            log.debug("Created AudioThread for AudioFactory {}", activeAudioFactory.toString());
041        }
042    }
043
044    /**
045     * Main loop for processing commands. Starts out asleep, and also sleeps
046     * once finished processing commands, so must be interrupted to process any
047     * queued commands.
048     */
049    @Override
050    public void run() {
051
052        // Start out asleep (5 minutes)
053        snooze(300000);
054
055        while (!dying()) {
056            // Process the command queue
057            activeAudioFactory.audioCommandQueue(null);
058
059            // Wait for more commands (5 minutes)
060            if (!dying()) {
061                snooze(300000);
062            }
063        }
064
065        // Finish up
066        if (log.isDebugEnabled()) {
067            log.debug("Clean up thread {}", this.getName());
068        }
069        cleanup();
070    }
071
072    /**
073     * Shuts this thread down and clears references to created objects
074     */
075    @Override
076    protected void cleanup() {
077
078        // Thread is to shutdown
079        die();
080
081        // Clear references to objects
082        this.activeAudioFactory = null;
083
084        // Finalise cleanup in super-class
085        super.cleanup();
086    }
087
088    private static final Logger log = LoggerFactory.getLogger(AudioCommandThread.class);
089
090}