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