speedscheduler
Class SpeedSchedulerThread

java.lang.Object
  extended by java.lang.Thread
      extended by speedscheduler.SpeedSchedulerThread
All Implemented Interfaces:
java.lang.Runnable, ScheduleChangeListener

public class SpeedSchedulerThread
extends java.lang.Thread
implements ScheduleChangeListener

This thread is responsible for monitoring the user-configured schedules and adjusting the Azureus max upload speed based thereupon.


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
private  java.util.Vector activeSchedules
          The list of schedules that are currently active based on day of week and time of day (the run() method chooses these).
private static int DEFAULT_SLEEP_TIME
           
private  int defaultMaxDownloadRate
           
private  int defaultMaxUploadRate
          When no schedule applies, the default rate is used.
private static SpeedSchedulerThread instance
           
(package private)  java.lang.String maxDownloadSpeedParam
          To store the name of the configuration parameter we are interested in.
(package private)  java.lang.String maxUploadSpeedParam
          To store the name of the configuration parameter we are interested in.
private static int MIN_SLEEP_TIME
           
(package private)  org.gudy.azureus2.plugins.PluginConfig pluginConfig
          Interrace to Azureus configuration.
(package private)  org.gudy.azureus2.plugins.PluginInterface pluginInterface
          Interrace to Azureus.
private  boolean runFlag
          Flag that tells the thread's loop whether to continue or pause.
private  SpeedSchedulerThread runningThread
          A reference to the running thread.
private  java.util.Vector schedules
          The list of the user configured schedules.
private  java.util.Vector scheduleSelectionListeners
          Maintains a list of the registered listeners.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
SpeedSchedulerThread()
          Creates a new SpeedSchedulerThread, which registers a listener with the SchedulePersistencyManager.
 
Method Summary
private  void addPausedDownload(org.gudy.azureus2.plugins.download.Download download)
          Records that we paused the specified download in a persistent such that we will un-pause this download again when schedules indicate such.
 void addScheduleSelectionListener(ScheduleSelectionChangeListener listener)
          Call this method to sign-up to receive notification when the SpeedSchedulerThread selects a new schedule to use when limiting download/upload speeds.
private  org.gudy.azureus2.plugins.download.Download getDownloadFromHash(java.lang.String hash)
          Gets the download with the specified hash (if any).
static SpeedSchedulerThread getInstance()
           
private  org.gudy.azureus2.plugins.download.Download[] getPausedDownloads()
          Gets all the Downloads that we have paused as a result of schedules.
private  java.lang.String getPausedDownloadsFile()
          Gets the name of the file where paused download hashes are stored.
private  void notifyScheduleSelectionListeners(java.util.Vector schedules)
          Helper function that notifies all ScheduleSelectionListeners that the SpeedSchedulerThread has chosen a new schedule.
private  void pauseTorrent(org.gudy.azureus2.plugins.download.Download download)
          Pauses the specified download and adds it to the list of downloads that we have paused, that we can thereby recall later which downloads we have paused when it is time to un-pause them.
private  void removePausedDownload(org.gudy.azureus2.plugins.download.Download download)
          Removes a download's hash from the persistent file of downloads that we have paused.
 void run()
          Start your engines and never come back.
 void schedulesChanged(java.util.Vector newSchedules, int newDefaultMaxUploadRate, int newDefaultMaxDownloadRate)
          Called by the SchedulePersistencyManager whenever the user changes the schedules.
private  void unPauseTorrent(org.gudy.azureus2.plugins.download.Download download)
          Unpauses the specified download (ie, restarts it), and removes it from the persistent list of paused downloads.
private  boolean weAlreadyPausedDownload(org.gudy.azureus2.plugins.download.Download download)
          Alias for wePausedDownload()
private  boolean wePausedTorrent(org.gudy.azureus2.plugins.download.Download download)
          Returns whether we paused the specified download based on its hash and the contents of the persistent paused downloads file.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

defaultMaxUploadRate

private int defaultMaxUploadRate
When no schedule applies, the default rate is used.


defaultMaxDownloadRate

private int defaultMaxDownloadRate

DEFAULT_SLEEP_TIME

private static final int DEFAULT_SLEEP_TIME
See Also:
Constant Field Values

MIN_SLEEP_TIME

private static final int MIN_SLEEP_TIME
See Also:
Constant Field Values

schedules

private java.util.Vector schedules
The list of the user configured schedules.


activeSchedules

private java.util.Vector activeSchedules
The list of schedules that are currently active based on day of week and time of day (the run() method chooses these).


pluginInterface

org.gudy.azureus2.plugins.PluginInterface pluginInterface
Interrace to Azureus.


pluginConfig

org.gudy.azureus2.plugins.PluginConfig pluginConfig
Interrace to Azureus configuration.


