examples/Base/ArraysAndLists/linkedlist/deltaque/src/timerentry.cpp

Go to the documentation of this file.
00001 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 // Contains definition of functions defined in the CTimerEntry class.
00015 //
00016 
00017 
00018 
00023 #include "timerentry.h"
00024 
00029 CTimerEntry* CTimerEntry::NewL(CConsoleBase* aConsole)
00030         {
00031         CTimerEntry* self = new (ELeave) CTimerEntry(aConsole);
00032         CleanupStack::PushL(self);
00033         self->ConstructL();
00034         CleanupStack::Pop(self);
00035         return self;
00036         }
00037 
00041 void CTimerEntry::ConstructL()
00042         {
00043         // Construct the timer object.
00044         CTimer::ConstructL();
00045         // Add the object to the active scheduler.
00046         CActiveScheduler::Add(this);
00047         }
00048 
00053 CTimerEntry::CTimerEntry(CConsoleBase* aConsole):
00054                                                                                                 // Constructor of the timer object.
00055                                                                                                 CTimer(CActive::EPriorityStandard),
00056                                                                                                 // Create the iterator object.
00057                                                                                                 iIterator(iQueue),
00058                                                                                                 // Set count to -1.
00059                                                                                                 iCount(-1),
00060                                                                                                 // Set increment operation as
00061                                                                                                 // the default operation.
00062                                                                                                 iOp(EInsert),
00063                                                                                                 iConsole(aConsole),
00064                                                                                                 iWaitFlag(ETrue)
00065         {
00066         }
00067 
00071 void CTimerEntry::RunL()
00072         {
00073         // Call the arbitrator function.
00074         ArbitratorL();
00075         // Print the contents of the queue.
00076         PrintQue();
00077         // Maintain an outstanding request.
00078         IssueRequest();
00079         }
00080 
00081 
00085 void CTimerEntry::IssueRequest()
00086         {
00087         // Get a random number for number of seconds to wait.
00088         // Issue the wait request.
00089         TUint waitTime = KWaitTime;
00090         if (iWaitFlag)
00091                 {
00092                 waitTime = Math::Random() % KMaxTimeToWait;
00093                 // Make sure that time to wait is not zero;
00094                 waitTime++;
00095                 }
00096         CTimer::After((waitTime) * 1000000);
00097         }
00098 
00104 void CTimerEntry::ArbitratorL()
00105         {
00106         // The increment operation.
00107         // Add elements to the list.
00108         if(iOp == EInsert)
00109                 {
00110                 // Get the tick count.
00111                 TInt tc = (TInt)User::TickCount();
00112                 // Try to add an element to the list.
00113                 TBool redo = AddToQueueL(tc);
00114                 // The queue is full.
00115                 if(!redo)
00116                         {
00117                         _LIT(KTextQueFull,"Queue is full!\n");
00118                         iConsole->Printf(KTextQueFull);
00119                         // Stop adding elements to the queue.
00120                         // Set iWaitFlag to EFalse.
00121                         // The timer will wait for 1 second before removing an element
00122                         // from the queue.
00123                         iWaitFlag = EFalse;
00124                         }
00125                 }
00126         // The decrement operation.
00127         // Remove elements from the list.
00128         else
00129                 {
00130                 // Try to remove an element from the list.
00131                 TBool redo = RemoveFromQueue();
00132                 // The queue is empty.
00133                 if(!redo)
00134                         {
00135                         _LIT(KTextQueEmpty,"Queue is empty!\n");
00136                         iConsole->Printf(KTextQueEmpty);
00137                         // Stop removing elements from the queue.
00138                         // Set iWaitFlag to ETrue.
00139                         // The timer will wait for a random interval of time
00140                         // before adding an element to the queue.
00141                         iWaitFlag = ETrue;
00142                         }
00143                 }
00144         }
00145 
00151 TBool CTimerEntry::AddToQueueL(TInt aTicks)
00152         {
00153         // Check if the queue is full.
00154         if(iCount >=  (KMaxEntry - 1))
00155                         {
00156                         iOp = ERemove;
00157                         return EFalse;
00158                         }
00159 
00160         // Add the tick count to the delta queue.
00161         _LIT(KTextAddTick,"Added %d to the queue\n");
00162         iConsole->Printf(KTextAddTick,aTicks);
00163         iCount++;
00164 
00165         TTimerEntry* ptr = new (ELeave) TTimerEntry;
00166         iQueue.Add(*ptr,aTicks);
00167 
00168         return ETrue;
00169         }
00170 
00174 void CTimerEntry::PrintQue()
00175         {
00176         _LIT(KTextCont,"Contents of the List:\n***\nINDEX\tDELTA\n");
00177         iConsole->Printf(KTextCont);
00178         // Set the iterator to the first node of the list.
00179         iIterator.SetToFirst();
00180         // Iterate the list and print all TDigit objects
00181         TTimerEntry *ptr = iIterator++;
00182         // List index.
00183         TInt ix = 0;
00184         while (ptr != NULL)
00185                 {
00186                 _LIT(KTextFormat,"%d\t\t%d\n");
00187                 iConsole->Printf(KTextFormat,ix,ptr->iLink.iDelta);
00188                 ptr = iIterator++;
00189                 ix++;
00190                 }
00191         _LIT(KTextEnd,"***\n");
00192         iConsole->Printf(KTextEnd);
00193         }
00194 
00199 TBool CTimerEntry::RemoveFromQueue()
00200         {
00201         // Check if the queue is empty.
00202         if(iCount <  0)
00203                         {
00204                         iOp = EInsert;
00205                         return EFalse;
00206                         }
00207 
00208         iIterator.SetToFirst();
00209 
00210         // The index of the node to be removed in the list.
00211         TInt pos = 0;
00212 
00213         // Get a random value for position.
00214         if(iCount > 0)
00215                 {
00216                 pos = Math::Random() % iCount;
00217                 }
00218 
00219         // Iterate the list until the node pointed to by
00220         // the pos variable is reached.
00221         for(TInt ix = 1; ix <= pos; ix++)
00222                 {
00223                 iIterator++;
00224                 }
00225 
00226         TTimerEntry* ptr = iIterator;
00227 
00228         _LIT(KTextRemove,"Removed an element from the queue. Index: %d\n");
00229         iConsole->Printf(KTextRemove,pos);
00230 
00231         // Remove an element from the delta queue.
00232         iQueue.Remove(*ptr);
00233         delete ptr;
00234 
00235         iCount--;
00236 
00237         return ETrue;
00238         }
00239 
00245 CTimerEntry::~CTimerEntry()
00246         {
00247         // Cancel any outstanding request.
00248         Cancel();
00249         // Delete the elements from the list
00250         if(iIterator)
00251                 {
00252                 iIterator.SetToFirst();
00253                 // Iterate the list and delete all TDigit objects
00254                 TTimerEntry *ptr = iIterator++;
00255                 while (ptr != NULL)
00256                         {
00257                         delete ptr;
00258                         ptr = iIterator++;
00259                         }
00260                 }
00261         }
00262 
00266 void CTimerEntry::DoCancel()
00267         {
00268         // Call DoCancel() on the base class.
00269         CTimer::DoCancel();
00270         }
00271 

Generated by  doxygen 1.6.2