crashanalysercmd/Libraries/Engine/CrashItemLib/PluginAPI/FW/CFFReader.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.Generic;
       
    20 using System.Text;
       
    21 using SymbianUtils;
       
    22 using SymbianDebugLib.Engine;
       
    23 using CrashItemLib.Crash;
       
    24 using CrashItemLib.Engine;
       
    25 
       
    26 namespace CrashItemLib.PluginAPI
       
    27 {
       
    28 	public abstract class CFFReader
       
    29 	{
       
    30         #region Enumerations
       
    31         public enum TState
       
    32         {
       
    33             EStateUninitialised = 0,
       
    34             EStateProcessing,
       
    35             EStateReady,
       
    36             EStateCorrupt
       
    37         }
       
    38         #endregion
       
    39 
       
    40         #region Delegates & events
       
    41 		public enum TEvent
       
    42 		{
       
    43 			EReadingStarted = 0,
       
    44 			EReadingProgress,
       
    45             EReadingContainerCreated,
       
    46 			EReadingComplete,
       
    47 		}
       
    48 
       
    49 		public delegate void ReaderObserver( TEvent aEvent, CFFReader aReader, object aContext );
       
    50         public event ReaderObserver Observer;
       
    51 
       
    52         public delegate void ReaderExceptionHandler( Exception aException, CFFReader aReader );
       
    53         public event ReaderExceptionHandler ExceptionHandler;
       
    54 		#endregion
       
    55 
       
    56 		#region Constructors
       
    57 		protected CFFReader( CFFPlugin aEngine, CFFSource aDescriptor )
       
    58 		{
       
    59             iPlugin = aEngine;
       
    60             iDescriptor = aDescriptor;
       
    61 		}
       
    62 		#endregion
       
    63 
       
    64 		#region API - abstract
       
    65         /// <summary>
       
    66         /// Called when Crash Analyser wants this plugin to read a
       
    67         /// native, i.e. format-specific crash file (i.e. not a trace file) 
       
    68         /// synchronously.
       
    69         /// </summary>
       
    70         public virtual void NativeReadInit()
       
    71         {
       
    72         }
       
    73 
       
    74         /// <summary>
       
    75         /// Called when Crash Analyser is going to start to send
       
    76         /// trace lines (that it will read on behalf of the plugin)
       
    77         /// that should be processed in order to identify embedded
       
    78         /// traces.
       
    79         /// 
       
    80         /// This method will only be called if the plugin claims to have
       
    81         /// some confidence in reading trace data.
       
    82         /// </summary>
       
    83         public virtual void TraceReadInit()
       
    84         {
       
    85         }
       
    86 
       
    87         /// <summary>
       
    88         /// Receive an individual trace line and process it if relevant
       
    89         /// </summary>
       
    90         /// <param name="aLine"></param>
       
    91         public virtual bool TraceReadOffer( CFFTraceLine aLine )
       
    92         {
       
    93             // Nothing to do
       
    94             return false;
       
    95         }
       
    96 
       
    97         /// <summary>
       
    98         /// Called when all trace lines have been delivered
       
    99         /// </summary>
       
   100         public virtual void TraceReadComplete()
       
   101         {
       
   102         }
       
   103 		#endregion
       
   104 
       
   105         #region API - framework
       
   106         protected byte[] RawData
       
   107         {
       
   108             get { return Descriptor.RawData; }
       
   109         }
       
   110 
       
   111         protected void RawDataAdd()
       
   112         {
       
   113             // Save entire file
       
   114             RawDataClear();
       
   115             RawDataAdd( Descriptor.MasterFile );
       
   116         }
       
   117 
       
   118         protected void RawDataAdd( FileInfo aFile )
       
   119         {
       
   120             byte[] bytes = File.ReadAllBytes( aFile.FullName );
       
   121             RawDataAdd( bytes );
       
   122         }
       
   123 
       
   124         protected void RawDataClear()
       
   125         {
       
   126             Descriptor.RawDataClear();
       
   127         }
       
   128 
       
   129         protected void RawDataAdd( byte[] aRawData )
       
   130         {
       
   131             Descriptor.RawDataAdd( aRawData );
       
   132         }
       
   133         #endregion
       
   134 
       
   135         #region Properties
       
   136         public TState State
       
   137         {
       
   138             get
       
   139             {
       
   140                 lock ( this )
       
   141                 {
       
   142                     return iState;
       
   143                 }
       
   144             }
       
   145             set
       
   146             {
       
   147                 lock ( this )
       
   148                 {
       
   149                     iState = value;
       
   150                 }
       
   151             }
       
   152         }
       
   153 
       
   154         public CFFPlugin Plugin
       
   155         {
       
   156             get { return iPlugin; }
       
   157         }
       
   158 
       
   159         public CFFSource Descriptor
       
   160         {
       
   161             get { return iDescriptor; }
       
   162         }
       
   163 
       
   164         public CIEngine CIEngine
       
   165 		{
       
   166             get { return Plugin.DataProvider.Engine; }
       
   167 		}
       
   168 		#endregion
       
   169 
       
   170 		#region Internal methods
       
   171 		protected void NotifyEvent( TEvent aEvent )
       
   172 		{
       
   173             NotifyEvent( aEvent, null );
       
   174 		}
       
   175 
       
   176         protected void NotifyEvent( TEvent aEvent, object aContext )
       
   177         {
       
   178             // We must notify about "reading started" and "reading complete" only
       
   179             // once.
       
   180             //
       
   181             // When the plugin sends the "reading started" event, we must transition
       
   182             // to 'EStateProcessing.'
       
   183             //
       
   184             // When the plugin sends the "reading complete" event, we must transition
       
   185             // to 'EStateReady' except if there was an exception during processing,
       
   186             // in which case we remain as 'EStateCorrupt.'
       
   187             //
       
   188             // NB: NotifyException makes the transition to EStateCorrupt.
       
   189             //
       
   190             TState oldState = State;
       
   191             //
       
   192             bool notify = false;
       
   193             switch ( aEvent )
       
   194             {
       
   195             case TEvent.EReadingContainerCreated:
       
   196                 notify = true;
       
   197                 break;
       
   198             case TEvent.EReadingStarted:
       
   199                 State = TState.EStateProcessing;
       
   200                 notify = ( State != oldState );
       
   201                 break;
       
   202             case TEvent.EReadingProgress:
       
   203                 notify = true;
       
   204                 break;
       
   205             case TEvent.EReadingComplete:
       
   206                 if ( State == TState.EStateCorrupt )
       
   207                 {
       
   208                     // There was an exception during processing. Do not
       
   209                     // change to 'ready' state in this situation.
       
   210                     // 
       
   211                     // However, we must still notify about the completion event!
       
   212                     notify = true;
       
   213                 }
       
   214                 else
       
   215                 {
       
   216                     State = TState.EStateReady;
       
   217                     notify = ( State != oldState );
       
   218                 }
       
   219                 break;
       
   220             default:
       
   221                 throw new NotSupportedException();
       
   222             }
       
   223             
       
   224             if ( notify )
       
   225             {
       
   226                 if ( Observer != null )
       
   227                 {
       
   228                     Observer( aEvent, this, aContext );
       
   229                 }
       
   230             }
       
   231         }
       
   232 
       
   233         protected void NotifyException( Exception aException )
       
   234         {
       
   235             State = TState.EStateCorrupt;
       
   236             //
       
   237             if ( ExceptionHandler != null )
       
   238             {
       
   239                 ExceptionHandler( aException, this );
       
   240             }
       
   241         }
       
   242 		#endregion
       
   243 
       
   244         #region Internal methods - called by CIEngineSource to carry out operations
       
   245         internal void OnNativeReadInit()
       
   246         {
       
   247             // Indicate reading started
       
   248             NotifyEvent( TEvent.EReadingStarted );
       
   249             try
       
   250             {
       
   251                 NativeReadInit();
       
   252             }
       
   253             catch ( Exception e )
       
   254             {
       
   255                 NotifyException( e );
       
   256                 NotifyEvent( TEvent.EReadingComplete );
       
   257             }
       
   258         }
       
   259 
       
   260         internal void OnTraceReadInit()
       
   261         {
       
   262             // Indicate reading started
       
   263             NotifyEvent( TEvent.EReadingStarted );
       
   264             try
       
   265             {
       
   266                 TraceReadInit();
       
   267             }
       
   268             catch ( Exception e )
       
   269             {
       
   270                 NotifyException( e );
       
   271                 NotifyEvent( TEvent.EReadingComplete );
       
   272             }
       
   273         }
       
   274 
       
   275         internal void OnTraceReadOffer( CFFTraceLine aLine )
       
   276         {
       
   277             try
       
   278             {
       
   279                 bool consumed = TraceReadOffer( aLine );
       
   280                 if ( consumed )
       
   281                 {
       
   282                     RawDataAdd( aLine.ToBinary() );
       
   283                 }
       
   284             }
       
   285             catch ( Exception e )
       
   286             {
       
   287                 NotifyException( e );
       
   288             }
       
   289         }
       
   290 
       
   291         internal void OnTraceReadComplete()
       
   292         {
       
   293             try
       
   294             {
       
   295                 TraceReadComplete();
       
   296             }
       
   297             catch ( Exception e )
       
   298             {
       
   299                 NotifyException( e );
       
   300                 NotifyEvent( TEvent.EReadingComplete );
       
   301             }
       
   302         }
       
   303         #endregion
       
   304 
       
   305 		#region Data members
       
   306         private TState iState = TState.EStateUninitialised;
       
   307         private readonly CFFPlugin iPlugin;
       
   308         private readonly CFFSource iDescriptor;
       
   309 		#endregion
       
   310 	}
       
   311 }