wim/WimServer/src/WimTimer.cpp
changeset 0 164170e6151a
equal deleted inserted replaced
-1:000000000000 0:164170e6151a
       
     1 /*
       
     2 * Copyright (c) 2002 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:  Timer class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    "WimTimer.h"
       
    22 #include    "WimConsts.h"
       
    23 #include    "WimTrace.h"
       
    24 
       
    25 
       
    26 // ============================ MEMBER FUNCTIONS ===============================
       
    27 
       
    28 // -----------------------------------------------------------------------------
       
    29 // CWimTimer::NewL
       
    30 // Two-phased constructor.
       
    31 // -----------------------------------------------------------------------------
       
    32 //
       
    33 CWimTimer* CWimTimer::NewL( MWimTimerListener* aTimerListener )
       
    34     {
       
    35     _WIMTRACE(_L("WIM | WIMServer | CWimTimer::NewL | Begin"));
       
    36     CWimTimer *self = new( ELeave ) CWimTimer;
       
    37     CleanupStack::PushL( self );
       
    38     self->ConstructL( aTimerListener );
       
    39     CleanupStack::Pop( self );
       
    40     return self;
       
    41     }
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // CWimTimer::CWimTimer
       
    45 // C++ default constructor can NOT contain any code, that
       
    46 // might leave.
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 CWimTimer::CWimTimer() : CActive( EPriorityStandard ), iHeartBeatCount( 0 )
       
    50     {
       
    51     _WIMTRACE(_L("WIM | WIMServer | CWimTimer::CWimTimer | Begin"));
       
    52     }
       
    53 
       
    54 // Destructor
       
    55 CWimTimer::~CWimTimer()
       
    56     {
       
    57     _WIMTRACE(_L("WIM | WIMServer | CWimTimer::~CWimTimer | Begin"));
       
    58     Cancel();
       
    59     iTimer.Close();
       
    60     delete iHeartbeat;
       
    61     }
       
    62 
       
    63 // -----------------------------------------------------------------------------
       
    64 // CWimTimer::ConstructL
       
    65 // Symbian 2nd phase constructor can leave.
       
    66 // -----------------------------------------------------------------------------
       
    67 //
       
    68 void CWimTimer::ConstructL( MWimTimerListener* aTimerListener )
       
    69     {
       
    70     _WIMTRACE(_L("WIM | WIMServer | CWimTimer::ConstructL | Begin"));
       
    71     User::LeaveIfError( iTimer.CreateLocal() );
       
    72     iTimerListener = aTimerListener;
       
    73     CActiveScheduler::Add( this );
       
    74     iDelay = 0;
       
    75     iHeartbeat = CHeartbeat::NewL( 0 ); // neutral priority
       
    76     }
       
    77 
       
    78 // -----------------------------------------------------------------------------
       
    79 // CWimTimer::SetCloseAfter
       
    80 // Set timeout of timer, delay given in seconds
       
    81 // -----------------------------------------------------------------------------
       
    82 //
       
    83 void CWimTimer::SetCloseAfter( const RMessage2& aMessage )
       
    84     {
       
    85     _WIMTRACE(_L("WIM | WIMServer | CWimTimer::SetCloseAfter | Begin"));
       
    86     TInt closeAfter = aMessage.Int0();
       
    87 
       
    88     // Check that given value is in limits
       
    89     if ( closeAfter < KWimNoTimeout || closeAfter > KWimTimeoutMax )
       
    90         {
       
    91         aMessage.Complete( KErrArgument );
       
    92         }
       
    93     else // Value is OK, set timer
       
    94         {
       
    95         DoSetCloseAfter( closeAfter );
       
    96         aMessage.Complete( KErrNone );
       
    97         }
       
    98     }
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // CWimTimer::SetCloseAfter
       
   102 // Set timeout of timer, delay given in seconds
       
   103 // -----------------------------------------------------------------------------
       
   104 //
       
   105 void CWimTimer::DoSetCloseAfter( TInt aCloseAfter )
       
   106     {
       
   107     _WIMTRACE(_L("WIM | WIMServer | CWimTimer::DoSetCloseAfter | Begin"));
       
   108     if ( IsActive() ) // Timeout already set, cancel
       
   109         {
       
   110         Cancel();
       
   111         }
       
   112     if ( aCloseAfter == KWimNoTimeout ) // No timeout, cancel timer
       
   113         {
       
   114         Cancel();
       
   115         iDelay = KWimNoTimeout;
       
   116         }
       
   117     else
       
   118         {
       
   119         iDelay = aCloseAfter * 1000 * 1000; // Timer takes delay in microseconds
       
   120         iTimer.After( iStatus, iDelay );
       
   121         SetActive();
       
   122         StartHeartBeat();
       
   123         }
       
   124     }
       
   125 // -----------------------------------------------------------------------------
       
   126 // CWimTimer::GetCloseAfterL
       
   127 // Return delay given in SetCloseAfter in seconds
       
   128 // If no timeout is set return -1 (KWimNoTimeout)
       
   129 // -----------------------------------------------------------------------------
       
   130 //
       
   131 void CWimTimer::GetCloseAfterL( const RMessage2& aMessage ) const
       
   132     {
       
   133     _WIMTRACE(_L("WIM | WIMServer | CWimTimer::Delay | Begin"));
       
   134 
       
   135     TInt delay = 0;
       
   136 
       
   137     if ( iDelay.Int() == KWimNoTimeout )
       
   138         {
       
   139         delay = KWimNoTimeout;
       
   140         }
       
   141     else
       
   142         {
       
   143         // Timer takes delay in microseconds
       
   144         delay = iDelay.Int() / ( 1000 * 1000 );
       
   145         }
       
   146 
       
   147     TPckgBuf<TInt> closeAfterPckg( delay  );
       
   148     aMessage.WriteL( 0, closeAfterPckg );
       
   149     aMessage.Complete( KErrNone );
       
   150     }
       
   151 
       
   152 // -----------------------------------------------------------------------------
       
   153 // CWimTimer::RunL()
       
   154 // Handles an active object’s request completion event
       
   155 // -----------------------------------------------------------------------------
       
   156 //
       
   157 void CWimTimer::RunL()
       
   158     {
       
   159     _WIMTRACE(_L("WIM | WIMServer | CWimTimer::RunL | Begin"));
       
   160     if ( iTimerListener )
       
   161         {
       
   162         iTimerListener->TimerExpired();
       
   163         }
       
   164     }
       
   165 
       
   166 // -----------------------------------------------------------------------------
       
   167 // CWimTimer::DoCancel
       
   168 // Cancellation of an outstanding request
       
   169 // -----------------------------------------------------------------------------
       
   170 //
       
   171 void CWimTimer::DoCancel()
       
   172     {
       
   173     _WIMTRACE(_L("WIM | WIMServer | CWimTimer::DoCancel | Begin"));
       
   174     iTimer.Cancel();
       
   175     iHeartbeat->Cancel();
       
   176     }
       
   177 
       
   178 // -----------------------------------------------------------------------------
       
   179 // CWimTimer::ResetTimer
       
   180 // Resets active timer.
       
   181 // -----------------------------------------------------------------------------
       
   182 //
       
   183 void CWimTimer::ResetTimer()
       
   184     {
       
   185     _WIMTRACE(_L("WIM | WIMServer | CWimTimer::ResetTimer | Begin"));
       
   186     if ( IsActive() )
       
   187         {
       
   188         Cancel();
       
   189         iTimer.After( iStatus, iDelay );
       
   190         SetActive();
       
   191         iHeartbeat->Cancel();
       
   192         StartHeartBeat();
       
   193         }
       
   194     }
       
   195 
       
   196 // -----------------------------------------------------------------------------
       
   197 // CWimTimer::TimeRemainingL
       
   198 // Returns remaining time of the timer in seconds. If timer has no timeout
       
   199 // then KWimNoTimeout (-1) is returned.
       
   200 // -----------------------------------------------------------------------------
       
   201 //
       
   202 void CWimTimer::TimeRemainingL( const RMessage2& aMessage  ) const
       
   203     {
       
   204     _WIMTRACE(_L("WIM | WIMServer | CWimTimer::TimeRemainingL | Begin"));
       
   205 
       
   206     TInt timeRemaining;
       
   207 
       
   208     if ( iDelay.Int() == KWimNoTimeout )
       
   209         {
       
   210         timeRemaining = KWimNoTimeout;
       
   211         }
       
   212     else
       
   213         {
       
   214         // Timer takes delay in microseconds
       
   215         TInt delay = iDelay.Int() / ( 1000 * 1000 );
       
   216         timeRemaining = delay - iHeartBeatCount;
       
   217         }
       
   218 
       
   219     TPckgBuf<TInt> timeRemainingPckg( timeRemaining  );
       
   220     aMessage.WriteL( 0, timeRemainingPckg );
       
   221     aMessage.Complete( KErrNone );
       
   222     }
       
   223     
       
   224 // -----------------------------------------------------------------------------
       
   225 // CWimTimer::TimeRemaining
       
   226 // Used inside wimserver
       
   227 // -----------------------------------------------------------------------------
       
   228 //
       
   229 TInt CWimTimer::TimeRemaining() const
       
   230     {
       
   231     _WIMTRACE(_L("WIM | WIMServer | CWimTimer::TimeRemaining | Begin"));
       
   232 
       
   233     TInt timeRemaining;
       
   234 
       
   235     if ( iDelay.Int() == KWimNoTimeout )
       
   236         {
       
   237         timeRemaining = KWimNoTimeout;
       
   238         }
       
   239     else
       
   240         {
       
   241         // Timer takes delay in microseconds
       
   242         TInt delay = iDelay.Int() / ( 1000 * 1000 );
       
   243         timeRemaining = delay - iHeartBeatCount;
       
   244         }
       
   245 
       
   246     return timeRemaining; 
       
   247     }
       
   248 
       
   249 // -----------------------------------------------------------------------------
       
   250 // CWimTimer::StartHeartBeat
       
   251 // Start heart beating
       
   252 // -----------------------------------------------------------------------------
       
   253 //
       
   254 void CWimTimer::StartHeartBeat()
       
   255     {
       
   256     _WIMTRACE(_L("WIM | WIMServer | CWimTimer::StartHeartBeat | Begin"));
       
   257     iHeartbeat->Start( ETwelveOClock, this ); // 1 second intervals
       
   258     }
       
   259 
       
   260 // -----------------------------------------------------------------------------
       
   261 // CWimTimer::Beat
       
   262 // Beat has occurred, increase counter
       
   263 // -----------------------------------------------------------------------------
       
   264 //
       
   265 void CWimTimer::Beat()
       
   266     {
       
   267     _WIMTRACE(_L("WIM | WIMServer | CWimTimer::Beat | Begin"));
       
   268     iHeartBeatCount++;
       
   269     }
       
   270 
       
   271 // -----------------------------------------------------------------------------
       
   272 // CWimTimer::Synchronize
       
   273 // Synchronize the timer.
       
   274 // -----------------------------------------------------------------------------
       
   275 //
       
   276 void CWimTimer::Synchronize()
       
   277     {
       
   278     _WIMTRACE(_L("WIM | WIMServer | CWimTimer::Synchronize | Begin"));
       
   279     }
       
   280 
       
   281 //  End of File