commsprocess/commsrootserverconfig/ts_rootserver/testcpm/testcpm.cpp
changeset 72 ae47d0499bee
parent 68 5da8188e392b
child 77 c9776eadbffd
equal deleted inserted replaced
68:5da8188e392b 72:ae47d0499bee
     1 // Copyright (c) 2003-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 #include <cflog.h>
       
    17 #include <cfshared.h>
       
    18 using namespace CommsFW;
       
    19 #include <cfmsgs.h>
       
    20 #include <rsshared.h>
       
    21 #include "../RootServerTest.h"
       
    22 
       
    23 __CFLOG_STMT(_LIT8(KLogTestCPM, "TestCPM");) // subsystem name
       
    24 _LIT( KTestCpmPanic, "TestCpm" );
       
    25 const TText8* const KSubModules[] = { _S8("Test Protocol1"), _S8("Test Protocol2"), _S8("Test Protocol3") };
       
    26 
       
    27 
       
    28 class CDeathTimer : public CTimer
       
    29 	{
       
    30 public:
       
    31 	static CDeathTimer* NewL(TInt aAfter)
       
    32 		{
       
    33 		CDeathTimer* self = new(ELeave) CDeathTimer;
       
    34 		CleanupStack::PushL(self);
       
    35 		self->ConstructL();
       
    36 		CleanupStack::Pop(self);
       
    37 		CActiveScheduler::Add(self);
       
    38 		self->After(aAfter);
       
    39 		return self;
       
    40 		}
       
    41 protected:
       
    42 	CDeathTimer() : CTimer(EPriorityStandard)
       
    43 		{
       
    44 		}
       
    45 	void RunL()
       
    46 		{
       
    47 		// Go not gracefully into that good night
       
    48 		User::Invariant();
       
    49 		}
       
    50 	};
       
    51 
       
    52 class CChannelHandler;
       
    53 class CTestModule : public CBase
       
    54 	{
       
    55 public:
       
    56 	enum {EPriority=1000}; //< priority of this Active Object
       
    57 public:
       
    58 	~CTestModule();
       
    59 	void CFMessageReceived(TCFMessage& aMsg);
       
    60 	static CTestModule* NewL(TCFModuleInfo* aStartupArgs);
       
    61 	void ConstructL(TCFModuleInfo* aStartupArgs);
       
    62 	void LoggedAfter(TInt aAfterMS, const TDesC8& aDescription);
       
    63 private:
       
    64 	CChannelHandler* iChannelHandler;
       
    65 	TTestModuleIniDataContainer iDelayTimes;
       
    66 	TInt iDiscoverPos;
       
    67 	CDeathTimer* iDeathTimer;
       
    68 	};
       
    69 
       
    70 class CChannelHandler:public CActive
       
    71 	{
       
    72 public:
       
    73 	static CChannelHandler* NewL(RCFChannel::TMsgQueues aRxQueues, 
       
    74 									RCFChannel::TMsgQueues aTxQueues, CTestModule* aModule);
       
    75 	void RunL();
       
    76 	TInt Send(TCFMessage);
       
    77 	~CChannelHandler();
       
    78 protected:
       
    79 	virtual void DoCancel();
       
    80 private:
       
    81 	void ConstructL(RCFChannel::TMsgQueues aRxQueues, 
       
    82 					RCFChannel::TMsgQueues aTxQueues, CTestModule* aModule);
       
    83 	CChannelHandler();
       
    84 private:
       
    85 	CTestModule* iModule;
       
    86 	RCFChannelPair iChannelPair;
       
    87 	};
       
    88 
       
    89 
       
    90 //
       
    91 // CChannelHandler class definitions
       
    92 //
       
    93 
       
    94 CChannelHandler::CChannelHandler():CActive(CActive::EPriorityStandard)
       
    95 	{
       
    96 	CActiveScheduler::Add(this);
       
    97 	}
       
    98 
       
    99 CChannelHandler* CChannelHandler::NewL(RCFChannel::TMsgQueues aRxQueues, 
       
   100 									RCFChannel::TMsgQueues aTxQueues, CTestModule* aModule)
       
   101 	{
       
   102 	CChannelHandler* pHandler = new (ELeave) CChannelHandler;
       
   103 	pHandler->ConstructL(aRxQueues, aTxQueues, aModule);
       
   104 	return pHandler;
       
   105 	}
       
   106 
       
   107 void CChannelHandler::ConstructL(RCFChannel::TMsgQueues aRxQueues, 
       
   108 									RCFChannel::TMsgQueues aTxQueues, CTestModule* aModule)
       
   109 	{
       
   110 	iModule = aModule;
       
   111 	iChannelPair.CreateSend(aTxQueues);
       
   112 	iChannelPair.CreateRecv(aRxQueues);
       
   113 	SetActive();
       
   114 	iChannelPair.NotifyDataAvailable(*this);
       
   115 	}
       
   116 
       
   117 void CChannelHandler::DoCancel(void)
       
   118 	{
       
   119 	iChannelPair.CancelDataAvailable();
       
   120 	}
       
   121 
       
   122 TInt CChannelHandler::Send(TCFMessage aMsg)
       
   123 	{
       
   124 	TRequestStatus status;
       
   125 	iChannelPair.NotifySpaceAvailable(*this);
       
   126 	User::WaitForRequest(status);
       
   127 	return iChannelPair.Send(aMsg);
       
   128 	}
       
   129 
       
   130 void CChannelHandler::RunL()
       
   131 	{
       
   132 
       
   133 	if (iStatus != KErrNone)
       
   134 		{
       
   135 		User::Invariant();
       
   136 		}
       
   137 
       
   138 	// No error. Take the message from the channel.
       
   139 	TCFMessage msg;
       
   140 	TInt err = iChannelPair.Receive(msg);
       
   141 	if (KErrNone == err)
       
   142 		{
       
   143 		//Pass the message
       
   144 		iModule->CFMessageReceived(msg);
       
   145 		//Pend again
       
   146 		SetActive();
       
   147 		iChannelPair.NotifyDataAvailable(*this);
       
   148 		}
       
   149 	else
       
   150 		{
       
   151 		User::Invariant();
       
   152 		}
       
   153 	}
       
   154 
       
   155 CChannelHandler::~CChannelHandler()
       
   156 	{
       
   157 	if(IsActive())
       
   158 		Cancel();
       
   159 	iChannelPair.CloseNonGracefully();
       
   160 	}	
       
   161 
       
   162 
       
   163 //
       
   164 // implementation of CTestServer
       
   165 //
       
   166 
       
   167 CTestModule* CTestModule::NewL(TCFModuleInfo* aStartupArgs)
       
   168 /**
       
   169  * Create a new CCommServer.
       
   170  *
       
   171  * This is not a normal 2 phase contruction. If the allocation of the object
       
   172  * fails we will panic with -4 which means that something is very wrong.
       
   173  */
       
   174 	{
       
   175 	CTestModule* pS;
       
   176 	CleanupStack::PushL(pS = new CTestModule);
       
   177 	__ASSERT_ALWAYS(pS!=NULL, User::Panic( KTestCpmPanic, KErrNoMemory));
       
   178 #ifdef SYMBIAN_C32ROOT_API_V3
       
   179 	pS->ConstructL(aStartupArgs);
       
   180 #else
       
   181 	TRAPD(r, pS->ConstructL(aStartupArgs));
       
   182 	__ASSERT_ALWAYS(r==KErrNone, User::Panic( KTestCpmPanic, KErrNoMemory));
       
   183 #endif
       
   184 	CleanupStack::Pop(pS);
       
   185 	return pS;
       
   186 	}
       
   187 
       
   188 
       
   189 void CTestModule::ConstructL(TCFModuleInfo* aStartupArgs)
       
   190 	{
       
   191 	if (aStartupArgs->iIniData != NULL)
       
   192 		{
       
   193 		if (aStartupArgs->iIniData->Length() == sizeof(iDelayTimes))
       
   194 			{
       
   195 			TTestModuleIniData pkg;
       
   196 			pkg.Copy(*(aStartupArgs->iIniData));
       
   197 #if defined(SYMBIAN_C32ROOT_API_V3)
       
   198 			delete aStartupArgs->iIniData;
       
   199 #endif
       
   200 
       
   201 			iDelayTimes = pkg.iParams;
       
   202 
       
   203 			if(iDelayTimes.iInitDelay == EXIT_BEFORE_RENDEZVOUS)
       
   204 				User::Leave(KErrGeneral);
       
   205 			else if(iDelayTimes.iInitDelay > 0)
       
   206 				LoggedAfter(iDelayTimes.iInitDelay, _L8("Init"));
       
   207 			else if(iDelayTimes.iInitDelay == PANIC_BEFORE_RENDEZVOUS)
       
   208 				User::Panic(_L("TESTCPM"), 1);
       
   209 
       
   210 			__CFLOG_5( KLogTestCPM, KLogCode, 
       
   211 				_L8("TestCpm[%LU] - iIniData delays (msec): discoveryDelay %d bindDelay %d unbindDelay %d shutdownDelay %d"), 
       
   212 						 RThread().Id().Id(), iDelayTimes.iDiscoveryDelay, iDelayTimes.iBindDelay,
       
   213 						 iDelayTimes.iUnbindDelay, iDelayTimes.iShutdownDelay);
       
   214 
       
   215 			iChannelHandler = CChannelHandler::NewL(aStartupArgs->iRxQueues, aStartupArgs->iTxQueues, this);
       
   216 
       
   217 			RThread::Rendezvous(KErrNone);
       
   218 
       
   219 			if(iDelayTimes.iInitDelay == PANIC_AFTER_RENDEZVOUS)
       
   220 				User::Panic(_L("TESTCPM"), 1);
       
   221 
       
   222 			}
       
   223 		else
       
   224 			{
       
   225 			__CFLOG_1( KLogTestCPM, KLogCode, _L("TestCpm[%LU] - iIniData mismatched"), RThread().Id().Id() );
       
   226 #if defined(SYMBIAN_C32ROOT_API_V3)
       
   227 			delete aStartupArgs->iIniData;
       
   228 #endif
       
   229 			ASSERT(0);
       
   230 			}
       
   231 		}
       
   232 	else
       
   233 		{
       
   234 		__CFLOG_1( KLogTestCPM, KLogCode, _L("TestCpm[%LU] - No iIniData"), RThread().Id().Id() );
       
   235 		ASSERT(0);
       
   236 		}
       
   237 	}
       
   238 
       
   239 void CTestModule::CFMessageReceived(TCFMessage& aMsg)
       
   240 	{
       
   241 	
       
   242 	//It is called when a new message arrives from incomming channel
       
   243 	switch (aMsg.Code())
       
   244 		{
       
   245 		case TCFCommsMessage::ECodeDiscover:
       
   246 			{
       
   247 			// now wait for delay
       
   248 			__CFLOG(KLogTestCPM, KLogCode, _L("TestCpm Discover message received"));
       
   249 
       
   250 			LoggedAfter(iDelayTimes.iDiscoveryDelay, _L8("Discovery"));
       
   251 			const TCFDiscoverMsg& msg = reinterpret_cast<const TCFDiscoverMsg&>(aMsg);
       
   252 			// we return our sub-modules one at a time to make sure the RootServer copes with iterative discovery
       
   253 			const TInt numSubModules = sizeof(KSubModules) / sizeof(KSubModules[0]);
       
   254 			ASSERT(msg.Size() > 0);
       
   255 			ASSERT(iDiscoverPos < numSubModules);
       
   256 			if(msg.Reset())
       
   257 				iDiscoverPos = 0;
       
   258 			TCFSubModuleName name;
       
   259 			const TText8* subMod = KSubModules[iDiscoverPos++];
       
   260 			name.Copy(subMod);
       
   261 			msg.SubModuleNames()[0] = name;
       
   262 			TBool discoveryEnded = (iDiscoverPos == numSubModules);
       
   263 			TCFDiscoverRespMsg diskresp(msg.Identifier(), 1, !discoveryEnded);
       
   264 			TInt err = iChannelHandler->Send(diskresp);
       
   265 			if(KErrNone != err)
       
   266 				{
       
   267        		    User::Invariant();
       
   268 				}
       
   269 			if(discoveryEnded && iDelayTimes.iDeathDelay != NO_PREMATURE_DEATH)
       
   270 				{
       
   271 				TRAP(err, iDeathTimer = CDeathTimer::NewL(iDelayTimes.iDeathDelay * 1000));
       
   272 				ASSERT(iDeathTimer);
       
   273 				}
       
   274 				
       
   275 			}
       
   276 			break;
       
   277 
       
   278 		case TCFCommsMessage::ECodeBind:
       
   279 			{
       
   280 			__CFLOG(KLogTestCPM, KLogCode, _L("TestCpm Bind message received"));
       
   281 			// now wait for delay
       
   282 			LoggedAfter(iDelayTimes.iBindDelay, _L8("Bind"));
       
   283 			const TCFBindMsg& msg = reinterpret_cast<const TCFBindMsg&>(aMsg);
       
   284 			TCFBindCompleteMsg resp(msg.Identifier(), KErrNone);
       
   285 			TInt err = iChannelHandler->Send(resp);
       
   286 			if(KErrNone != err)
       
   287 				{
       
   288 				User::Invariant();
       
   289 				}
       
   290 			}
       
   291 			break;
       
   292 
       
   293 		case TCFCommsMessage::ECodeUnbind: 
       
   294 			{
       
   295 			__CFLOG(KLogTestCPM, KLogCode, _L("TestCpm UnBind message received"));
       
   296 			// now wait for delay
       
   297 			LoggedAfter(iDelayTimes.iUnbindDelay, _L8("Unbind"));
       
   298 			const TCFUnbindMsg& msg = reinterpret_cast<const TCFUnbindMsg&>(aMsg);
       
   299 			TCFUnbindCompleteMsg resp(msg.Identifier(), KErrNone);
       
   300 			TInt err = iChannelHandler->Send(resp);
       
   301 			if(KErrNone != err)
       
   302 				{
       
   303 				User::Invariant();
       
   304 				}
       
   305 			}
       
   306 			break;
       
   307 
       
   308 		case TCFCommsMessage::ECodeShutdown:
       
   309 			// now wait for delay
       
   310 			__CFLOG(KLogTestCPM, KLogCode, _L("TestCpm Shutdown message received"));
       
   311 			LoggedAfter(iDelayTimes.iShutdownDelay, _L8("Shutdown"));
       
   312 
       
   313 			CActiveScheduler::Stop();
       
   314 			break;
       
   315 		default:;
       
   316 			User::Invariant();
       
   317 		}
       
   318 	}
       
   319 
       
   320 	
       
   321 CTestModule::~CTestModule()
       
   322 	{
       
   323 	delete iChannelHandler;
       
   324 	delete iDeathTimer;
       
   325 	}
       
   326 
       
   327 void CTestModule::LoggedAfter(TInt aAfterMS, const TDesC8& aDescription)
       
   328 	{ 
       
   329 	(void) aDescription;
       
   330 	__CFLOG_2(KLogTestCPM, KLogCode, _L8("TestCpm: %d ms %S pause"), aAfterMS, &aDescription);
       
   331 	User::After(aAfterMS * 1000);
       
   332 	__CFLOG_1(KLogTestCPM, KLogCode, _L8("TestCpm: %S pause complete"), &aDescription);
       
   333 	}
       
   334 
       
   335 
       
   336 //---------------
       
   337 
       
   338 void DoModuleThreadL(TAny * aArg)
       
   339 	{
       
   340 	TCFModuleInfo* args = reinterpret_cast<TCFModuleInfo*>(aArg);
       
   341 	
       
   342 	CActiveScheduler*  scheduler = new (ELeave) CActiveScheduler;
       
   343 	CleanupStack::PushL(scheduler);
       
   344 	CActiveScheduler::Install(scheduler); 
       
   345 
       
   346 	CTestModule *pTm = CTestModule::NewL(args);
       
   347 
       
   348     __CFLOG_1( KLogTestCPM, KLogCode, _L("TestCpm[%LU] - Completed rendezvous."), RThread().Id().Id() );
       
   349 
       
   350 	CActiveScheduler::Start();
       
   351 
       
   352 	__CFLOG_1( KLogTestCPM, KLogCode, _L("TestCpm[%LU] - Exiting thread function"), RThread().Id().Id() );
       
   353 
       
   354 	delete pTm;
       
   355 	CActiveScheduler::Install(NULL);
       
   356 	CleanupStack::PopAndDestroy(scheduler);
       
   357 	}
       
   358 
       
   359 EXPORT_C TInt ModuleThread(TAny * aArg)
       
   360 	{
       
   361 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
   362 	if(!cleanup)
       
   363 		return KErrNoMemory;	
       
   364 
       
   365 	TRAPD(ret, DoModuleThreadL(aArg))
       
   366 	if (ret!=KErrNone)
       
   367 		{
       
   368 		__CFLOG(KLogTestCPM, KLogCode, _L("ModuleThread failed to create TestCpm"));
       
   369 		}
       
   370 
       
   371 	delete cleanup;
       
   372 	return ret;
       
   373 	}
       
   374 
       
   375 
       
   376