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

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

Generated by  doxygen 1.6.2