mmappfw_plat/mpx_common_api/tsrc/ui_commontestclass/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 #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 // CSimpleTimeout::~CSimpleTimeout()
       
    75 // Destructor.
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 CSimpleTimeout::~CSimpleTimeout()
       
    79     {
       
    80     FTRACE(FPrint(_L("CSimpleTimeout::~CSimpleTimeout")));
       
    81     Cancel();
       
    82     iTimer.Close();
       
    83     }
       
    84 
       
    85 // -----------------------------------------------------------------------------
       
    86 // CSimpleTimeout::Start()
       
    87 // Start timeout counting.
       
    88 // -----------------------------------------------------------------------------
       
    89 //
       
    90 void CSimpleTimeout::Start(TTimeIntervalMicroSeconds aTimeout)
       
    91     {
       
    92     FTRACE(FPrint(_L("CSimpleTimeout::Start")));
       
    93     if (IsActive())
       
    94         {
       
    95         Cancel();
       
    96         }
       
    97     // Request timer
       
    98     TTime endTime;
       
    99     endTime.HomeTime();
       
   100     endTime = endTime + aTimeout;
       
   101 
       
   102     TInt64 miliseconds = aTimeout.Int64();
       
   103     miliseconds /= 1000;
       
   104 
       
   105     TBuf<30> dateString;
       
   106     TRAPD(err,endTime.FormatL(dateString, KFormatTimeStamp));
       
   107     iLog->Log(_L("Timer=%LD ms, EndTime=%S"), miliseconds, &dateString);
       
   108 
       
   109     // Store absolute timeout
       
   110     iTestCaseTimeout = endTime;
       
   111 
       
   112     // Taken from STIF engine
       
   113     // Note: iTimer.After() method cannot use because there needed
       
   114     // TTimeIntervalMicroSeconds32 and it is 32 bit. So then cannot create
       
   115     // timeout time that is long enough. At() uses 64 bit value=>Long enough.
       
   116     iTimer.At( iStatus, endTime );
       
   117     SetActive();
       
   118     }
       
   119 
       
   120 // -----------------------------------------------------------------------------
       
   121 // CSimpleTimeout::Stop()
       
   122 // Start timeout counting.
       
   123 // -----------------------------------------------------------------------------
       
   124 //
       
   125 void CSimpleTimeout::Stop()
       
   126     {
       
   127     FTRACE(FPrint(_L("CSimpleTimeout::Stop")));
       
   128     if (IsActive())
       
   129         {
       
   130         Cancel();
       
   131         }
       
   132     }
       
   133 
       
   134 // -----------------------------------------------------------------------------
       
   135 // CSimpleTimeout::RunL()
       
   136 // RunL handles completed timeouts.
       
   137 // -----------------------------------------------------------------------------
       
   138 //
       
   139 void CSimpleTimeout::RunL()
       
   140     {
       
   141     FTRACE(FPrint(_L("CSimpleTimeout::RunL")));
       
   142 	iLog->Log(_L("CSimpleTimeout::RunL"));
       
   143     TTime timeout;
       
   144     timeout.HomeTime();
       
   145     // Handle the abort case when system time gets changed, but timeout is
       
   146     // still valid. All other cases should timeout since they invalidate the
       
   147     // logic of the timers.
       
   148     if ( iStatus == KErrAbort)
       
   149         {
       
   150         if ( iTestCaseTimeout > timeout )
       
   151             {
       
   152             RDebug::Print( _L( "Absolute timer still valid. Restaring timer. iStatus: %d" ), iStatus.Int() );
       
   153             // Start new timer
       
   154             iStatus = KErrNone; // reset value
       
   155             iTimer.At ( iStatus, iTestCaseTimeout );  // restart timer
       
   156             SetActive();
       
   157             }
       
   158         else
       
   159             {
       
   160             // Absolute timer no longer valid. Must timeout.
       
   161             iLog->Log(_L("Absolute timeout no longer valid"));
       
   162             iObserver->HandleTimeout(KErrNone);
       
   163             }
       
   164         }
       
   165     else
       
   166         {
       
   167         // Status was not KErrAbort. Timing out!
       
   168         // iLog->Log(_L("CSimpleTimeout::RunL - Timeout !!"), iTimeout);
       
   169         iLog->Log(_L("Timing out"));
       
   170         iObserver->HandleTimeout(KErrNone);
       
   171         }
       
   172     }
       
   173 
       
   174 // -----------------------------------------------------------------------------
       
   175 // CSimpleTimeout::DoCancel()
       
   176 // Cancel active request.
       
   177 // -----------------------------------------------------------------------------
       
   178 //
       
   179 void CSimpleTimeout::DoCancel()
       
   180     {
       
   181     FTRACE(FPrint(_L("CSimpleTimeout::DoCancel")));
       
   182     iTimer.Cancel();
       
   183     }
       
   184 
       
   185 // -----------------------------------------------------------------------------
       
   186 // CSimpleTimeout::RunError()
       
   187 // Handle errors. Just let framework handle errors because
       
   188 // RunL does not leave.
       
   189 // Returns: Symbian OS error code
       
   190 // -----------------------------------------------------------------------------
       
   191 //
       
   192 TInt CSimpleTimeout::RunError( TInt aError )
       
   193     {
       
   194     FTRACE(FPrint(_L("CSimpleTimeout::RunError")));
       
   195     iLog->Log(_L("Timeout error %d"), aError);
       
   196     iObserver->HandleTimeout(aError);
       
   197     return aError;
       
   198     }
       
   199