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