/CCGOnline/CCGOnline/PackageManager/Source/PackageInputState.cs

https://github.com/bretambrose/CCGOnlinePublic · C# · 170 lines · 119 code · 23 blank · 28 comment · 8 complexity · 033c4922d50fb7559fcfc740e40424b3 MD5 · raw file

  1. /**********************************************************************************************************************
  2. PackageInputState.cs
  3. Component that tracks per-input state for the package manager and initiates all background operations for a
  4. given input
  5. (c) Copyright 2011, Bret Ambrose (mailto:bretambrose@gmail.com).
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. **********************************************************************************************************************/
  17. using System;
  18. namespace PackageManager
  19. {
  20. public enum EPackageInputID
  21. {
  22. Invalid = 0
  23. }
  24. public enum EInputPackageState
  25. {
  26. Start,
  27. PendingDownload,
  28. Downloading,
  29. HashingDownload,
  30. Decompressing,
  31. Finished,
  32. Error
  33. }
  34. public class CPackageInputState
  35. {
  36. // Construction
  37. public CPackageInputState( CPackageInputEntry config )
  38. {
  39. ID = ++m_IDTracker;
  40. Config = config;
  41. State = EInputPackageState.Start;
  42. DownloadedFile = "";
  43. }
  44. // Methods
  45. // Public interface
  46. public void Service()
  47. {
  48. switch ( State )
  49. {
  50. case EInputPackageState.Start:
  51. State = EInputPackageState.PendingDownload;
  52. break;
  53. case EInputPackageState.PendingDownload:
  54. Service_Pending_Download();
  55. break;
  56. case EInputPackageState.Downloading:
  57. Service_Downloading();
  58. break;
  59. case EInputPackageState.HashingDownload:
  60. Service_Hashing_Download();
  61. break;
  62. case EInputPackageState.Decompressing:
  63. Service_Decompressing();
  64. break;
  65. default:
  66. break;
  67. }
  68. }
  69. // Private interface
  70. private void Service_Pending_Download()
  71. {
  72. if ( m_BackgroundTask == null && CPackageManager.Get_Input_Packages_In_State_Count( EInputPackageState.Downloading ) < CConfigSettings.Instance.MaxConcurrentDownloads )
  73. {
  74. State = EInputPackageState.Downloading;
  75. m_BackgroundTask = new CDownloadWorker( Config );
  76. m_BackgroundTask.RunWorkerAsync();
  77. Console.WriteLine( "Downloading package: " + Config.Name );
  78. }
  79. }
  80. private void Service_Downloading()
  81. {
  82. switch ( m_BackgroundTask.Get_Completion_Status() )
  83. {
  84. case EWorkStatus.CompletionFailure:
  85. case EWorkStatus.Invalid:
  86. throw new Exception( "Attempt to download package failed" );
  87. case EWorkStatus.CompletionSuccess:
  88. Console.WriteLine( "Successfully downloaded package: " + Config.Name );
  89. DownloadedFile = ( m_BackgroundTask as CDownloadWorker ).DownloadedFileName;
  90. State = EInputPackageState.HashingDownload;
  91. m_BackgroundTask = new CHashWorker( DownloadedFile, 8 );
  92. m_BackgroundTask.RunWorkerAsync();
  93. Console.WriteLine( "Hashing package: " + Config.Name );
  94. break;
  95. }
  96. }
  97. private void Service_Hashing_Download()
  98. {
  99. switch ( m_BackgroundTask.Get_Completion_Status() )
  100. {
  101. case EWorkStatus.CompletionFailure:
  102. case EWorkStatus.Invalid:
  103. throw new Exception( "Attempt to hash downloaded file failed" );
  104. case EWorkStatus.CompletionSuccess:
  105. Console.WriteLine( "Successfully hashed downloaded file: " + DownloadedFile );
  106. CHash download_hash = ( m_BackgroundTask as CHashWorker ).Hash;
  107. if ( !Config.Verify_Download_Hash( download_hash ) )
  108. {
  109. throw new Exception( "Download hash check failure on package: " + Config.Name );
  110. }
  111. State = EInputPackageState.Decompressing;
  112. m_BackgroundTask = new CDecompressWorker( DownloadedFile, CPackageManager.UnpackDirectory );
  113. m_BackgroundTask.RunWorkerAsync();
  114. Console.WriteLine( "Decompressing package: " + Config.Name );
  115. break;
  116. }
  117. }
  118. private void Service_Decompressing()
  119. {
  120. switch ( m_BackgroundTask.Get_Completion_Status() )
  121. {
  122. case EWorkStatus.CompletionFailure:
  123. case EWorkStatus.Invalid:
  124. throw new Exception( "Attempt to decompress file failed" );
  125. case EWorkStatus.CompletionSuccess:
  126. Console.WriteLine( "Successfully decompressed downloaded file: " + DownloadedFile );
  127. State = EInputPackageState.Finished;
  128. m_BackgroundTask = null;
  129. break;
  130. }
  131. }
  132. // Properties
  133. public EPackageInputID ID { get; private set; }
  134. public CPackageInputEntry Config { get; private set; }
  135. public EInputPackageState State { get; set; }
  136. private string DownloadedFile { get; set; }
  137. // Fields
  138. private static EPackageInputID m_IDTracker = EPackageInputID.Invalid;
  139. private CWorker m_BackgroundTask = null;
  140. }
  141. }