maxUploadSpeedParam

java.lang.String maxUploadSpeedParam
To store the name of the configuration parameter we are interested in.


maxDownloadSpeedParam

java.lang.String maxDownloadSpeedParam
To store the name of the configuration parameter we are interested in.


runningThread

private SpeedSchedulerThread runningThread
A reference to the running thread.


instance

private static SpeedSchedulerThread instance

runFlag

private boolean runFlag
Flag that tells the thread's loop whether to continue or pause.


scheduleSelectionListeners

private java.util.Vector scheduleSelectionListeners
Maintains a list of the registered listeners.

Constructor Detail

SpeedSchedulerThread

public SpeedSchedulerThread()
Creates a new SpeedSchedulerThread, which registers a listener with the SchedulePersistencyManager.

Method Detail

getInstance

public static SpeedSchedulerThread getInstance()

run

public void run()
Start your engines and never come back.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

pauseTorrent

private void pauseTorrent(org.gudy.azureus2.plugins.download.Download download)
Pauses the specified download and adds it to the list of downloads that we have paused, that we can thereby recall later which downloads we have paused when it is time to un-pause them.

Parameters:
download - The download to pause.
See Also:
Download

addPausedDownload

private void addPausedDownload(org.gudy.azureus2.plugins.download.Download download)
Records that we paused the specified download in a persistent such that we will un-pause this download again when schedules indicate such.

Parameters:
download - The download to pause and record as such.
See Also:
Download

wePausedTorrent

private boolean wePausedTorrent(org.gudy.azureus2.plugins.download.Download download)
Returns whether we paused the specified download based on its hash and the contents of the persistent paused downloads file.

Parameters:
download - The download to check.
Returns:
True if we have already paused this download, or false otherwise.
See Also:
Download

weAlreadyPausedDownload

private boolean weAlreadyPausedDownload(org.gudy.azureus2.plugins.download.Download download)
Alias for wePausedDownload()

See Also:
wePausedDownload, Download

getPausedDownloadsFile

private java.lang.String getPausedDownloadsFile()
Gets the name of the file where paused download hashes are stored.

Returns:
The name of the file. Example: "C:/Documents and Settings/Dave/Application Data/Azureus/Plugins/SpeedScheduler/PausedTorrents.conf"
See Also:
Download

getPausedDownloads

private org.gudy.azureus2.plugins.download.Download[] getPausedDownloads()
Gets all the Downloads that we have paused as a result of schedules. Note that we don't pause downloads that are already paused manually or for some other reason. Thus, we store these persistently, and we access them when it is time to un-pause all transfers again.

Returns:
An array of Download objects that can be unpaused
See Also:
Download

getDownloadFromHash

private org.gudy.azureus2.plugins.download.Download getDownloadFromHash(java.lang.String hash)
Gets the download with the specified hash (if any). Contacts the DownloadManager and searchces for the Torrent that has the specified hash. If no such torrent is found, returns null.

Parameters:
hash - The hash of the Torrent, whose Download object to fetch.
Returns:
The Download if found or null if not found.

unPauseTorrent

private void unPauseTorrent(org.gudy.azureus2.plugins.download.Download download)
Unpauses the specified download (ie, restarts it), and removes it from the persistent list of paused downloads.

Parameters:
download - The download to unpause.

removePausedDownload

private void removePausedDownload(org.gudy.azureus2.plugins.download.Download download)
Removes a download's hash from the persistent file of downloads that we have paused.

Parameters:
download - The Download to remove from the file.

schedulesChanged

public void schedulesChanged(java.util.Vector newSchedules,
                             int newDefaultMaxUploadRate,
                             int newDefaultMaxDownloadRate)
Called by the SchedulePersistencyManager whenever the user changes the schedules.

Specified by:
schedulesChanged in interface ScheduleChangeListener
Parameters:
newSchedules - The vector of schedules that are now selected
newDefaultMaxUploadRate - The new default max upload rate
newDefaultMaxDownloadRate - The new default max download rate

addScheduleSelectionListener

public void addScheduleSelectionListener(ScheduleSelectionChangeListener listener)
Call this method to sign-up to receive notification when the SpeedSchedulerThread selects a new schedule to use when limiting download/upload speeds.

Parameters:
listener - The listener that implements the ScheduleSelectionLisetener interface who will be notified.
See Also:
ScheduleSelectionChangeListener

notifyScheduleSelectionListeners

private void notifyScheduleSelectionListeners(java.util.Vector schedules)
Helper function that notifies all ScheduleSelectionListeners that the SpeedSchedulerThread has chosen a new schedule.

Parameters:
schedules - Vector of Schedules that we just chose.
See Also:
ScheduleSelectionChangeListener