crashanalysercmd/Libraries/Engine/CrashItemLib/Engine/Sources/CIEngineSource.cs
changeset 0 818e61de6cd1
equal deleted inserted replaced
-1:000000000000 0:818e61de6cd1
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 * 
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 using System;
       
    18 using System.IO;
       
    19 using System.Collections;
       
    20 using System.Collections.Generic;
       
    21 using System.Text;
       
    22 using SymbianUtils;
       
    23 using CrashItemLib.PluginAPI;
       
    24 using CrashItemLib.Crash.Source;
       
    25 using CrashItemLib.Crash.Container;
       
    26 using CrashItemLib.Engine.Sources.Types;
       
    27 
       
    28 namespace CrashItemLib.Engine.Sources
       
    29 {
       
    30     public class CIEngineSource : IEnumerable<CIContainer>
       
    31     {
       
    32         #region Enumerations
       
    33         public enum TState
       
    34         {
       
    35             EStateUninitialised = 0,
       
    36             EStateProcessing,
       
    37             EStateReady,
       
    38             EStateReadyNoItems,
       
    39             EStateReadyCorrupt
       
    40         }
       
    41         #endregion
       
    42 
       
    43         #region Static constructors
       
    44         /// <summary>
       
    45         /// Create a source that is linked with a single crash file format plugin
       
    46         /// </summary>
       
    47         public static CIEngineSource NewNative( CIEngine aEngine, CFFSource aEntry )
       
    48         {
       
    49             CIEngineSource ret = new CIEngineSource( aEngine, aEntry );
       
    50             return ret;
       
    51         }
       
    52 
       
    53         /// <summary>
       
    54         /// Create a source that is associated with a trace file and potentially one
       
    55         /// or more plugins which all think they are capable of interpreting trace-based
       
    56         /// content.
       
    57         /// </summary>
       
    58         public static CIEngineSource NewTrace( CIEngine aEngine, CFFSource[] aEntries )
       
    59         {
       
    60             CIEngineSource ret = new CIEngineSource( aEngine, aEntries );
       
    61             return ret;
       
    62         }
       
    63         #endregion
       
    64 
       
    65         #region Constructors
       
    66         private CIEngineSource( CIEngine aEngine, CFFSource aEntry )
       
    67         {
       
    68             iEngine = aEngine;
       
    69             iFile = aEntry.MasterFile;
       
    70             iReader = new CIEngineSourceReaderNative( this, aEntry );
       
    71         }
       
    72 
       
    73         private CIEngineSource( CIEngine aEngine, CFFSource[] aEntries )
       
    74         {
       
    75             if ( aEntries == null || aEntries.Length == 0 )
       
    76             {
       
    77                 throw new ArgumentException( "Cannot create trace-based source without entry list" );
       
    78             }
       
    79 
       
    80             iEngine = aEngine;
       
    81             iFile = aEntries[0].MasterFile;
       
    82             iReader = new CIEngineSourceReaderTrace( this, aEntries );
       
    83         }
       
    84         #endregion
       
    85 
       
    86         #region Framework API
       
    87         #endregion
       
    88 
       
    89         #region API
       
    90         internal void SaveContainer( CIContainer aContainer )
       
    91         {
       
    92             // Update list of created containers
       
    93             lock ( iCreatedContainers )
       
    94             {
       
    95                 iCreatedContainers.Add( aContainer );
       
    96             }
       
    97 
       
    98             // Notify the engine which will cascade to UI
       
    99             iEngine.Add( aContainer );
       
   100         }
       
   101 
       
   102         internal void Read()
       
   103         {
       
   104             lock ( iCreatedContainers )
       
   105             {
       
   106                 iCreatedContainers.Clear();
       
   107             }
       
   108             State = TState.EStateProcessing;
       
   109 
       
   110             // We need this in order to report progress to the engine
       
   111             System.Diagnostics.Debug.Assert( iCollection != null );
       
   112 
       
   113             // Initiate synchronous read and record any exceptions
       
   114             TState finalState = iReader.Read();
       
   115             this.State = finalState;
       
   116         }
       
   117 
       
   118         internal void OnSourceReadingProgress( int aProgress )
       
   119         {
       
   120             iCollection.OnSourceProgress( this, aProgress );
       
   121         }
       
   122         #endregion
       
   123 
       
   124         #region Properties
       
   125         public TState State
       
   126         {
       
   127             get
       
   128             {
       
   129                 lock ( iSyncRoot )
       
   130                 {
       
   131                     return iState;
       
   132                 }
       
   133             }
       
   134             protected set
       
   135             {
       
   136                 lock ( iSyncRoot )
       
   137                 {
       
   138                     iState = value;
       
   139                 }
       
   140                 //
       
   141                 iCollection.OnSourceStateChanged( this );
       
   142             }
       
   143         }
       
   144 
       
   145         public bool IsReady
       
   146         {
       
   147             get
       
   148             {
       
   149                 lock ( iSyncRoot )
       
   150                 {
       
   151                     bool ready = false;
       
   152                     //
       
   153                     switch ( iState )
       
   154                     {
       
   155                     case TState.EStateReady:
       
   156                     case TState.EStateReadyCorrupt:
       
   157                     case TState.EStateReadyNoItems:
       
   158                         ready = true;
       
   159                         break;
       
   160                     default:
       
   161                         break;
       
   162                     }
       
   163                     //
       
   164                     return ready;
       
   165                 }
       
   166             }
       
   167         }
       
   168 
       
   169         public FileInfo File
       
   170         {
       
   171             get { return iFile; }
       
   172         }
       
   173 
       
   174         public string FileName
       
   175         {
       
   176             get { return iFile.FullName; }
       
   177         }
       
   178 
       
   179         public int ContainerCount
       
   180         {
       
   181             get
       
   182             {
       
   183                 lock ( iCreatedContainers )
       
   184                 {
       
   185                     return iCreatedContainers.Count;
       
   186                 }
       
   187             }
       
   188         }
       
   189 
       
   190         internal CFFSource.TReaderOperationType OpType
       
   191         {
       
   192             get { return iReader.OpType; }
       
   193         }
       
   194 
       
   195         internal CIEngine Engine
       
   196         {
       
   197             get { return iEngine; }
       
   198         }
       
   199 
       
   200         internal CIEngineSourceCollection Collection
       
   201         {
       
   202             get { return iCollection; }
       
   203             set { iCollection = value; }
       
   204         }
       
   205         #endregion
       
   206 
       
   207         #region Internal methods
       
   208         #endregion
       
   209 
       
   210         #region From IEnumerable<CIContainer>
       
   211         public IEnumerator<CIContainer> GetEnumerator()
       
   212         {
       
   213             foreach ( CIContainer container in iCreatedContainers )
       
   214             {
       
   215                 yield return container;
       
   216             }
       
   217         }
       
   218 
       
   219         IEnumerator IEnumerable.GetEnumerator()
       
   220         {
       
   221             foreach ( CIContainer container in iCreatedContainers )
       
   222             {
       
   223                 yield return container;
       
   224             }
       
   225         }
       
   226         #endregion
       
   227 
       
   228         #region Data members
       
   229         private readonly CIEngine iEngine;
       
   230         private readonly FileInfo iFile;
       
   231         private readonly CIEngineSourceReader iReader;
       
   232         private object iSyncRoot = new object();
       
   233         private TState iState = TState.EStateUninitialised;
       
   234         private CIContainerCollection iCreatedContainers = new CIContainerCollection();
       
   235         private CIEngineSourceCollection iCollection = null;
       
   236         #endregion
       
   237     }
       
   238 }