clock2/clockengines/clockserver/server/src/clockservermain.cpp
changeset 0 f979ecb2b13e
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 /*
       
     2 * Copyright (c) 2008 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:   Has the main entry point of clockserver.exe
       
    15 *
       
    16 */
       
    17 
       
    18 // System includes
       
    19 #include <e32base.h>
       
    20 
       
    21 // User includes
       
    22 #include "clockservercommon.h"
       
    23 #include "clockservermain.h"
       
    24 #include "clockserver.h"
       
    25 
       
    26 // Literals
       
    27 _LIT( KLabelFault, "ClockServerThread-fault");
       
    28 
       
    29 // ---------------------------------------------------------
       
    30 // Fault(TSrvFault aFault)
       
    31 // rest of the details are commented in the header
       
    32 // ---------------------------------------------------------
       
    33 //
       
    34 GLDEF_C void Fault( TSrvFault aFault )
       
    35     {
       
    36     User::Panic( KLabelFault, aFault );
       
    37     }
       
    38 
       
    39 // ---------------------------------------------------------
       
    40 // TheServerThread( TAny* /*aPr*/ )
       
    41 // rest of the details are commented in the header
       
    42 // ---------------------------------------------------------
       
    43 //
       
    44 EXPORT_C TInt TheServerThread( TAny* /*aPr*/ )
       
    45     {
       
    46     RSemaphore semaphoreHandle;
       
    47 	// Check if a semaphore is already associated with ClockServer
       
    48     if( KErrNone != semaphoreHandle.OpenGlobal( KNameClockServerStartSemaphore ) )
       
    49         {
       
    50 		// If no, then create one.
       
    51         if( KErrNone != semaphoreHandle.CreateGlobal( KNameClockServerStartSemaphore, 0 ) )
       
    52             {
       
    53 			// There's a problem with creating the semaphore.
       
    54             Fault( EMainCreateSemaphore );
       
    55             }
       
    56         }
       
    57     else
       
    58         {
       
    59 		// The global semaphore is already there.
       
    60         semaphoreHandle.Close();
       
    61         return KErrNone;
       
    62         }
       
    63 
       
    64     __UHEAP_MARK;		// Mark beginning of the HEAP
       
    65     
       
    66 	TInt errCode = KErrNone ; 
       
    67 	
       
    68 	// Try renaming the current thread.
       
    69 	errCode = User::RenameThread( KNameClockServer );
       
    70     __ASSERT_ALWAYS( errCode == KErrNone || errCode == KErrAlreadyExists, Fault( EMainClockServerThreadRename ) );
       
    71 
       
    72 	// Construct and active scheduler
       
    73     CActiveScheduler* activeScheduler = new CActiveScheduler;
       
    74     __ASSERT_ALWAYS( activeScheduler != NULL, Fault( EMainCreateScheduler ) );
       
    75 
       
    76 	// Install the active scheduler
       
    77 	CActiveScheduler::Install( activeScheduler );
       
    78 	
       
    79     CTrapCleanup* trapCleanup = CTrapCleanup::New();
       
    80     CClkSrvMain* clockServer = NULL;
       
    81 
       
    82 	// Constructing the CClkSrvMain object, this inturn starts the server in CClkSrvMain::ConstructL -> StartL
       
    83     TRAPD( error, clockServer = CClkSrvMain::NewL() );
       
    84 	__ASSERT_DEBUG( !error, Fault( EMainServerNotStarted ) );
       
    85 
       
    86     if( KErrNone == error )
       
    87         {
       
    88 		// Start the active scheduler
       
    89         CActiveScheduler::Start();
       
    90         }
       
    91 	
       
    92 	// Cleanup
       
    93     delete clockServer;
       
    94     delete CActiveScheduler::Current();
       
    95     delete trapCleanup;
       
    96 
       
    97     __UHEAP_MARKEND;	// Mark end of the HEAP
       
    98 
       
    99     // Close the handle to the RSemaphore
       
   100 	semaphoreHandle.Close();
       
   101 
       
   102     return KErrNone;
       
   103     }
       
   104 
       
   105 // ---------------------------------------------------------
       
   106 // IsServerLoaded()
       
   107 // rest of the details are commented in the header
       
   108 // ---------------------------------------------------------
       
   109 //
       
   110 LOCAL_C TBool IsServerLoaded()
       
   111     {
       
   112     TFindServer findServer( KNameClockServer );
       
   113     TFullName fullName;
       
   114     return( findServer.Next( fullName ) == KErrNone );
       
   115     }
       
   116 
       
   117 // ---------------------------------------------------------
       
   118 // E32Main()
       
   119 // rest of the details are commented in the header
       
   120 // ---------------------------------------------------------
       
   121 //
       
   122 GLDEF_C TInt E32Main()
       
   123     {
       
   124     return ( IsServerLoaded() ? KErrInUse : TheServerThread( NULL ) );
       
   125     }
       
   126 
       
   127 
       
   128 // End of file