emailservices/emailservermonitor/src/emailservermonitorutilities.cpp
branchRCL_3
changeset 64 3533d4323edc
equal deleted inserted replaced
63:d189ee25cf9d 64:3533d4323edc
       
     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 * Implementation of EmailServerMonitor utility functions
       
    16 *
       
    17 */
       
    18 //  Include Files  
       
    19 
       
    20 #include <e32base.h>
       
    21 #include <e32std.h>
       
    22 #include <e32des16.h>                   // Descriptors
       
    23 
       
    24 #include "emailtrace.h"
       
    25 #include "emailservermonitorutilities.h"
       
    26 
       
    27 // One second as micro seconds
       
    28 const TInt KEmailServerMonitorOneSecond = 1000000;
       
    29 
       
    30 // ---------------------------------------------------------------------------
       
    31 // Wait some time to give other applications and servers some time to shut
       
    32 // down themselves gracefully
       
    33 // ---------------------------------------------------------------------------
       
    34 //
       
    35 void Wait( TInt aWaitTimeInSeconds /*= KDefaultTimeToWaitInSeconds*/ )
       
    36     {
       
    37     FUNC_LOG;
       
    38     User::After( aWaitTimeInSeconds * KEmailServerMonitorOneSecond );
       
    39     }
       
    40 
       
    41 // ---------------------------------------------------------------------------
       
    42 // Checks if the process is already running
       
    43 // ---------------------------------------------------------------------------
       
    44 //
       
    45 TBool IsProcessRunning( TSecureId aUid, RProcess* aProcess /* = NULL */ )
       
    46     {
       
    47     FUNC_LOG;
       
    48     
       
    49     // Get our own UID, if we are checking wheter our own process is running,
       
    50     // we are basically checking is there some other instance already running.
       
    51     // So it's allowed to have one process running (ourselves), but not more.
       
    52     // In normal case already one instance is counted as running.
       
    53     TInt numberOfInstancesRunning = 0;
       
    54     TInt numberOfInstancesAllowed = 0;
       
    55     RProcess ownProcess;
       
    56     if( ownProcess.SecureId() == aUid )
       
    57         {
       
    58         numberOfInstancesAllowed++;
       
    59         }
       
    60     ownProcess.Close();
       
    61     
       
    62     TFileName fileName;
       
    63     TFindProcess finder;
       
    64     while ( finder.Next( fileName ) == KErrNone )
       
    65         {
       
    66         RProcess process;
       
    67         TInt outcome = process.Open( fileName );
       
    68         if ( outcome != KErrNone )
       
    69             {
       
    70             INFO( "Could not open process. Checking next one." );
       
    71             continue;
       
    72             }
       
    73         
       
    74         // Check is this the searched process and is the process running
       
    75         if ( process.SecureId() == aUid && 
       
    76              process.ExitType() == EExitPending )
       
    77             {
       
    78             numberOfInstancesRunning++;
       
    79 
       
    80             if ( numberOfInstancesRunning > numberOfInstancesAllowed )
       
    81                 {
       
    82                 // We either found second instance of ourselves or first
       
    83                 // instance of other searched process
       
    84                 process.Close();
       
    85                 // If process handle given, update it with found process
       
    86                 if( aProcess )
       
    87                     {
       
    88                     aProcess->Close();
       
    89                     TInt error = aProcess->Open( fileName );
       
    90                     // If process handle was given, it's assumed to be
       
    91                     // up-to-date if we return success, so we need to return
       
    92                     // failure if we can't reopen any of the found process'.
       
    93                     // This should be very very rare case.
       
    94                     if( error != KErrNone )
       
    95                         {
       
    96                         continue;
       
    97                         }
       
    98                     }
       
    99                 return ETrue;
       
   100                 }
       
   101             }
       
   102         
       
   103         process.Close();
       
   104         }
       
   105     
       
   106     return EFalse;
       
   107     }
       
   108 
       
   109 
       
   110 /////////////////////////////////////////////////////////////////////////////////
       
   111 // GENERIC TIMER
       
   112 //
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 // CEmailServerMonitorTimer::NewL
       
   116 // NewL function. Returns timer object.
       
   117 // -----------------------------------------------------------------------------
       
   118 //
       
   119 CEmailServerMonitorTimer* CEmailServerMonitorTimer::NewL(
       
   120     MEmailServerMonitorTimerCallback* aCallback,
       
   121     const TInt aPriority )
       
   122     {
       
   123     FUNC_LOG;
       
   124     CEmailServerMonitorTimer* self = NewLC( aCallback, aPriority );
       
   125     CleanupStack::Pop( self );
       
   126     return self;
       
   127     }
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 // CEmailServerMonitorTimer::NewL
       
   131 // NewL function. Returns timer object.
       
   132 // -----------------------------------------------------------------------------
       
   133 //
       
   134 CEmailServerMonitorTimer* CEmailServerMonitorTimer::NewLC(
       
   135     MEmailServerMonitorTimerCallback* aCallback,
       
   136     const TInt aPriority )
       
   137     {
       
   138     FUNC_LOG;
       
   139     CEmailServerMonitorTimer* self = new (ELeave) CEmailServerMonitorTimer( aCallback, aPriority );
       
   140     CleanupStack::PushL( self );
       
   141     self->ConstructL();
       
   142     return self;
       
   143     }
       
   144 
       
   145 // -----------------------------------------------------------------------------
       
   146 // CEmailServerMonitorTimer::NewL
       
   147 // NewL function. Returns timer object.
       
   148 // -----------------------------------------------------------------------------
       
   149 //
       
   150 void CEmailServerMonitorTimer::ConstructL()
       
   151     {
       
   152     FUNC_LOG;
       
   153     CTimer::ConstructL();
       
   154     CActiveScheduler::Add( this );
       
   155     }
       
   156 
       
   157 // -----------------------------------------------------------------------------
       
   158 // CEmailServerMonitorTimer::~CEmailServerMonitorTimer
       
   159 // D'tor
       
   160 // -----------------------------------------------------------------------------
       
   161 //
       
   162 CEmailServerMonitorTimer::~CEmailServerMonitorTimer()
       
   163     {
       
   164     FUNC_LOG;
       
   165     Cancel();
       
   166     iCallback = NULL;
       
   167     }
       
   168 
       
   169 // -----------------------------------------------------------------------------
       
   170 // CEmailServerMonitorTimer::CEmailServerMonitorTimer
       
   171 // C'tor
       
   172 // -----------------------------------------------------------------------------
       
   173 //
       
   174 CEmailServerMonitorTimer::CEmailServerMonitorTimer(
       
   175     MEmailServerMonitorTimerCallback* aCallback,
       
   176     const TInt aPriority )
       
   177     : CTimer( aPriority ),
       
   178     iCallback( aCallback )
       
   179     {
       
   180     }
       
   181 
       
   182 // -----------------------------------------------------------------------------
       
   183 // CEmailServerMonitorTimer::RunL
       
   184 // Timer trigger function.
       
   185 // -----------------------------------------------------------------------------
       
   186 //
       
   187 void CEmailServerMonitorTimer::RunL()
       
   188     {
       
   189     FUNC_LOG;
       
   190     if ( iCallback )
       
   191         {
       
   192         iCallback->TimerEventL( this );
       
   193         }
       
   194     }
       
   195 
       
   196 // -----------------------------------------------------------------------------
       
   197 // CEmailServerMonitorTimer::Start
       
   198 // Timer starting function.
       
   199 // -----------------------------------------------------------------------------
       
   200 //
       
   201 void CEmailServerMonitorTimer::Start( TInt aInterval )
       
   202     {
       
   203     FUNC_LOG;
       
   204     Cancel();
       
   205     After( TTimeIntervalMicroSeconds32( aInterval ) );
       
   206     }
       
   207 
       
   208 // -----------------------------------------------------------------------------
       
   209 // CEmailServerMonitorTimer::Stop
       
   210 // Timer stopping function
       
   211 // -----------------------------------------------------------------------------
       
   212 //
       
   213 void CEmailServerMonitorTimer::Stop()
       
   214     {
       
   215     FUNC_LOG;
       
   216     Cancel();
       
   217     }