00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "CommonFramework.h"
00039
00040
00041
00042
00043 _LIT(KTxtNewLine,"\n");
00044
00045
00046
00047 enum
00048 {
00049 EPanicAlreadyActive=1000,
00050 };
00051
00052
00054
00055
00056
00058 class CTimedMessenger : public CTimer
00059 {
00060 public:
00061
00062 CTimedMessenger();
00063
00064 ~CTimedMessenger();
00065
00066 public:
00067
00068 static CTimedMessenger* NewLC(const TDesC& aGreeting,
00069 TInt aTicksRequested,
00070 TInt aTicksInterval
00071 );
00072 static CTimedMessenger* NewL(const TDesC& aGreeting,
00073 TInt aTicksRequested,
00074 TInt aTicksInterval
00075 );
00076
00077 public:
00078
00079 void ConstructL(const TDesC& aGreeting,
00080 TInt aTicksRequested,
00081 TInt aTicksInterval
00082 );
00083
00084
00085 void IssueRequest();
00086
00087
00088
00089
00090 void DoCancel();
00091
00092
00093
00094
00095 void RunL();
00096
00097 public:
00098
00099 TBufC<20> iGreeting;
00100 TInt iTicksRequested;
00101
00102 TInt iTicksInterval;
00103 TInt iTicksDone;
00104 };
00105
00106
00108
00109
00110
00112 class CActiveConsole;
00113 class CExampleScheduler : public CActiveScheduler
00114 {
00115 public:
00116 void Error (TInt aError) const;
00117 void WaitForAnyRequest();
00118 void SetActiveObject(CActiveConsole* aActiveConsole);
00119 private:
00120
00121 CActiveConsole* iActiveConsole;
00122 };
00123
00124
00126
00127
00128
00129
00130
00132 class CActiveConsole : public CActive
00133 {
00134 public:
00135
00136 CActiveConsole(CConsoleBase* aConsole);
00137 void ConstructL();
00138
00139
00140 ~CActiveConsole();
00141
00142
00143 void RequestCharacter();
00144
00145
00146
00147
00148 void DoCancel();
00149
00150
00151
00152
00153 void RunL();
00154
00155
00156
00157 virtual void ProcessKeyPress(TChar aChar) = 0;
00158
00159 protected:
00160
00161 CConsoleBase* iConsole;
00162 };
00163
00164
00166
00167
00168
00169
00170
00171
00172
00174 class CWriteKeyProcessor : public CActiveConsole
00175 {
00176 public:
00177
00178 CWriteKeyProcessor(CConsoleBase* aConsole);
00179
00180 public:
00181
00182 static CWriteKeyProcessor *NewLC (CConsoleBase* aConsole);
00183 static CWriteKeyProcessor *NewL(CConsoleBase* aConsole);
00184
00185
00186 void ProcessKeyPress(TChar aChar);
00187 };
00188
00189
00191
00192
00193
00195 CTimedMessenger::CTimedMessenger()
00196 : CTimer(CActive::EPriorityStandard)
00197
00198 {};
00199
00200 CTimedMessenger* CTimedMessenger::NewLC(const TDesC& aGreeting,
00201 TInt aTicksRequested,
00202 TInt aTicksInterval
00203 )
00204 {
00205 CTimedMessenger* self=new (ELeave) CTimedMessenger;
00206 CleanupStack::PushL(self);
00207 self->ConstructL(aGreeting,aTicksRequested,aTicksInterval);
00208 return self;
00209 }
00210
00211 CTimedMessenger* CTimedMessenger::NewL(const TDesC& aGreeting,
00212 TInt aTicksRequested,
00213 TInt aTicksInterval
00214 )
00215 {
00216 CTimedMessenger* self = NewLC(aGreeting,aTicksRequested,aTicksInterval);
00217 CleanupStack::Pop();
00218 return self;
00219 }
00220
00221 void CTimedMessenger::ConstructL(const TDesC& aGreeting,
00222 TInt aTicksRequested,
00223 TInt aTicksInterval
00224 )
00225 {
00226
00227 CTimer::ConstructL();
00228
00229 iGreeting = aGreeting;
00230 iTicksRequested = aTicksRequested;
00231 iTicksInterval = aTicksInterval;
00232
00233 CActiveScheduler::Add(this);
00234 }
00235
00236
00237 CTimedMessenger::~CTimedMessenger()
00238 {
00239
00240 Cancel();
00241 }
00242
00243 void CTimedMessenger::DoCancel()
00244 {
00245
00246 CTimer::DoCancel();
00247
00248 iTicksDone = 0;
00249
00250 _LIT(KTxtOutMessReqCancelled,"Outstanding Messenger request cancelled\n");
00251 console->Printf(KTxtOutMessReqCancelled);
00252 }
00253
00254 void CTimedMessenger::IssueRequest()
00255 {
00256
00257 _LIT(KTxtIsAlreadyActive,"Is already Active");
00258 __ASSERT_ALWAYS(!IsActive(),User::Panic(KTxtIsAlreadyActive,EPanicAlreadyActive));
00259
00260 CTimer::After( iTicksInterval*1000000);
00261 }
00262
00263 void CTimedMessenger::RunL()
00264 {
00265
00266
00267 iTicksDone++;
00268
00269 _LIT(KFormat1,"%S \n");
00270 console->Printf(KFormat1,&iGreeting);
00271
00272 if (iTicksDone < iTicksRequested)
00273 {
00274 IssueRequest();
00275 }
00276 else
00277 {
00278 _LIT(KTxtMessengerFinished,"Messenger finished \n");
00279 console->Printf(KTxtMessengerFinished);
00280
00281 iTicksDone=0;
00282
00283 CActiveScheduler::Stop();
00284 }
00285 }
00286
00287
00289
00290
00291
00293 void CExampleScheduler::Error(TInt aError) const
00294 {
00295 _LIT(KTxtSchedulerError,"CExampleScheduler - error");
00296 User::Panic(KTxtSchedulerError,aError);
00297 }
00298
00299
00300 void CExampleScheduler::WaitForAnyRequest()
00301 {
00302 if (!(iActiveConsole->IsActive()))
00303 iActiveConsole->RequestCharacter();
00304 CActiveScheduler::WaitForAnyRequest();
00305 }
00306
00307 void CExampleScheduler::SetActiveObject(CActiveConsole* aActiveConsole)
00308 {
00309 iActiveConsole = aActiveConsole;
00310 }
00311
00312
00314
00315
00316
00318 CActiveConsole::CActiveConsole( CConsoleBase* aConsole)
00319 : CActive(CActive::EPriorityUserInput)
00320
00321 {
00322 iConsole = aConsole;
00323 }
00324
00325 void CActiveConsole::ConstructL()
00326 {
00327
00328 CActiveScheduler::Add(this);
00329 }
00330
00331 CActiveConsole::~CActiveConsole()
00332 {
00333
00334 Cancel();
00335 }
00336
00337 void CActiveConsole::DoCancel()
00338 {
00339 iConsole->ReadCancel();
00340 }
00341
00342 void CActiveConsole::RunL()
00343 {
00344
00345 ProcessKeyPress(TChar(iConsole->KeyCode()));
00346 }
00347
00348 void CActiveConsole::RequestCharacter()
00349 {
00350
00351
00352 iConsole->Read(iStatus);
00353 SetActive();
00354 }
00355
00356
00358
00359
00360
00362 CWriteKeyProcessor::CWriteKeyProcessor(CConsoleBase* aConsole)
00363 : CActiveConsole(aConsole)
00364
00365 {};
00366
00367 CWriteKeyProcessor* CWriteKeyProcessor::NewLC(CConsoleBase* aConsole)
00368 {
00369 CWriteKeyProcessor* self=new (ELeave) CWriteKeyProcessor(aConsole);
00370 CleanupStack::PushL(self);
00371 self->ConstructL();
00372 return self;
00373 }
00374
00375 CWriteKeyProcessor* CWriteKeyProcessor::NewL(CConsoleBase* aConsole)
00376 {
00377 CWriteKeyProcessor* self=NewLC(aConsole);
00378 CleanupStack::Pop();
00379 return self;
00380 }
00381
00382 void CWriteKeyProcessor::ProcessKeyPress(TChar aChar)
00383 {
00384
00385 if (aChar == EKeyEscape)
00386 {
00387 iConsole->Printf(KTxtNewLine);
00388 CActiveScheduler::Stop();
00389 return;
00390 }
00391
00392
00393
00394
00395 if (aChar == EKeyEnter)
00396 iConsole->Printf(KTxtNewLine);
00397 else
00398 {
00399 _LIT(KFormat2,"%c");
00400 _LIT(KFormat3,"%d");
00401 if (aChar.IsAlphaDigit()|| aChar.IsSpace())
00402
00403 iConsole->Printf(KFormat2,TUint(aChar));
00404 else
00405 iConsole->Printf(KFormat3,TUint(aChar));
00406 }
00407
00408
00409 RequestCharacter();
00410 }
00411
00412
00414
00415
00416
00417
00418
00419
00420
00421
00422
00424 class CMessageKeyProcessor : public CActiveConsole
00425 {
00426 public:
00427
00428 CMessageKeyProcessor(CConsoleBase* aConsole, CTimedMessenger* iMessenger);
00429 void ConstructL();
00430
00431 public:
00432
00433 static CMessageKeyProcessor* NewLC(CConsoleBase* aConsole,
00434 CTimedMessenger* iMessenger
00435 );
00436 static CMessageKeyProcessor* NewL(CConsoleBase* aConsole,
00437 CTimedMessenger* iMessenger
00438 );
00439
00440 public:
00441
00442 void ProcessKeyPress(TChar aChar);
00443
00444 private:
00445
00446 CTimedMessenger* iMessenger;
00447 };
00448
00450
00451
00452
00454 CMessageKeyProcessor::CMessageKeyProcessor(CConsoleBase* aConsole,
00455 CTimedMessenger* aMessenger
00456 )
00457 : CActiveConsole(aConsole)
00458
00459 {
00460 iMessenger = aMessenger;
00461 }
00462
00463 CMessageKeyProcessor* CMessageKeyProcessor::NewLC(CConsoleBase* aConsole,
00464 CTimedMessenger* aMessenger
00465 )
00466 {
00467 CMessageKeyProcessor* self=new (ELeave) CMessageKeyProcessor(aConsole,
00468 aMessenger
00469 );
00470 CleanupStack::PushL(self);
00471 self->ConstructL();
00472 return self;
00473 }
00474
00475 CMessageKeyProcessor* CMessageKeyProcessor::NewL(CConsoleBase* aConsole,
00476 CTimedMessenger* aMessenger
00477 )
00478 {
00479 CMessageKeyProcessor* self = NewLC(aConsole, aMessenger);
00480 CleanupStack::Pop();
00481 return self;
00482 }
00483
00484 void CMessageKeyProcessor::ConstructL()
00485 {
00486
00487 CActiveScheduler::Add(this);
00488
00489 ((CExampleScheduler*)(CActiveScheduler::Current()))->SetActiveObject(this);
00490 }
00491
00492 void CMessageKeyProcessor::ProcessKeyPress(TChar aChar)
00493 {
00494
00495
00496
00497 if (aChar == EKeyEscape)
00498 {
00499 iMessenger->Cancel();
00500 CActiveScheduler::Stop();
00501 return;
00502 }
00503
00504
00505
00506
00507
00508 if (aChar == 'm' || aChar == 'M')
00509 {
00510 _LIT(KTxtStartingMess,"Starting Messenger.... \n");
00511 iConsole->Printf(KTxtStartingMess);
00512 iMessenger->Cancel();
00513 iMessenger ->IssueRequest();
00514 }
00515
00516
00517
00518 if (aChar == 'c' || aChar == 'C')
00519 iMessenger->Cancel();
00520 }
00521
00522
00524
00525
00526
00528 LOCAL_C void doExampleL()
00529 {
00530
00531 CExampleScheduler* exampleScheduler = new (ELeave) CExampleScheduler;
00532
00533
00534 CleanupStack::PushL(exampleScheduler);
00535
00536
00537 CActiveScheduler::Install(exampleScheduler);
00538
00539
00540
00541 _LIT(KTxtGoodMorning,"Good Morning!");
00542 CTimedMessenger* messenger = CTimedMessenger::NewLC(KTxtGoodMorning, 3, 2);
00543
00544
00545 _LIT(KTxtMain1,"A single CKeyMessengerProcessor active object ");
00546 _LIT(KTxtMain2,"Active scheduler renews request.\n");
00547 _LIT(KTxtMain3,"Press 'm' to activate messenger; Press 'c' to cancel it.\n");
00548 _LIT(KTxtMain4,"Press ESC to end.\n\n");
00549 console->Printf(KTxtMain1);
00550 console->Printf(KTxtMain2);
00551 console->Printf(KTxtMain3);
00552 console->Printf(KTxtMain4);
00553 CMessageKeyProcessor::NewLC(console,messenger);
00554
00555
00556 CActiveScheduler::Start();
00557
00558
00559
00560
00561
00562 CleanupStack::PopAndDestroy(3);
00563 }
00564
00565
00566