crashanalysercmd/Libraries/Engine/CrashItemLib/Crash/Container/CIContainerIndex.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.Collections.Generic;
       
    19 using System.Text;
       
    20 using CrashItemLib.Engine;
       
    21 using CrashItemLib.Crash.InfoSW;
       
    22 
       
    23 namespace CrashItemLib.Crash.Container
       
    24 {
       
    25     sealed internal class CIContainerIndex : IEnumerable< KeyValuePair<uint, CIContainerCollection> >
       
    26     {
       
    27         #region Constructors
       
    28         public CIContainerIndex( CIEngine aEngine )
       
    29         {
       
    30             aEngine.CrashObservers += new CIEngine.CIEngineCrashObserver( Engine_CrashObserver );
       
    31         }
       
    32         #endregion
       
    33 
       
    34         #region API
       
    35         public CIContainerCollection DequeueNextContainer()
       
    36         {
       
    37             CIContainerCollection ret = null;
       
    38             //
       
    39             lock ( iDictionary )
       
    40             {
       
    41                 Dictionary<uint, CIContainerCollection>.Enumerator enumerator = iDictionary.GetEnumerator();
       
    42                 bool next = enumerator.MoveNext();
       
    43                 if ( next )
       
    44                 {
       
    45                     ret = enumerator.Current.Value;
       
    46                     iDictionary.Remove( enumerator.Current.Key );
       
    47                 }
       
    48             }
       
    49             //
       
    50             return ret;
       
    51         }
       
    52 
       
    53         public static uint GetRomChecksum( CIContainer aContainer )
       
    54         {
       
    55             uint ret = 0;
       
    56 
       
    57             // Find the infosw item in order to obtain the image checksum
       
    58             CIInfoSW infoSW = aContainer.ChildByType( typeof( CIInfoSW ) ) as CIInfoSW;
       
    59             if ( infoSW != null )
       
    60             {
       
    61                 ret = infoSW.ImageCheckSum;
       
    62             }
       
    63 
       
    64             return ret;
       
    65         }
       
    66         #endregion
       
    67 
       
    68         #region Properties
       
    69         public int Count
       
    70         {
       
    71             get { return iDictionary.Count; }
       
    72         }
       
    73         #endregion
       
    74 
       
    75         #region Event handlers
       
    76         private void Engine_CrashObserver( CIEngine.TCrashEvent aEvent, CIContainer aContainer )
       
    77         {
       
    78             if ( aEvent == CIEngine.TCrashEvent.EEventCrashRemovedAll )
       
    79             {
       
    80                 lock ( iDictionary )
       
    81                 {
       
    82                     iDictionary.Clear();
       
    83                 }
       
    84             }
       
    85             else if ( aEvent == CIEngine.TCrashEvent.EEventCrashRemoved )
       
    86             {
       
    87                 RemoveFromDictionary( aContainer );
       
    88             }
       
    89             else if ( aEvent == CIEngine.TCrashEvent.EEventCrashAdded )
       
    90             {
       
    91                 AddToDictionary( aContainer );
       
    92             }
       
    93         }
       
    94         #endregion
       
    95 
       
    96         #region Internal methods
       
    97         private void RemoveFromDictionary( CIContainer aContainer )
       
    98         {
       
    99             uint checksum = GetRomChecksum( aContainer );
       
   100             CIContainerCollection collection = null;
       
   101             //
       
   102             lock ( iDictionary )
       
   103             {
       
   104                 if ( iDictionary.TryGetValue( checksum, out collection ) )
       
   105                 {
       
   106                     collection.Remove( aContainer );
       
   107 
       
   108                     // If the collection is empty, remove the mapping also
       
   109                     if ( collection.Count == 0 )
       
   110                     {
       
   111                         iDictionary.Remove( checksum );
       
   112                     }
       
   113                 }
       
   114             }
       
   115         }
       
   116 
       
   117         private void AddToDictionary( CIContainer aContainer )
       
   118         {
       
   119             uint checksum = GetRomChecksum( aContainer );
       
   120 
       
   121             // Check if there is a collection for this key already
       
   122             CIContainerCollection collection = null;
       
   123             lock ( iDictionary )
       
   124             {
       
   125                 if ( iDictionary.TryGetValue( checksum, out collection ) == false )
       
   126                 {
       
   127                     // Nope, collection not yet registers
       
   128                     collection = new CIContainerCollection();
       
   129                     iDictionary.Add( checksum, collection );
       
   130                 }
       
   131             }
       
   132 
       
   133             // Now save
       
   134             collection.Add( aContainer );
       
   135         }
       
   136         #endregion
       
   137             
       
   138         #region From IEnumerable< KeyValuePair<uint, CIContainerCollection> >
       
   139         public IEnumerator<KeyValuePair<uint,CIContainerCollection>> GetEnumerator()
       
   140         {
       
   141             foreach( KeyValuePair<uint,CIContainerCollection> kvp in iDictionary )
       
   142             {
       
   143                 yield return kvp;
       
   144             }
       
   145         }
       
   146 
       
   147         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
       
   148         {
       
   149             foreach( KeyValuePair<uint,CIContainerCollection> kvp in iDictionary )
       
   150             {
       
   151                 yield return kvp;
       
   152             }
       
   153         }
       
   154         #endregion
       
   155 
       
   156         #region Data members
       
   157         private Dictionary<uint, CIContainerCollection> iDictionary = new Dictionary<uint, CIContainerCollection>();
       
   158         #endregion
       
   159     }
       
   160 }