watchdog/src/watchdog.cpp
changeset 0 c53acadfccc6
child 15 3cebc1a84278
child 21 50bf9db68373
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     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: Monitoring application for servers restarting & IAD
       
    15 *
       
    16 */
       
    17 
       
    18 #include "mdscommoninternal.h"
       
    19 #include "watchdog.h"
       
    20 
       
    21 _LIT( KHarvesterServerProcess, "HarvesterServer*" );
       
    22 _LIT( KHarvesterServerExe, "harvesterserver.exe" );
       
    23 
       
    24 // Print macro
       
    25 #ifdef _DEBUG
       
    26 #include <e32svr.h>
       
    27 #define PRINT(x) RDebug::Print x
       
    28 #else
       
    29 #define PRINT(x)
       
    30 #endif
       
    31 
       
    32 
       
    33 // ======== MEMBER FUNCTIONS ========
       
    34 // ---------------------------------------------------------------------------
       
    35 // Constructor.
       
    36 // ---------------------------------------------------------------------------
       
    37 //
       
    38 CWatchdog* CWatchdog::NewL()
       
    39     {
       
    40     PRINT(_L("CWatchdog::NewL()"));
       
    41     CWatchdog* self = new ( ELeave ) CWatchdog();
       
    42     CleanupStack::PushL( self );
       
    43     self->ConstructL();
       
    44     CleanupStack::Pop( self );
       
    45 
       
    46     return self;
       
    47     }
       
    48 
       
    49 // ---------------------------------------------------------------------------
       
    50 // C++ constructor.
       
    51 // ---------------------------------------------------------------------------
       
    52 //
       
    53 CWatchdog::CWatchdog() : 
       
    54     CActive( CActive::EPriorityLow ), iState(EIdle)
       
    55     {
       
    56     // Add to active scheduler.
       
    57     CActiveScheduler::Add( this );
       
    58     }
       
    59 
       
    60 // ---------------------------------------------------------------------------
       
    61 // 2nd phase constructor.
       
    62 // ---------------------------------------------------------------------------
       
    63 //
       
    64 void CWatchdog::ConstructL()
       
    65     {
       
    66     PRINT(_L("CWatchdog::ConstructL()"));
       
    67         
       
    68     TFullName name;
       
    69         
       
    70     // find Harvester Server
       
    71     TFindProcess findProcess( KHarvesterServerProcess );
       
    72     if ( findProcess.Next(name) == KErrNone )
       
    73         {                
       
    74         PRINT(_L("CWatchdog::ConstructL() - found server, start listening"));
       
    75         
       
    76         User::LeaveIfError( iProcess.Open(name) );
       
    77         iState = ERunning;
       
    78         // logon to get termination signal
       
    79         iProcess.Logon(iStatus);        
       
    80         SetActive();
       
    81         }        
       
    82     else
       
    83         {               
       
    84         PRINT(_L("CWatchdog::ConstructL() - start Harvester server"));
       
    85         // start new Harvester            
       
    86         Start();
       
    87         }
       
    88     
       
    89     PRINT(_L("CWatchdog::ConstructL() - create observer"));
       
    90     iShutdownObserver = CWDShutdownObserver::NewL( *this );        
       
    91     iSelfShutdownObserver = CWDSelfShutdownObserver::NewL( *this ); 
       
    92     
       
    93     RProcess process;
       
    94     process.SetPriority( EPriorityBackground );
       
    95     process.Close();
       
    96     }
       
    97 
       
    98 void CWatchdog::Start()
       
    99     {
       
   100     
       
   101     PRINT(_L("CWatchdog::Start()"));
       
   102 
       
   103     // Create the server process
       
   104     TInt res( KErrNone );
       
   105     
       
   106     // KNullDesC param causes server's E32Main() to be run
       
   107     res = iProcess.Create( KHarvesterServerExe, KNullDesC );
       
   108     if ( res != KErrNone )
       
   109         {
       
   110         PRINT(_L("CWatchdog::ConstructL() - error in server creation"));
       
   111         return;
       
   112         }
       
   113     
       
   114     // start process and wait until it is constructed                
       
   115     iProcess.Rendezvous(iStatus);                                         
       
   116     
       
   117     if( iStatus != KRequestPending )
       
   118         {
       
   119         iProcess.Kill( 0 );   // abort startup
       
   120         }
       
   121     else
       
   122         {
       
   123         iProcess.Resume();    // logon OK - start the server   
       
   124         iState = EWaitingRendezvous;
       
   125         SetActive();
       
   126         }
       
   127     
       
   128     PRINT(_L("CWatchdog::ConstructL() - Start end"));
       
   129     }    
       
   130     
       
   131 
       
   132 // ---------------------------------------------------------------------------
       
   133 // Destructor
       
   134 // ---------------------------------------------------------------------------
       
   135 //
       
   136 CWatchdog::~CWatchdog()
       
   137     {
       
   138     delete iShutdownObserver;    
       
   139     delete iSelfShutdownObserver;
       
   140     Cancel();
       
   141     }
       
   142 
       
   143 // ---------------------------------------------------------------------------
       
   144 // Active object's request handling.
       
   145 // ---------------------------------------------------------------------------
       
   146 //
       
   147 void CWatchdog::RunL()
       
   148     {
       
   149     PRINT(_L("CWatchdog::RunL"));
       
   150     	
       
   151     	switch (iState)
       
   152     	{
       
   153           case EIdle:
       
   154               {
       
   155               PRINT(_L("CWatchdog::RunL() - EIdle"));
       
   156               break;
       
   157               }
       
   158               
       
   159           case EWaitingRendezvous:          
       
   160               {
       
   161               PRINT(_L("CWatchdog::RunL() - EWaitingRendezvous"));
       
   162               iState = ERunning;              
       
   163               // logon to get termination signal
       
   164               iProcess.Logon(iStatus);        
       
   165               SetActive();
       
   166               break;
       
   167               }             
       
   168               
       
   169           case ERunning:
       
   170               {
       
   171               PRINT(_L("CWatchdog::RunL() - server died"));
       
   172               // server died unexpectedly, start it    
       
   173               Start();
       
   174               break;
       
   175               }
       
   176               
       
   177           case EShuttingDown:
       
   178               {
       
   179               PRINT(_L("CWatchdog::RunL() - IAD shutdown"));
       
   180               // Do nothing              
       
   181               break;
       
   182               }
       
   183               
       
   184           case ERestarting:
       
   185               {
       
   186               PRINT(_L("CWatchdog::RunL() - IAD restart"));
       
   187               Start();
       
   188               break;
       
   189               }
       
   190             
       
   191           default:
       
   192               break;
       
   193               
       
   194           
       
   195     	}
       
   196     
       
   197     }
       
   198 
       
   199 // ---------------------------------------------------------------------------
       
   200 // Active object's request error handling.
       
   201 // ---------------------------------------------------------------------------
       
   202 //
       
   203 TInt CWatchdog::RunError( TInt /*aError*/ )
       
   204     {
       
   205     return KErrNone;
       
   206     }
       
   207 
       
   208 // ---------------------------------------------------------------------------
       
   209 // Cancel the request.
       
   210 // ---------------------------------------------------------------------------
       
   211 //
       
   212 void CWatchdog::DoCancel()
       
   213     {
       
   214     PRINT(_L("CWatchdog::DoCancel()"));
       
   215     iProcess.LogonCancel(iStatus);
       
   216     iProcess.Close();
       
   217     }
       
   218 
       
   219 
       
   220 // -----------------------------------------------------------------------------
       
   221 // CWatchdog::ShutdownNotification
       
   222 // -----------------------------------------------------------------------------
       
   223 //
       
   224 void CWatchdog::ShutdownNotification()
       
   225     {
       
   226 
       
   227     iState = EShuttingDown;
       
   228     
       
   229     PRINT((_L("CWatchdog::ShutdownNotification() IsActive() = %d"), IsActive() ));
       
   230     
       
   231     
       
   232     }
       
   233 
       
   234 // -----------------------------------------------------------------------------
       
   235 // CWatchdog::ShutdownNotification
       
   236 // -----------------------------------------------------------------------------
       
   237 //
       
   238 void CWatchdog::RestartNotification()
       
   239     {
       
   240     PRINT(_L("CWatchdog::RestartNotification()"));
       
   241     
       
   242     if (!IsActive())
       
   243         SetActive();
       
   244     
       
   245     iState = ERestarting;    
       
   246     TRequestStatus *status = &iStatus;
       
   247     User::RequestComplete(status, KErrNone);
       
   248         
       
   249     }
       
   250 
       
   251 void CWatchdog::SelfShutdownNotification()
       
   252     {
       
   253     PRINT(_L("CWatchdog::SelfShutdownNotification()"));
       
   254     CActiveScheduler::Stop();
       
   255     
       
   256     }
       
   257 
       
   258 void MainL()
       
   259     {
       
   260     CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
       
   261     CActiveScheduler::Install( scheduler );
       
   262     CleanupStack::PushL( scheduler );
       
   263     
       
   264     CWatchdog* w = CWatchdog::NewL();
       
   265     CleanupStack::PushL( w );
       
   266     CActiveScheduler::Start();
       
   267     
       
   268     CleanupStack::PopAndDestroy( 2, scheduler );
       
   269     }
       
   270 
       
   271 TInt E32Main()
       
   272     {
       
   273     TInt err = KErrNoMemory;
       
   274    
       
   275     CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
   276     if ( cleanupStack )
       
   277         {
       
   278         TRAP( err, MainL() );
       
   279         }
       
   280   
       
   281     delete cleanupStack;
       
   282     return err;
       
   283     }