commsfwtools/preparedefaultcommsdatabase/te_commsdat/src/notificationtester.cpp
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     1 // Copyright (c) 2005-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 notificationtester.cpp
       
    18 */
       
    19 #include "notificationtester.h"
       
    20  
       
    21 CNotificationTester* CNotificationTester::NewL(const TDesC &aThreadName, CommsDat::CMDBSession* aSession, CommsDat::CMDBElement* aElement)
       
    22 	{
       
    23 	CNotificationTester* self = new(ELeave) CNotificationTester(aSession, aElement);
       
    24 	CleanupStack::PushL(self);
       
    25 	self->ConstructL(aThreadName);
       
    26 	CleanupStack::Pop(self);
       
    27 	
       
    28 	return self;
       
    29 	}
       
    30 	
       
    31 CNotificationTester::CNotificationTester(CommsDat::CMDBSession* aSession, CommsDat::CMDBElement* aElement)
       
    32 :CActive(0)
       
    33 	{
       
    34 	iSession = aSession;
       
    35 	iElement = aElement;
       
    36 	}
       
    37 
       
    38 void CNotificationTester::ConstructL(const TDesC &aThreadName)
       
    39 	{
       
    40 	iThreadRunning = EFalse;
       
    41 
       
    42 	iThread.Create(aThreadName, &ThreadFunction, KDefaultStackSize, NULL, this);
       
    43 	TRequestStatus waitSchedulerInstallation;  		// Create a rendezvous to ensure that thread
       
    44 	iThread.Rendezvous(waitSchedulerInstallation);  // completes installation of scheduler before ConstructL finishes
       
    45 	iThread.Resume(); 
       
    46 	
       
    47 	User::WaitForRequest(waitSchedulerInstallation);
       
    48 	}
       
    49 
       
    50 CNotificationTester::~CNotificationTester()
       
    51 	{
       
    52 	Cancel();
       
    53 	iThread.Close();
       
    54 	}
       
    55 	
       
    56 void CNotificationTester::SetTester()
       
    57 	{
       
    58 	if(iThread.ExitReason() >0 )
       
    59 		{
       
    60 		iReason = iThread.ExitReason();
       
    61 		return;
       
    62 		}
       
    63 
       
    64 	iElement->RequestNotification(*iSession, iStatus);
       
    65 	SetActive();
       
    66 	}
       
    67 	
       
    68 	
       
    69 void CNotificationTester::LogOn(TRequestStatus &aStatus)
       
    70 	{
       
    71 	iThread.Logon(aStatus);
       
    72 	}
       
    73 
       
    74 void CNotificationTester::RunL()
       
    75 	{
       
    76 	CActiveScheduler::Stop();
       
    77 	}
       
    78 
       
    79 void CNotificationTester::DoCancel()
       
    80 	{
       
    81 	if((iElement != NULL) && (iSession != NULL))
       
    82 		iElement->CancelNotification(*iSession, iStatus);
       
    83 	}
       
    84 	
       
    85 void CNotificationTester::CancelNotification(TRequestStatus& /*aStatus*/)	
       
    86 	{
       
    87 	// Note: this method is not called in the same thread that originally created and
       
    88 	// registered the CNotificationTester.  Consequently, issuing Cancel() here will perform
       
    89 	// the DoCancel(), which will complete the CNotificationTester in the creating thread,
       
    90 	// but then erroneously perform a User::WaitForRequest() in the *current* thread, swallowing
       
    91 	// the next unsuspecting completion signal in the current thread.
       
    92 	DoCancel();
       
    93 	}
       
    94 
       
    95 TInt CNotificationTester::ThreadFunction(TAny *aPtr)
       
    96 	{
       
    97 	CTrapCleanup* cleanup=CTrapCleanup::New();
       
    98 	
       
    99 	TRAPD(errCode, DoThreadFunctionL(aPtr));
       
   100 	
       
   101 	delete cleanup;
       
   102 	return errCode;
       
   103 	}
       
   104 	
       
   105 TInt CNotificationTester::DoThreadFunctionL(TAny *aPtr)
       
   106 	{
       
   107 	
       
   108 	CActiveScheduler* scheduler=new (ELeave) CActiveScheduler;
       
   109     CleanupStack::PushL(scheduler);
       
   110     CActiveScheduler::Install(scheduler);
       
   111     ((CNotificationTester *)aPtr)->iThreadRunning = ETrue;	
       
   112 	CActiveScheduler::Add((CActive *)aPtr);
       
   113 	
       
   114 	CNotificationTester *tester = (CNotificationTester*)(aPtr);
       
   115 	tester->SetTester();
       
   116 	
       
   117 	RThread::Rendezvous(KErrNone); // Signal the main thread that notification has been requested
       
   118 
       
   119 	CActiveScheduler::Start();
       
   120 	CleanupStack::PopAndDestroy(scheduler);	
       
   121 	
       
   122 	return KErrNone;
       
   123 	}
       
   124