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

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description: Contains definition of functions defined in the CTimerEntry class.  
00028 */
00029 
00030 
00031 
00032 
00037 #include "timerentry.h"
00038 
00043 CTimerEntry* CTimerEntry::NewL(CConsoleBase* aConsole)
00044         {
00045         CTimerEntry* self = new (ELeave) CTimerEntry(aConsole);
00046         CleanupStack::PushL(self);
00047         self->ConstructL();
00048         CleanupStack::Pop(self);
00049         return self;
00050         }
00051 
00055 void CTimerEntry::ConstructL()
00056         {
00057         // Construct the timer object.
00058         CTimer::ConstructL();
00059         // Add the object to the active scheduler.
00060         CActiveScheduler::Add(this);
00061         }
00062 
00067 CTimerEntry::CTimerEntry(CConsoleBase* aConsole):
00068                                                                                                 // Constructor of the timer object.
00069                                                                                                 CTimer(CActive::EPriorityStandard),
00070                                                                                                 // Create the iterator object.
00071                                                                                                 iIterator(iQueue),
00072                                                                                                 // Set count to -1.
00073                                                                                                 iCount(-1),
00074                                                                                                 // Set increment operation as
00075                                                                                                 // the default operation.
00076                                                                                                 iOp(EInsert),
00077                                                                                                 iConsole(aConsole),
00078                                                                                                 iWaitFlag(ETrue)
00079         {
00080         }
00081 
00085 void CTimerEntry::RunL()
00086         {
00087         // Call the arbitrator function.
00088         ArbitratorL();
00089         // Print the contents of the queue.
00090         PrintQue();
00091         // Maintain an outstanding request.
00092         IssueRequest();
00093         }
00094 
00095 
00099 void CTimerEntry::IssueRequest()
00100         {
00101         // Get a random number for number of seconds to wait.
00102         // Issue the wait request.
00103         TUint waitTime = KWaitTime;
00104         if (iWaitFlag)
00105                 {
00106                 waitTime = Math::Random() % KMaxTimeToWait;
00107                 // Make sure that time to wait is not zero;
00108                 waitTime++;
00109                 }
00110         CTimer::After((waitTime) * 1000000);
00111         }
00112 
00118 void CTimerEntry::ArbitratorL()
00119         {
00120         // The increment operation.
00121         // Add elements to the list.
00122         if(iOp == EInsert)
00123                 {
00124                 // Get the tick count.
00125                 TInt tc = (TInt)User::TickCount();
00126                 // Try to add an element to the list.
00127                 TBool redo = AddToQueueL(tc);
00128                 // The queue is full.
00129                 if(!redo)
00130                         {
00131                         _LIT(KTextQueFull,"Queue is full!\n");
00132                         iConsole->Printf(KTextQueFull);
00133                         // Stop adding elements to the queue.
00134                         // Set iWaitFlag to EFalse.
00135                         // The timer will wait for 1 second before removing an element
00136                         // from the queue.
00137                         iWaitFlag = EFalse;
00138                         }
00139                 }
00140         // The decrement operation.
00141         // Remove elements from the list.
00142         else
00143                 {
00144                 // Try to remove an element from the list.
00145                 TBool redo = RemoveFromQueue();
00146                 // The queue is empty.
00147                 if(!redo)
00148                         {
00149                         _LIT(KTextQueEmpty,"Queue is empty!\n");
00150                         iConsole->Printf(KTextQueEmpty);
00151                         // Stop removing elements from the queue.
00152                         // Set iWaitFlag to ETrue.
00153                         // The timer will wait for a random interval of time
00154                         // before adding an element to the queue.
00155                         iWaitFlag = ETrue;
00156                         }
00157                 }
00158         }
00159 
00165 TBool CTimerEntry::AddToQueueL(TInt aTicks)
00166         {
00167         // Check if the queue is full.
00168         if(iCount >=  (KMaxEntry - 1))
00169                         {
00170                         iOp = ERemove;
00171                         return EFalse;
00172                         }
00173 
00174         // Add the tick count to the delta queue.
00175         _LIT(KTextAddTick,"Added %d to the queue\n");
00176         iConsole->Printf(KTextAddTick,aTicks);
00177         iCount++;
00178 
00179         TTimerEntry* ptr = new (ELeave) TTimerEntry;
00180         iQueue.Add(*ptr,aTicks);
00181 
00182         return ETrue;
00183         }
00184 
00188 void CTimerEntry::PrintQue()
00189         {
00190         _LIT(KTextCont,"Contents of the List:\n***\nINDEX\tDELTA\n");
00191         iConsole->Printf(KTextCont);
00192         // Set the iterator to the first node of the list.
00193         iIterator.SetToFirst();
00194         // Iterate the list and print all TDigit objects
00195         TTimerEntry *ptr = iIterator++;
00196         // List index.
00197         TInt ix = 0;
00198         while (ptr != NULL)
00199                 {
00200                 _LIT(KTextFormat,"%d\t\t%d\n");
00201                 iConsole->Printf(KTextFormat,ix,ptr->iLink.iDelta);
00202                 ptr = iIterator++;
00203                 ix++;
00204                 }
00205         _LIT(KTextEnd,"***\n");
00206         iConsole->Printf(KTextEnd);
00207         }
00208 
00213 TBool CTimerEntry::RemoveFromQueue()
00214         {
00215         // Check if the queue is empty.
00216         if(iCount <  0)
00217                         {
00218                         iOp = EInsert;
00219                         return EFalse;
00220                         }
00221 
00222         iIterator.SetToFirst();
00223 
00224         // The index of the node to be removed in the list.
00225         TInt pos = 0;
00226 
00227         // Get a random value for position.
00228         if(iCount > 0)
00229                 {
00230                 pos = Math::Random() % iCount;
00231                 }
00232 
00233         // Iterate the list until the node pointed to by
00234         // the pos variable is reached.
00235         for(TInt ix = 1; ix <= pos; ix++)
00236                 {
00237                 iIterator++;
00238                 }
00239 
00240         TTimerEntry* ptr = iIterator;
00241 
00242         _LIT(KTextRemove,"Removed an element from the queue. Index: %d\n");
00243         iConsole->Printf(KTextRemove,pos);
00244 
00245         // Remove an element from the delta queue.
00246         iQueue.Remove(*ptr);
00247         delete ptr;
00248 
00249         iCount--;
00250 
00251         return ETrue;
00252         }
00253 
00259 CTimerEntry::~CTimerEntry()
00260         {
00261         // Cancel any outstanding request.
00262         Cancel();
00263         // Delete the elements from the list
00264         if(iIterator)
00265                 {
00266                 iIterator.SetToFirst();
00267                 // Iterate the list and delete all TDigit objects
00268                 TTimerEntry *ptr = iIterator++;
00269                 while (ptr != NULL)
00270                         {
00271                         delete ptr;
00272                         ptr = iIterator++;
00273                         }
00274                 }
00275         }
00276 
00280 void CTimerEntry::DoCancel()
00281         {
00282         // Call DoCancel() on the base class.
00283         CTimer::DoCancel();
00284         }
00285 

Generated by  doxygen 1.6.2