#region Disclaimer / License // Copyright (C) 2011, Kenneth Skovhede // http://www.hexad.dk, opensource@hexad.dk // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // #endregion using System; using System.Collections.Generic; using System.Text; namespace Duplicati.Library.Main.LiveControl { /// /// A delegate that is used to signal that the backup state has changed /// public delegate void BackupStateChangedDelegate(BackupState newState); /// /// An enumeration that changes describes the states a backup can be in /// public enum BackupState { Running, Paused, Stopped, Terminated, Completed } /// /// This class is an interface to interacting with a running backup /// internal class LiveControl : ILiveControl { /// /// The event that the thread waits for when paused /// private System.Threading.ManualResetEvent m_pauseEvent = new System.Threading.ManualResetEvent(true); /// /// The flag used to signal a stop has been requested /// private volatile bool m_stopRequested = false; /// /// The flag used to signal a pause has been requested /// private volatile bool m_pauseRequested = false; /// /// An event that is invoked when the backup state changes /// public event BackupStateChangedDelegate BackupStateChanged; /// /// The interface thread that owns this instance /// private System.Threading.Thread m_owner; /// /// A copy of the current options /// private Options m_options; /// /// The backup specified thread priority /// private System.Threading.ThreadPriority m_defaultPriority; /// /// The download limit specified in the backup /// private long m_downloadLimit; /// /// The upload limit specified in the backup /// private long m_uploadLimit; /// /// Default constructor /// internal LiveControl(System.Threading.Thread owner, Options options) { m_owner = owner; m_options = options; m_defaultPriority = m_owner.Priority; m_downloadLimit = m_options.MaxDownloadPrSecond; m_uploadLimit = m_options.MaxUploadPrSecond; } /// /// Helper method to invoke the event /// /// The state the backup is in internal void InvokeStateChangeEvent(BackupState state) { if (BackupStateChanged != null) BackupStateChanged(state); } /// /// Gets a value indicating if stop has been requested /// /// public bool IsStopRequested { get { return m_stopRequested; } } /// /// Gets a value indicating if pause has been requested /// /// public bool IsPauseRequested { get { return m_pauseRequested; } } /// /// Helper method to pause the current backup /// internal void PauseIfRequested() { while (!IsStopRequested && !m_pauseEvent.WaitOne(1000, false)) { //Just repeat } } /// /// Activates an asyncronous request for pausing the backup /// public void Pause() { lock (m_pauseEvent) if (!m_stopRequested) { m_pauseRequested = true; m_pauseEvent.Reset(); } } /// /// Activates an asyncronous request for resuming a paused backup /// public void Resume() { lock (m_pauseEvent) { m_pauseRequested = false; m_pauseEvent.Set(); } } /// /// Activates an asyncronous request for stopping the backup. /// Stopping a backup does not interrupt ongoing uploads. /// public void Stop() { lock (m_pauseEvent) { m_stopRequested = true; if (m_pauseRequested) Resume(); } } /// /// Activates an asyncronous request for terminating the backup. /// Terminating a backup breaks any ongoing uploads, and may leave partial files on the backend. /// public void Terminate() { if (m_owner.IsAlive) m_owner.Abort(); } public void SetUploadLimit(string limit) { long newLimit = Utility.Sizeparser.ParseSize(limit); if (newLimit <= 0) m_options.MaxUploadPrSecond = m_uploadLimit; else if (m_uploadLimit > 0) m_options.MaxUploadPrSecond = Math.Min(m_uploadLimit, newLimit); else m_options.MaxUploadPrSecond = newLimit; } public void SetDownloadLimit(string limit) { long newLimit = Utility.Sizeparser.ParseSize(limit); if (newLimit <= 0) m_options.MaxDownloadPrSecond = m_downloadLimit; else if (m_downloadLimit > 0) m_options.MaxDownloadPrSecond = Math.Min(m_downloadLimit, newLimit); else m_options.MaxDownloadPrSecond = newLimit; } public void SetThreadPriority(System.Threading.ThreadPriority priority) { m_owner.Priority = priority; } public void UnsetThreadPriority() { m_owner.Priority = m_defaultPriority; } } }