realtimenetprots/sipfw/ProfileAgent/Server/Src/SipProfileCSServer.cpp
changeset 0 307788aac0a8
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Name        : SIPProfileCSServer.cpp
       
    15 // Part of     : SIP Profile Server
       
    16 // implementation
       
    17 // Version     : 1.0
       
    18 //
       
    19 
       
    20 
       
    21 
       
    22 // INCLUDE FILES
       
    23 #include "SipProfileActiveScheduler.h"
       
    24 #include "SipProfileCSServer.h"
       
    25 #include "SipProfileCSSession.h"
       
    26 #include "SipProfileCSServer.pan"
       
    27 #include "SipProfileCSServerCloseTimer.h"
       
    28 #include "SipProfileSecurityPolicy.h"
       
    29 #include "SipProfileLog.h"
       
    30 
       
    31 // ============================ MEMBER FUNCTIONS ===============================
       
    32 
       
    33 // -----------------------------------------------------------------------------
       
    34 // CSIPProfileCSServer::NewL
       
    35 // -----------------------------------------------------------------------------
       
    36 //
       
    37 CSIPProfileCSServer*
       
    38 CSIPProfileCSServer::NewL(CSIPProfileServerCore& aServerCore)
       
    39 	{
       
    40     CSIPProfileCSServer* self = CSIPProfileCSServer::NewLC(aServerCore);
       
    41     CleanupStack::Pop(self);
       
    42     return self;
       
    43 	}
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // CSIPProfileCSServer::NewLC
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 CSIPProfileCSServer*
       
    50 CSIPProfileCSServer::NewLC(CSIPProfileServerCore& aServerCore)
       
    51 	{
       
    52 	CSIPProfileCSServer* self =
       
    53 	    new (ELeave) CSIPProfileCSServer(aServerCore,EPriorityHigh);
       
    54     CleanupStack::PushL(self);
       
    55     self->ConstructL();
       
    56     return self;
       
    57 	}
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // CSIPProfileCSServer::ConstructL
       
    61 // -----------------------------------------------------------------------------
       
    62 //
       
    63 void CSIPProfileCSServer::ConstructL()
       
    64 	{
       
    65 	const TUint	KSIPProfileServerTimerValue = 5;
       
    66     StartL(KSipProfileServerName);	
       
    67 	if (iServerCore.CanServerStop()) 
       
    68 		{
       
    69 		PROFILE_DEBUG1("ProfileServer shutdown timer started")
       
    70 		iCloseTimer = CSipProfileCSServerCloseTimer::NewL();
       
    71 		iCloseTimer->StopActiveSchedulerAfter(KSIPProfileServerTimerValue);
       
    72 		}
       
    73 	}
       
    74 
       
    75 // -----------------------------------------------------------------------------
       
    76 // CSIPProfileCSServer::CSIPProfileCSServer
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 CSIPProfileCSServer::CSIPProfileCSServer(
       
    80     CSIPProfileServerCore& aServerCore,
       
    81     TInt aPriority)
       
    82  : CPolicyServer(aPriority, TSipProfilePlatSecPolicy),
       
    83    iServerCore(aServerCore)
       
    84 	{
       
    85 	}
       
    86 
       
    87 // -----------------------------------------------------------------------------
       
    88 // CSIPProfileCSServer::~CSIPProfileCSServer
       
    89 // -----------------------------------------------------------------------------
       
    90 //
       
    91 CSIPProfileCSServer::~CSIPProfileCSServer()
       
    92 	{
       
    93 	delete iCloseTimer;
       
    94 	}
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 // CSIPProfileCSServer::NewSessionL
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 CSession2* CSIPProfileCSServer::NewSessionL(const TVersion &aVersion,
       
   101                                             const RMessage2& /*aMessage*/) const
       
   102 	{
       
   103 	PROFILE_DEBUG1("CSIPProfileCSServer::NewSessionL")
       
   104 
       
   105 	// Check we're the right version
       
   106 	if (!User::QueryVersionSupported(
       
   107 		TVersion(KSipProfileServerMajorVersionNumber,
       
   108                  KSipProfileServerMinorVersionNumber,
       
   109                  KSipProfileServerBuildVersionNumber),
       
   110                  aVersion))
       
   111 		{
       
   112 		User::Leave(KErrNotSupported);
       
   113 		}
       
   114 
       
   115 	if (iServerCore.BackupInProgress())
       
   116 		{
       
   117 		User::Leave(KErrLocked);
       
   118 		}
       
   119 
       
   120 	PROFILE_DEBUG1("CSIPProfileCSServer::NewSessionL, exit")
       
   121     return CSIPProfileCSSession::NewL(*const_cast<CSIPProfileCSServer*>(this),
       
   122                                       iServerCore);
       
   123 	}
       
   124 
       
   125 // -----------------------------------------------------------------------------
       
   126 // CSIPProfileCSServer::IncrementSessions
       
   127 // -----------------------------------------------------------------------------
       
   128 //
       
   129 void CSIPProfileCSServer::IncrementSessions()
       
   130 	{
       
   131     iSessionCount++;
       
   132 	if (iCloseTimer)
       
   133 		{
       
   134 		PROFILE_DEBUG1("ProfileServer shutdown timer stopped")
       
   135 		delete iCloseTimer;
       
   136 		iCloseTimer = NULL;
       
   137 		}
       
   138 	}
       
   139 
       
   140 // -----------------------------------------------------------------------------
       
   141 // CSIPProfileCSServer::DecrementSessions
       
   142 // -----------------------------------------------------------------------------
       
   143 //
       
   144 void CSIPProfileCSServer::DecrementSessions()
       
   145 	{
       
   146     iSessionCount--;
       
   147 
       
   148 	if (iSessionCount == 0 && iServerCore.CanServerStop())
       
   149 		{
       
   150 	    CActiveScheduler::Stop();
       
   151 		}
       
   152 	}
       
   153 
       
   154 // -----------------------------------------------------------------------------
       
   155 // CSIPProfileCSServer::ServerCanStop
       
   156 // -----------------------------------------------------------------------------
       
   157 //
       
   158 void CSIPProfileCSServer::ServerCanStop()
       
   159 	{
       
   160 	if (iSessionCount == 0)
       
   161 		{
       
   162 		CActiveScheduler::Stop();
       
   163 		}	
       
   164 	}
       
   165 
       
   166 // -----------------------------------------------------------------------------
       
   167 // CSIPProfileCSSession::ShutdownL
       
   168 // -----------------------------------------------------------------------------
       
   169 //
       
   170 void CSIPProfileCSServer::ShutdownL()
       
   171 	{
       
   172 	}
       
   173 
       
   174 // -----------------------------------------------------------------------------
       
   175 // CSIPProfileCSSession::CleanShutdownL
       
   176 // -----------------------------------------------------------------------------
       
   177 //
       
   178 void CSIPProfileCSServer::CleanShutdownL()
       
   179 	{
       
   180 	}
       
   181 
       
   182 // -----------------------------------------------------------------------------
       
   183 // CSIPProfileCSServer::RunError
       
   184 // -----------------------------------------------------------------------------
       
   185 //
       
   186 TInt CSIPProfileCSServer::RunError(TInt aError)
       
   187 	{
       
   188     Message().Complete(aError);
       
   189 	// The leave will result in an early return from CServer::RunL(), skipping
       
   190 	// the call to request another message. So do that now in order to keep the
       
   191 	// server running.
       
   192 	ReStart();
       
   193 	return KErrNone; // handled the error fully
       
   194 	}
       
   195 
       
   196 // -----------------------------------------------------------------------------
       
   197 // CSIPProfileCSServer::PanicClient
       
   198 // -----------------------------------------------------------------------------
       
   199 //
       
   200 void CSIPProfileCSServer::PanicClient(const RMessage2& aMessage,
       
   201                                       TSIPProfileCSPanic aPanic)
       
   202 	{
       
   203     aMessage.Panic(KSipProfileCSServerPanic, aPanic);
       
   204 	}
       
   205 
       
   206 // -----------------------------------------------------------------------------
       
   207 // CSIPProfileCSServer::PanicServer
       
   208 // -----------------------------------------------------------------------------
       
   209 //
       
   210 void CSIPProfileCSServer::PanicServer(TSIPProfileCSPanic aPanic)
       
   211 	{
       
   212     User::Panic(KSipProfileCSServerPanic, aPanic);
       
   213 	}
       
   214 
       
   215 // -----------------------------------------------------------------------------
       
   216 // CSIPProfileCSServer::ThreadFunctionL
       
   217 // -----------------------------------------------------------------------------
       
   218 //
       
   219 void CSIPProfileCSServer::ThreadFunctionL(RMutex& aMutex)
       
   220 	{
       
   221 	// Give a name to this thread
       
   222 	User::LeaveIfError(User::RenameThread(KSipProfileServerName));
       
   223 
       
   224     // Construct our server
       
   225     CActiveScheduler* activeScheduler = new (ELeave) CSIPProfileActiveScheduler;
       
   226     CActiveScheduler::Install(activeScheduler);
       
   227     CleanupStack::PushL(activeScheduler);
       
   228     CSIPProfileServerCore::NewLC();
       
   229 	if (SignalClientSemaphore() == KErrNone)
       
   230 		{
       
   231 		// Semaphore signalled ok
       
   232 		PROFILE_DEBUG1("Start initiated by client")
       
   233 		}
       
   234 	else
       
   235 		{
       
   236 		PROFILE_DEBUG1("Start initiated by non-client")
       
   237 		// Server started by non-client. No need to signal.
       
   238 		}	
       
   239 	aMutex.Signal();
       
   240     aMutex.Close();
       
   241 
       
   242 	// Start handling requests
       
   243 	CActiveScheduler::Start();
       
   244 
       
   245     // This will be executed after the active scheduler has been stopped:
       
   246     CleanupStack::PopAndDestroy(2); // server, activeScheduler
       
   247 	}
       
   248 
       
   249 // -----------------------------------------------------------------------------
       
   250 // CSIPProfileCSServer::ThreadFunction
       
   251 // -----------------------------------------------------------------------------
       
   252 //
       
   253 TInt CSIPProfileCSServer::ThreadFunction(TAny* /*aNone*/)
       
   254 	{
       
   255 	_LIT(KSipProfileServerMutexName, "SipProfileServerMutex");
       
   256 
       
   257     CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
   258 	if (!cleanupStack) 
       
   259 	    {
       
   260 	    PanicServer(ECreateTrapCleanup);
       
   261 	    }
       
   262 
       
   263 	// Protect server starting code with a mutex.
       
   264 	RMutex mutex;
       
   265 	TInt err = mutex.CreateGlobal(KSipProfileServerMutexName);
       
   266     if (err != KErrNone)
       
   267         {
       
   268         err = mutex.OpenGlobal(KSipProfileServerMutexName);
       
   269         if (err != KErrNone)
       
   270             {
       
   271             delete cleanupStack;
       
   272             PanicServer(ESrvCreateServer);
       
   273             return err; // silence warnings
       
   274             }
       
   275         }
       
   276     mutex.Wait(); // Wait here until nobody else executes the code below.
       
   277 
       
   278     TFindServer findServer(KSipProfileServerName);
       
   279     TFullName name;
       
   280 	if (findServer.Next(name) == KErrNone) 
       
   281         {
       
   282         // Server already running.
       
   283         SignalClientSemaphore();
       
   284         mutex.Signal();
       
   285         mutex.Close();
       
   286         }
       
   287     else
       
   288         {
       
   289         // Server not running. Try to start it.
       
   290         TRAP(err, ThreadFunctionL(mutex));
       
   291         if (err != KErrNone)
       
   292             {
       
   293             SignalClientSemaphore();
       
   294 	        mutex.Signal();
       
   295 	        mutex.Close();	
       
   296             } 
       
   297         }
       
   298 	               
       
   299     delete cleanupStack;
       
   300     return err;
       
   301 	}
       
   302 
       
   303 // -----------------------------------------------------------------------------
       
   304 // CSIPProfileCSServer::SignalClientSemaphore
       
   305 // -----------------------------------------------------------------------------
       
   306 //
       
   307 TInt CSIPProfileCSServer::SignalClientSemaphore()
       
   308     {
       
   309     RSemaphore semaphore;
       
   310 	TInt err = semaphore.OpenGlobal(KSipProfileServerSemaphoreName);
       
   311 	if (err == KErrNone)
       
   312 	    {
       
   313 		semaphore.Signal();
       
   314 		semaphore.Close();
       
   315 		}
       
   316 	return err; 
       
   317     }
       
   318 
       
   319 // -----------------------------------------------------------------------------
       
   320 // E32Main
       
   321 // -----------------------------------------------------------------------------
       
   322 //
       
   323 TInt E32Main() 
       
   324     { 
       
   325 	PROFILE_DEBUG1("Process created")
       
   326     return CSIPProfileCSServer::ThreadFunction(NULL);
       
   327     }