commsconfig/commsdatabaseshim/commdbshim/Notifier/src/NotifierServ.CPP
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     1 // Copyright (c) 2006-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 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @deprecated since v9.1. Functionality is replaced with commsdat.
       
    19 */
       
    20 
       
    21 #include <c32comm.h>
       
    22 #include <e32svr.h>
       
    23 
       
    24 #include <commsdat.h>
       
    25 #include "NotifierServ.H"
       
    26 
       
    27 const TInt noPendingEvents = -1;
       
    28 
       
    29 void InitL()
       
    30 	{
       
    31 	RProcess().SetPriority(EPriorityHigh);
       
    32 	CCommsdatNotifierScheduler* pA = new(ELeave) CCommsdatNotifierScheduler();
       
    33 	CActiveScheduler::Install(pA);
       
    34 	pA->iServer = CCommsdatNotifierServer::NewL();	
       
    35 	}
       
    36 
       
    37 /**
       
    38 CCommsdatNotifier server process entry point
       
    39 */
       
    40 TInt E32Main()
       
    41 	{
       
    42 	__UHEAP_MARK;	
       
    43 	TInt ret = KErrNone;
       
    44 	
       
    45 	CTrapCleanup* cleanup=CTrapCleanup::New();
       
    46 	if (cleanup==NULL)
       
    47 		{
       
    48 		ret=KErrNoMemory;	
       
    49 		}		
       
    50 
       
    51 	if (ret==KErrNone)
       
    52 		{
       
    53 		TRAP(ret,InitL());	
       
    54 		}		
       
    55 
       
    56 	RProcess::Rendezvous(ret);
       
    57 	
       
    58 	if (ret==KErrNone)
       
    59 		{
       
    60 		CActiveScheduler::Start();	
       
    61 		}
       
    62 		
       
    63 
       
    64 	delete CActiveScheduler::Current();
       
    65 	delete cleanup;
       
    66 	
       
    67 	__UHEAP_MARKEND;
       
    68 
       
    69 	return ret;
       
    70 	}
       
    71 	
       
    72 
       
    73 	
       
    74 //**********************************
       
    75 //CCommsdatNotifierServer
       
    76 //**********************************
       
    77 
       
    78 CCommsdatNotifierServer::CCommsdatNotifierServer()
       
    79 	: CServer2(EPriorityHigh, EGlobalSharableSessions)
       
    80 	{
       
    81 	}
       
    82 
       
    83 CCommsdatNotifierServer* CCommsdatNotifierServer::NewL()
       
    84 // Create and start a new count server
       
    85 	{
       
    86 	CCommsdatNotifierServer* self = new(ELeave) CCommsdatNotifierServer;
       
    87 	CleanupStack::PushL(self);
       
    88 	self->ConstructL();
       
    89 	CleanupStack::Pop(self);
       
    90 	return self;
       
    91 	}
       
    92 
       
    93 void CCommsdatNotifierServer::ConstructL()
       
    94 	{
       
    95 	iShutdownWatchDog = CShutdownWatchDog::NewL(*this);
       
    96 	StartL(COMMSDATNOTIFIER_SERVER_NAME);
       
    97 	}
       
    98 
       
    99 CCommsdatNotifierServer::~CCommsdatNotifierServer()
       
   100 	{
       
   101 	delete iShutdownWatchDog;
       
   102 	}
       
   103 
       
   104 void CCommsdatNotifierServer::IncrementSessionCount()
       
   105 	{
       
   106 	iSessionCount++;
       
   107 	}
       
   108 
       
   109 void CCommsdatNotifierServer::DecrementSessionCount()
       
   110 	{
       
   111 	iSessionCount--;
       
   112 	if (iSessionCount<=0)
       
   113 		{
       
   114 		iShutdownWatchDog->Cancel();	//Cancel any outstanding request and start again
       
   115 		iShutdownWatchDog->StartShutdown();
       
   116 		}		
       
   117 	}
       
   118 	
       
   119 void CCommsdatNotifierServer::MaybeShutDown()
       
   120      {
       
   121       if (iSessionCount<=0)
       
   122          {
       
   123       	 CActiveScheduler::Stop();
       
   124       	 }
       
   125      }
       
   126      
       
   127 CSession2* CCommsdatNotifierServer::NewSessionL(const TVersion &aVersion, const RMessage2& /*aMessage*/) const
       
   128 // Create a new server session - check we're the right version and make session
       
   129 	{
       
   130 	TVersion v(KCommsdatNotifierMajorVersionNumber,KCommsdatNotifierMinorVersionNumber,KCommsdatNotifierBuildVersionNumber);
       
   131 	
       
   132 	TBool r=User::QueryVersionSupported(v,aVersion);
       
   133 	if (r==EFalse)
       
   134 		{
       
   135 		User::Leave(KErrNotSupported);
       
   136 		}		
       
   137 	
       
   138 	TBool clientSpecialVersion = (aVersion.iBuild==KCommsdatNotifierSpecialBuildVersionNumber);
       
   139 	
       
   140 	return CCommsdatNotifierSession::NewL((CCommsdatNotifierServer*)this, clientSpecialVersion);
       
   141 	}
       
   142 	
       
   143 
       
   144 //**********************************
       
   145 //CCommsdatNotifierSession
       
   146 //**********************************
       
   147 
       
   148 CCommsdatNotifierSession* CCommsdatNotifierSession::NewL(CCommsdatNotifierServer* aServer, TBool aClientSpecialVersion)
       
   149 	{
       
   150 	CCommsdatNotifierSession* thisptr = new(ELeave) CCommsdatNotifierSession(aServer);
       
   151 	CleanupStack::PushL(thisptr);
       
   152 	thisptr->ConstructL(aClientSpecialVersion);
       
   153 	CleanupStack::Pop(thisptr);
       
   154 	return thisptr;
       
   155 	}
       
   156 	
       
   157 void CCommsdatNotifierSession::ConstructL(TBool aClientSpecialVersion)
       
   158 	{
       
   159 	if (aClientSpecialVersion)
       
   160 		iNotifierHandler = CCommsdatNotifierSessionHandler2::NewL();	
       
   161 	else
       
   162 		iNotifierHandler = CCommsdatNotifierSessionHandler::NewL();		
       
   163 	}	
       
   164 		
       
   165 CCommsdatNotifierSession::CCommsdatNotifierSession(CCommsdatNotifierServer* aServer)
       
   166 	: iNotifierSvr(aServer)
       
   167 	{
       
   168 	iNotifierSvr->IncrementSessionCount();
       
   169 	}
       
   170 
       
   171 CCommsdatNotifierSession::~CCommsdatNotifierSession()
       
   172 	{
       
   173 	iNotifierSvr->DecrementSessionCount();
       
   174 	iNotifierHandler->Cancel();	
       
   175 	delete iNotifierHandler;
       
   176 	}
       
   177 
       
   178 void CCommsdatNotifierSession::ServiceL(const RMessage2& aMessage)
       
   179 	{
       
   180 	switch (aMessage.Function())
       
   181         {
       
   182 		case ERequestNotify:
       
   183 			iNotifierHandler->RegisterNotifyL(aMessage);
       
   184 			break;
       
   185 		case ECancelNotify:
       
   186 			iNotifierHandler->CancelNotify(aMessage);
       
   187 			break;
       
   188 		case ECancelAllNotify:
       
   189 			iNotifierHandler->CancelAllNotify(aMessage);
       
   190 			break;			
       
   191 		default:
       
   192 			PanicClient(KErrArgument,aMessage);
       
   193 			break;
       
   194         }
       
   195 	}
       
   196 void CCommsdatNotifierSession::PanicClient(TInt aPanic,const RMessage2& aMessage) const
       
   197 // Panic the client
       
   198 	{
       
   199 	_LIT(KClientFault,"Commdb Shim Notifier Server");
       
   200 	aMessage.Panic(KClientFault,aPanic);
       
   201 	}
       
   202 	
       
   203 //**********************************
       
   204 //CCommsdatNotifierSessionHandlerBase
       
   205 //**********************************
       
   206 
       
   207 void CCommsdatNotifierSessionHandlerBase::ConstructL()
       
   208 	{
       
   209 	TInt err = iProperty.Attach(KUidSystemCategory, KUidCommsDatNotificationEvent.iUid);
       
   210 	User::LeaveIfError(err);
       
   211 	}	
       
   212 	
       
   213 CCommsdatNotifierSessionHandlerBase::CCommsdatNotifierSessionHandlerBase()
       
   214 	: CActive(EPriorityHigh)
       
   215 	{
       
   216 	CActiveScheduler::Add(this);
       
   217 	}
       
   218 
       
   219 CCommsdatNotifierSessionHandlerBase::~CCommsdatNotifierSessionHandlerBase()
       
   220 	{		
       
   221 	iProperty.Close();
       
   222 	}	
       
   223 	
       
   224 void CCommsdatNotifierSessionHandlerBase::DoCancel()		
       
   225 	{
       
   226 	iProperty.Cancel();
       
   227 	}
       
   228 
       
   229 void CCommsdatNotifierSessionHandlerBase::CompleteMessage(const RMessage2& aMessage,TInt aVal)
       
   230      {
       
   231      if(!aMessage.IsNull())
       
   232            {	
       
   233            aMessage.Complete(aVal);	
       
   234            }
       
   235      }	
       
   236 
       
   237 //**********************************
       
   238 //CCommsdbNotifierSessionHandler
       
   239 //**********************************
       
   240 
       
   241 CCommsdatNotifierSessionHandler* CCommsdatNotifierSessionHandler::NewL()
       
   242 	{
       
   243 	CCommsdatNotifierSessionHandler* self = new(ELeave) CCommsdatNotifierSessionHandler;
       
   244 	CleanupStack::PushL(self);
       
   245 	self->ConstructL();
       
   246 	CleanupStack::Pop(self);
       
   247 	return self;
       
   248 	}
       
   249 	
       
   250 void CCommsdatNotifierSessionHandler::ConstructL()
       
   251 	{
       
   252 	CCommsdatNotifierSessionHandlerBase::ConstructL();	
       
   253 	}	
       
   254 	
       
   255 void CCommsdatNotifierSessionHandler::RegisterNotifyL(const RMessage2& aMessage)
       
   256 	{
       
   257 	// if there is already an outstanding request, complete
       
   258 	// immediately with an error
       
   259 	
       
   260 	if (! iMessage.IsNull())
       
   261 		{
       
   262 		aMessage.Complete(KErrServerBusy);
       
   263 		return;
       
   264 		}
       
   265 
       
   266 	// there are no outstanding requests
       
   267 
       
   268 	// if there is already a pending event, complete immediately
       
   269 	if (iPendingEvent > noPendingEvents)
       
   270 		{
       
   271 		aMessage.Complete(iPendingEvent);
       
   272 		// reset iPendingEvent
       
   273 		iPendingEvent = noPendingEvents;
       
   274 		return;
       
   275 		}
       
   276 
       
   277 	// there are no outstanding events (iPendingEvent == noPendingEvents)
       
   278 
       
   279 	iMessage = aMessage;
       
   280 
       
   281 	if (!IsActive())
       
   282 	    {
       
   283 	    iProperty.Subscribe(iStatus);
       
   284 	    SetActive();
       
   285 	    }
       
   286 	}
       
   287 
       
   288 CCommsdatNotifierSessionHandler::CCommsdatNotifierSessionHandler()
       
   289 	: CCommsdatNotifierSessionHandlerBase(), iPendingEvent(noPendingEvents)
       
   290 	{
       
   291 	}
       
   292 
       
   293 CCommsdatNotifierSessionHandler::~CCommsdatNotifierSessionHandler()
       
   294 	{		
       
   295 	}	
       
   296 	
       
   297 void CCommsdatNotifierSessionHandler::CancelNotify(const RMessage2& aMessage)
       
   298 	{
       
   299 	Cancel();
       
   300 	iPendingEvent = noPendingEvents;			
       
   301 	
       
   302 	// Complete outstanding async request for notification
       
   303 	CompleteMessage(iMessage, KErrCancel);
       
   304 	
       
   305 	// Complete the cancel message
       
   306 	CompleteMessage(aMessage, KErrNone);
       
   307 	}
       
   308 	
       
   309 void CCommsdatNotifierSessionHandler::CancelAllNotify(const RMessage2& aMessage)
       
   310 	{
       
   311 		CancelNotify(aMessage);
       
   312 	}		
       
   313 	
       
   314 void CCommsdatNotifierSessionHandler::RunL()
       
   315 	{
       
   316 	TInt stat = iStatus.Int();
       
   317 // according to the present Symbian Publish and Subscribe API
       
   318 // documentation and implementation, the following code should ensure
       
   319 // that further events are not missed during RunL
       
   320 	iProperty.Subscribe(iStatus);
       
   321 	SetActive();
       
   322 
       
   323 	if (stat == KErrNone)
       
   324 		{
       
   325 		// use stat for storing the new event
       
   326 		User::LeaveIfError(iProperty.Get(KUidSystemCategory, KUidCommsDatNotificationEvent.iUid, stat));
       
   327 
       
   328 		if (iMessage.IsNull())
       
   329 			{
       
   330 			// if the client is not ready to receive notifications,
       
   331 			// store the new event in iPendingEvent,
       
   332 			//but check first if it is more important than the current
       
   333 			//pending event (if there is one)
       
   334 			if (stat > iPendingEvent)
       
   335                 {
       
   336 				iPendingEvent = stat;
       
   337                 }
       
   338 			}
       
   339 		else
       
   340 			{
       
   341 			// if the client is ready to receive notifications, complete
       
   342 
       
   343 			if (iPendingEvent > noPendingEvents)
       
   344 				{
       
   345 				// if there is a pending event
       
   346 
       
   347 				// complete with the new event, if it is more
       
   348 				// important than the pending event
       
   349 				if (stat > iPendingEvent)
       
   350 					{
       
   351 					iMessage.Complete(stat);
       
   352 					iPendingEvent = stat;
       
   353 					}
       
   354 				// otherwise complete with the pending event
       
   355 				else
       
   356 					{
       
   357 					iMessage.Complete(iPendingEvent);
       
   358 					}
       
   359 				}
       
   360 			else
       
   361 				{
       
   362 				iMessage.Complete(stat);
       
   363 				iPendingEvent = stat;
       
   364 				}
       
   365 			}
       
   366 		}
       
   367 	else
       
   368 		{
       
   369 		if (!iMessage.IsNull())
       
   370 			{
       
   371 			// if there is a pending event, complete immediately
       
   372 			if (iPendingEvent > noPendingEvents)
       
   373 				{
       
   374 				iMessage.Complete(iPendingEvent);
       
   375 				}
       
   376 			else
       
   377 				{
       
   378 				// otherwise, complete with error
       
   379 				iMessage.Complete(stat);
       
   380 				}
       
   381 			}
       
   382 		}
       
   383 	}
       
   384 
       
   385 TInt CCommsdatNotifierSessionHandler::RunError(TInt aError)		
       
   386 	{
       
   387 	CompleteMessage(iMessage,aError);
       
   388 	return KErrNone;
       
   389 	}
       
   390 
       
   391 //**********************************
       
   392 //CCommsdatNotifierSessionHandler2
       
   393 //**********************************
       
   394 
       
   395 CCommsdatNotifierSessionHandler2* CCommsdatNotifierSessionHandler2::NewL()
       
   396 	{
       
   397 	CCommsdatNotifierSessionHandler2* self = new(ELeave) CCommsdatNotifierSessionHandler2;
       
   398 	CleanupStack::PushL(self);
       
   399 	self->ConstructL();
       
   400 	CleanupStack::Pop(self);
       
   401 	return self;
       
   402 	}
       
   403 	
       
   404 void CCommsdatNotifierSessionHandler2::ConstructL()
       
   405 	{	
       
   406 	CCommsdatNotifierSessionHandlerBase::ConstructL();	
       
   407 	
       
   408 	iMessagePool = new(ELeave)CArrayFixFlat<RMessage2>(5);
       
   409 	}	
       
   410 	
       
   411 void CCommsdatNotifierSessionHandler2::CompleteRequests(TInt aErrorCode)
       
   412 	{
       
   413 	for (TInt i = iMessagePool->Count() ; i > 0 ; i--)
       
   414 		{
       
   415 		CompleteMessage(iMessagePool->At(i - 1), aErrorCode);
       
   416 		}
       
   417 	iMessagePool->Reset();
       
   418 	}
       
   419 
       
   420 
       
   421 void CCommsdatNotifierSessionHandler2::ResetRequestVector()
       
   422 	{
       
   423 	iMessagePool->Reset();
       
   424 	}	
       
   425 	
       
   426 	
       
   427 void CCommsdatNotifierSessionHandler2::RegisterNotifyL(const RMessage2& aMessage)
       
   428 	{
       
   429 	for (TInt i = iMessagePool->Count() ; i > 0 ; i--)
       
   430 		{
       
   431 		if (iMessagePool->At(i - 1).ClientStatus()==aMessage.ClientStatus())
       
   432 		{
       
   433 			// do nothing we already have this outstanding request 
       
   434 			return;
       
   435 		}
       
   436 		}	
       
   437 	
       
   438 	iMessagePool->AppendL(aMessage);
       
   439 
       
   440 	if (!IsActive())
       
   441 	    {
       
   442 	    iProperty.Subscribe(iStatus);
       
   443 	    SetActive();
       
   444 	    }
       
   445 	}
       
   446 
       
   447 CCommsdatNotifierSessionHandler2::CCommsdatNotifierSessionHandler2()
       
   448 	: 	CCommsdatNotifierSessionHandlerBase()
       
   449 	{
       
   450 	}
       
   451 
       
   452 CCommsdatNotifierSessionHandler2::~CCommsdatNotifierSessionHandler2()
       
   453 	{		
       
   454 	ResetRequestVector();
       
   455 	delete iMessagePool;
       
   456 	}	
       
   457 	
       
   458 void CCommsdatNotifierSessionHandler2::CancelNotify(const RMessage2& aMessage)
       
   459 /**
       
   460 Cancel a previously registered notification.
       
   461 
       
   462 @param aMessage the cancel notification request.  Ptr0() identifies the notification to
       
   463 cancel and contains the address of its TRequestStatus.
       
   464 */
       
   465 
       
   466 	{
       
   467 	for (TInt i = iMessagePool->Count() ; i > 0 ; i--)
       
   468 		{
       
   469 		if (iMessagePool->At(i - 1).ClientStatus() == aMessage.Ptr0())
       
   470 			{
       
   471 			CompleteMessage(iMessagePool->At(i - 1), KErrCancel);	// complete the registered notification
       
   472 			iMessagePool->Delete(i - 1);
       
   473 			break;
       
   474 			}
       
   475 		}
       
   476 		
       
   477 	aMessage.Complete(KErrNone); // complete the cancel notification
       
   478 	}
       
   479 	
       
   480 void CCommsdatNotifierSessionHandler2::CancelAllNotify(const RMessage2& aMessage)
       
   481 	{
       
   482 	iProperty.Cancel();
       
   483 	CompleteRequests(KErrCancel);
       
   484 	aMessage.Complete(KErrCancel);
       
   485 	}	
       
   486 	
       
   487 void CCommsdatNotifierSessionHandler2::RunL()
       
   488 	{
       
   489 	TInt stat = iStatus.Int();
       
   490 	iProperty.Subscribe(iStatus);
       
   491 	SetActive();
       
   492 	
       
   493 	CompleteRequests(stat);
       
   494 	}
       
   495 	
       
   496 TInt CCommsdatNotifierSessionHandler2::RunError(TInt aError)		
       
   497 	{
       
   498 	CompleteRequests(aError);
       
   499 	return KErrNone;
       
   500 	}	
       
   501 
       
   502 //**********************************
       
   503 //CShutdownWatchDog
       
   504 //**********************************
       
   505 const TInt KNotifierServerShutdownLatencyMicroSeconds = 30 * 1000 * 1000;
       
   506 
       
   507 CShutdownWatchDog::CShutdownWatchDog(CCommsdatNotifierServer& aNotifierServer)
       
   508 	:CTimer(EPriorityStandard),iNotifierServer(aNotifierServer)
       
   509 	{
       
   510 	}
       
   511 
       
   512 void CShutdownWatchDog::StartShutdown()
       
   513 /**
       
   514 Queue ourselves to shut this server down after a suitable delay.
       
   515 */
       
   516 	{
       
   517 	if (!IsAdded())
       
   518 		{
       
   519 		CActiveScheduler::Add(this);
       
   520 		}
       
   521 	if(!IsActive())
       
   522 		{
       
   523 		After(KNotifierServerShutdownLatencyMicroSeconds);
       
   524 		}
       
   525 	}
       
   526 
       
   527 void CShutdownWatchDog::RunL()
       
   528 /**
       
   529 Call the server to shut itself down - this will do nothing if there are active sessions.
       
   530 */
       
   531 	{
       
   532 	iNotifierServer.MaybeShutDown();
       
   533 	}
       
   534 
       
   535 CShutdownWatchDog* CShutdownWatchDog::NewL(CCommsdatNotifierServer& aNotifierServer)
       
   536 	{
       
   537 	CShutdownWatchDog* self=new CShutdownWatchDog(aNotifierServer);
       
   538 	CleanupStack::PushL(self);
       
   539 	self->ConstructL();
       
   540 	CleanupStack::Pop(self);
       
   541 	return self;
       
   542 	}
       
   543         
       
   544         
       
   545         	
       
   546