mpx/tsrc/public/basic/common/basetest/src/timeoutcontroller.cpp
changeset 64 92dbd2a406d9
equal deleted inserted replaced
61:3b098142db83 64:92dbd2a406d9
       
     1 /*
       
     2 * Copyright (c) 2002 - 2007 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:  Timeout controller
       
    15 *
       
    16 */
       
    17 
       
    18 #include "timeoutcontroller.h"
       
    19 #include "debug.h"
       
    20 
       
    21 /*
       
    22 -------------------------------------------------------------------------------
       
    23 Class: CSimpleTimeout
       
    24 Method: CSimpleTimeout
       
    25 Description: Default constructor
       
    26 C++ default constructor can NOT contain any code, that might leave.
       
    27 Parameters: None
       
    28 Return Values: None
       
    29 Errors/Exceptions: None
       
    30 Status: Approved
       
    31 -------------------------------------------------------------------------------
       
    32 */
       
    33 CSimpleTimeout::CSimpleTimeout() : CActive (CActive::EPriorityStandard)
       
    34 {
       
    35     FTRACE(FPrint(_L("CSimpleTimeout::CSimpleTimeout")));
       
    36 }
       
    37 
       
    38 // -----------------------------------------------------------------------------
       
    39 // CSimpleTimeout::ConstructL()
       
    40 // Symbian OS second phase constructor.
       
    41 // -----------------------------------------------------------------------------
       
    42 //
       
    43 void CSimpleTimeout::ConstructL( MTimeoutObserver* aObserver,
       
    44 CStifLogger* aLogger)
       
    45 {
       
    46     FTRACE(FPrint(_L("CSimpleTimeout::ConstructL")));
       
    47     iObserver = aObserver;
       
    48     iLog = aLogger;
       
    49     iTimer.CreateLocal();
       
    50     iTestCaseTimeout = 0;  // Initialize
       
    51 
       
    52     // Add to active scheduler
       
    53     CActiveScheduler::Add ( this );
       
    54 }
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // CSimpleTimeout::NewL()
       
    58 // Two-phased constructor.
       
    59 // Returns: CSimpleTimeout* : pointer to created object
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 CSimpleTimeout* CSimpleTimeout::NewL( MTimeoutObserver* aTestClass,
       
    63 CStifLogger* aLogger)
       
    64 {
       
    65     FTRACE(FPrint(_L("CSimpleTimeout::NewL")));
       
    66     CSimpleTimeout* self = new ( ELeave ) CSimpleTimeout();
       
    67     CleanupStack::PushL( self );
       
    68     self->ConstructL( aTestClass, aLogger);
       
    69     CleanupStack::Pop( self );
       
    70     return self;
       
    71 
       
    72 }
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // CSimpleTimeout::~CSimpleTimeout()
       
    76 // Destructor.
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 CSimpleTimeout::~CSimpleTimeout()
       
    80 {
       
    81     FTRACE(FPrint(_L("CSimpleTimeout::~CSimpleTimeout")));
       
    82     Cancel();
       
    83     iTimer.Close();
       
    84 }
       
    85 
       
    86 // -----------------------------------------------------------------------------
       
    87 // CSimpleTimeout::Start()
       
    88 // Start timeout counting.
       
    89 // -----------------------------------------------------------------------------
       
    90 //
       
    91 void CSimpleTimeout::Start(TTimeIntervalMicroSeconds aTimeout)
       
    92 {
       
    93     FTRACE(FPrint(_L("CSimpleTimeout::Start")));
       
    94     if (IsActive())
       
    95     {
       
    96         Cancel();
       
    97     }
       
    98 
       
    99     // Request timer
       
   100     TTime endTime;
       
   101     endTime.HomeTime();
       
   102     endTime = endTime + aTimeout;
       
   103 
       
   104     TInt64 miliseconds = aTimeout.Int64();
       
   105     miliseconds /= 1000;
       
   106 
       
   107     TBuf<30> dateString;
       
   108     endTime.FormatL(dateString, KFormatTimeStamp);
       
   109     iLog->Log(_L("Timer=%LD ms, EndTime=%S"), miliseconds, &dateString);
       
   110 
       
   111     // Store absolute timeout
       
   112     iTestCaseTimeout = endTime;
       
   113 
       
   114     // Taken from STIF engine
       
   115     // Note: iTimer.After() method cannot use because there needed
       
   116     // TTimeIntervalMicroSeconds32 and it is 32 bit. So then cannot create
       
   117     // timeout time that is long enough. At() uses 64 bit value=>Long enough.
       
   118     iTimer.At( iStatus, endTime );
       
   119     SetActive();
       
   120 }
       
   121 
       
   122 // -----------------------------------------------------------------------------
       
   123 // CSimpleTimeout::Stop()
       
   124 // Start timeout counting.
       
   125 // -----------------------------------------------------------------------------
       
   126 //
       
   127 void CSimpleTimeout::Stop()
       
   128 {
       
   129     FTRACE(FPrint(_L("CSimpleTimeout::Stop")));
       
   130     if (IsActive())
       
   131     {
       
   132         Cancel();
       
   133     }
       
   134 }
       
   135 
       
   136 // -----------------------------------------------------------------------------
       
   137 // CSimpleTimeout::RunL()
       
   138 // RunL handles completed timeouts.
       
   139 // -----------------------------------------------------------------------------
       
   140 //
       
   141 void CSimpleTimeout::RunL()
       
   142 {
       
   143     FTRACE(FPrint(_L("CSimpleTimeout::RunL")));
       
   144 	iLog->Log(_L("CSimpleTimeout::RunL"));
       
   145     TTime timeout;
       
   146     timeout.HomeTime();
       
   147     // Handle the abort case when system time gets changed, but timeout is
       
   148     // still valid. All other cases should timeout since they invalidate the
       
   149     // logic of the timers.
       
   150     if ( iStatus == KErrAbort)
       
   151     {
       
   152         if ( iTestCaseTimeout > timeout )
       
   153         {
       
   154             RDebug::Print( _L( "Absolute timer still valid. Restaring timer. iStatus: %d" ), iStatus.Int() );
       
   155             // Start new timer
       
   156             iStatus = KErrNone; // reset value
       
   157             iTimer.At ( iStatus, iTestCaseTimeout );  // restart timer
       
   158             SetActive();
       
   159         }
       
   160         else
       
   161         {
       
   162             // Absolute timer no longer valid. Must timeout.
       
   163             iLog->Log(_L("Absolute timeout no longer valid"));
       
   164             iObserver->HandleTimeout(KErrNone);
       
   165         }
       
   166 
       
   167     }
       
   168     else
       
   169     {
       
   170         // Status was not KErrAbort. Timing out!
       
   171         // iLog->Log(_L("CSimpleTimeout::RunL - Timeout !!"), iTimeout);
       
   172         iLog->Log(_L("Timing out"));
       
   173         iObserver->HandleTimeout(KErrNone);
       
   174     }
       
   175 
       
   176 }
       
   177 
       
   178 // -----------------------------------------------------------------------------
       
   179 // CSimpleTimeout::DoCancel()
       
   180 // Cancel active request.
       
   181 // -----------------------------------------------------------------------------
       
   182 //
       
   183 void CSimpleTimeout::DoCancel()
       
   184 {
       
   185     FTRACE(FPrint(_L("CSimpleTimeout::DoCancel")));
       
   186     iTimer.Cancel();
       
   187 }
       
   188 
       
   189 // -----------------------------------------------------------------------------
       
   190 // CSimpleTimeout::RunError()
       
   191 // Handle errors. Just let framework handle errors because
       
   192 // RunL does not leave.
       
   193 // Returns: Symbian OS error code
       
   194 // -----------------------------------------------------------------------------
       
   195 //
       
   196 TInt CSimpleTimeout::RunError( TInt aError )
       
   197 {
       
   198     FTRACE(FPrint(_L("CSimpleTimeout::RunError")));
       
   199     iLog->Log(_L("Timeout error %d"), aError);
       
   200     iObserver->HandleTimeout(aError);
       
   201     return aError;
       
   202 }
       
   203