PageRenderTime 36ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/V1/trunk/Source/StockTraderRI/StockRI.Tests.AcceptanceTests/AutomatedTests/ApplicationObserver/StateDiagnosis.cs

#
C# | 153 lines | 84 code | 18 blank | 51 comment | 3 complexity | 5126d6a47403ea50fbfd054fb0e07727 MD5 | raw file
  1. //===============================================================================
  2. // Microsoft patterns & practices
  3. // Composite Application Guidance for Windows Presentation Foundation
  4. //===============================================================================
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8. // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. // FITNESS FOR A PARTICULAR PURPOSE.
  10. //===============================================================================
  11. // The example companies, organizations, products, domain names,
  12. // e-mail addresses, logos, people, places, and events depicted
  13. // herein are fictitious. No association with any real company,
  14. // organization, product, domain name, email address, logo, person,
  15. // places, or events is intended or should be inferred.
  16. //===============================================================================
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Linq;
  20. using System.Text;
  21. using System.Collections;
  22. using System.Timers;
  23. using StockTraderRI.AcceptanceTests.Helpers;
  24. using System.Globalization;
  25. namespace StockTraderRI.AcceptanceTests.ApplicationObserver
  26. {
  27. /// <summary>
  28. /// Class to diagnose the state of the object and the control flow.
  29. /// </summary>
  30. public sealed class StateDiagnosis
  31. {
  32. private static readonly StateDiagnosis instance = new StateDiagnosis();
  33. //maintain the mapping of all Observers and Timers
  34. private Dictionary<IStateObserver, Timer> observerTimerList = new Dictionary<IStateObserver, Timer>();
  35. private static bool isFailed;
  36. private string waitTime = ConfigHandler.GetValue("ApplicationLoadWaitTime");
  37. private StateDiagnosis()
  38. { }
  39. public static StateDiagnosis Instance
  40. {
  41. get { return instance; }
  42. }
  43. /// <summary>
  44. /// Static property to set the diagnosis status.
  45. /// </summary>
  46. public static Boolean IsFailed
  47. {
  48. get { return isFailed; }
  49. }
  50. /// <summary>
  51. /// Initiate the diagnosis
  52. /// </summary>
  53. /// <param name="observer">Object of type IStateObserver</param>
  54. public void StartDiagnosis(IStateObserver observer)
  55. {
  56. // Starting a timer for this observer to keep track of changes
  57. StartTimer(observer);
  58. }
  59. /// <summary>
  60. /// Stop Diagnosis
  61. /// </summary>
  62. /// <param name="observer">Object of type IStateObserver</param>
  63. public void StopDiagnosis(IStateObserver observer)
  64. {
  65. // Stop the timer as the caller is stopping the diagnosis.
  66. StopTimer(observer);
  67. }
  68. #region Private Helper Methods
  69. /// <summary>
  70. /// Start the timer.
  71. /// </summary>
  72. private void StartTimer(IStateObserver observer)
  73. {
  74. ResolveTimer(observer).Start();
  75. }
  76. /// <summary>
  77. /// Stop the timer.
  78. /// </summary>
  79. private void StopTimer(IStateObserver observer)
  80. {
  81. Timer timer = observerTimerList[observer];
  82. if (null != timer)
  83. {
  84. timer.Stop();
  85. }
  86. observerTimerList.Remove(observer);
  87. isFailed = false;
  88. }
  89. private Timer ResolveTimer(IStateObserver observer)
  90. {
  91. if (observerTimerList.ContainsKey(observer))
  92. {
  93. return observerTimerList[observer];
  94. }
  95. else
  96. {
  97. Timer timer = CreateTimer();
  98. observerTimerList.Add(observer, timer);
  99. return timer;
  100. }
  101. }
  102. private Timer CreateTimer()
  103. {
  104. Timer timer = new Timer();
  105. //check after 10 sec
  106. timer.Interval = Convert.ToDouble(waitTime, CultureInfo.InvariantCulture);
  107. timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
  108. return timer;
  109. }
  110. /// <summary>
  111. /// ElapsedEventHandler for Timer
  112. /// </summary>
  113. /// <param name="sender">object</param>
  114. /// <param name="e">Elapsed Event Arguments</param>
  115. private void OnTimedEvent(object sender, ElapsedEventArgs e)
  116. {
  117. Timer timer = (Timer)sender;
  118. // Stop the timer and notify the observers.
  119. timer.Stop();
  120. isFailed = true;
  121. // Notify only the hooked up observer.
  122. IStateObserver observer = observerTimerList.First(list => list.Value.Equals(timer)).Key;
  123. NotifyObserver(observer);
  124. }
  125. /// <summary>
  126. /// Method to notify observer about the state change.
  127. /// </summary>
  128. private static void NotifyObserver(IStateObserver observer)
  129. {
  130. // Notify the observer who is hooked up with the timer.
  131. observer.Notify();
  132. }
  133. #endregion
  134. }
  135. }