emailservices/emailservermonitor/src/main.cpp
changeset 0 8466d47a6819
child 12 f5907b1a1053
equal deleted inserted replaced
-1:000000000000 0:8466d47a6819
       
     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 * Main entry point of EmailServerMonitor
       
    16 *
       
    17 */
       
    18 
       
    19 //  Include Files  
       
    20 #include <e32base.h>
       
    21 #include <e32std.h>
       
    22 #include <e32des16.h>                   // Descriptors
       
    23 #include <bacline.h>                    // CCommandLineArguments
       
    24 
       
    25 #include "emailtrace.h"
       
    26 #include "emailshutdownconst.h"
       
    27 #include "emailservermonitorconst.h"
       
    28 #include "emailservermonitorutilities.h"
       
    29 #include "emailservermonitor.h"
       
    30 #include "emailshutter.h"
       
    31 
       
    32 
       
    33 // ---------------------------------------------------------------------------
       
    34 // Defines operation mode by parsing command line parameters
       
    35 // ---------------------------------------------------------------------------
       
    36 //
       
    37 LOCAL_C TEmailServerMonitorMode ParseCommandLineArgumentsL()
       
    38     {
       
    39     FUNC_LOG;
       
    40     TEmailServerMonitorMode mode = EEsmModeNormal;
       
    41     
       
    42     CCommandLineArguments* args = CCommandLineArguments::NewLC();
       
    43     // Check if some command line arguments were given
       
    44     if( args->Count() > 1 )
       
    45        {
       
    46        TPtrC argumentPtr( args->Arg(1) );
       
    47        if( argumentPtr == KEmailShutdownHandlerArgOnlyShutter )
       
    48            {
       
    49            mode = EEsmModeOnlyShutter;
       
    50            }
       
    51        else if( argumentPtr == KEmailShutdownHandlerArgRestart )
       
    52            {
       
    53            mode = EEsmModeRestartAfterInstallation;
       
    54            }
       
    55        }
       
    56     CleanupStack::PopAndDestroy(args);
       
    57     
       
    58     INFO_1( "TEmailServerMonitorMode: %d", mode );
       
    59     return mode;
       
    60     }
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 // Starts active scheduler, runs the needed operations, and then deletes
       
    64 // the active scheduler when everything is done
       
    65 // ---------------------------------------------------------------------------
       
    66 //
       
    67 LOCAL_C void DoStartL()
       
    68     {
       
    69     FUNC_LOG;
       
    70     // Create active scheduler (to run active objects)
       
    71     CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
       
    72     CleanupStack::PushL(scheduler);
       
    73     CActiveScheduler::Install(scheduler);
       
    74     
       
    75     TEmailServerMonitorMode mode = ParseCommandLineArgumentsL();
       
    76 
       
    77     // Start email shutter functionality
       
    78     CEmailShutter* shutter = CEmailShutter::NewLC();
       
    79     if( mode == EEsmModeRestartAfterInstallation )
       
    80         {
       
    81         shutter->SetPsKeyInstallationFinished();
       
    82         shutter->RestartServicesAfterInstallation();
       
    83         }
       
    84     shutter->StartObservingShutdownEvent();
       
    85 
       
    86     CEmailServerMonitor* monitor = NULL;
       
    87     if( mode != EEsmModeOnlyShutter )
       
    88         {
       
    89         // Start email server observing functionality
       
    90         monitor = CEmailServerMonitor::NewLC();
       
    91         monitor->Start();
       
    92         shutter->SetMonitor( monitor );
       
    93         }
       
    94 
       
    95     // Complete client's Rendezvous
       
    96     RProcess::Rendezvous( KErrNone );
       
    97     
       
    98     // Start asynchronous services
       
    99     CActiveScheduler::Start();
       
   100     
       
   101     INFO( "ActiveScheduler stopped, closing down" );
       
   102     // Cancel any pending requests and delete handler objects
       
   103     // after active scheduler is stopped
       
   104     
       
   105     if( monitor )
       
   106         {
       
   107         monitor->Cancel();
       
   108         CleanupStack::PopAndDestroy( monitor );
       
   109         }
       
   110     
       
   111     shutter->Cancel();
       
   112     CleanupStack::PopAndDestroy( shutter );
       
   113 
       
   114     // Delete active scheduler
       
   115     CleanupStack::PopAndDestroy( scheduler );
       
   116     }
       
   117 
       
   118 // ======== GLOBAL FUNCTIONS ========
       
   119 
       
   120 // ---------------------------------------------------------------------------
       
   121 // Executable main entry point
       
   122 // ---------------------------------------------------------------------------
       
   123 //
       
   124 GLDEF_C TInt E32Main()
       
   125     {
       
   126     FUNC_LOG;
       
   127     __UHEAP_MARK;
       
   128 
       
   129     RProcess ownProcess;
       
   130     TSecureId ownUid = ownProcess.SecureId();
       
   131     INFO_1( "%S", &ownProcess.FileName() );
       
   132     ownProcess.Close();
       
   133 
       
   134     // Just exit if there is another process already running
       
   135     if( IsProcessRunning( ownUid ) )
       
   136         {
       
   137         INFO( "Another instance of Email Server Monitor already running!" );
       
   138         }
       
   139     else
       
   140         {
       
   141         // Rename thread for debug purposes, return code can be ignored
       
   142         RThread::RenameMe( KEmailServerMonitorName );
       
   143     
       
   144         // Create the cleanup stack
       
   145         CTrapCleanup* cleanup = NULL;
       
   146         cleanup = CTrapCleanup::New();
       
   147         if ( !cleanup )
       
   148             {
       
   149             // No errors leaked outside, return success.
       
   150             INFO( "Could not create clean up stack!" );
       
   151             return KErrNone;
       
   152             }
       
   153     
       
   154         // Run application code inside TRAP harness
       
   155         TRAPD( mainError, DoStartL() );
       
   156         
       
   157         if( mainError != KErrNone )
       
   158             {
       
   159             ERROR_1( mainError, "Email Server Monitor main error code: %d", mainError );
       
   160             }
       
   161         
       
   162         delete cleanup;
       
   163         }
       
   164 
       
   165     INFO( "Email Server Monitor finished!" );
       
   166     __UHEAP_MARKEND;
       
   167     return KErrNone;
       
   168     }