00001 /* 00002 * ============================================================================== 00003 * Name : timeserver.cpp 00004 * Part of : CSSync 00005 * Interface : 00006 * Description : 00007 * Version : 00008 * 00009 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies). 00010 * All rights reserved. 00011 * This component and the accompanying materials are made available 00012 * under the terms of "Eclipse Public License v1.0" 00013 * which accompanies this distribution, and is available 00014 * at the URL "http://www.eclipse.org/legal/epl-v10.html". 00015 * ============================================================================== 00016 */ 00017 00018 00019 // INCLUDE FILES 00020 #include <eikstart.h> 00021 #include <e32svr.h> 00022 #include <e32math.h> 00023 00024 #include "TimeServer.h" 00025 #include "ClientServerCommon.h" 00026 #include "TimeSession.h" 00027 00028 // ========================= MEMBER FUNCTIONS ================================== 00029 00030 // ----------------------------------------------------------------------------- 00031 // CTimeServer::NewL() 00032 // Two-phased constructor. 00033 // ----------------------------------------------------------------------------- 00034 // 00035 CTimeServer* CTimeServer::NewL() 00036 { 00037 CTimeServer* timeServer = CTimeServer::NewLC(); 00038 CleanupStack::Pop( timeServer ); 00039 return timeServer; 00040 } 00041 00042 // ----------------------------------------------------------------------------- 00043 // CTimeServer::NewLC() 00044 // Two-phased constructor. 00045 // ----------------------------------------------------------------------------- 00046 // 00047 CTimeServer* CTimeServer::NewLC() 00048 { 00049 CTimeServer* timeServer = new ( ELeave ) CTimeServer( EPriorityNormal ); 00050 CleanupStack::PushL( timeServer ); 00051 timeServer->ConstructL(); 00052 return timeServer; 00053 } 00054 00055 // ----------------------------------------------------------------------------- 00056 // CTimeServer::ConstructL() 00057 // Symbian 2nd phase constructor can leave. 00058 // ----------------------------------------------------------------------------- 00059 // 00060 void CTimeServer::ConstructL() 00061 { 00062 StartL( KTimeServerName ); 00063 } 00064 00065 // ----------------------------------------------------------------------------- 00066 // CTimeServer::CTimeServer() 00067 // C++ default constructor can NOT contain any code, that might leave. 00068 // ----------------------------------------------------------------------------- 00069 // 00070 CTimeServer::CTimeServer( TInt aPriority ) 00071 : CServer2( aPriority ) 00072 { 00073 // Implementation not required 00074 } 00075 00076 // ----------------------------------------------------------------------------- 00077 // CTimeServer::NewSessionL() 00078 // Creates a time server session. 00079 // ----------------------------------------------------------------------------- 00080 // 00081 CSession2* CTimeServer::NewSessionL( const TVersion& aVersion, 00082 const RMessage2& /*aMessage*/ ) const 00083 { 00084 // Check we are the right version 00085 if ( !User::QueryVersionSupported( TVersion( KTimeServMajorVersionNumber, 00086 KTimeServMinorVersionNumber, 00087 KTimeServBuildVersionNumber ), 00088 aVersion ) ) 00089 { 00090 User::Leave( KErrNotSupported ); 00091 } 00092 00093 // Make new session 00094 return CTimeServerSession::NewL( *const_cast<CTimeServer*> ( this ) ); 00095 } 00096 00097 // ----------------------------------------------------------------------------- 00098 // CTimeServer::IncrementSessions() 00099 // Increments the count of the active sessions for this server. 00100 // ----------------------------------------------------------------------------- 00101 // 00102 void CTimeServer::IncrementSessions() 00103 { 00104 iSessionCount++; 00105 } 00106 00107 // ----------------------------------------------------------------------------- 00108 // CTimeServer::DecrementSessions() 00109 // Decrements the count of the active sessions for this server. 00110 // ----------------------------------------------------------------------------- 00111 // 00112 void CTimeServer::DecrementSessions() 00113 { 00114 iSessionCount--; 00115 if ( iSessionCount <= 0 ) 00116 { 00117 CActiveScheduler::Stop(); 00118 } 00119 } 00120 00121 // ----------------------------------------------------------------------------- 00122 // CTimeServer::RunError() 00123 // Processes any errors. 00124 // ----------------------------------------------------------------------------- 00125 // 00126 TInt CTimeServer::RunError( TInt aError ) 00127 { 00128 if ( aError == KErrBadDescriptor ) 00129 { 00130 // A bad descriptor error implies a badly programmed client, 00131 // so panic it; otherwise report the error to the client 00132 PanicClient( Message(), EBadDescriptor ); 00133 } 00134 else 00135 { 00136 Message().Complete( aError ); 00137 } 00138 00139 // The leave will result in an early return from CServer::RunL(), skipping 00140 // the call to request another message. So do that now in order to keep the 00141 // server running. 00142 ReStart(); 00143 00144 return KErrNone; // Handled the error fully 00145 } 00146 00147 // ----------------------------------------------------------------------------- 00148 // CTimeServer::PanicClient() 00149 // Panics the client. 00150 // ----------------------------------------------------------------------------- 00151 // 00152 void CTimeServer::PanicClient( const RMessage2& aMessage, TTimeServPanic aPanic ) 00153 { 00154 aMessage.Panic( KCSSyncServer, aPanic ); 00155 } 00156 00157 // ----------------------------------------------------------------------------- 00158 // CTimeServer::PanicServer() 00159 // Panics the server. 00160 // ----------------------------------------------------------------------------- 00161 // 00162 void CTimeServer::PanicServer( TTimeServPanic aPanic ) 00163 { 00164 User::Panic( KCSSyncServer, aPanic ); 00165 } 00166 00167 // ----------------------------------------------------------------------------- 00168 // CTimeServer::ThreadFunctionL() 00169 // Second stage startup for the server thread. 00170 // ----------------------------------------------------------------------------- 00171 // 00172 void CTimeServer::ThreadFunctionL() 00173 { 00174 // Construct active scheduler 00175 CActiveScheduler* activeScheduler = new ( ELeave ) CActiveScheduler; 00176 CleanupStack::PushL( activeScheduler ); 00177 00178 // Install active scheduler 00179 // We don't need to check whether an active scheduler is already installed 00180 // as this is a new thread, so there won't be one 00181 CActiveScheduler::Install( activeScheduler ); 00182 00183 // Construct our server 00184 CTimeServer::NewLC(); // Anonymous 00185 00186 RSemaphore semaphore; 00187 User::LeaveIfError( semaphore.OpenGlobal( KTimeServerSemaphoreName ) ); 00188 00189 // Semaphore opened ok 00190 semaphore.Signal(); 00191 semaphore.Close(); 00192 00193 // Start handling requests 00194 CActiveScheduler::Start(); 00195 00196 CleanupStack::PopAndDestroy( 2, activeScheduler ); //Anonymous CTimeServer 00197 } 00198 00199 // ----------------------------------------------------------------------------- 00200 // CTimeServer::ThreadFunction() 00201 // Main function for the server thread. 00202 // ----------------------------------------------------------------------------- 00203 // 00204 TInt CTimeServer::ThreadFunction( TAny* /*aNone*/ ) 00205 { 00206 CTrapCleanup* cleanupStack = CTrapCleanup::New(); 00207 if ( !( cleanupStack ) ) 00208 { 00209 PanicServer( ECreateTrapCleanup ); 00210 } 00211 00212 TRAPD( err, ThreadFunctionL() ); 00213 if ( err != KErrNone ) 00214 { 00215 PanicServer( ESrvCreateServer ); 00216 } 00217 00218 delete cleanupStack; 00219 cleanupStack = NULL; 00220 00221 return KErrNone; 00222 } 00223 00224 // ----------------------------------------------------------------------------- 00225 // E32Main() 00226 // Provides the API for the operating system to start the executable. 00227 // Returns the address of the function to be called. 00228 // ----------------------------------------------------------------------------- 00229 // 00230 TInt E32Main() 00231 { 00232 return CTimeServer::ThreadFunction( NULL ); 00233 } 00234 00235 00236 00237 // End of File