Path: blob/main/src/lwjgl/java/paulscode/sound/CommandThread.java
8644 views
package paulscode.sound;12/**3* The CommandThread class is designed to move all command processing into a4* single thread to be run in the background and avoid conflicts between5* threads. Commands are processed in the order that they were queued. The6* arguements for each command are stored in a7* {@link paulscode.sound.CommandObject CommandObject}. The Command Queue is8* located in the {@link paulscode.sound.SoundSystem SoundSystem} class.9* Calling kill() stops the thread, and this should be immediatly followed10* by a call to interrupt() to wake up the thread so it may end. This class11* also checks for temporary sources that are finished playing, and removes12* them.13*14* NOTE: The command thread is created automatically by the sound system, so it15* is unlikely that the user would ever need to use this class.16*<br><br>17*<b><i> SoundSystem License:</b></i><br><b><br>18* You are free to use this library for any purpose, commercial or otherwise.19* You may modify this library or source code, and distribute it any way you20* like, provided the following conditions are met:21*<br>22* 1) You may not falsely claim to be the author of this library or any23* unmodified portion of it.24*<br>25* 2) You may not copyright this library or a modified version of it and then26* sue me for copyright infringement.27*<br>28* 3) If you modify the source code, you must clearly document the changes29* made before redistributing the modified source code, so other users know30* it is not the original code.31*<br>32* 4) You are not required to give me credit for this library in any derived33* work, but if you do, you must also mention my website:34* http://www.paulscode.com35*<br>36* 5) I the author will not be responsible for any damages (physical,37* financial, or otherwise) caused by the use if this library or any part38* of it.39*<br>40* 6) I the author do not guarantee, warrant, or make any representations,41* either expressed or implied, regarding the use of this library or any42* part of it.43* <br><br>44* Author: Paul Lamb45* <br>46* http://www.paulscode.com47* </b>48*/49public class CommandThread extends SimpleThread50{51/**52* Processes status messages, warnings, and error messages.53*/54protected SoundSystemLogger logger;5556/**57* Handle to the Sound System. This is where the Command Queue is located.58*/59private SoundSystem soundSystem;6061/**62* Name of this class.63*/64protected String className = "CommandThread";6566/**67* Constructor: Takes a handle to the SoundSystem object as a parameter.68* @param s Handle to the SoundSystem.69*/70public CommandThread( SoundSystem s )71{72// grab a handle to the message logger:73logger = SoundSystemConfig.getLogger();7475soundSystem = s;76}7778/**79* Shuts the thread down and removes references to all instantiated objects.80* NOTE: Method alive() will return false when cleanup() has finished.81*/82@Override83protected void cleanup()84{85kill();8687logger = null;88soundSystem = null;8990super.cleanup(); // Important!91}9293/**94* The main loop for processing commands. The Command Thread starts out95* asleep, and it sleeps again after it finishes processing commands, so it96* must be interrupted when commands are queued for processing.97*/98@Override99public void run()100{101long previousTime = System.currentTimeMillis();102long currentTime = previousTime;103104if( soundSystem == null )105{106errorMessage( "SoundSystem was null in method run().", 0 );107cleanup();108return;109}110111// Start out asleep:112snooze( 3600000 );113114while( !dying() )115{116// Perform user-specific source management:117soundSystem.ManageSources();118119// Process all queued commands:120soundSystem.CommandQueue( null );121122// Remove temporary sources every ten seconds:123currentTime = System.currentTimeMillis();124if( (!dying()) && ((currentTime - previousTime) > 10000) )125{126previousTime = currentTime;127soundSystem.removeTemporarySources();128}129130// Wait for more commands:131if( !dying() )132snooze( 3600000 );133}134135cleanup(); // Important!136}137138/**139* Prints a message.140* @param message Message to print.141*/142protected void message( String message, int indent )143{144logger.message( message, indent );145}146147/**148* Prints an important message.149* @param message Message to print.150*/151protected void importantMessage( String message, int indent )152{153logger.importantMessage( message, indent );154}155156/**157* Prints the specified message if error is true.158* @param error True or False.159* @param message Message to print if error is true.160* @return True if error is true.161*/162protected boolean errorCheck( boolean error, String message )163{164return logger.errorCheck( error, className, message, 0 );165}166167/**168* Prints an error message.169* @param message Message to print.170*/171protected void errorMessage( String message, int indent )172{173logger.errorMessage( className, message, indent );174}175}176177178