messagingfw/msgtest/integration/biomsg/src/sendrecv.cpp
changeset 0 8e480a14352b
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 1999-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 "sendrecv.h"
       
    17 #include <smuthdr.h>
       
    18 
       
    19 #include <txtrich.h>
       
    20 #include <txtfmlyr.h>
       
    21 
       
    22 #include <bitsids.h>
       
    23 
       
    24 const TTimeIntervalMicroSeconds32 KWaitForReceiveIteration = 2000000;
       
    25 const TTimeIntervalMicroSeconds32 KMaxWaitForMsg = 60000000;
       
    26 
       
    27 CSendReceive::~CSendReceive()
       
    28 	{
       
    29 	delete iEntryForObserver;
       
    30 
       
    31 	iReceived = NULL;
       
    32 	delete iReceived;
       
    33 	if (iOperation)
       
    34 		{
       
    35 		iOperation->Cancel();
       
    36 		delete iOperation;
       
    37 		iOperation = NULL;
       
    38 		}
       
    39 
       
    40 	delete iSelection;
       
    41 	iSelection = NULL;
       
    42 
       
    43 	if (iTimer)
       
    44 		{
       
    45 		iTimer->Cancel();
       
    46 		delete iTimer;
       
    47 		iTimer = NULL;
       
    48 		}
       
    49 	}
       
    50 
       
    51 
       
    52 CSendReceive* CSendReceive::NewL(CSmsTestUtils& aTestUtils)
       
    53 	{
       
    54 	CSendReceive* self = new (ELeave) CSendReceive(aTestUtils);
       
    55 	CleanupStack::PushL(self);
       
    56 
       
    57 	self->ConstructL();
       
    58 
       
    59 	CleanupStack::Pop();
       
    60 	return self;
       
    61 	}
       
    62 
       
    63 
       
    64 void CSendReceive::TestSendingL()
       
    65 //
       
    66 //
       
    67 //
       
    68 	{
       
    69 	iSmsTest.Test().Next(_L("Sending"));
       
    70 
       
    71 	TTime now;
       
    72 	now.HomeTime();
       
    73 	now += (TTimeIntervalSeconds) 5;
       
    74 
       
    75 	iSelection->Reset();
       
    76 
       
    77 	iSmsTest.DeleteSmsMessagesL(KMsvGlobalOutBoxIndexEntryId);
       
    78 
       
    79 	TBool read = EFalse;
       
    80 	iSmsTest.Printf(_L("Creating msgs in outbox from script:"));
       
    81 	iSmsTest.Printf(KSendRecvScript);
       
    82 	TRAPD(err, read = iSmsTest.ReadScriptL(KSendRecvScript, KMsvGlobalOutBoxIndexEntryId, *iSelection, now));
       
    83 
       
    84 	iSmsTest.Test()(!err && read);
       
    85 
       
    86 	// read any test messages from file
       
    87 //	CreateWappMessagesL();
       
    88 
       
    89 	iSmsTest.Printf(_L("Send and Receive %d messages...\n"), iSelection->Count());
       
    90 
       
    91 	iCurrentMessageNum = -1;
       
    92 	}
       
    93 /*
       
    94 void CSendReceive::CreateWappMessagesL()
       
    95 	{
       
    96 
       
    97 	TBufC<KMaxFileName> currentFile;
       
    98 
       
    99 	CMsvEntrySelection* selection = new(ELeave)CMsvEntrySelection();
       
   100 	CleanupStack::PushL(selection);
       
   101 
       
   102 	TInt err = Session().FileSession().GetDir(KMessageFileDirectory, KEntryAttMatchMask, ESortByName, iDir);
       
   103 	if (err == KErrPathNotFound)
       
   104 		{
       
   105 		TInt makeDirErr = Session().FileSession().MkDirAll(KMessageFileDirectory);	
       
   106 		makeDirErr==KErrNone ? User::Leave(KErrNotFound): User::Leave(makeDirErr);
       
   107 		}
       
   108 	else if (err!=KErrNone)
       
   109 		{
       
   110 		User::Leave(err);
       
   111 		}
       
   112 
       
   113 	// Set the session path for the RFs
       
   114 	Session().FileSession().SetSessionPath(KMessageFileDirectory);
       
   115 	if (iDir) 
       
   116 		delete iDir;
       
   117 	User::LeaveIfError(Session().FileSession().GetDir(_L("wapp*.txt"), KEntryAttNormal, ESortByName, iDir));
       
   118     TInt count = iDir->Count();
       
   119     
       
   120 	TBuf<60> outputBuf;
       
   121 
       
   122 	for(TInt loop = 0; loop < count; loop++)
       
   123 		{
       
   124 		currentFile=( (*iDir)[loop].iName );
       
   125 		// Not processed all the messages - so keep the current state
       
   126 		
       
   127 		}
       
   128 	CleanupStack::Pop(); // selection
       
   129 	}
       
   130 
       
   131 HBufC* CSendReceive::ReadWappMessageFromFileL(const TDesC& aFile)
       
   132 	{
       
   133 	RFile           file;
       
   134 	TBuf8<1024>     lineBuffer;   // Buffer for Read function.
       
   135 	TInt			err=KErrNone;
       
   136 
       
   137 	err = file.Open(Session().FileSession(),aFile,EFileStreamText|EFileRead|EFileShareAny);
       
   138 
       
   139     if(err != KErrNone)  // Didn't find the file, so leave - should only get valid filenames!
       
   140         {
       
   141         User::Leave(KErrNotFound);
       
   142 		}
       
   143 
       
   144 	// In real life this is set by the Socket Port Observer, or the SMS server.
       
   145 	// So,in this test code we must do it by creating a random  telephone number.
       
   146 	//	iMessageDetails = CreateDummyPhoneNumberL();
       
   147 
       
   148     HBufC*	bioBuf=HBufC::NewLC(1024); // Create a new descriptor on the heap.
       
   149 	HBufC*	copyBuffer=HBufC::NewLC(1024);
       
   150 
       
   151     do // Read in the text from file, and also check if there is a name field:
       
   152 		{
       
   153 		err = file.Read(lineBuffer);// Read upto 256 chars, '\n' and all...
       
   154 		//err = ReadLineL(file,lineBuffer);
       
   155 		if (err==KErrNone)				// Made a valid read,
       
   156 			if (lineBuffer.Length()==0) // but read 0 chars
       
   157 				err=KErrEof;			// so set err value to end processing
       
   158 
       
   159 		if(err == KErrNone)
       
   160 			{
       
   161 			copyBuffer->Des().Copy(lineBuffer);// Copy, and overwrite existing text
       
   162 			if( (bioBuf->Length() + copyBuffer->Length()) > bioBuf->Des().MaxLength() )
       
   163 					{
       
   164 					bioBuf = bioBuf->ReAllocL(bioBuf->Length() + copyBuffer->Length());
       
   165 					}
       
   166 			bioBuf->Des().Append(*copyBuffer);
       
   167             //bioBuf->Des().Append(_L("\n"));
       
   168 			}
       
   169 		}
       
   170     while(err != KErrEof);
       
   171 
       
   172 	CleanupStack::PopAndDestroy(); // Destroy the copyBuffer.
       
   173 	CleanupStack::Pop();// Remove the bioBuf.
       
   174     
       
   175 	file.Close();
       
   176 	return bioBuf;  // Transfer ownership and responsibility of the buffer.
       
   177 	}
       
   178 
       
   179 void CSendReceive::CreateMessageL(const TDesC& aFile)
       
   180 	{
       
   181 	CParaFormatLayer* paraFormat = CParaFormatLayer::NewL();
       
   182 	CleanupStack::PushL(paraFormat);
       
   183 	CCharFormatLayer* charFormat = CCharFormatLayer::NewL();
       
   184 	CleanupStack::PushL(charFormat);
       
   185 	CRichText* richText = CRichText::NewL(paraFormat, charFormat);
       
   186 	CleanupStack::PushL(richText);
       
   187 
       
   188 	HBufC* buffer = ReadWappMessageFromFileL(aFile);
       
   189 	CleanupStack::PushL(richText);
       
   190 	
       
   191 	richText->InsertL(0,*buffer);
       
   192 
       
   193 	CSmsHeader* header = CSmsHeader::NewL(CSmsPDU::ESmsSubmit, *richText);
       
   194 	CleanupStack::PushL(header);
       
   195 
       
   196 	TTime now;
       
   197     now.HomeTime();
       
   198 
       
   199 	TMsvEntry entry;
       
   200 	entry.SetSendingState(KMsvSendStateUponRequest);
       
   201 	entry.iBioType = KUidBIOWAPAccessPointMsg;
       
   202 
       
   203 	iSmsTest.CreateMessageL(KMsvGlobalInBoxIndexEntryId,entry,*header,*richText,now);
       
   204 
       
   205 	delete buffer;
       
   206 	CleanupStack::PopAndDestroy(5);
       
   207 	}
       
   208 */
       
   209 TBool CSendReceive::SendNextMessageL()
       
   210 	{
       
   211 	iCurrentMessageNum++;
       
   212 	const TInt count = iSelection->Count();
       
   213 
       
   214 	if (iCurrentMessageNum < count)
       
   215 		{
       
   216 		iSmsTest.Printf(_L("Sending message %d of %d\n"), iCurrentMessageNum + 1, count);
       
   217 
       
   218 		iSmsTest.SetEntryL(KMsvGlobalOutBoxIndexEntryId);
       
   219 
       
   220 		delete iOperation;
       
   221 		iOperation = NULL;
       
   222 
       
   223 		CMsvEntrySelection* sel = new (ELeave) CMsvEntrySelection();
       
   224 		CleanupStack::PushL(sel);
       
   225 		sel->AppendL(iSelection->At(iCurrentMessageNum));
       
   226 		iOperation = MsvEntry().CopyL(*sel, iSmsTest.iSmsServiceId, iStatus);
       
   227 		CleanupStack::PopAndDestroy(); //sel
       
   228 		
       
   229 		iState = EStateSending;
       
   230 		SetActive();
       
   231 		CActiveScheduler::Start();
       
   232 		return ETrue;
       
   233 		}
       
   234 
       
   235 	return EFalse;
       
   236 	}
       
   237 
       
   238 void CSendReceive::TestWaitForReceiveL()
       
   239 	{
       
   240 	iSmsTest.Test().Next(_L("Waiting to receive message"));
       
   241 	iState = EStateWaitingToReceive;
       
   242 	TRequestStatus* status = &iStatus;
       
   243 	User::RequestComplete(status, KErrNone);
       
   244 	SetActive();
       
   245 	}
       
   246 
       
   247 CSendReceive::CSendReceive(CSmsTestUtils& aTestUtils)
       
   248 : CActive(EPriorityStandard),iCurrentMessageNum(-1),iSmsTest(aTestUtils),iState(EStateWaiting)
       
   249 	{
       
   250 	}
       
   251 
       
   252 void CSendReceive::StartL(CMsvEntrySelection* aReceived)
       
   253 	{
       
   254 	iCurrentTest = 0;
       
   255 	iReceived = aReceived;
       
   256 	
       
   257 	iSmsTest.TestStart(++iCurrentTest, _L("Read Script"));
       
   258 	TestSendingL();
       
   259 	User::LeaveIfError(iStatus.Int());
       
   260 	iSmsTest.TestFinish(iCurrentTest, KErrNone);
       
   261 
       
   262 	FOREVER
       
   263 		{
       
   264 		iSmsTest.TestStart(++iCurrentTest, _L("Send Message"));
       
   265 		TBool sendNext = SendNextMessageL();
       
   266 		if (sendNext)
       
   267 			{
       
   268 			User::LeaveIfError(iStatus.Int());
       
   269 			iSmsTest.TestFinish(iCurrentTest, KErrNone);
       
   270 			}
       
   271 		else
       
   272 			{
       
   273 			iSmsTest.Printf(_L("No more messages to send!"));
       
   274 			iSmsTest.TestFinish(iCurrentTest, KErrNone);
       
   275 			break;
       
   276 			}
       
   277 		}
       
   278 
       
   279 	iSmsTest.TestStart(++iCurrentTest, _L("Wait to receive extra message(s)"));
       
   280 	User::LeaveIfError(iStatus.Int());
       
   281 	iSmsTest.TestFinish(iCurrentTest, KErrNone);
       
   282 	}
       
   283 
       
   284 void CSendReceive::ConstructL()
       
   285 	{
       
   286 	iSelection = new (ELeave) CMsvEntrySelection();
       
   287 	iTimer = CTestTimer::NewL();
       
   288 	CActiveScheduler::Add(this);
       
   289 
       
   290 	iSmsTest.SetCommDbServiceCenterFromScriptL(KSendRecvScript);
       
   291 	}
       
   292 
       
   293 void CSendReceive::RunL()
       
   294 	{
       
   295 	TRAPD(err, DoRunL());
       
   296 	if (err && iStatus == KErrNone)
       
   297 		{
       
   298 		iStatus = err;
       
   299 		}
       
   300 	if(!IsActive())
       
   301 		CActiveScheduler::Stop();
       
   302 	}
       
   303 
       
   304 void CSendReceive::DoRunL()
       
   305 	{
       
   306 	switch (iState)
       
   307 		{
       
   308 		case EStateSending:
       
   309 			{
       
   310 			DoRunSendingL();
       
   311 			delete iOperation;
       
   312 			iOperation = NULL;
       
   313 			break;
       
   314 			}
       
   315 		case EStateWaitingToReceive:
       
   316 			{
       
   317 			DoRunWaitingToReceiveL();
       
   318 			break;
       
   319 			}
       
   320 		case EStateWaitingToReceiveAnyExtra:
       
   321 			{
       
   322 			DoRunWaitForExtraL();
       
   323 			break;
       
   324 			}
       
   325 		case EStateMessageReceived:
       
   326 			{
       
   327 			break;
       
   328 			}
       
   329 		default:
       
   330 			{
       
   331 			break;
       
   332 			}
       
   333 		}
       
   334 	}
       
   335 
       
   336 void CSendReceive::DoCancel()
       
   337 	{
       
   338 	if (iOperation)
       
   339 		{
       
   340 		iOperation->Cancel();
       
   341 		iSmsTest.Printf(_L("Operation Cancelled!\n"));
       
   342 		}
       
   343 	delete iOperation;
       
   344 	iOperation = NULL;
       
   345 	}
       
   346 
       
   347 void CSendReceive::DoRunSendingL()
       
   348 	{
       
   349 	if (iOperation)
       
   350 		iSmsTest.SetProgressL(*iOperation);
       
   351 
       
   352 	TInt count = iSelection->Count();
       
   353 	TBool wait = EFalse;
       
   354 
       
   355 	while (iStatus == KErrNone && count--)
       
   356 		{
       
   357 		TRAPD(err, iSmsTest.SetEntryL(iSelection->At(count)));
       
   358 
       
   359 		if (err == KErrNone)
       
   360 			{
       
   361 			TMsvEntry entry = MsvEntry().Entry();
       
   362 			iSmsTest.SetEntryL(KMsvGlobalOutBoxIndexEntryId);
       
   363 
       
   364 			TBuf<0x100> output;
       
   365 			TBuf<0x50>  nowBuf;
       
   366 
       
   367 			switch (entry.SendingState())
       
   368 				{
       
   369 				case KMsvSendStateSending:
       
   370 					output.AppendFormat(_L("Message %d Sending with error %d\n"), entry.Id(), entry.iError);
       
   371 					wait = ETrue;
       
   372 					break;
       
   373 				case KMsvSendStateResend:
       
   374 					output.AppendFormat(_L("Message %d Resend at "), entry.Id());
       
   375 					entry.iDate.FormatL(nowBuf, _L("%-B%:0%J%:1%T%:2%S%:3%+B"));
       
   376 					output.Append(nowBuf);
       
   377 					output.Append(_L("\n"));
       
   378 					wait = ETrue;
       
   379 					break;
       
   380 				case KMsvSendStateScheduled:
       
   381 					output.AppendFormat(_L("Message %d Scheduled at "), entry.Id());					entry.iDate.FormatL(nowBuf, _L("%-B%:0%J%:1%T%:2%S%:3%+B"));
       
   382 					entry.iDate.FormatL(nowBuf, _L("%-B%:0%J%:1%T%:2%S%:3%+B"));
       
   383 					output.Append(nowBuf);
       
   384 					output.Append(_L("\n"));
       
   385 					wait = ETrue;
       
   386 					break;
       
   387 				case KMsvSendStateFailed:
       
   388 					output.AppendFormat(_L("Message %d Failed with error %d\n"), entry.Id(), entry.iError);
       
   389 					if (entry.iError)
       
   390 						iStatus = entry.iError;
       
   391 					else
       
   392 						iStatus = KErrGeneral;
       
   393 
       
   394 					break;
       
   395 				case KMsvSendStateWaiting:
       
   396 					output.AppendFormat(_L("Message %d Waiting with error %d\n"), entry.Id(), entry.iError);
       
   397 					wait = ETrue;
       
   398 					break;
       
   399 				case KMsvSendStateSent:
       
   400 				default:
       
   401 					break;
       
   402 					//nothing
       
   403 				}
       
   404 
       
   405 			iSmsTest.Printf(output);
       
   406 			}
       
   407 		}
       
   408 
       
   409 	if (wait)
       
   410 		{
       
   411 		iTimer->AfterReq(10000000, iStatus);
       
   412 		SetActive();
       
   413 		iState = EStateSending;
       
   414 		}
       
   415 	else
       
   416 		{
       
   417 		iSmsTest.Printf(_L("Sending completed with error %d\n"), iStatus);
       
   418 		
       
   419 		if (iStatus == KErrNone)
       
   420 			{
       
   421 			TestWaitForReceiveL();
       
   422 			}
       
   423 		else
       
   424 			{
       
   425 			iState = EStateWaiting;
       
   426 			}
       
   427 		}
       
   428 	}
       
   429 
       
   430 void CSendReceive::WaitForExtraL()
       
   431 	{
       
   432 	iSmsTest.Printf(_L("Waiting for any extra to arrive...\n"));
       
   433 	iCountWaits = 0;
       
   434 	iState = EStateWaitingToReceiveAnyExtra;
       
   435 	TRequestStatus* status = &iStatus;
       
   436 	User::RequestComplete(status, KErrNone);
       
   437 	SetActive();
       
   438 	}
       
   439 
       
   440 void CSendReceive::DoRunWaitForExtraL()
       
   441 	{
       
   442 	iCountWaits++;
       
   443 
       
   444 	TInt totalSentCount = iSelection->Count();
       
   445 	TInt recvCount = iReceived->Count();
       
   446 
       
   447 	if (recvCount > totalSentCount)
       
   448 		{
       
   449 		iSmsTest.Printf(_L("Too many messages received:\nSent %d Rcvd %d\n"), totalSentCount, recvCount);
       
   450 		iStatus = KErrOverflow;
       
   451 		}
       
   452 	else
       
   453 		DoWaitL();
       
   454 	}
       
   455 
       
   456 void CSendReceive::DoRunWaitingToReceiveL()
       
   457 	{
       
   458 	iCountWaits++;
       
   459 
       
   460 	TInt totalSentCount = iSelection->Count();
       
   461 	TInt recvCount = iReceived->Count();
       
   462 
       
   463 	if (recvCount > totalSentCount || recvCount > (iCurrentMessageNum + 1))
       
   464 		{
       
   465 		iSmsTest.Printf(_L("Too many messages received:\nSent %d Rcvd %d\n"), totalSentCount, recvCount);
       
   466 		iStatus = KErrOverflow;
       
   467 		}
       
   468 	else 
       
   469 		{
       
   470 		if (totalSentCount == recvCount)
       
   471 			{
       
   472 			iSmsTest.Printf(_L("All Messages Received!!\n"));
       
   473 			}
       
   474 		else if (recvCount == iCurrentMessageNum + 1)
       
   475 			{
       
   476 			//Message received
       
   477 			iState = EStateMessageReceived;
       
   478 			iCountWaits = 0;
       
   479 			TRequestStatus* status = &iStatus;
       
   480 			User::RequestComplete(status, KErrNone);
       
   481 			SetActive();
       
   482 			}
       
   483 		else
       
   484 			{
       
   485 			DoWaitL();
       
   486 			}
       
   487 		}
       
   488 	}
       
   489 
       
   490 void CSendReceive::DoWaitL()
       
   491 	{
       
   492 	TInt totalSentCount = iSelection->Count();
       
   493 	TInt recvCount = iReceived->Count();
       
   494 
       
   495 	TInt maxCount = KMaxWaitForMsg.Int() / KWaitForReceiveIteration.Int();
       
   496 	TInt waitSecs = KWaitForReceiveIteration.Int() * (maxCount - iCountWaits) / 1000000;
       
   497 	iSmsTest.Printf(_L("Waiting %d secs to recv msg\n"), waitSecs);
       
   498 
       
   499 	if (iCountWaits < maxCount)
       
   500 		{
       
   501 		iTimer->AfterReq(KWaitForReceiveIteration, iStatus);
       
   502 		SetActive();
       
   503 		}
       
   504 	else
       
   505 		{
       
   506 		iSmsTest.Printf(_L("Time Out: Message NOT Received\n"));
       
   507 
       
   508 		if (totalSentCount != recvCount)
       
   509 			{
       
   510 			iStatus = KErrTimedOut;
       
   511 			}
       
   512 		}
       
   513 	}