examples/Base/IPC/Async/RunComplete/RunComplete.cpp

00001 // Copyright (c) 2000-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 // Demonstrates asynchronous keyboard processing with messenger program.
00015 // Uses a single CMessageTimer active object which runs until completion.
00016 //
00017 
00018 #include "CommonFramework.h"
00019 
00020 // panics
00021 enum
00022         {
00023         EPanicAlreadyActive=1000,
00024         };
00025 
00026 
00028 //
00029 // -----> CTimedMessenger (definition)
00030 //
00032 class CTimedMessenger : public CTimer
00033         {
00034 public:
00035           // Construction
00036         CTimedMessenger();
00037       // Destruction
00038         ~CTimedMessenger();
00039 
00040 public:
00041           // Static construction
00042         static CTimedMessenger* NewLC(const TDesC& aGreeting,
00043                                           TInt aTicksRequested,
00044                                                                   TInt aTicksInterval
00045                                                                  );
00046         static CTimedMessenger* NewL(const TDesC& aGreeting,
00047                                          TInt aTicksRequested,
00048                                                                  TInt aTicksInterval
00049                                                                 );
00050 
00051 public:
00052           // Second phase construction
00053         void ConstructL(const TDesC& aGreeting,
00054                             TInt aTicksRequested,
00055                                         TInt aTicksInterval
00056                                    );
00057 
00058           // issue request
00059         void IssueRequest(); 
00060 
00061           // Cancel request
00062           // Defined as pure virtual by CActive;
00063           // implementation provided by this class.
00064         void DoCancel();
00065 
00066           // service completed request.
00067           // Defined as pure virtual by CActive;
00068           // implementation provided by this class.
00069         void RunL();
00070 
00071 public:
00072           // data members defined by this class
00073         TBufC<20> iGreeting;   // Text of the greeting.
00074         TInt iTicksRequested;  // Total number of greetings CTimedMessenger
00075                                // will emit.
00076         TInt iTicksInterval;   // Number of seconds between each greeting.
00077         TInt iTicksDone;       // Number of greetings issued so far.
00078         };
00079 
00080 
00082 //
00083 // -----> CExampleScheduler (definition)
00084 //
00086 class CExampleScheduler : public CActiveScheduler
00087         {
00088 public:
00089         void Error (TInt aError) const;
00090         };
00091 
00092 
00094 //
00095 // -----> CTimedMessenger (implementation)
00096 //
00098 CTimedMessenger::CTimedMessenger()
00099         : CTimer(CActive::EPriorityStandard)
00100           // Construct standard-priority active object
00101         {};
00102 
00103 CTimedMessenger* CTimedMessenger::NewLC(const TDesC& aGreeting,
00104                                                                                 TInt aTicksRequested,
00105                                                                                 TInt aTicksInterval
00106                                                                            )
00107         {
00108         CTimedMessenger* self=new (ELeave) CTimedMessenger;
00109         CleanupStack::PushL(self);
00110         self->ConstructL(aGreeting,aTicksRequested,aTicksInterval);
00111         return self;
00112         }
00113 
00114 CTimedMessenger* CTimedMessenger::NewL(const TDesC& aGreeting,
00115                                                                    TInt aTicksRequested,
00116                                                                            TInt aTicksInterval
00117                                                                           )
00118         {
00119         CTimedMessenger* self = NewLC(aGreeting,aTicksRequested,aTicksInterval);
00120         CleanupStack::Pop();
00121         return self;
00122         }
00123 
00124 void CTimedMessenger::ConstructL(const TDesC& aGreeting,
00125                                                                  TInt aTicksRequested,
00126                                                                  TInt aTicksInterval
00127                                                                 )
00128         {
00129           // Base class second-phase construction.
00130         CTimer::ConstructL();
00131           // Set members from arguments
00132         iGreeting       = aGreeting;       // Set greeting text.
00133         iTicksRequested = aTicksRequested; // Ticks requested
00134         iTicksInterval  = aTicksInterval;  // Interval between ticks
00135           // Add active object to active scheduler
00136         CActiveScheduler::Add(this); 
00137         }
00138 
00139 
00140 CTimedMessenger::~CTimedMessenger()
00141         {
00142           // Make sure we're cancelled
00143         Cancel();
00144         }
00145 
00146 void CTimedMessenger::DoCancel()
00147         {
00148           // Base class
00149         CTimer::DoCancel(); 
00150           // Reset this variable - needed if the object is re-activated later
00151         iTicksDone = 0;
00152           // Tell user
00153         _LIT(KMsgCancelled,"Outstanding Messenger request cancelled\n");
00154         console->Printf(KMsgCancelled); 
00155         }
00156 
00157 void CTimedMessenger::IssueRequest()
00158         {
00159           // There should never be an outstanding request at this point.
00160         _LIT(KMsgAlreadyActive,"Is already Active");
00161         __ASSERT_ALWAYS(!IsActive(),User::Panic(KMsgAlreadyActive,EPanicAlreadyActive));
00162           // Request another wait
00163         CTimer::After( iTicksInterval*1000000);
00164         }
00165 
00166 void CTimedMessenger::RunL()
00167         {
00168           // Handle request completion
00169           // One more tick done
00170         iTicksDone++;
00171           // Print greeting
00172         _LIT(KFormatString,"%S \n");
00173         console->Printf(KFormatString,&iGreeting);
00174           // Issue new request, or stop if we have reached the limit
00175         if (iTicksDone  < iTicksRequested)
00176                 {
00177                 IssueRequest();
00178                 }
00179         else
00180                 {
00181                 _LIT(KMsgFinished,"Messenger finished \n");
00182                 console->Printf(KMsgFinished);
00183                   // Reset this variable - needed if the object is re-activated later
00184                 iTicksDone=0;
00185                   // Can now stop the active scheduler
00186                 CActiveScheduler::Stop();
00187                 }
00188         }
00189 
00190 
00192 //
00193 // -----> CExampleScheduler (implementation)
00194 //
00196 void CExampleScheduler::Error(TInt aError) const
00197         {
00198         _LIT(KMsgSchedErr,"CExampleScheduler-error");
00199         User::Panic(KMsgSchedErr,aError);
00200         }
00201 
00202 
00204 //
00205 // Do the example
00206 //
00208 LOCAL_C void doExampleL()
00209     {
00210           // Construct and install the active scheduler
00211         CExampleScheduler*  exampleScheduler = new (ELeave) CExampleScheduler;
00212 
00213           // Push onto the cleanup stack
00214         CleanupStack::PushL(exampleScheduler);
00215 
00216           // Install as the active scheduler
00217         CActiveScheduler::Install(exampleScheduler); 
00218 
00219       // Create a CTimedMessenger active object which will emit 3 messages
00220           // with an interval of 2 seconds between messages
00221         _LIT(KMsgExplanation,"A single CMessageTimer active object which runs until completion\n\n");
00222         console->Printf(KMsgExplanation);
00223         _LIT(KMsgGoodMorning,"Good Morning!");
00224         CTimedMessenger* myTimedMessage = CTimedMessenger::NewLC(KMsgGoodMorning,3,2);
00225 
00226           // Issue the first request
00227         myTimedMessage->IssueRequest();
00228 
00229       // Main part of program is a wait loop
00230           // This function completes when the scheduler stops
00231         CActiveScheduler::Start();
00232 
00233           // Remove from the cleanup stack and destroy:
00234           // 1. the CTimedMessenger active object
00235           // 2. exampleScheduler
00236         CleanupStack::PopAndDestroy(2); 
00237         }
00238 

Generated by  doxygen 1.6.2