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