PageRenderTime 29ms CodeModel.GetById 17ms app.highlight 6ms RepoModel.GetById 2ms 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
 18using System;
 19using System.Collections.Generic;
 20using System.Linq;
 21using System.Text;
 22using System.Collections;
 23using System.Timers;
 24using StockTraderRI.AcceptanceTests.Helpers;
 25using System.Globalization;
 26
 27namespace StockTraderRI.AcceptanceTests.ApplicationObserver
 28{
 29    /// <summary>
 30    /// Class to diagnose the state of the object and the control flow.
 31    /// </summary>
 32    public sealed class StateDiagnosis
 33    {
 34        private static readonly StateDiagnosis instance = new StateDiagnosis();
 35        //maintain the mapping of all Observers and Timers
 36        private Dictionary<IStateObserver, Timer> observerTimerList = new Dictionary<IStateObserver, Timer>();
 37        private static bool isFailed;
 38        private string waitTime = ConfigHandler.GetValue("ApplicationLoadWaitTime");
 39
 40        private StateDiagnosis()
 41        { }
 42
 43        public static StateDiagnosis Instance
 44        {
 45            get { return instance; } 
 46        }
 47
 48        /// <summary>
 49        /// Static property to set the diagnosis status.
 50        /// </summary>
 51        public static Boolean IsFailed
 52        {
 53            get { return isFailed; }
 54        } 
 55
 56        /// <summary>
 57        /// Initiate the diagnosis
 58        /// </summary>
 59        /// <param name="observer">Object of type IStateObserver</param>
 60        public void StartDiagnosis(IStateObserver observer)
 61        {
 62            // Starting a timer for this observer to keep track of changes
 63            StartTimer(observer);
 64        }
 65
 66        /// <summary>
 67        /// Stop Diagnosis
 68        /// </summary>
 69        /// <param name="observer">Object of type IStateObserver</param>
 70        public void StopDiagnosis(IStateObserver observer)
 71        {
 72            // Stop the timer as the caller is stopping the diagnosis.
 73            StopTimer(observer);
 74        }
 75
 76        #region Private Helper Methods
 77
 78        /// <summary>
 79        /// Start the timer.
 80        /// </summary>
 81        private void StartTimer(IStateObserver observer)
 82        {
 83            ResolveTimer(observer).Start();
 84        }
 85
 86        /// <summary>
 87        /// Stop the timer.
 88        /// </summary>
 89        private void StopTimer(IStateObserver observer)
 90        {
 91            Timer timer = observerTimerList[observer];
 92            if (null != timer)
 93            {
 94                timer.Stop();
 95            }
 96            observerTimerList.Remove(observer);
 97
 98            isFailed = false;
 99        }
100
101        private Timer ResolveTimer(IStateObserver observer)
102        {
103            if (observerTimerList.ContainsKey(observer))
104            {
105                return observerTimerList[observer];
106            }
107            else
108            {
109                Timer timer = CreateTimer();
110                observerTimerList.Add(observer, timer);
111                return timer;
112            }
113        }
114
115        private Timer CreateTimer()
116        {
117            Timer timer = new Timer();
118            //check after 10 sec
119            timer.Interval = Convert.ToDouble(waitTime, CultureInfo.InvariantCulture);
120            timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
121            return timer;
122        }
123
124        /// <summary>
125        /// ElapsedEventHandler for Timer
126        /// </summary>
127        /// <param name="sender">object</param>
128        /// <param name="e">Elapsed Event Arguments</param>
129        private void OnTimedEvent(object sender, ElapsedEventArgs e)
130        {
131            Timer timer = (Timer)sender;
132
133            // Stop the timer and notify the observers.
134            timer.Stop();
135            isFailed = true;
136
137            // Notify only the hooked up observer.
138            IStateObserver observer = observerTimerList.First(list => list.Value.Equals(timer)).Key;
139            NotifyObserver(observer);
140        }
141
142        /// <summary>
143        /// Method to notify observer about the state change.
144        /// </summary>
145        private static void NotifyObserver(IStateObserver observer)
146        {
147            // Notify the observer who is hooked up with the timer.
148            observer.Notify();
149        }
150
151        #endregion
152    }
153}